public static Ball CopyBall(BallControl bc)
        {
            Transform tf = bc.transform;

            Position pos = new Position(tf.position);
            Velocity vel = new Velocity(bc.rb2d.velocity);

            Ball nb = new Ball(bc.uuid, pos, vel);

            return(nb);
        }
 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 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));
        }
Example #4
0
 void StartBallMovement()
 {
     EdgeManager.localPlayer.gameObject.GetComponent <SpriteRenderer>().material = activePlayerMaterial;
     if (EdgeManager.localPlayer.playerIndex == 0) //first player in the room owns the ball
     {
         if (theBall == null)
         {
             Observable ballObservable = EdgeManager.localPlayer.CreateObservableObject("Ball", Vector3.zero, Quaternion.identity, SyncOptions.SyncPosition);
             theBall = ballObservable.observeredTransform.gameObject;
         }
         BallControl bc = theBall.GetComponent <BallControl>();
         bc.rb2d = theBall.GetComponent <Rigidbody2D>();
         bc.transform.position = Vector3.zero;
         bc.rb2d.velocity      = Vector3.zero;
         Vector2 startingForce = new Vector2(Random.Range(-10, 10), Random.Range(-10, 10));
         bc.rb2d.AddForce(startingForce);
         bc.rb2d.velocity = new Vector2(Random.Range(1, 10), Random.Range(1, 10));
     }
 }
        // Copies everything, using the current ball and player
        public GameState(GameState gs, BallControl ballc, PlayerControls currentPlayer)
        {
            this.source   = "client";
            this.sequence = gs.sequence;
            this.gameId   = gs.gameId;

            this.currentPlayer = currentPlayer.uuid;

            int plen = 2;

            this.players = new Player[plen];

            Ball b;
            int  blen = 1;

            this.balls = new Ball[blen];
            for (var idx = 0; idx < plen; idx++)
            {
                b = gs.balls[idx];
                if (b.uuid != currentPlayer.uuid)
                {
                    this.balls[idx] = new Ball(b.uuid, new Position(Vector2.zero), new Velocity(Vector2.zero));
                }
            }

            Player p;

            for (var idx = 0; idx < plen; idx++)
            {
                p = gs.players[idx];
                if (p.uuid != currentPlayer.uuid)
                {
                    this.players[idx] = new Player(p);
                }
                else
                {
                    this.players[idx] = Player.CopyPlayer(currentPlayer);
                }
            }
        }
        public bool JoinGame(GameJoin gj)
        {
            clog("Told to join gameId: " + gj.gameId + ", side: " + gj.side);
            if (gj.gameId == "")
            {
                clog("Bad gameId!");
                return(false);
            }

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

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

            gs.currentPlayer = networkManager.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;
                }
            }

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

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

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

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

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

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

            clog("Transition to inGame.");
            networkManager.gameSession.status = STATUS.INGAME;
            return(true);
        }
        public void UpdateLocalGame(GameState serverGameState)
        {
            if (serverGameState.sequence == 0)
            {
                networkManager.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 == networkManager.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 == networkManager.gameSession.uuidPlayer)
                            {
                                cpIdx = i;
                            }
                            else if (a.uuid == networkManager.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;
        }