diff --git a/client/autoloads/gamestate.gd b/client/autoloads/gamestate.gd index 8a423b0c78ceb35205902743a2cee3f2145d05f3..ecd7d578fdb10fc0817624f2d03397661cee2dba 100644 --- a/client/autoloads/gamestate.gd +++ b/client/autoloads/gamestate.gd @@ -10,6 +10,8 @@ var isPlaying:int = NOT_PLAYING var serverNetworkId:int = 0 var isInServer:bool = false +signal gameFullyLoaded + ################################################################# ### GODOT SIGNALS @@ -116,12 +118,20 @@ func serverAlert(text:String): # --- Called by network --- # Start the game -func startGame(players): +func loadGame(players): for playerId in players: registerPlayer(playerId) interfaces.loadNewScene(interfaces.GAME_PATH) +func startGame(): + emit_signal("gameFullyLoaded") + + +func clientGameLoaded(): + network.clientGameLoaded(self.serverNetworkId) + + # --- Called by network --- #Â Register the players of the same game func registerPlayer(networkId:int): diff --git a/client/autoloads/network.gd b/client/autoloads/network.gd index 520818feeb77023348c4fdc176c40e57751020b0..f87025c676a0df10cd2537a83d8beaa734407e71 100644 --- a/client/autoloads/network.gd +++ b/client/autoloads/network.gd @@ -118,11 +118,19 @@ remote func serverAlert(text:String): gamestate.serverAlert(text) -remote func startGame(players:Array): +remote func loadGame(players:Array): if get_tree().get_rpc_sender_id()==gamestate.serverNetworkId: - gamestate.startGame(players) + gamestate.loadGame(players) +remote func startGame(): + if get_tree().get_rpc_sender_id()==gamestate.serverNetworkId: + gamestate.startGame() + + +func clientGameLoaded(serverId:int): + rpc_id(serverId, "clientGameLoaded") + ################################################################# ### !!! THIS SET OF FUNCTIONS HAS TO BE UPDATED ACCORDINGLY TO THE MAIN NETWORK TEAM WORK !!! ### (still rpc there for testing purpose) diff --git a/client/levels/test/game.gd b/client/levels/test/game.gd index 922a8083014d6052ee7e752c78e2fed5618f6326..7e0c2f14fd497d4198b82f8954aca7a5e17451b2 100644 --- a/client/levels/test/game.gd +++ b/client/levels/test/game.gd @@ -38,9 +38,6 @@ var itemsType = ["beer","hot_dog"] func _ready(): var player_scene:PackedScene = preload("res://entities/characters/player.tscn") - # TODO: check (and perhaps) the scene changing functionality - # get_tree().change_scene("res://scenes/levels/game/game.tscn") - #Next evey player will spa every other player including the server's own client! Try to move this to server only for peer_id in gamestate.players: var player:Node = player_scene.instance() @@ -61,6 +58,16 @@ func _ready(): initialSize = safeZone.scale initialTr = killArea.translation + + gamestate.clientGameLoaded() + + print("prepause true") + get_tree().set_pause(true) + print("postpause true") + yield(gamestate, "gameFullyLoaded") + print("gameFullyLoaded") + + get_tree().set_pause(false) ### NEEDS REFACTORING diff --git a/client/project.godot b/client/project.godot index e577438779e750ebb60adae1cff6c0929cbd671c..b93eebd92051af2769213209b453dcdb54d81ca3 100644 --- a/client/project.godot +++ b/client/project.godot @@ -61,6 +61,11 @@ secondaryAttack={ "events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":2,"pressed":false,"doubleclick":false,"script":null) ] } +itemInput={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":69,"unicode":0,"echo":false,"script":null) + ] +} [rendering] diff --git a/server/autoloads/gamestate.gd b/server/autoloads/gamestate.gd index 66a0feb42939d746554bb6fca13407885a0e447c..bc6e881d6f2ec466907a3c89307939a26682c68a 100644 --- a/server/autoloads/gamestate.gd +++ b/server/autoloads/gamestate.gd @@ -10,9 +10,15 @@ var maxClients:int var awaitedClients:Array = [] var registeredClients:Array = [] +var missingClients:Array = [] + const START_UP_DELAY:int = 5 signal serverValidation +const LOADING_DELAY:float = 10.0 +var clientsLoaded:Timer +signal readyToProcess + const GAMEPATH:String = "/root/game/" enum Layer { ENVIRONMENT = 0, @@ -186,23 +192,51 @@ func spawnPlayer(): # Wait for server validation and start the game # => TODO: server validation timeout ? -func startGame(): +func startGame() -> void: network.gameHasStarted() yield(self, "serverValidation") + missingClients = registeredClients.duplicate() + + clientsLoaded = Timer.new() + add_child(clientsLoaded) + + clientsLoaded.set_wait_time(LOADING_DELAY) + clientsLoaded.start() #Â Instancing the map and adding it to the scene tree var game:Object = preload("res://levels/test/game.tscn").instance() get_tree().get_root().add_child(game) spawnPlayer() - - # Broadcast the beginning of the game + + # Broadcast the beginning of the game to the clients loaded for clientNetworkId in registeredClients: - network.startGame(clientNetworkId, registeredClients) - + network.loadGame(clientNetworkId, registeredClients) + + + yield(clientsLoaded, "timeout") + print("loading timeout") + emit_signal("readyToProcess") + + # Screw the latecomers + for clientNetworkId in missingClients: + kickPlayer(clientNetworkId) + + for clientNetworkId in registeredClients: + network.startGame(clientNetworkId) + game.spawnInitItem() - print("Game started with peers ", registeredClients) + print("S:", serverId, "Game started with peers ", registeredClients) + + +func clientGameLoaded(clientId:int): + var index:int = missingClients.find(clientId) + if index==-1: + print("S:", self.serverId, " Client loaded, but unknown client tho (WTF)") + return + + missingClients.erase(clientId) # Kick a client from the game with a message @@ -218,7 +252,11 @@ func endGame(): network.gameHasEnded(registeredClients[0]) registeredClients.clear() - get_node("/root/game").queue_free() + + print("S:", serverId, " Game has ended, self-destruction initiated !") + + get_node("/root/").queue_free() + get_tree().quit() ### diff --git a/server/autoloads/network.gd b/server/autoloads/network.gd index ff41da5945124d6b0662ab3c884828f5696f8fa1..c098b14da95b1d140f65ed0556f5733cc51ba187 100644 --- a/server/autoloads/network.gd +++ b/server/autoloads/network.gd @@ -93,10 +93,18 @@ func serverAlert(clientNetworkId:int, text:String): rpc_id(clientNetworkId, "serverAlert", text) -# Start the game in the client app +# Load the game in the client app # => will probably change after the network merge !!! -func startGame(clientNetworkId:int, registeredClients:Array): - rpc_id(clientNetworkId, "startGame", registeredClients) +func loadGame(clientNetworkId:int, registeredClients:Array): + rpc_id(clientNetworkId, "loadGame", registeredClients) + + +func startGame(clientNetworkId:int): + rpc_id(clientNetworkId, "startGame") + + +remote func clientGameLoaded(): + gamestate.clientGameLoaded(get_tree().get_rpc_sender_id()) # Make the client leave the game with a message displayed diff --git a/server/levels/test/game.gd b/server/levels/test/game.gd index 7dba2cba3d04ac1cbd75ec282af76ce432dfd310..d3f35b1cdbb545d5f47e685da9b506a03056714e 100644 --- a/server/levels/test/game.gd +++ b/server/levels/test/game.gd @@ -59,17 +59,19 @@ func _ready(): initialIslandSize = islandsShape.scale initialAreaTr = killArea.translation initialIAreaTr = islandArea.translation + + yield(gamestate, "readyToProcess") waitZoning() - func _physics_process(delta): - # Check for the endgame conditions - if (gamestate.registeredClients.size()==1): - gamestate.endGame() - if (zoning): + + # Check for the endgame conditions + if (gamestate.registeredClients.size()==1): + gamestate.endGame() + safeShape.scale = initialShapeSize.linear_interpolate(Vector3(ZONESIZE,ZONESIZE,initialShapeSize.z),timeToTarget) safeZone.scale = initialZoneSize.linear_interpolate(Vector3(ZONESIZE,ZONESIZE,initialZoneSize.z),timeToTarget) islandsShape.scale = initialIslandSize.linear_interpolate(Vector3(ISLANDSIZE,ISLANDSIZE,initialIslandSize.z),timeToTarget)