clockwork-city/prefabs/board_old.gd

275 lines
9.0 KiB
GDScript3
Raw Permalink Normal View History

class_name BoardOLD extends Node2D
enum Direction { NONE, UP, DOWN, LEFT, RIGHT }
var buildings: Dictionary[Vector2i, Building] = {}
var active_building_id: int = -1
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] = []
var is_controlling_camera: bool = false
@onready var tile_map: SceneTileMapLayer = %Tiles
@onready var tile_preview_map: TileMapLayer = %TilesPreview
@onready var wall_map: SceneTileMapLayer = %Walls
@onready var wall_preview_map: SceneTileMapLayer = %WallPreviews
func _input(event: InputEvent) -> void:
if event.is_action_pressed("camera_engage"):
is_controlling_camera = true
return
if event.is_action_released("camera_engage"):
is_controlling_camera = false
if is_controlling_camera:
return
if Globals.board_game.current_player.id != Globals.game.this_player.id:
return
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:
place_active_tile.rpc(current_map_coord, active_tile_id)
active_tile_id = -1
elif active_building != null:
place_active_building.rpc(current_map_coord, active_building_id)
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)
@rpc("any_peer", "call_local", "reliable")
func place_active_building(coords: Vector2i, building_id: int) -> void:
var building = Globals.board_game.building_id_array[building_id].duplicate()
add_child(building)
if building is Home:
building.player = Globals.board_game.current_player
building.starting_coord = coords
for coord in building.get_tile_coords():
buildings[coord] = building
building.position = tile_map.map_to_local(coords)
building.modulate = Color(1, 1, 1, 1)
building.is_placing = false
if active_building != null:
Globals.game.add_player_money(Globals.board_game.current_player.id, -active_building.cost)
if building is Home:
print("here")
Globals.board_game.place_home()
if is_instance_valid(active_building):
active_building.queue_free()
@rpc("any_peer", "call_local", "reliable")
func place_active_tile(coords: Vector2i, tile_id: int) -> void:
tile_preview_map.set_cell(coords)
tile_map.set_cell(coords)
await tile_map.child_exiting_tree
tile_map.set_cell(coords, 0, Vector2i.ZERO, tile_id)
if tile_id >= 7 and tile_id <= 10:
Globals.board_game.handle_spawn_placed()
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)
@rpc("any_peer", "call_local", "reliable")
func set_active_building(building: Building, player: Player = null) -> void:
active_building_id = Globals.board_game.get_id_for_building(building)
active_building = building
add_child(active_building)
active_building.is_placing = true
active_building.modulate = Color(1, 1, 1, 0.5)
if building is Home:
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)
func initialize() -> void:
for i in range(Globals.game.players.size()):
var random_spawn = Vector2i(randi_range(0, 9), randi_range(0, 9))
for x in range(10):
for y in range(10):
var coord = Vector2i(x, y)
if i == 1:
coord.x += 10
if i == 2:
coord.y += 10
if i == 3:
coord += Vector2i(10, 10)
tile_map.set_cell(coord)
if x == random_spawn.x and y == random_spawn.y:
var possible_spawns = [7, 8, 9, 10]
if random_spawn.x == 9:
possible_spawns.erase(9)
elif random_spawn.x == 0:
possible_spawns.erase(8)
if random_spawn.y == 9:
possible_spawns.erase(7)
elif random_spawn.y == 0:
possible_spawns.erase(10)
tile_map.set_cell(coord, 0, Vector2i.ZERO, possible_spawns.pick_random())
else:
tile_map.set_cell(coord, 0, Vector2i.ZERO, 1)
var extra_spawn_coord := Vector2i(
randi_range(1, tile_map.get_used_rect().size.x - 1),
randi_range(1, tile_map.get_used_rect().size.y - 1)
)
tile_map.set_cell(extra_spawn_coord, 0, Vector2i.ZERO, randi_range(7, 10))
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