79 lines
2.4 KiB
GDScript
79 lines
2.4 KiB
GDScript
extends Spatial
|
|
|
|
var players = {} # keys are String, values are Players
|
|
const Player = preload("res://Scenes/Player.tscn")
|
|
var deaths : int = 0
|
|
|
|
var rematch_requests = []
|
|
|
|
func init() -> void:
|
|
randomize()
|
|
yield(get_tree(), "idle_frame")
|
|
get_parent().console_print('Starting arena inilisation...')
|
|
for player_id in get_parent().ids:
|
|
get_parent().console_print('Placing player with id: ' + String(player_id))
|
|
players[player_id] = Player.instance()
|
|
players[player_id].set_name('player' + String(player_id))
|
|
add_child(players[player_id])
|
|
players[player_id].set_translation(generate_translation())
|
|
players[player_id].set_rotation(generate_rotation())
|
|
players[player_id].set_linear_velocity(Vector3.ZERO)
|
|
players[player_id].set_angular_velocity(Vector3.ZERO)
|
|
players[player_id].id = player_id
|
|
get_parent().console_print('Player Coords: ' + String(players[player_id].get_translation()) + String(players[player_id].get_rotation()) )
|
|
rpc('place_player', player_id, players[player_id].get_translation(), players[player_id].get_rotation())
|
|
players[player_id].init()
|
|
|
|
func _process(delta : float) -> void:
|
|
var all_ready = true
|
|
for player in players.values():
|
|
if !player.is_ready and !player.has_died:
|
|
all_ready = false
|
|
if all_ready:
|
|
for player in players.values():
|
|
if player.is_alive:
|
|
player.is_ready = false
|
|
player.calculate_plans()
|
|
|
|
func generate_translation() -> Vector3:
|
|
return Vector3(rand_range(-5,5),rand_range(-5,5),rand_range(-5,5))
|
|
|
|
func generate_rotation() -> Vector3:
|
|
return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()
|
|
|
|
func play_full_plans():
|
|
for player in players.values():
|
|
player.play_full_plan()
|
|
|
|
func register_death() -> bool:
|
|
deaths += 1
|
|
if deaths == len(players.keys()) -1:
|
|
return true
|
|
return false
|
|
|
|
func finish_game() -> void:
|
|
for player in players.values():
|
|
if player.is_alive:
|
|
declare_winner(player)
|
|
|
|
func declare_winner(player) -> void:
|
|
var name = get_parent().name_dict[player.id]
|
|
rpc("declare_winner", name)
|
|
|
|
remote func request_rematch(id) -> void:
|
|
if (not id in rematch_requests) and id in get_parent().ids:
|
|
rematch_requests.append(id)
|
|
if len(rematch_requests) == len(get_parent().ids):
|
|
start_rematch()
|
|
|
|
|
|
|
|
func start_rematch() -> void:
|
|
for player_id in players.keys():
|
|
players[player_id].queue_free()
|
|
players.clear()
|
|
yield(get_tree(), "idle_frame")
|
|
deaths = 0
|
|
rematch_requests.clear()
|
|
init()
|