public bool UpdateBallGhost(MoveEvent moveItem)
 {
     if (moveItem.objectType == "Ball")
     {
         BallControl bc = ghostBall.GetComponent <BallControl>();
         if (bc != null)
         {
             bc.setPosition(moveItem.position);
             bc.setVelocity(moveItem.velocity);
         }
     }
     return(true);
 }
 public bool UpdatePlayerGhost(MoveEvent moveItem)
 {
     if (moveItem.objectType == "Player" &&
         moveItem.uuid == networkManager.gameSession.uuidPlayer)
     {
         // Ghost is a variant of the regular player paddle.
         // There's just one pre-assigned ghost.
         PlayerControls pc = ghostPlayer.GetComponent <PlayerControls>();
         if (pc != null)
         {
             pc.setPosition(moveItem.position);
             pc.setVelocity(moveItem.velocity);
         }
     }
     return(true);
 }
        public void UpdateBall()
        {
            var  bc   = theBall.GetComponent <BallControl>();
            Ball ball = Ball.CopyBall(bc);

            networkManager.gameSession.currentGs.balls[0] = ball;

            MoveEvent moveEvent = new MoveEvent
            {
                uuid       = bc.uuid,
                playeruuid = networkManager.gameSession.uuidPlayer,
                gameId     = networkManager.gameSession.gameId,
                objectType = "Ball",
                position   = new Position(ball.position),
                velocity   = new Velocity(ball.velocity)
            };

            networkManager.webSocketClient.Send(Messaging <MoveEvent> .Serialize(moveEvent));
        }
        public void UpdatePlayer()
        {
            // Client side dict needed.
            Player[] gsPlayers = new Player[networkManager.gameSession.currentGs.players.Length];
            Player   selected  = null;
            // Only ever need to tell the server of own location (for now)

            int idx = 0;

            foreach (GameObject gpc in players)
            {
                PlayerControls pc = gpc.GetComponent <PlayerControls>();
                if (pc.ownPlayer)
                {
                    selected         = Player.CopyPlayer(pc);
                    gsPlayers[idx++] = selected;
                }
                else
                { // Stright copy and update.
                    gsPlayers[idx++] = Player.CopyPlayer(pc);
                }
            }
            networkManager.gameSession.currentGs.players = gsPlayers;

            MoveEvent moveEvent = new MoveEvent
            {
                uuid       = selected.uuid,
                playeruuid = selected.uuid,
                gameId     = networkManager.gameSession.gameId,
                objectType = "Player",
                position   = new Position(selected.position),
                velocity   = new Velocity(selected.velocity)
            };

            networkManager.webSocketClient.Send(Messaging <MoveEvent> .Serialize(moveEvent));
        }
        public bool UpdatePosition(MoveEvent moveItem)
        {
            //clog("moveItem: " + moveItem.uuid);
            var gs = networkManager.gameSession.currentGs;

            if (moveItem.sequence < networkManager.gameSession.currentGs.sequence)
            {
                clog("old event.");
                return(false); // Old.
            }

            if (moveItem.uuid == gs.balls[0].uuid)
            {
                // If the source is the other player, and that's the last contact,
                // update local ball.
                if (networkManager.gameSession.lastUuidPing == networkManager.gameSession.uuidOtherPlayer &&
                    moveItem.playeruuid == networkManager.gameSession.uuidOtherPlayer)
                {
                    // Other player...
                    var bc = theBall.GetComponent <BallControl>();

                    gs.balls[0].position = moveItem.position;
                    gs.balls[0].velocity = moveItem.velocity;
                    bc.setPosition(gs.balls[0].position);
                    bc.setVelocity(gs.balls[0].velocity);
                }
                else // Self echo.
                {
                    UpdateBallGhost(moveItem);
                }
            }

            if (moveItem.uuid == networkManager.gameSession.uuidPlayer)
            {
                UpdatePlayerGhost(moveItem);
                // Server echo of current player position and velocity.
                // Add a gameObject if not existing, and show it along with the current player's position.
                // Also, if significantly different, jump player to "server" position, or interpolate postion over time.
            }
            else if (moveItem.uuid == networkManager.gameSession.uuidOtherPlayer) // Other player, blind copy.
            {
                foreach (var player in gs.players)
                {
                    if (player.uuid == networkManager.gameSession.uuidOtherPlayer)
                    {
                        player.position = moveItem.position;
                        player.velocity = moveItem.velocity;
                        // Apply to GameObject of player:
                        var gp = GameObject.FindGameObjectsWithTag("Player");
                        foreach (var g in gp)
                        {
                            var p = g.GetComponent <PlayerControls>();
                            if (p.uuid == networkManager.gameSession.uuidOtherPlayer)
                            {
                                p.setPosition(moveItem.position);
                                p.setVelocity(moveItem.velocity);
                            }
                        }
                    }
                }
            }


            return(true);
        }
Ejemplo n.º 6
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;
            }
        }