summaryrefslogtreecommitdiff
path: root/stage/stage.gd
blob: b25e1daae0f21e4bef7f9976d6ea679a40e0c2de (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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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)
	
	for node in find_children("PlayerUnit*", "PlayerUnit"):
		grid.set_point_solid($Ground.local_to_map(node.global_position - $Floor.position), true)
	
	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.current_team_name = "1"
	else:
		Network.local_player.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"):
		grid_selector.process_mode = Node.PROCESS_MODE_DISABLED
		$HUDMain.enable() # TODO: shouldn't be possible to open when unitmenu open
		$HUDUnit.visible = false


@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.current_player = Network.current_player
	unit.modulate = Network.current_player.get_color()
	add_child(unit, true)
	
	Network.current_player.units.append(unit)
	
	# 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))
	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:
	if grid_selector.range_select_current_mode == GridSelectorStateRangeSelect.Mode.Attack:
		Network.current_player.current_attack_points -= 1
		attack_entity.rpc(grid_position, grid_selector.current_entity.get_path(), entity.get_path())

@rpc("any_peer", "call_local")
func attack_entity(grid_position: Vector2i, attacking_entity_node_path: String, target_entity_node_path: String):
	var attacking_entity := get_node(attacking_entity_node_path) as Unit
	var target_entity = get_node(target_entity_node_path)
	
	if target_entity is PlayerUnit:
		target_entity.current_hp -= 1
		print(target_entity, target_entity.current_hp)
		if target_entity.current_hp <= 0:
			target_entity.queue_free()
			print("GAME OVER!!")
	
	elif target_entity is Unit:
		var damage: int = attacking_entity.unit_data.attack - target_entity.unit_data.defense
		if damage > 0:
			target_entity.current_hp -= damage
		
		if target_entity.current_hp <= 0:
			if target_entity in Network.current_player.units:
				target_entity.current_player.units.erase(target_entity)
			
			target_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)