void Update() { if (Input.GetKeyDown(KeyCode.G)) { // Testing seed = Random.Range(0, Int32.MaxValue); dungeonGeneration.Clear(); spawner.SetServer(this); spawner.seed = seed; spawner.ClearMonsters(); dungeonGeneration.Generate(seed); } if (Input.GetKeyDown(KeyCode.K)) { //Testing for (int i = 0; i < spawner.monsters.Length; i++) { if (spawner.monsters[i] != null) { spawner.monsters[i].GetComponent <Monster>().health = 0; } } } if (Input.GetKeyDown(KeyCode.Escape)) { panelsManager.ShowMenuPanel(); } if (Input.GetKeyDown(KeyCode.Space)) { StartShooting(); } if (Input.GetKeyUp(KeyCode.Space)) { StopShooting(); } switch (state) { case GameState.STOP: break; case GameState.START: break; case GameState.GAME: speed = joystick.InputVector * Time.deltaTime * 12; player.animator.SetBool("Walking", false); // Get server player if (speed.magnitude > 0 && !player.died) { //player.transform.position += speed; Rigidbody2D playerRb2D = player.GetComponent <Rigidbody2D>(); playerRb2D.MovePosition(playerRb2D.position + speed); player.SetRotation(joystick.InputVector); player.animator.SetBool("Walking", true); } movementElapsed += Time.deltaTime; // Shooting if (player.shooting) { player.AnimateShooting(player.shootingElapsed); //Debug.Log(player.shootingElapsed); if (player.shootingElapsed >= player.frequency) { player.shootingElapsed = player.shootingElapsed % player.frequency; shootingElapsed = shootingElapsed % player.frequency; ServerShoot(); } } // Movement if (movementElapsed >= GameManager.FREQUENCY) { movementElapsed = movementElapsed % GameManager.FREQUENCY; dungeonGeneration.HighlightRoom(player); if (sharingMovements) { ShareMovements(); } else { sharingMovements = true; } } // Server movement Lerp for (int i = 0; i < nbOfPlayers; i++) { Player playerIndex = players[i].GetComponent <Player>(); Vector2 startClientPos = startPlayersPositions[i]; Vector2 endClientPos = endPlayersPositions[i]; float startClientRot = startPlayersRotations[i]; float endClientRot = endPlayersRotations[i]; float timeStartedLerping = timesStartedLerping[i]; playerIndex.shootingElapsed += Time.deltaTime; if (i != 0) { float lerpPercentage = (Time.time - timeStartedLerping) / GameManager.FREQUENCY; //Debug.Log(string.Format("lerpPercent[{0}] = (time[{1}] - tS[{2}]) / tTRG[{3}]", lerpPercentage, Time.time, timeStartedLerping, GameManager.FREQUENCY)); playerIndex.transform.position = Vector3.Lerp(startClientPos, endClientPos, lerpPercentage); float lerpedRotation = Mathf.LerpAngle(startClientRot, endClientRot, lerpPercentage); playerIndex.SetRotation(lerpedRotation); // If we havent moved then we are not walking if (Vector2.Distance(startClientPos, endClientPos) < 0.1f) { playerIndex.animator.SetBool("Walking", false); } else { playerIndex.animator.SetBool("Walking", true); } if (playerIndex.shooting) { playerIndex.AnimateShooting(playerIndex.shootingElapsed); if (playerIndex.shootingElapsed >= playerIndex.frequency) { playerIndex.shootingElapsed = playerIndex.shootingElapsed % playerIndex.frequency; ClientShoot(i); } } else { playerIndex.StopShooting(); } } } break; } // Server read received messages switch (s.received.type) { case MessageType.NONE: { } break; case MessageType.CLIENT_NEW_PLAYER: { Debug.LogError("Received Start Server from client"); AddPlayer(); SharePlayers(); } break; case MessageType.MOVEMENT: { // Received movement from client MovementMessage mm = (MovementMessage)s.received; //Debug.Log("Movement from " + mm.playerId); lastClientMovement = mm.playerId; startPlayersPositions[lastClientMovement] = players[lastClientMovement].transform.position; startPlayersRotations[lastClientMovement] = players[lastClientMovement].GetComponent <Player>().visorRotation; endPlayersRotations[lastClientMovement] = mm.visorRotation; endPlayersPositions[lastClientMovement] = new Vector3(mm.x, mm.y); players[lastClientMovement].GetComponent <Player>().shooting = mm.shooting; timesStartedLerping[lastClientMovement] = Time.time; } break; case MessageType.CLIENT_GO_TO_NEXT_ROOM: { ClientGoToNextRoomMessage goToNextRoomMessage = (ClientGoToNextRoomMessage)s.received; GoToNextRoom(goToNextRoomMessage.seed); } break; case MessageType.CLIENT_SHARE_MONSTERS_SPAWN: { ClientShareMonstersSpawnMessage shareMonstersSpawnMessage = (ClientShareMonstersSpawnMessage)s.received; //spawner.SpawnMonsters(shareMonstersSpawnMessage.roomId, shareMonstersSpawnMessage.seed); // Teleportation if (shareMonstersSpawnMessage.teleport) { Player player = players[shareMonstersSpawnMessage.playerId].GetComponent <Player>(); Vector2 teleportPosition = dungeonGeneration.GetPositionByPlayerDirection(0, shareMonstersSpawnMessage.roomId, player.GetVisorDirection()); players[0].transform.position = teleportPosition; } ShareSpawnMonsters(shareMonstersSpawnMessage.roomId, shareMonstersSpawnMessage.playerId, shareMonstersSpawnMessage.seed, shareMonstersSpawnMessage.teleport); } break; case MessageType.CLIENT_DIE: { ClientDieMessage clientDieMessage = (ClientDieMessage)s.received; players[clientDieMessage.playerId].GetComponent <Player>().SetDied(); ShareDeath(clientDieMessage.playerId); } break; } s.received.OnRead(); }
void Update() { if (Input.GetKeyDown(KeyCode.Escape)) { panelsManager.ShowMenuPanel(); } if (Input.GetKeyDown(KeyCode.Space)) { StartShooting(); } if (Input.GetKeyUp(KeyCode.Space)) { StopShooting(); } switch (state) { case GameState.STOP: // Debug.LogError("Send Start Server from client"); ClientNewPlayerMessage newPlayerMessage = new ClientNewPlayerMessage(); if (c.Connected()) { c.ClientSend(newPlayerMessage); state = GameState.START; } break; case GameState.START: break; case GameState.GAME: speed = joystick.InputVector * Time.deltaTime * 12; player.animator.SetBool("Walking", false); if (speed.magnitude > 0 && !player.died) { //player.transform.position += speed; Rigidbody2D playerRb2D = player.GetComponent <Rigidbody2D>(); playerRb2D.MovePosition(playerRb2D.position + speed); player.SetRotation(joystick.InputVector); player.animator.SetBool("Walking", true); } movementElapsed += Time.deltaTime; shootingElapsed += Time.deltaTime; // Shooting if (player.shooting) { player.AnimateShooting(shootingElapsed); if (shootingElapsed >= player.frequency) { shootingElapsed = shootingElapsed % player.frequency; ClientShoot(playerId); } } // Movement if (movementElapsed >= GameManager.FREQUENCY) { movementElapsed = movementElapsed % GameManager.FREQUENCY; dungeonGeneration.HighlightRoom(player); if (sharingMovements) { SendPosition(player.gameObject); } else { sharingMovements = true; } } // Lerp server shared movements for (int i = 0; i < nbOfPlayers; i++) { Player playerIndex = players[i].GetComponent <Player>(); Vector2 startServerPos = startPlayersPositions[i]; Vector2 endServerPos = endPlayersPositions[i]; float startServerRot = startPlayersRotations[i]; float endServerRot = endPlayersRotations[i]; float timeStartedLerping = timesStartedLerping[i]; if (i != playerId) { float lerpPercentage = (Time.time - timeStartedLerping) / GameManager.FREQUENCY; // Position playerIndex.transform.position = Vector3.Lerp(startServerPos, endServerPos, lerpPercentage); // Rotation of the visor float lerpedRotation = Mathf.LerpAngle(startServerRot, endServerRot, lerpPercentage); playerIndex.SetRotation(lerpedRotation); // If we havent moved then we are not walking if (Vector2.Distance(startServerPos, endServerPos) < 0.1f) { playerIndex.animator.SetBool("Walking", false); } else { playerIndex.animator.SetBool("Walking", true); } // Shooting if (playerIndex.shooting) { playerIndex.AnimateShooting(shootingElapsed); if (shootingElapsed >= playerIndex.frequency) { shootingElapsed = shootingElapsed % playerIndex.frequency; ClientShoot(i); } } else { playerIndex.StopShooting(); } } } // Lerp server shared monsters movement if (monstersTimesStartedLerping != null && monstersTimesStartedLerping.Length > 0) { for (int i = 0; i < startMonstersPositions.Length; i++) { //Debug.Log("What do I have inside monsters ? " + spawner.monsters.transform.GetChild(i).gameObject.name); if (spawner.monsters[i] != null) { GameObject monster = spawner.monsters[i].gameObject; Vector2 startServerPos = startMonstersPositions[i]; Vector2 endServerPos = endMonstersPositions[i]; float timeStartedLerping = monstersTimesStartedLerping[i]; float lerpPercentage = (Time.time - timeStartedLerping) / GameManager.FREQUENCY; //Position monster.transform.position = Vector3.Lerp(startServerPos, endServerPos, lerpPercentage); } } } break; } // Client read received messages //Debug.Log(c.received.type); switch (c.received.type) { case MessageType.NONE: break; case MessageType.SERVER_START_GAME: { panelsManager.ShowGamePanel(); // Define player and camera only once at the start of the game player = players[playerId].GetComponent <Player>(); player.client = this; // Set camera as a child of the player Camera.main.transform.parent = player.transform; player.isPlayed = true; // Set the healthbar of the client player player.healthBar = healthBar; player.healthBar.value = player.health; state = GameState.GAME; } break; case MessageType.SERVER_SHARE_PLAYERS: { ServerSharePlayersMessage sharePlayersMessage = (ServerSharePlayersMessage)c.received; // The first message sets the player number if (!setPlayerId) { playerId = sharePlayersMessage.playerId; setPlayerId = true; seed = sharePlayersMessage.seed; dungeonGeneration.SetClient(this); dungeonGeneration.Generate(seed); spawner.seed = seed; spawner.SetClient(this); spawner.ClearMonsters(); spawner.rooms = dungeonGeneration.rooms; } // Destroy old players for (int i = 0; i < nbOfPlayers; i++) { Destroy(players[i]); players[i] = null; } // Add new players nbOfPlayers = sharePlayersMessage.x.Length; for (int i = 0; i < nbOfPlayers; i++) { players[i] = Instantiate(playerPrefab, new Vector2(sharePlayersMessage.x[i], sharePlayersMessage.y[i]), Quaternion.identity); // Lobby playerPanels[i].SetActivePlayer(true); } } break; case MessageType.SERVER_SHARE_MOVEMENT: { ServerShareMovementMessage shareMovementsMessage = (ServerShareMovementMessage)c.received; for (int i = 0; i < shareMovementsMessage.x.Length; i++) { if (i != playerId) { startPlayersPositions[i] = players[i].transform.position; startPlayersRotations[i] = players[i].GetComponent <Player>().visorRotation; endPlayersRotations[i] = shareMovementsMessage.visorRotation[i]; endPlayersPositions[i] = new Vector2(shareMovementsMessage.x[i], shareMovementsMessage.y[i]); players[i].GetComponent <Player>().shooting = shareMovementsMessage.shooting[i]; timesStartedLerping[i] = Time.time; } } startMonstersPositions = new Vector2[GameManager.MAX_MONSTERS]; endMonstersPositions = new Vector2[GameManager.MAX_MONSTERS]; monstersTimesStartedLerping = new float[GameManager.MAX_MONSTERS]; for (int i = 0; i < GameManager.MAX_MONSTERS; i++) { // If the monster exists set health and position from server if (spawner.monsters[i] != null) { startMonstersPositions[i] = spawner.monsters[i].transform.position; endMonstersPositions[i] = new Vector3(shareMovementsMessage.mx[i], shareMovementsMessage.my[i]); spawner.monsters[i].health = shareMovementsMessage.health[i]; monstersTimesStartedLerping[i] = Time.time; } } } break; case MessageType.SERVER_GO_TO_NEXT_ROOM: { //Debug.Log("Server go to next room"); ServerGoToNextRoomMessage ssm = (ServerGoToNextRoomMessage)c.received; for (int i = 0; i < ssm.x.Length; i++) { players[i].transform.position = new Vector3(ssm.x[i], ssm.y[i], 0); } spawner.ClearMonsters(); dungeonGeneration.Clear(); dungeonGeneration.Generate(ssm.seed); } break; case MessageType.SERVER_SHARE_MONSTERS_SPAWN: { ServerShareMonstersSpawnMessage ssmsm = (ServerShareMonstersSpawnMessage)c.received; // If I'm not the one that has sent the request to spawn monsters if (ssmsm.playerId != playerId) { //spawner.SpawnMonsters(ssmsm.roomId, ssmsm.seed); if (ssmsm.teleport) { Player player = players[ssmsm.playerId].GetComponent <Player>(); Vector2 teleportPosition = dungeonGeneration.GetPositionByPlayerDirection(playerId, ssmsm.roomId, player.GetVisorDirection()); players[playerId].transform.position = teleportPosition; } } } break; case MessageType.SERVER_DIE: { ServerDieMessage sdm = (ServerDieMessage)c.received; if (sdm.playerId != playerId) { players[sdm.playerId].GetComponent <Player>().SetDied(); } } break; } c.received.OnRead(); }