summaryrefslogtreecommitdiff
path: root/stage/stage.gd
blob: 9b1689feaf2e71f2811fc35fc98f69e710222375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
class_name Stage
extends Node2D


var grid := AStarGrid2D.new()
var teams: Dictionary[String, Team] = {}

@onready var grid_selector: GridSelector = %GridSelector


func _ready() -> void:
	grid.region = $Ground.get_used_rect()
	grid.cell_size = $Floor.tile_set.tile_size
	grid.cell_shape = AStarGrid2D.CELL_SHAPE_ISOMETRIC_DOWN
	grid.diagonal_mode = AStarGrid2D.DIAGONAL_MODE_NEVER
	grid.update()
	
	# set whole grid non-walkable initially
	grid.fill_solid_region(grid.region, true)
	
	# pre-set floor tiles as walkable
	for tile in $Floor.get_used_cells():
		grid.set_point_solid(tile, false)
	
	grid_selector.move_mode_max_range = Network.current_player.current_move_points
	
	# teams
	var team_one := Team.new()
	team_one.placement_tile_source_id = 1
	team_one.placement_tile_atlas_coordinates = Vector2(1, 6)
	team_one.placement_tile_alternative_tile = 0
	var team_two := Team.new()
	team_two.placement_tile_source_id = 1
	team_two.placement_tile_atlas_coordinates = Vector2(3, 6)
	team_two.placement_tile_alternative_tile = 0
	teams["1"] = team_one
	teams["2"] = team_two
	
	if multiplayer.is_server():
		Network.local_player.set_current_team_name("1")
	else:
		Network.local_player.set_current_team_name("2")
	
	grid_selector.current_team = Network.local_player.current_team_name
	
	# remote selector
	Network.current_player_changed.connect(func():
		if Network.is_my_turn():
			%RemoteSelector.visible = false
			_on_grid_selector_moved($Ground.local_to_map(%GridSelector.global_position))
		else:
			await get_tree().process_frame
			%RemoteSelector.modulate = Network.current_player.get_color()
			%RemoteSelector.visible = true
	)
	%RemoteSelector.modulate = Network.current_player.get_color()
	if Network.is_my_turn():
		%RemoteSelector.visible = false


func _input(event: InputEvent) -> void:
	if event.is_action_pressed("menu") and Network.is_my_turn():
		$HUDMain.visible = true
		grid_selector.process_mode = Node.PROCESS_MODE_DISABLED
		$HUDMain/PanelContainer/VBoxContainer/Button.grab_focus()


@rpc("any_peer", "call_local")
func place_tiles(grid_positions: Array, team: String) -> void:
	for p in grid_positions:
		$Floor.set_cell(
			p,
			teams[team].placement_tile_source_id,
			teams[team].placement_tile_atlas_coordinates,
			teams[team].placement_tile_alternative_tile
		)
	
	for p in grid_positions:
		grid.set_point_solid(p, false)
	
	grid_selector.current_state.draw($Ground.local_to_map(grid_selector.base_position))
	grid_selector.adjust_height(%RemoteSelector)


@rpc("any_peer", "call_local")
func place_unit(unit_position: Vector2, team: String) -> void:
	var unit = preload("res://unit/unit.tscn").instantiate()
	unit.global_position = unit_position + $Floor.position
	unit.current_team = team
	unit.modulate = Network.current_player.get_color()
	add_child(unit, true)
	
	# block unit tile for movement
	grid.set_point_solid($Ground.local_to_map(unit_position), true)


@rpc("any_peer", "call_remote")
func move_unit(unit_node_path: String, path: Array) -> void:
	var unit: Node2D = get_node(unit_node_path)
	var tween = get_tree().create_tween()
	for p in path.slice(1): # remove starting position
		tween.tween_property(unit, "global_position", p + $Floor.position, 0.1)
	
	await tween.finished
	
	# clear previous tile and set new tile solid
	grid.set_point_solid($Ground.local_to_map(path[0]), false)
	grid.set_point_solid($Ground.local_to_map(path[path.size() - 1]), true)


func _on_grid_selector_placed_tiles(grid_positions: Array) -> void:
	place_tiles.rpc(grid_positions, Network.current_player.current_team_name)
	place_unit.rpc(%GridSelector.global_position, Network.current_player.current_team_name)
	grid_selector.current_state = grid_selector.state_select


func _on_grid_selector_move_mode_confirmed(path: Array) -> void:
	grid_selector.process_mode = Node.PROCESS_MODE_DISABLED
	
	move_unit.rpc(grid_selector.current_entity.get_path(), path)
	await move_unit(grid_selector.current_entity.get_path(), path)
	
	grid_selector.process_mode = Node.PROCESS_MODE_INHERIT
	grid_selector.current_state = grid_selector.get_node("StateSelect")
	grid_selector.current_state.draw($Ground.local_to_map(path[path.size() - 1]))
	
	Network.current_player.set_current_move_points(Network.current_player.current_move_points - (path.size() - 1))
	grid_selector.move_mode_max_range = Network.current_player.current_move_points


func _on_grid_selector_range_select_confirmed(grid_position: Vector2i, entity: Node2D) -> void:
	entity.queue_free()
	grid.set_point_solid(grid_position, false)


func _on_grid_selector_moved(grid_position: Vector2i) -> void:
	if Network.is_my_turn():
		move_remote_selector.rpc(grid_position)

@rpc("any_peer", "call_remote")
func move_remote_selector(grid_position: Vector2i) -> void:
	%RemoteSelector.global_position = $Ground.map_to_local(grid_position)
	grid_selector.adjust_height(%RemoteSelector)