142 lines
3.8 KiB
GDScript
142 lines
3.8 KiB
GDScript
|
|
#------------------------------------------------------------
|
|
# Script: Board Manager
|
|
# Author: NotDinoBits
|
|
# Contributors:
|
|
#
|
|
# Godot Version: 4.7
|
|
# Language: GDScript
|
|
#
|
|
# Description: Manages the exploration board that the player
|
|
# navigates. The board consists of a grid of
|
|
# square tiles, with dimensions determined by
|
|
# width and height.
|
|
#
|
|
# Currently, the board generates a checkerboard
|
|
# pattern and prevents the player from moving
|
|
# outside the board boundaries.
|
|
#
|
|
# Future additions:
|
|
# - Wall collision
|
|
# - Loot placement and collection
|
|
# - Enemy placement and encounters
|
|
#------------------------------------------------------------
|
|
|
|
extends TileMapLayer
|
|
|
|
|
|
#------------------------------------------------------------
|
|
# Board Variables
|
|
#------------------------------------------------------------
|
|
|
|
# Number of tiles horizontally and vertically.
|
|
# These values can be changed in the Godot Inspector.
|
|
@export var width: int = 10
|
|
@export var height: int = 10
|
|
|
|
# Size of each tile in pixels.
|
|
# Currently each tile is 64x64 pixels.
|
|
@export var tile_size: int = 64
|
|
|
|
|
|
#------------------------------------------------------------
|
|
# Initialization
|
|
#------------------------------------------------------------
|
|
|
|
# Called once when the node enters the scene tree.
|
|
func _ready() -> void:
|
|
|
|
# Requests that Godot redraw the board.
|
|
# This causes the _draw() function to execute.
|
|
queue_redraw()
|
|
|
|
|
|
#------------------------------------------------------------
|
|
# Board Generation
|
|
#------------------------------------------------------------
|
|
|
|
# Draws a visible grid based on the board dimensions.
|
|
# This currently uses placeholder colors instead of textures.
|
|
func _draw() -> void:
|
|
|
|
# Loop through every coordinate on the board.
|
|
for x in range(width):
|
|
for y in range(height):
|
|
|
|
# Default tile color.
|
|
var color := Color.LIGHT_GRAY
|
|
|
|
# Alternate tile colors to create a checkerboard.
|
|
# Even coordinate sums use gray; odd sums use light gray.
|
|
if (x + y) % 2 == 0:
|
|
color = Color.GRAY
|
|
|
|
# Calculate the position and size of the tile.
|
|
# Multiplying x and y by tile_size converts
|
|
# grid coordinates into pixel coordinates.
|
|
var tile_rect := Rect2(
|
|
x * tile_size,
|
|
y * tile_size,
|
|
tile_size,
|
|
tile_size
|
|
)
|
|
|
|
# Draw the filled tile.
|
|
draw_rect(tile_rect, color)
|
|
|
|
# Draw a black outline around each tile.
|
|
draw_rect(tile_rect, Color.BLACK, false, 1.0)
|
|
|
|
|
|
#------------------------------------------------------------
|
|
# Movement Validation
|
|
#------------------------------------------------------------
|
|
|
|
# Checks whether a player can move to a given tile.
|
|
#
|
|
# Parameter:
|
|
# tile - The grid coordinate the player wants to enter.
|
|
#
|
|
# Returns:
|
|
# true - The coordinate is inside the board.
|
|
# false - The coordinate is outside the board.
|
|
|
|
func can_move_to(tile: Vector2i) -> bool:
|
|
|
|
return (
|
|
tile.x >= 0
|
|
and tile.x < width
|
|
and tile.y >= 0
|
|
and tile.y < height
|
|
)
|
|
|
|
|
|
#------------------------------------------------------------
|
|
# Coordinate Conversion
|
|
#------------------------------------------------------------
|
|
|
|
# Converts a grid coordinate into a world position.
|
|
#
|
|
# The player uses this function to position itself
|
|
# at the center of a tile after moving.
|
|
#
|
|
# Parameter:
|
|
# tile - The grid coordinate to convert.
|
|
#
|
|
# Returns:
|
|
# The center of the tile in world coordinates.
|
|
|
|
func get_tile_world_position(tile: Vector2i) -> Vector2:
|
|
|
|
# Calculate the center of the tile in local coordinates.
|
|
# Adding 0.5 shifts the position from the top-left
|
|
# corner of the tile to its center.
|
|
var tile_center := Vector2(
|
|
(tile.x + 0.5) * tile_size,
|
|
(tile.y + 0.5) * tile_size
|
|
)
|
|
|
|
# Convert the local tile position to world coordinates.
|
|
# This also accounts for the Board node's position.
|
|
return to_global(tile_center)
|