Round system
This commit is contained in:
parent
bcb42f8d16
commit
3df8247a84
32 changed files with 573 additions and 123 deletions
|
|
@ -9,10 +9,15 @@ signal lobby_joined
|
|||
signal lobby_closed
|
||||
signal update_teams_state
|
||||
|
||||
var in_lobby: bool = false
|
||||
|
||||
var attack_team: Array[int] = []
|
||||
var defence_team: Array[int] = []
|
||||
var specators_team: Array[int] = []
|
||||
|
||||
var win_score = 13
|
||||
var half_rounds = 12
|
||||
|
||||
func _ready() -> void:
|
||||
multiplayer.peer_disconnected.connect(player_left)
|
||||
multiplayer.server_disconnected.connect(server_disconnected)
|
||||
|
|
@ -36,6 +41,7 @@ func host() -> void:
|
|||
multiplayer.multiplayer_peer = peer
|
||||
lobby_created.emit()
|
||||
specators_team.append(multiplayer.get_unique_id())
|
||||
in_lobby = true
|
||||
|
||||
func join(ip: String) -> Error:
|
||||
var peer: ENetMultiplayerPeer = ENetMultiplayerPeer.new()
|
||||
|
|
@ -44,6 +50,7 @@ func join(ip: String) -> Error:
|
|||
return res
|
||||
multiplayer.multiplayer_peer = peer
|
||||
lobby_joined.emit()
|
||||
in_lobby = true
|
||||
return Error.OK
|
||||
|
||||
func leave() -> void:
|
||||
|
|
@ -52,6 +59,7 @@ func leave() -> void:
|
|||
defence_team.clear()
|
||||
specators_team.clear()
|
||||
lobby_closed.emit()
|
||||
in_lobby = false
|
||||
|
||||
func add_and_sync_peer(id: int) -> void:
|
||||
if multiplayer.is_server() == false:
|
||||
|
|
@ -100,3 +108,16 @@ func team_switch_notification(id: int, team: int) -> void:
|
|||
@rpc("authority","call_local","reliable")
|
||||
func start_game() -> void:
|
||||
get_tree().change_scene_to_file("res://levels/prototype_scene.tscn")
|
||||
if multiplayer.is_server():
|
||||
await get_tree().scene_changed
|
||||
Session.start_session()
|
||||
|
||||
func get_team() -> Session.TEAMS:
|
||||
var id = multiplayer.get_unique_id()
|
||||
if attack_team.has(id):
|
||||
return Session.TEAMS.ATTACK
|
||||
if defence_team.has(id):
|
||||
return Session.TEAMS.DEFENCE
|
||||
if specators_team.has(id):
|
||||
return Session.TEAMS.SPECTATE
|
||||
return Session.TEAMS.UNASSIGNED
|
||||
|
|
|
|||
|
|
@ -4,3 +4,6 @@ extends Node
|
|||
|
||||
func _ready() -> void:
|
||||
get_parent().visible = (get_multiplayer_authority() != multiplayer.get_unique_id()) == visible_for_others
|
||||
|
||||
func reverse() -> void:
|
||||
get_parent().visible = not get_parent().visible
|
||||
|
|
|
|||
|
|
@ -6,15 +6,186 @@ enum TEAMS {
|
|||
SPECTATE,
|
||||
UNASSIGNED
|
||||
}
|
||||
|
||||
enum ROUND_STATES {
|
||||
NOT_SET = 0,
|
||||
BUY = 1,
|
||||
ROUND = 2,
|
||||
AFTER_ROUND = 3,
|
||||
AFTER_PLANT = 4,
|
||||
AFTER_SESSION = 5,
|
||||
}
|
||||
|
||||
const ATTACK_LAYER: int = 0b10000
|
||||
const DEFENCE_LAYER: int = 0b100000
|
||||
|
||||
signal round_started
|
||||
signal round_state_changed(state: int)
|
||||
|
||||
var player_nodes: Dictionary[int,Player] = {}
|
||||
var object_containers: Array[ObjectContainer]
|
||||
|
||||
var dynamic_objects_spawner: MultiplayerSpawner
|
||||
var plants: Array[PlantSite]
|
||||
var plant_deadzones: Dictionary[StringName, Area3D]
|
||||
|
||||
var current_round: int = 0
|
||||
var attacker_score: int = 0
|
||||
var defender_score: int = 0
|
||||
var attackers_alive: int = 0
|
||||
var defenders_alive: int = 0
|
||||
|
||||
var bomb_timer: Timer
|
||||
var round_timer: Timer
|
||||
var buy_timer: Timer
|
||||
|
||||
var round_state: ROUND_STATES
|
||||
var session_started: bool = false
|
||||
|
||||
var reference_round_time: float
|
||||
|
||||
func _ready() -> void:
|
||||
if multiplayer.is_server() == false:
|
||||
return
|
||||
|
||||
bomb_timer = Timer.new()
|
||||
bomb_timer.wait_time = 45.0
|
||||
bomb_timer.one_shot = true
|
||||
bomb_timer.timeout.connect(end_round.bind(TEAMS.ATTACK))
|
||||
|
||||
round_timer = Timer.new()
|
||||
round_timer.wait_time = 150
|
||||
round_timer.one_shot = true
|
||||
round_timer.timeout.connect(end_round.bind(TEAMS.DEFENCE))
|
||||
|
||||
buy_timer = Timer.new()
|
||||
buy_timer.wait_time = 15
|
||||
buy_timer.one_shot = true
|
||||
buy_timer.timeout.connect(begin_main_stage)
|
||||
|
||||
add_child(bomb_timer)
|
||||
add_child(round_timer)
|
||||
add_child(buy_timer)
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if multiplayer.is_server() == false or not session_started:
|
||||
return
|
||||
match round_state:
|
||||
ROUND_STATES.BUY:
|
||||
reference_round_time = buy_timer.time_left
|
||||
ROUND_STATES.ROUND:
|
||||
reference_round_time = round_timer.time_left
|
||||
ROUND_STATES.AFTER_PLANT:
|
||||
reference_round_time = bomb_timer.time_left
|
||||
_:
|
||||
reference_round_time = 0
|
||||
update_clock.rpc(reference_round_time)
|
||||
|
||||
@rpc("authority","call_remote","unreliable")
|
||||
func update_clock(time: float):
|
||||
reference_round_time = time
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func start_session() -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
start_session.rpc()
|
||||
|
||||
current_round = 0
|
||||
attacker_score = 0
|
||||
defender_score = 0
|
||||
session_started = true
|
||||
|
||||
start_round()
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func end_session() -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
end_session.rpc()
|
||||
|
||||
bomb_timer.stop()
|
||||
round_timer.stop()
|
||||
buy_timer.stop()
|
||||
object_containers.clear()
|
||||
|
||||
session_started = false
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func start_round() -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
buy_timer.start()
|
||||
start_round.rpc()
|
||||
for container in object_containers:
|
||||
container.despawn()
|
||||
attackers_alive = 0
|
||||
defenders_alive = 0
|
||||
|
||||
current_round += 1
|
||||
|
||||
round_started.emit.call_deferred()
|
||||
round_state = ROUND_STATES.BUY
|
||||
round_state_changed.emit.call_deferred(round_state)
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func end_round(win_team: int) -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
get_tree().create_timer(5).timeout.connect(start_round)
|
||||
end_round.rpc(win_team)
|
||||
|
||||
if win_team == TEAMS.DEFENCE:
|
||||
defender_score += 1
|
||||
elif win_team == TEAMS.ATTACK:
|
||||
attacker_score += 1
|
||||
|
||||
round_state = ROUND_STATES.AFTER_ROUND
|
||||
round_state_changed.emit(round_state)
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func begin_main_stage() -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
round_timer.start()
|
||||
begin_main_stage.rpc()
|
||||
|
||||
round_state = ROUND_STATES.ROUND
|
||||
round_state_changed.emit(round_state)
|
||||
|
||||
@rpc("any_peer","call_remote","reliable")
|
||||
func begin_bomb_stage() -> void:
|
||||
if not is_server_request():
|
||||
return
|
||||
if multiplayer.is_server():
|
||||
bomb_timer.start()
|
||||
round_timer.stop()
|
||||
begin_bomb_stage.rpc()
|
||||
|
||||
round_state = ROUND_STATES.AFTER_PLANT
|
||||
round_state_changed.emit(round_state)
|
||||
|
||||
@rpc("any_peer","call_local","reliable")
|
||||
func add_dead(team: int):
|
||||
if multiplayer.is_server() == false:
|
||||
return
|
||||
if team == TEAMS.ATTACK:
|
||||
attackers_alive -= 1
|
||||
if attackers_alive == 0 and round_state != ROUND_STATES.AFTER_ROUND:
|
||||
end_round(TEAMS.DEFENCE)
|
||||
if team == TEAMS.DEFENCE:
|
||||
defenders_alive -= 1
|
||||
if defenders_alive == 0 and round_state != ROUND_STATES.AFTER_ROUND:
|
||||
end_round(TEAMS.ATTACK)
|
||||
|
||||
func is_server_request() -> bool:
|
||||
return multiplayer.is_server() or multiplayer.get_remote_sender_id() == 1
|
||||
|
||||
## Spawns dynamic object at game scene [br]
|
||||
## Dictionary keys: [br]
|
||||
## (Required) scene - path/uuid to scene [br]
|
||||
|
|
|
|||
|
|
@ -7,6 +7,14 @@ func _ready() -> void:
|
|||
if not multiplayer.is_server():
|
||||
queue_free()
|
||||
return
|
||||
Session.round_started.connect(spawn)
|
||||
|
||||
func _exit_tree() -> void:
|
||||
if not multiplayer.is_server():
|
||||
return
|
||||
Session.round_started.disconnect(spawn)
|
||||
|
||||
func spawn():
|
||||
match team:
|
||||
Session.TEAMS.ATTACK:
|
||||
for attacker in Lobby.attack_team:
|
||||
|
|
@ -23,6 +31,10 @@ func spawn_player(id: int) -> void:
|
|||
var inst: Player = player.instantiate()
|
||||
Session.player_nodes[id] = inst
|
||||
inst.name = str(id)
|
||||
if team == Session.TEAMS.DEFENCE:
|
||||
Session.defenders_alive += 1
|
||||
elif team == Session.TEAMS.ATTACK:
|
||||
Session.attackers_alive += 1
|
||||
|
||||
deferred_setup.bind(inst,team).call_deferred()
|
||||
|
||||
|
|
@ -34,7 +46,7 @@ func spawn_spectator(id: int) -> void:
|
|||
deferred_setup.bind(inst,Session.TEAMS.SPECTATE).call_deferred()
|
||||
|
||||
func deferred_setup(what: Node3D, new_team: Session.TEAMS):
|
||||
get_parent().add_child(what)
|
||||
get_parent().add_child(what,true)
|
||||
var distance = randf_range(0,spawn_radius)
|
||||
var angle = randf_range(0,TAU)
|
||||
var new_position = global_position + Vector3.RIGHT.rotated(Vector3.UP,angle) * distance
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue