summaryrefslogtreecommitdiff
path: root/Game/Client.gd
diff options
context:
space:
mode:
authorDaniel Weipert <git@mail.dweipert.de>2024-11-26 17:13:40 +0100
committerDaniel Weipert <git@mail.dweipert.de>2024-11-26 17:13:40 +0100
commitcb9bb9d1c757b53707c741a3c8c218b513ffb5ec (patch)
tree1e3741a70672504d71685db98b22f7fe61e0e6cb /Game/Client.gd
parent72863ffa31e27f4c6be862d80472cfb897fccaea (diff)
next commit
Diffstat (limited to 'Game/Client.gd')
-rw-r--r--Game/Client.gd63
1 files changed, 51 insertions, 12 deletions
diff --git a/Game/Client.gd b/Game/Client.gd
index 13fae56..fd24a07 100644
--- a/Game/Client.gd
+++ b/Game/Client.gd
@@ -9,6 +9,12 @@ 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
@@ -23,6 +29,12 @@ var player: Player:
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
@@ -39,14 +51,6 @@ func place_tower(tower: Tower):
tower.owner_id = network_id
tower.name = "Tower@" + str(network_id) + "@" + str(Time.get_ticks_usec())
- #tower.attack_range = [
- #current_stage.map.tile_set.tile_size.x * 2,
- #current_stage.map.tile_set.tile_size.x * 3,
- #current_stage.map.tile_set.tile_size.x * 4,
- #].pick_random()
- #tower.attack_power = [1, 2, 3, 4].pick_random()
- #tower.attack_speed = [1, 2, 3, 4].pick_random()
-
current_stage.place_tower.rpc(inst_to_dict(tower.to_network_data()))
@@ -83,7 +87,7 @@ func update_tower(path: NodePath, data: Tower.NetworkData):
func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null):
- if not current_stage.can_spawn_unit():
+ if not current_stage.can_spawn_unit(unit):
return
var network_id = multiplayer.get_unique_id()
@@ -92,13 +96,10 @@ func spawn_unit(unit: Unit, spawn: Spawn, overwrite_target: PathNode = null):
unit.global_position = spawn.spawn_position
unit.target = spawn.next_node
- #unit.hp = randi_range(5, 10) #20000b
- #unit.base_speed = randi_range(50, 150)
if overwrite_target:
unit.target = overwrite_target
- #var data := UnitCreationData.fromUnit(unit)
current_stage.spawn_unit.rpc(inst_to_dict(unit.to_network_data()))
@@ -124,3 +125,41 @@ func array_intersect(first, second):
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