public bool RestartGame(GameRestart gr) { clog("Restarting game, gameId: " + gr.gameId); if (gr.gameId != networkManager.gameSession.gameId) { return(false); } networkManager.gameSession.status = STATUS.RESTART; //theBall.SendMessage("ResetBall", null, SendMessageOptions.RequireReceiver); PlayerScore1 = 0; PlayerScore2 = 0; networkManager.gameSession.currentGs.sequence = 0; networkManager.gameSession.status = STATUS.INGAME; // The ball position and velocity is chosen at random by server, and sent out to players. // For now, just trust it. BallControl bc = theBall.GetComponent <BallControl>(); bc.setPosition(new Position(Vector2.zero)); bc.setVelocity(new Velocity(Vector2.zero)); // Not correct. FIXME: Vector2 startingForce = new Vector2(gr.balls[0].velocity.x, gr.balls[0].velocity.y); // Not deterministic? //bc.rb2d.AddForce(startingForce); bc.setVelocity(gr.balls[0].velocity); return(false); }
public bool StartNextRound(NextRound nr) { clog("Start next round for game, gameId: " + nr.gameId); if (nr.gameId != networkManager.gameSession.gameId) { return(false); } networkManager.gameSession.lastUuidPing = null; networkManager.gameSession.status = STATUS.INGAME; // The ball position and velocity is chosen at random by server, and sent out to players. // For now, just trust it. BallControl bc = theBall.GetComponent <BallControl>(); bc.setPosition(new Position(Vector2.zero)); bc.setVelocity(new Velocity(Vector2.zero)); // Not correct. FIXME: Vector2 startingForce = new Vector2(nr.balls[0].velocity.x, nr.balls[0].velocity.y); // Not deterministic? //bc.rb2d.AddForce(startingForce); bc.setVelocity(nr.balls[0].velocity); return(false); }
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 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 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; }