duncgibbs cb0c6e18f5
Some checks failed
linting & formatting / build (push) Failing after 22s
creates all initial tiles and buildings
2026-04-15 10:45:43 -05:00

208 lines
6.8 KiB
GDScript

class_name Board extends Node2D
enum Direction { NONE, UP, DOWN, LEFT, RIGHT }
var buildings: Dictionary[Vector2i, Building] = {}
var active_building: Building
var active_tile_id: int = -1
var current_map_coord: Vector2i
var prev_map_coord: Vector2i
var is_placing_walls: bool = false:
set(value):
is_placing_walls = value
if !is_placing_walls:
_clear_wall_preview()
var current_wall_direction: Direction = Direction.DOWN
var real_wall_coords: Array[Vector2i] = []
@onready var tile_map: SceneTileMapLayer = %Tiles
@onready var tile_preview_map: SceneTileMapLayer = %TilesPreview
@onready var wall_map: SceneTileMapLayer = %Walls
@onready var wall_preview_map: SceneTileMapLayer = %WallPreviews
func _input(event: InputEvent) -> void:
if event is InputEventMouseMotion:
current_map_coord = tile_map.local_to_map(tile_map.get_local_mouse_position())
_place_wall_preview()
_place_tile_preview()
if active_building != null:
active_building.position = tile_map.map_to_local(current_map_coord)
prev_map_coord = current_map_coord
if event.is_action_pressed("select"):
if is_placing_walls:
_place_wall(current_map_coord, current_wall_direction)
match current_wall_direction:
Direction.UP:
_place_wall(current_map_coord + Vector2i.UP, Direction.DOWN)
Direction.DOWN:
_place_wall(current_map_coord + Vector2i.DOWN, Direction.UP)
Direction.LEFT:
_place_wall(current_map_coord + Vector2i.LEFT, Direction.RIGHT)
Direction.RIGHT:
_place_wall(current_map_coord + Vector2i.RIGHT, Direction.LEFT)
elif active_tile_id != -1:
tile_preview_map.set_cell(current_map_coord)
tile_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id)
if active_tile_id >= 7 and active_tile_id <= 10:
active_tile_id = -1
Globals.game.handle_spawn_placed()
else:
active_tile_id = -1
elif active_building != null:
active_building.starting_coord = current_map_coord
for coord in active_building.get_tile_coords():
buildings[coord] = active_building
active_building.position = tile_map.map_to_local(current_map_coord)
active_building.modulate = Color(1, 1, 1, 1)
active_building.is_placing = false
active_building = null
if event.is_action_pressed("rotate_wall_up") and is_placing_walls:
current_wall_direction = get_next_direction(current_wall_direction)
var wall_preview = wall_preview_map.get_cell_scene(current_map_coord)
if is_instance_valid(wall_preview):
wall_preview.set_wall(current_wall_direction)
elif event.is_action_pressed("rotate_wall_down") and is_placing_walls:
current_wall_direction = get_previous_direction(current_wall_direction)
var wall_preview = wall_preview_map.get_cell_scene(current_map_coord)
if is_instance_valid(wall_preview):
wall_preview.set_wall(current_wall_direction)
_handle_building_rotation(event)
_handle_spawn_rotation(event)
func set_active_tile(tile: Tile) -> void:
active_tile_id = tile_map.tile_set.get_tile_id(tile)
if active_tile_id != -1:
tile_preview_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id)
func set_active_building(building: Building, player: Player = null) -> void:
active_building = building.duplicate()
add_child(active_building)
active_building.is_placing = true
active_building.modulate = Color(1, 1, 1, 0.5)
if player != null:
active_building.player = player
func _place_wall(wall_coord: Vector2i, wall_direction: Direction) -> void:
var current_wall = wall_map.get_cell_scene(wall_coord)
if is_instance_valid(current_wall):
current_wall.add_wall(wall_direction)
else:
var wall_preview = wall_map.get_cell_scene(wall_coord)
if is_instance_valid(wall_preview):
wall_preview.set_wall(wall_direction)
func _place_wall_preview() -> void:
if is_placing_walls:
if prev_map_coord != current_map_coord:
var previous_preview = wall_preview_map.get_cell_scene(prev_map_coord)
if is_instance_valid(previous_preview):
previous_preview.set_wall(Direction.NONE)
var wall_preview = wall_preview_map.get_cell_scene(current_map_coord)
if is_instance_valid(wall_preview):
wall_preview.set_wall(current_wall_direction)
func _clear_wall_preview() -> void:
wall_preview_map.get_cell_scene(prev_map_coord).set_wall(Direction.NONE)
wall_preview_map.get_cell_scene(current_map_coord).set_wall(Direction.NONE)
func _place_tile_preview() -> void:
if active_tile_id != -1:
if prev_map_coord != current_map_coord:
var prev_preview_tile_id = tile_preview_map.get_cell_alternative_tile(prev_map_coord)
if prev_preview_tile_id != -1:
tile_preview_map.set_cell(prev_map_coord)
tile_preview_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id)
func _handle_building_rotation(event: InputEvent) -> void:
if active_building == null:
return
if event.is_action_pressed("rotate_tile_up"):
active_building.rotation_degrees += 90
active_building.tile_rotation = get_next_direction(active_building.tile_rotation)
elif event.is_action_pressed("rotate_tile_down"):
active_building.rotation_degrees -= 90
active_building.tile_rotation = get_previous_direction(active_building.tile_rotation)
func _handle_spawn_rotation(event: InputEvent) -> void:
if active_tile_id < 7 or active_tile_id > 10:
return
if event.is_action_pressed("rotate_tile_up"):
active_tile_id += 1
if active_tile_id > 10:
active_tile_id = 7
tile_preview_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id)
elif event.is_action_pressed("rotate_tile_down"):
active_tile_id -= 1
if active_tile_id < 7:
active_tile_id = 10
tile_preview_map.set_cell(current_map_coord, 0, Vector2i.ZERO, active_tile_id)
static func get_next_direction(direction: Direction, count: int = 1) -> Direction:
var result := direction
if count == 0:
return result
match direction:
Direction.UP:
result = Direction.RIGHT
Direction.RIGHT:
result = Direction.DOWN
Direction.DOWN:
result = Direction.LEFT
Direction.LEFT:
result = Direction.UP
if count == 1:
return result
return get_next_direction(result, count - 1)
static func get_previous_direction(direction: Direction) -> Direction:
match direction:
Direction.UP:
return Direction.LEFT
Direction.RIGHT:
return Direction.UP
Direction.DOWN:
return Direction.RIGHT
Direction.LEFT:
return Direction.DOWN
_:
return direction
static func get_direction_vector(direction: Direction) -> Vector2i:
match direction:
Direction.UP:
return Vector2i.UP
Direction.RIGHT:
return Vector2i.RIGHT
Direction.DOWN:
return Vector2i.DOWN
Direction.LEFT:
return Vector2i.LEFT
_:
return Vector2i.ZERO
#func generate() -> void:
#for child in get_children():
#child.queue_free()
#for x in range(width):
#for y in range(height):
#var tile: Control
#if x == 1 and y == 1:
#tile = spawn_scene.instantiate()
#else:
#tile = ground_scene.instantiate()
#add_child(tile)
#tile.position = get_coordinate(x, y)
#tile.owner = self