78 lines
2.3 KiB
GDScript
78 lines
2.3 KiB
GDScript
extends Node3D
|
|
class_name Level
|
|
|
|
enum GameState {
|
|
WAITING, # Stop state to be used by default, nothing should react to this
|
|
TAGGER_GROUNDED, # Tagger is on the ground
|
|
TAGGER_UNGROUNDED, # Tagger is off the ground
|
|
TAGGER_CHANGE, # Tagger has caught someone, and new person must go to ground
|
|
}
|
|
|
|
var game_state: GameState = GameState.WAITING
|
|
var game_tagger: Player
|
|
|
|
signal state_change(old: GameState, new: GameState)
|
|
signal state_enter_tagger_grounded
|
|
signal state_leave_tagger_grounded
|
|
signal state_enter_tagger_ungrounded
|
|
signal state_leave_tagger_ungrounded
|
|
signal state_enter_tagger_change(new_tagger: Player)
|
|
signal state_leave_tagger_change
|
|
|
|
func set_tagger_grounded(v: bool):
|
|
if v and game_state == GameState.TAGGER_CHANGE:
|
|
_change_state(GameState.TAGGER_GROUNDED)
|
|
return
|
|
if not (game_state == GameState.TAGGER_GROUNDED or game_state == GameState.TAGGER_UNGROUNDED):
|
|
return
|
|
if (not v) and game_state == GameState.TAGGER_GROUNDED:
|
|
_change_state(GameState.TAGGER_UNGROUNDED)
|
|
if v and game_state == GameState.TAGGER_UNGROUNDED:
|
|
_change_state(GameState.TAGGER_GROUNDED)
|
|
|
|
func set_tagger(who: Player):
|
|
if game_state == GameState.WAITING:
|
|
return
|
|
game_tagger = who
|
|
_change_state(GameState.TAGGER_CHANGE)
|
|
|
|
func _change_state(new_state: GameState):
|
|
if game_state == new_state:
|
|
return
|
|
if new_state == GameState.WAITING:
|
|
game_tagger = null
|
|
state_change.emit(game_state, new_state)
|
|
if new_state == GameState.TAGGER_GROUNDED:
|
|
state_enter_tagger_grounded.emit()
|
|
if new_state == GameState.TAGGER_UNGROUNDED:
|
|
state_enter_tagger_ungrounded.emit()
|
|
if new_state == GameState.TAGGER_CHANGE:
|
|
state_enter_tagger_change.emit(game_tagger)
|
|
|
|
var old_state = game_state
|
|
game_state = new_state
|
|
|
|
if old_state == GameState.TAGGER_GROUNDED:
|
|
state_leave_tagger_grounded.emit()
|
|
if old_state == GameState.TAGGER_UNGROUNDED:
|
|
state_leave_tagger_ungrounded.emit()
|
|
if old_state == GameState.TAGGER_CHANGE:
|
|
state_leave_tagger_change.emit()
|
|
|
|
|
|
|
|
func _ready():
|
|
# Hacky way to start level without going though multiplayer screens
|
|
if get_parent() == get_tree().root:
|
|
%PlayerSpawner.spawn(multiplayer.get_unique_id())
|
|
|
|
# Called from the server
|
|
func server_add_player(id: int):
|
|
%PlayerSpawner.spawn(id)
|
|
|
|
static func find_level(node: Node) -> Level:
|
|
var parent := node
|
|
while not (node is Level):
|
|
node = node.get_parent()
|
|
return node
|