Added ability to restart server if enough players quit

pull/2/head
TechieDamien 2021-07-24 17:56:00 +01:00
parent e9048bc663
commit 8dbc9f6ca2
3 changed files with 30 additions and 9 deletions

View File

@ -27,7 +27,7 @@ func init() -> void:
func _process(delta : float) -> void:
var all_ready = true
for player in players.values():
if !player.is_ready and !player.has_died:
if !player.is_ready and !player.has_died and !player.is_zombie:
all_ready = false
if all_ready:
for player in players.values():
@ -36,12 +36,12 @@ func _process(delta : float) -> void:
player.calculate_plans()
func generate_translation() -> Vector3:
#return Vector3(rand_range(-5,5),rand_range(-5,5),rand_range(-5,5))
return Vector3(0,0,rand_range(-5,5))
return Vector3(rand_range(-5,5),rand_range(-5,5),rand_range(-5,5))
#return Vector3(0,0,rand_range(-5,5))
func generate_rotation() -> Vector3:
#return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()
return Vector3(0,0,1)
return Vector3(rand_range(-1,1),rand_range(-1,1),rand_range(-1,1)).normalized()
#return Vector3(0,0,1)
func play_full_plans():
for player in players.values():
@ -68,8 +68,6 @@ remote func request_rematch(id) -> void:
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()

View File

@ -22,6 +22,7 @@ var health : float = 100.0
var is_alive : bool = true
var time_of_death : float = -1
var has_died : bool = false
var is_zombie : bool = false
var turn_number : int = 0
@ -195,7 +196,6 @@ func die(time : float) -> void:
$CollisionShape.disabled = true
time_of_death = time
func play_all_plans():
get_parent().play_full_plans()
@ -217,6 +217,8 @@ func play_last_plan():
$CollisionShape.disabled = false
func calculate_plans():
if is_zombie:
create_zombie_plan()
if turn_number == 0:
play_full_plan()
else:
@ -240,3 +242,7 @@ func _on_player_enter_firing_arc(body):
func _on_player_exit_firing_arc(body):
players_in_firing_arc.erase(body)
func create_zombie_plan() -> void:
add_thrust_element(5, Vector3.ZERO, Vector3.ZERO)
add_weapons_element(5, false)

View File

@ -1,7 +1,7 @@
extends Node
const MAX_PLAYERS : int = 2
const MAX_PLAYERS : int = 3
const PORT : int = 9374
const Arena = preload("res://Scenes/Arena.tscn")
@ -10,6 +10,7 @@ var peer : NetworkedMultiplayerENet = null
var ids = []
var console_output : TextEdit = null
var ready_ids = []
var playing : bool = false
var name_dict = {}
var color_dict = {}
@ -42,6 +43,15 @@ func _peer_connected(id : int) -> void:
func _peer_disconnected(id : int) -> void:
ids.erase(id)
self.console_print("Player has disconnected with id " + String(id))
if !playing:
return
var alive_online_players : int = 0
$Arena.players[id].is_zombie = true
for id in ids:
if $Arena.players[id].is_alive:
alive_online_players += 1
if alive_online_players < 2:
self.server_request_exit()
remote func client_ready(id : int) -> void:
if id in ready_ids:
@ -57,6 +67,7 @@ func start_game() -> void:
console_print('Starting Game...')
send_name_dict()
send_color_dict()
playing = true
func send_name_dict() -> void:
for element in name_dict.keys():
@ -84,8 +95,14 @@ func end_game() -> void:
peer.close_connection()
peer.create_server( PORT, MAX_PLAYERS )
peer.refuse_new_connections = false
playing = false
remote func request_exit() -> void:
console_print("Exit requested...")
rpc("exit_requested")
$ExitTimer.start()
func server_request_exit() -> void:
console_print("Server requested exit...")
rpc("server_exit_requested")
$ExitTimer.start()