Exemplo n.º 1
0
        bool JoinGame(GameJoin gj)
        {
            clog("Told to join gameId: " + gj.gameId + ", side: " + gj.side);
            if (gj.gameId == "")
            {
                clog("Bad gameId!");
                return(false);
            }

            gameSession.gameId          = gj.gameId;
            gameSession.side            = gj.side;
            gameSession.uuidOtherPlayer = gj.uuidOtherPlayer;
            gameSession.status          = STATUS.JOINED;

            gameSession.currentGs = new GameState();
            var gs = gameSession.currentGs;

            gs.currentPlayer = gameSession.uuidPlayer;

            // Update Ball:
            if (gs.balls.Length == 0)
            {
                gs.balls = new Ball[1];
            }
            BallControl bc = theBall.GetComponent <BallControl>();

            bc.uuid     = gj.ballId; // Add uuid to ball.
            gs.balls[0] = Ball.CopyBall(bc);

            // Match Assignments:
            // Given a side, 0 one is player one (left). 1 other player (right)
            PlayerControls left  = null;
            PlayerControls right = null;

            if (players.Length != 2)
            {
                return(false); // Can't join this game.
            }

            foreach (GameObject g in players)
            {
                if (left == null)
                {
                    left = g.GetComponent <PlayerControls>();
                    continue;
                }

                right = g.GetComponent <PlayerControls>();
                if (right.transform.position.x < left.transform.position.x)
                {
                    var tmp = left;
                    left  = right;
                    right = tmp;
                }
            }

            Debug.Log("Left sel: " + left.transform.position.x + "Right other: " + right.transform.position.x);

            if (gameSession.side == 0)
            {
                left.uuid      = gameSession.uuidPlayer; // Player 1 assigned in match by server.
                left.ownPlayer = true;

                right.uuid      = gameSession.uuidOtherPlayer;
                right.ownPlayer = false;
                gs.players[0]   = Player.CopyPlayer(left);
                gs.players[1]   = Player.CopyPlayer(right);
            }
            else if (gameSession.side == 1)
            {
                right.uuid      = gameSession.uuidPlayer; // Player 2 assigned in match by server.
                right.ownPlayer = true;

                left.uuid      = gameSession.uuidOtherPlayer;
                left.ownPlayer = false;

                gs.players[0] = Player.CopyPlayer(right);
                gs.players[1] = Player.CopyPlayer(left);
            }

            // Assign player state:
            gameSession.currentGs = gs;

            clog("Transition to inGame.");
            gameSession.status = STATUS.INGAME;
            return(true);
        }
Exemplo n.º 2
0
        void UpdateLocalGame(GameState serverGameState)
        {
            if (serverGameState.sequence == 0)
            {
                gameSession.status = STATUS.INGAME;
            }

            GameState localGs = GatherGameState();

            // Resolve server versus local score:
            localGs.score1 = serverGameState.score1;
            localGs.score2 = serverGameState.score2;

            // Ball, just assume ball is fair, if close to server.
            BallControl bc   = theBall.GetComponent <BallControl>();
            Ball        ball = Ball.CopyBall(bc);
            /// Grab the first one...
            Ball serverBall = null;

            if (serverGameState.balls.Length > 0)
            {
                serverBall = serverGameState.balls[0];
                bool ballPositionOK = PositionInRange(ball.position, serverBall.position);
                bool ballVelocityOK = VelocityInRange(ball.velocity, serverBall.velocity);

                if (!ballPositionOK || !ballVelocityOK)
                {
                    // Blindly use server's ball position and velocity to resync. Better: Blend and rubber band.
                    bc.setPosition(serverBall.position);
                    bc.setVelocity(serverBall.velocity);
                }
            }
            else
            {
                // Perhaps a new game. No server info.
            }

            // Copy other paddle location(s) from server. Current player knows their own position.
            // TODO: need a map/ordered list.
            int cpIdx = -1; // current player
            int opIdx = -1; // other player

            Player[] serverPlayers = serverGameState.players;

            GameObject[] pcs = GameObject.FindGameObjectsWithTag("Player");
            foreach (GameObject p in pcs)
            {
                PlayerControls a = p.GetComponent <PlayerControls>();
                if (a.uuid == gameSession.uuidOtherPlayer)
                {
                    // Find other player in server view:
                    for (var i = 0; i < serverGameState.players.Length; i++)
                    {
                        if (a.uuid == serverPlayers[i].uuid)
                        {
                            a.setPosition(serverPlayers[i].position);
                            if (a.uuid == gameSession.uuidPlayer)
                            {
                                cpIdx = i;
                            }
                            else if (a.uuid == gameSession.uuidOtherPlayer)
                            {
                                opIdx = i;
                            }
                        }
                    }
                }
            }


            // Player ghost. The position of where the server *thinks* the current player is.



            // Copy server score.
            // Merge/tweak ball position and velocity, we only care the ball state is fair.
            //   - Player position is ultimately sort of cosmetic.
            // Save to current GS.

            // Next update, gather and send that to server.

            return;
        }