public bool HandleContactEvent(ContactEvent ce)
        {
            // This is an event everyone should (try) to agree on, even if the simulation diverges.
            // 1) It's the latest event.
            // 2) The other player has already observed this on their game simulation.
            BallControl bc = theBall.GetComponent <BallControl>();

            if (bc.uuid != networkManager.gameSession.currentGs.balls[0].uuid)
            {
                clog("Ball UUID is unknown! Contact event unknown.");
                return(false);
            }

            if (ce.playeruuid == networkManager.gameSession.uuidOtherPlayer)
            {
                clog("Matching local ball to remote player event: " + ce.playeruuid);
                bc.setPosition(ce.position);
                bc.setVelocity(ce.velocity);
                networkManager.gameSession.lastUuidPing = networkManager.gameSession.uuidOtherPlayer;
            }
            else if (ce.playeruuid == networkManager.gameSession.uuidPlayer)
            {
                clog("Updating ghost ball (once) to server version: " + ce.velocity);
                // Self echo, just update server ghost.
                var gbc = ghostBall.GetComponent <BallControl>();
                gbc.setPosition(ce.position);
                gbc.setVelocity(ce.velocity);
                networkManager.gameSession.lastUuidPing = networkManager.gameSession.uuidPlayer;
            }

            return(true);
        }
        public void SendContactEvent(PlayerControls c, BallControl b, Collision2D collision)
        {
            // If the contact is actually the other player
            ContactEvent ce = new ContactEvent
            {
                sequence   = networkManager.gameSession.currentGs.sequence,
                objectType = "Ball",
                uuid       = c.uuid,
                playeruuid = networkManager.gameSession.uuidPlayer, // Sender source of this event.
                gameId     = networkManager.gameSession.gameId,
                position   = new Position(b.rb2d.position),
                velocity   = new Velocity(b.rb2d.velocity)
            };

            networkManager.webSocketClient.Send(Messaging <ContactEvent> .Serialize(ce));
        }
Ejemplo n.º 3
0
        // Match whatever WebSocket text is sending
        // Consistency: General rule here is that the game state if not timestamped, events may not represent the same time window.
        void HandleMessage(string message)
        {
            var msg = MessageWrapper.UnWrapMessage(message);

            // Not quite symetric, but the server is text only.
            switch (msg.type)
            {
            case "qotd":
                break;

            case "notification":
                Notification notification = Messaging <Notification> .Deserialize(message);

                gameManager.clog(notification.notificationText);
                break;

            case "register":
                GameRegister register = Messaging <GameRegister> .Deserialize(message);

                gameSession.sessionId  = register.sessionId;
                gameSession.uuidPlayer = register.uuidPlayer;
                break;

            case "gameJoin":
                GameJoin gj = Messaging <GameJoin> .Deserialize(message);

                gameManager.JoinGame(gj);
                break;

            case "scoreEvent":
                ScoreEvent se = Messaging <ScoreEvent> .Deserialize(message);

                gameManager.UpdateScore(se);
                break;

            case "moveEvent":
                MoveEvent me = Messaging <MoveEvent> .Deserialize(message);

                gameManager.UpdatePosition(me);
                break;

            case "gameState":
                GameState serverGs = Messaging <GameState> .Deserialize(message);

                gameSession.currentGs.sequence = serverGs.sequence;
                //UpdateLocalGame(serverGs);
                break;

            case "contactEvent":
                ContactEvent ce = Messaging <ContactEvent> .Deserialize(message);

                gameManager.HandleContactEvent(ce);
                break;

            case "resign":
                GameResign resign = Messaging <GameResign> .Deserialize(message);

                gameManager.theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
                gameManager.ghostBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver);
                break;

            case "nextRound":
                NextRound nr = Messaging <NextRound> .Deserialize(message);

                gameManager.StartNextRound(nr);
                break;

            case "gameRestart":
                GameRestart gr = Messaging <GameRestart> .Deserialize(message);

                gameManager.RestartGame(gr);
                break;

            default:
                gameManager.clog("Unknown message arrived: " + msg.type + ", message: " + message);
                break;
            }
        }