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);
    }
Beispiel #2
0
 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);
     }
 }
Beispiel #3
0
 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (VHostBehavior.Instance != null)
     {
         if (collision.gameObject.CompareTag("Player"))
         {
             Player   p       = collision.gameObject.GetComponent <Player>();
             Player[] players = FindObjectsOfType <Player>();
             for (int i = 0; i < players.Length; i++) //remove all players except the winner for leaderboards
             {
                 if (players[i] != p)
                 {
                     GameManager.Instance.RemovePlayer(players[i].GetUsername());
                 }
             }
             PlayerWonMessage message = new PlayerWonMessage(p.GetUsername());
             VHostBehavior.Instance.SendMessageToAllPlayers(message);
             GameManager.Instance.PlayerHasWonGame(p);
         }
     }
 }