From 46d7e793031a4ac534aa50da43408f4330abe8dc Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 9 Feb 2024 00:05:05 -0500 Subject: Timer --- racing/load.gd | 25 +++++++++++++++++++++++++ racing/lobby.gd | 2 +- racing/manager.gd | 6 ++++++ racing/multiplayer.gd | 21 +++++++++++++++------ 4 files changed, 47 insertions(+), 7 deletions(-) (limited to 'racing') diff --git a/racing/load.gd b/racing/load.gd index cd60c65..1455171 100644 --- a/racing/load.gd +++ b/racing/load.gd @@ -29,14 +29,23 @@ func _load(): label.align = Label.ALIGN_RIGHT label.valign = Label.VALIGN_TOP + var timer_label = Label.new() + timer_label.set_name("timer_label") + timer_label.margin_left = 20.0 + timer_label.margin_top = 20.0 + timer_label.align = Label.ALIGN_LEFT + timer_label.valign = Label.VALIGN_TOP + var dynamic_font = DynamicFont.new() dynamic_font.font_data = load("res://fonts/Lingo.ttf") dynamic_font.size = 36 dynamic_font.outline_color = Color(0, 0, 0, 1) dynamic_font.outline_size = 2 label.add_font_override("font", dynamic_font) + timer_label.add_font_override("font", dynamic_font) add_child(label) + add_child(timer_label) var panel_script = load("res://nodes/panel_1.tscn") var victory_panel = panel_script.instance() @@ -56,6 +65,13 @@ func _load(): "MeshInstance5" ) + var timer = Timer.new() + timer.name = "Timer" + timer.set_wait_time(0.03) + timer.set_one_shot(false) + timer.connect("timeout", self, "bump_timer") + add_child(timer) + global._print("Hooked Load Stop") ._load() @@ -66,6 +82,7 @@ func _load(): func victory(): + get_node("Timer").stop() get_node("Multiplayer").player_victory() ingame_achieve("Victory!") @@ -76,3 +93,11 @@ func set_gridmap_tile(x, y, z, tile): var mapvec = gridmap.world_to_map(gridmap.to_local(Vector3(x, y, z))) gridmap.set_cell_item(mapvec.x, mapvec.y, mapvec.z, mesh_library.find_item_by_name(tile)) + + +func bump_timer(): + var race_manager = global.get_node("RaceManager") + var elapsed = float(Time.get_ticks_msec() - race_manager.started_time) / 1000.0 + get_node("timer_label").text = ( + "%d:%02d:%02.2f" % [int(elapsed) / 60 / 60, int(elapsed) / 60 % 60, fmod(elapsed, 60.0)] + ) diff --git a/racing/lobby.gd b/racing/lobby.gd index 38402de..c6a3e24 100644 --- a/racing/lobby.gd +++ b/racing/lobby.gd @@ -15,7 +15,7 @@ const MAX_PLAYERS = 250 const PROTOCOL_VERSION = 2 const RECIPIENT_BROADCAST_ALL = -1 const LOBBY_MAP_NAME = "ll1_racing" -const VERSION = "0.0.6" +const VERSION = "0.0.7" const LL1_AREAS = [ ["Starting Room", 0, 0, 0], diff --git a/racing/manager.gd b/racing/manager.gd index f8b7ef7..349b83e 100644 --- a/racing/manager.gd +++ b/racing/manager.gd @@ -6,6 +6,7 @@ var start_pos var end_pos var lobby_id var held_messages = [] +var started_time func everyone_ready(): @@ -29,3 +30,8 @@ func everyone_ready(): yield(get_tree().create_timer(0.5), "timeout") get_node("/root/Spatial/Multiplayer").send_timeout() + + +func start_timer(): + started_time = Time.get_ticks_msec() + get_node("/root/Spatial/Timer").start() diff --git a/racing/multiplayer.gd b/racing/multiplayer.gd index 52d76bf..0dfebf2 100644 --- a/racing/multiplayer.gd +++ b/racing/multiplayer.gd @@ -143,8 +143,10 @@ func _on_lobby_joined(lobby_id: int, _permissions: int, _locked: bool, result: i func _on_lobby_chat_update( - _lobby_id: int, _member_id: int, _making_change_id: int, _chat_state: int + _lobby_id: int, member_id: int, _making_change_id: int, chat_state: int ) -> void: + if chat_state == Steam.CHAT_MEMBER_STATE_CHANGE_LEFT: + messages.showMessage("%s has left the room" % Steam.getFriendPersonaName(member_id)) _update_lobby_members() @@ -231,7 +233,8 @@ func _handle_packet(packet): if "timeout" in data: _update_lobby_members() if "victory" in data: - _someone_victory(Steam.getFriendPersonaName(remote_id)) + _someone_victory(Steam.getFriendPersonaName(remote_id), data["victory"]) + messages.showMessage("%s reached the goal" % Steam.getFriendPersonaName(remote_id)) if "ack" in data: messages_needing_ack.erase(data["ack"]) _update_lobby_members() @@ -294,6 +297,7 @@ func time_to_start(): get_node("/root/Spatial/label").text = ( "Destination: %s" % global.get_node("RaceManager").end_pos[0] ) + global.get_node("RaceManager").start_timer() get_node("/root/Spatial").ingame_achieve("Go!") @@ -302,15 +306,20 @@ func player_victory(): global._print("(MP) Victory!") is_victory = true _send_p2p_packet( - {"victory": "true"}, RECIPIENT_BROADCAST_ALL, Steam.P2P_SEND_RELIABLE, true + {"victory": get_node("/root/Spatial/timer_label").text}, + RECIPIENT_BROADCAST_ALL, + Steam.P2P_SEND_RELIABLE, + true + ) + _someone_victory( + Steam.getFriendPersonaName(player_steam_id), get_node("/root/Spatial/timer_label").text ) - _someone_victory(Steam.getFriendPersonaName(player_steam_id)) -func _someone_victory(name): +func _someone_victory(name, time): if !victorious_players.has(name): global._print("(MP) Someone's victory") victorious_players.append(name) var label = get_node("/root/Spatial/label") - label.text = "%s\n%s is #%d" % [label.text, name, victorious_players.size()] + label.text = "%s\n#%d: %s (%s)" % [label.text, victorious_players.size(), name, time] -- cgit 1.4.1