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
|
extends Node
signal stage_state_changed(state: State)
@warning_ignore("unused_signal")
signal placed_tower(tower: Tower)
signal multi_select_finished(nodes)
var previous_scene: String
var game_lobby_url := "http://localhost:8910"
@onready var game_lobby_server: HTTPRequest = %GameLobbyServer
var current_game_id := ""
var state: State :
set(value):
state = value
stage_state_changed.emit(value)
var current_stage: Stage
var selection: SelectionManager
var player: Player:
get():
return Network.get_player(multiplayer.get_unique_id())
func _notification(what: int) -> void:
if what == NOTIFICATION_WM_CLOSE_REQUEST:
if multiplayer.is_server():
await Network.close_game() # TODO: doesn't work?
func initialize_stage(stage: Stage):
current_stage = stage
func ready_stage(_stage: Stage):
selection = preload("res://Game/Selection/SelectionManager.tscn").instantiate()
add_child(selection)
func place_tower(tower: Tower):
if not current_stage.can_place_tower():
return
var network_id = multiplayer.get_unique_id()
tower.owner_id = network_id
tower.name = "Tower@" + str(network_id) + "@" + str(Time.get_ticks_usec())
current_stage.place_tower.rpc(inst_to_dict(tower.to_network_data()))
func remove_tower(tower: Tower):
if tower.owner_id == multiplayer.get_unique_id():
destroy_tower(tower)
func destroy_tower(tower: Tower):
current_stage.destroy_tower(tower)
Network.destroy_tower.rpc(inst_to_dict(tower.to_network_data()))
player.towers.erase(tower.global_position)
func select_tower(tower: Tower):
if tower.owner_id == multiplayer.get_unique_id():
tower.is_selected = true
func multi_select(layer: int):
var selection_area = preload("res://Game/Selection/MultiSelectArea.tscn").instantiate()
selection_area.set_collision_mask_value(layer, true)
selection_area.select.connect(func(nodes):
for node in nodes:
Client.select_tower(node)
multi_select_finished.emit(nodes)
)
get_tree().current_scene.add_child(selection_area)
func update_tower(path: NodePath, data: Tower.NetworkData):
Network.update_tower.rpc(path, inst_to_dict(data))
func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null):
if not current_stage.can_spawn_unit(unit):
return
var network_id = multiplayer.get_unique_id()
unit.owner_id = network_id
unit.name = "Unit@" + str(network_id) + "@" + str(Time.get_ticks_usec())
unit.global_position = spawn.spawn_position
unit.target = spawn.next_node
if overwrite_target:
unit.target = overwrite_target
current_stage.spawn_unit.rpc(inst_to_dict(unit.to_network_data()))
func update_unit(path: NodePath, data: Unit.NetworkData):
Network.update_unit.rpc(path, inst_to_dict(data))
func change_scene_to_instance(scene: Node):
(func():
get_tree().current_scene.queue_free()
get_tree().root.add_child(scene)
get_tree().current_scene = scene
).call_deferred()
func array_intersect(first, second):
var compare = {}
for value in first:
compare[value] = true
for value in second:
if compare.get(value, false):
return true
return false
class HTTPResponse extends Resource:
var result_code: HTTPRequest.Result
var response_code: int
var headers: PackedStringArray
var body: PackedByteArray
func parse_json() -> Dictionary:
return JSON.parse_string(body.get_string_from_utf8())
func request(
url: String,
custom_headers: PackedStringArray = PackedStringArray(),
method: HTTPClient.Method = HTTPClient.METHOD_GET,
request_data: String = ""
) -> HTTPResponse:
var response = HTTPResponse.new()
%GameLobbyServer.request_completed.connect(
func (
result_code: int,
response_code: int,
headers: PackedStringArray,
body: PackedByteArray
):
response.result_code = result_code
response.response_code = response_code
response.headers = headers
response.body = body
)
%GameLobbyServer.request(url, custom_headers, method, request_data)
await %GameLobbyServer.request_completed
return response
func check_game_lobby_available() -> bool:
var response = await Client.request("%s" % [Client.game_lobby_url], [], HTTPClient.METHOD_GET)
return response.result_code == HTTPRequest.RESULT_SUCCESS
|