64 lines
1.5 KiB
GDScript
64 lines
1.5 KiB
GDScript
extends LevelState
|
|
|
|
|
|
func _enter(_previous_state_path: String, _data: Dictionary = {"round": 0, "restart": false}) -> void:
|
|
get_tree().paused = true
|
|
assert((_data["round"] as int) in range(3), "round number is invalid")
|
|
assert(
|
|
(_data["restart"] as bool) == true or
|
|
(_data["restart"] as bool) == false,
|
|
"round restart data is invalid"
|
|
)
|
|
level.current_round = _data["round"]
|
|
level.restarted = _data["restart"]
|
|
|
|
if level.restarted:
|
|
level.current_round = 0
|
|
level.scoreboard = Vector2i(0,0)
|
|
level.restarted = false
|
|
|
|
assert(
|
|
(level.player_1 != null and level.player_2 != null) or
|
|
(level.player_1 == null and level.player_2 == null),
|
|
"desynced player instantiation?"
|
|
)
|
|
|
|
if level.player_1 != null and level.player_2 != null:
|
|
level.player_1.queue_free()
|
|
level.player_2.queue_free()
|
|
|
|
level.current_round += 1
|
|
|
|
if level.current_round == 4:
|
|
finished.emit(FINISHING)
|
|
return
|
|
|
|
level.player_1 = _instantiate_player(1)
|
|
level.player_2 = _instantiate_player(2)
|
|
level.ball = _instantiate_ball()
|
|
|
|
|
|
func _instantiate_player(id: int) -> Player:
|
|
var s: PlayerSpawnPoint = level.player_1_spawn if id == 1 else level.player_2_spawn
|
|
var p: Player = s.player_scene.instantiate()
|
|
|
|
p.id = id
|
|
p.name = "Player"+str(id)
|
|
|
|
level.add_child(p)
|
|
|
|
p.global_position = s.global_position
|
|
p.global_position = s.global_position
|
|
|
|
return p
|
|
|
|
|
|
func _instantiate_ball() -> Ball:
|
|
var s: BallSpawnPoint = level.ball_spawn
|
|
var b: Ball = s.ball_scene.instantiate()
|
|
|
|
level.add_child(b)
|
|
|
|
b.global_position = s.global_position
|
|
|
|
return b
|