public override void HandleNetworkMessage(Message msg) { base.HandleNetworkMessage(msg); if (msg is PlayerConnectedMessage) //player sends this once connected to send name { PlayerConnectedMessage nMsg = msg as PlayerConnectedMessage; if (GameManager.Instance.GetPlayer(nMsg.playerID) != null) //player name already used, prepare to kick { m_ConnectionToAdd = new KeyValuePair <uint, string>(m_NetworkMessageConnectionSource, ""); } else { m_ConnectionToAdd = new KeyValuePair <uint, string>(m_NetworkMessageConnectionSource, nMsg.playerID); //Send the player connected info to everyone ClientPlayer cP = GameManager.Instance.ClientPlayer; if (cP != null) { SendMessageToAllExceptPlayer(nMsg.playerID, new PlayerConnectedMessage(nMsg.playerID), SendType.Reliable); } //send current players to the connected player List <Player> players = GameManager.Instance.m_Players; for (int i = 0; i < players.Count; i++) { if (players[i] is ClientPlayer) { m_Server.SendMessageToConnection(m_NetworkMessageConnectionSource, new PlayerConnectedMessage(GameManager.Instance.m_PlayerUsername).toBuffer(), SendType.Reliable); } else { m_Server.SendMessageToConnection(m_NetworkMessageConnectionSource, new PlayerConnectedMessage((players[i] as NetworkedPlayer).playerID).toBuffer(), SendType.Reliable); } } GameManager.Instance.AddPlayer(nMsg.playerID); Invoke("RealignPlayersAndSend", 0.1f); //need a delay for them to add the player } } if (msg is PlayerUpdateMessage) { PlayerUpdateMessage nMsg = msg as PlayerUpdateMessage; GameManager.Instance.UpdatePlayerInformation(ref nMsg.info, nMsg.playerID); SendMessageToAllExceptPlayer(nMsg.playerID, nMsg); } if (msg is ObstacleModifiedMessage) { Debug.Log("Obstacle Modified Message"); ObstacleModifiedMessage nMsg = msg as ObstacleModifiedMessage; SendMessageToAllExceptPlayer(nMsg.playerID, nMsg, SendType.Reliable); (GameManager.Instance.m_AllObstacles[(int)nMsg.obstacleID] as IObstacle).InteractedWith(GameManager.Instance.GetPlayer(nMsg.playerID)); } if (msg is PlayerAttackedPlayerMessage) { PlayerAttackedPlayerMessage nMsg = msg as PlayerAttackedPlayerMessage; SendMessageToAllExceptPlayer(nMsg.attackeePlayerID, nMsg, SendType.Reliable); GameManager.Instance.PlayerAttacked(nMsg.attackedPlayerID); } }
public static Message decipherMessage(byte[] msgBytes) { if (BitConverter.IsLittleEndian) { //Array.Reverse(msgBytes); } Message msg; byte[] eventTypeBytes = new byte[2]; Buffer.BlockCopy(msgBytes, 0, eventTypeBytes, 0, 2); ushort sho = BitConverter.ToUInt16(eventTypeBytes, 0); NetworkEvent eventType = (NetworkEvent)sho; switch (eventType) { case NetworkEvent.StartGame: msg = new GameStartMessage(msgBytes); break; case NetworkEvent.PlayerUpdateInfo: msg = new PlayerUpdateMessage(msgBytes); break; case NetworkEvent.PlayerConnected: msg = new PlayerConnectedMessage(msgBytes); break; case NetworkEvent.PlayerDisconnected: msg = new PlayerDisconnectedMessage(msgBytes); break; case NetworkEvent.ObstacleGenerated: msg = new ObstacleGeneratedMessage(msgBytes); break; case NetworkEvent.PlayerFellBehind: msg = new PlayerFellBehindMessage(msgBytes); break; case NetworkEvent.ObstacleModified: msg = new ObstacleModifiedMessage(msgBytes); break; case NetworkEvent.PlayerWonRace: msg = new PlayerWonMessage(msgBytes); break; case NetworkEvent.PlayerAttackPlayer: msg = new PlayerAttackedPlayerMessage(msgBytes); break; default: throw new Exception("oops " + eventType); } return(msg); }
public override void HandleNetworkMessage(Message msg) { base.HandleNetworkMessage(msg); if (msg is GameStartMessage) { GameStartMessage nMsg = msg as GameStartMessage; ConnectionStatus cStat = new ConnectionStatus(); m_Server.GetQuickConnectionStatus(m_Connection, cStat); uniformTimeOffset = ((nMsg.uniformTime.Ticks / TimeSpan.TicksPerSecond) + (((float)cStat.ping) * 0.01f)) - (DateTime.UtcNow.Ticks / TimeSpan.TicksPerSecond); GameManager.Instance.StartGameInSeconds(nMsg.timeAfterToSpawn + uniformTimeOffset); } if (msg is PlayerConnectedMessage) { PlayerConnectedMessage nMsg = msg as PlayerConnectedMessage; NetworkedPlayer p = GameManager.Instance.AddPlayer(nMsg.playerID); p.playerID = nMsg.playerID; } else if (msg is PlayerDisconnectedMessage) { PlayerDisconnectedMessage nMsg = msg as PlayerDisconnectedMessage; GameManager.Instance.RemovePlayer(nMsg.playerID); } else if (msg is PlayerUpdateMessage) { PlayerUpdateMessage nMsg = msg as PlayerUpdateMessage; GameManager.Instance.UpdatePlayerInformation(ref nMsg.info, nMsg.playerID); } else if (msg is ObstacleGeneratedMessage) { ObstacleGeneratedMessage nMsg = msg as ObstacleGeneratedMessage; TileManager.Instance.SpawnObstacle(nMsg.itemID, nMsg.itemPos, nMsg.itemType); } else if (msg is ObstacleModifiedMessage) { ObstacleModifiedMessage nMsg = msg as ObstacleModifiedMessage; (GameManager.Instance.m_AllObstacles[(int)nMsg.obstacleID] as IObstacle).InteractedWith(GameManager.Instance.GetPlayer(nMsg.playerID)); } else if (msg is PlayerFellBehindMessage) { PlayerFellBehindMessage nMsg = msg as PlayerFellBehindMessage; GameManager.Instance.PlayerFellBehind(nMsg.playerID); } else if (msg is PlayerWonMessage) { PlayerWonMessage nMsg = msg as PlayerWonMessage; GameManager.Instance.PlayerHasWonGame(GameManager.Instance.GetPlayer(nMsg.playerID)); } else if (msg is PlayerAttackedPlayerMessage) { PlayerAttackedPlayerMessage nMsg = msg as PlayerAttackedPlayerMessage; GameManager.Instance.PlayerAttacked(nMsg.attackedPlayerID); } }
public void InteractButtonPressed(InputAction.CallbackContext context) { if (m_ClosestObstacle != null && !m_IsSpinning && !m_IsDodging && m_InteractStaminaCost <= m_PlayerInfo.stamina) { //Interact with the obstacle m_PlayerInfo.stamina -= m_InteractStaminaCost; m_ClosestObstacle.GetComponent <SpriteRenderer>().color = Color.white; m_ClosestObstacle.HandleInteraction(this); ObstacleModifiedMessage msg = new ObstacleModifiedMessage(GameManager.Instance.m_PlayerUsername, m_ClosestObstacle.id); if (VHostBehavior.Instance != null) { VHostBehavior.Instance.SendMessageToAllPlayers(msg, Valve.Sockets.SendType.Reliable); } else { VOnlinePlayer.Instance.SendMessage(msg, Valve.Sockets.SendType.Reliable); } } }