SpaceServer/Scripts/Arena.gd

79 lines
2.4 KiB
GDScript
Raw Normal View History

extends Spatial
var players = {} # keys are String, values are Players
const Player = preload("res://Scenes/Player.tscn")
2021-05-14 10:27:05 +00:00
var deaths : int = 0
var rematch_requests = []
func init() -> void:
2021-05-14 10:27:05 +00:00
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()
2020-08-16 12:11:59 +00:00
players[player_id].set_name('player' + String(player_id))
add_child(players[player_id])
2020-08-16 12:11:59 +00:00
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)
2021-05-14 10:27:05 +00:00
players[player_id].id = player_id
2020-08-16 12:11:59 +00:00
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:
2020-08-16 12:11:59 +00:00
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:
2021-05-14 10:27:05 +00:00
return Vector3(rand_range(-5,5),rand_range(-5,5),rand_range(-5,5))
func generate_rotation() -> Vector3:
2021-05-14 10:27:05 +00:00
return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()
2020-08-29 16:33:30 +00:00
func play_full_plans():
for player in players.values():
player.play_full_plan()
2021-05-14 10:27:05 +00:00
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()