aboutsummaryrefslogtreecommitdiff
path: root/client/game.gd
diff options
context:
space:
mode:
Diffstat (limited to 'client/game.gd')
-rw-r--r--client/game.gd48
1 files changed, 27 insertions, 21 deletions
diff --git a/client/game.gd b/client/game.gd
index cd678897..7cfab9c9 100644
--- a/client/game.gd
+++ b/client/game.gd
@@ -21,9 +21,8 @@ extends Node3D
signal update_players(players: Dictionary)
signal data_updated()
signal in_lobby_updated(in_lobby: bool)
+signal join_state_updated(state: JoinState)
signal text_message(player: int, text: String, timeout_initial: float, timeout_remaining: float)
-signal joined()
-signal left()
signal update_tutorial_running(running: bool)
enum SpectatingMode {
@@ -31,6 +30,12 @@ enum SpectatingMode {
FREE,
}
+enum JoinState {
+ SPECTATING,
+ WAITING,
+ JOINED,
+}
+
var player_id: int = -1
var item_names: Array = []
var item_index_by_name: Dictionary = {}
@@ -42,10 +47,10 @@ var maps: Array = []
var bot_algos: Array
var text_message_history: Array[Array] = []
+var join_state: JoinState = JoinState.SPECTATING
+
var in_lobby := false
var is_replay := false
-var is_joined := false
-var join_sent := false
var tutorial_running := false
var tutorial_queue := []
var last_position := Vector2(0, 0)
@@ -99,9 +104,7 @@ func handle_packet(p):
in_lobby_updated.connect(player_instance.onscreen_controls.in_lobby_updated)
player_instance.onscreen_controls.in_lobby_updated(in_lobby)
camera.target = player_instance.movement_base
- is_joined = true
- join_sent = false
- joined.emit()
+ set_join_state(JoinState.JOINED)
else:
player_instance = Player.new(p.id, p.name, p.position, p.character, self)
players[p.id] = player_instance
@@ -115,8 +118,7 @@ func handle_packet(p):
tutorial_queue.erase(player.current_item_message)
pinned_items.clear_item(p.id)
if p.id == player_id:
- is_joined = false
- left.emit()
+ set_join_state(JoinState.SPECTATING)
camera.target = $Center
if player.hand != null:
player.hand.queue_free()
@@ -253,8 +255,8 @@ func handle_packet(p):
await get_parent()._menu_exit()
lobby.visible = in_lobby
- if lobby and not join_sent:
- join()
+ if lobby and join_state == JoinState.SPECTATING:
+ toggle_join()
in_lobby_updated.emit(in_lobby)
"score":
@@ -324,16 +326,20 @@ func handle_packet(p):
_: push_error("Unrecognized packet type: %s" % p.type)
-func join():
- if join_sent:
- push_error("Join was already sent. Tried to join twice.")
- return
- join_sent = true
- mp.send_join(Global.get_profile("username"), Global.get_profile("character"))
+func set_join_state(state: JoinState):
+ join_state = state
+ join_state_updated.emit(state)
-func leave():
- join_sent = false
- mp.send_leave(player_id)
+func toggle_join():
+ match join_state:
+ JoinState.SPECTATING:
+ set_join_state(JoinState.WAITING)
+ mp.send_join(Global.get_profile("username"), Global.get_profile("character"))
+ JoinState.WAITING:
+ push_error("Join/Leave action already toggled.")
+ JoinState.JOINED:
+ set_join_state(JoinState.WAITING)
+ mp.send_leave(player_id)
func _process(delta):
update_center()
@@ -360,7 +366,7 @@ func get_tile_interactive(pos: Vector2i) -> bool:
func update_center():
- if is_joined:
+ if join_state != JoinState.SPECTATING:
return
if Input.get_vector("left", "right", "forwards", "backwards").normalized().length() > .1:
spectating_mode = SpectatingMode.FREE