public void SendMyPlayerUpdate() { PlayerUpdate update = new PlayerUpdate(); update.ShipPosition = ship.Position; update.Outline = ship.Outline; update.ShipVelocity = ship.Velocity; update.State = ship.State; update.WaitCount = ship.WaitCount; update.DeathCount = ship.DeathCount; update.FlameIndex = ship.FlameIndex; update.Sounds = (int)ship.Sounds; update.Score = ship.Score; ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotAge = new int[Constants.NumShots]; shotUpdate.ShotPosition = new Vector2[Constants.NumShots]; Shot[] shotArray = ship.ShotHandler.GetShotArray(); for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = shotArray[i].Position; shotUpdate.ShotAge[i] = shotArray[i].Age; } netPeer.SendPlayerUpdate(update, shotUpdate); }
public void SendMyPlayerUpdate() { if (!networkEnabled) { return; } PlayerUpdate update = new PlayerUpdate(); update.WorldMatrix = playerShip.Position.WorldMatrix; update.State = (int)playerShip.State; update.WaitCount = playerShip.WaitCount; update.DeathCount = playerShip.DeathCount; update.Sounds = (int)playerShip.Sounds; update.Score = playerShip.Score; ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotAge = new float[Constants.NumShots]; shotUpdate.ShotPosition = new Vector3[Constants.NumShots]; shotUpdate.ShotAlive = new bool[Constants.NumShots]; Photon[] shotArray = playerShip.ShotHandler.GetShotArray(); for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = shotArray[i].Location; shotUpdate.ShotAge[i] = shotArray[i].Age; shotUpdate.ShotAlive[i] = shotArray[i].Alive; } peer.SendPlayerUpdate(update, shotUpdate); }
// These routines handle the communication between the game peers. public void SendPlayerUpdate(PlayerUpdate update, ShotUpdate shotUpdate) { if (inSession) { NetworkPacket packet = new NetworkPacket(); packet.Write((byte)MessageType.PlayerUpdateID); packet.Write(update); for (int i = 0; i < Constants.NumShots; i++) { packet.Write(shotUpdate.ShotPosition[i]); packet.Write(shotUpdate.ShotAge[i]); } peerObject.SendTo((int)PlayerID.AllPlayers, packet, 0, SendFlags.NoComplete | SendFlags.NoLoopback); } }
public void DataReceived(object sender, ReceiveEventArgs rea) { int senderID = rea.Message.SenderID; //Ignore messages received before we are initialized if ((gameState == GameStates.Loading) || (gameState == GameStates.Config)) { rea.Message.ReceiveData.Dispose(); return; } byte mType = (byte)rea.Message.ReceiveData.Read(typeof(byte)); MessageType messageType = (MessageType)mType; switch (messageType) { case MessageType.PlayerUpdateID: { PlayerUpdate update = (PlayerUpdate)rea. Message.ReceiveData.Read(typeof(PlayerUpdate)); ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotPosition = new Vector2[Constants.NumShots]; shotUpdate.ShotAge = new int[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = (Vector2)rea.Message.ReceiveData.Read(typeof(Vector2)); shotUpdate.ShotAge[i] = (int)rea.Message.ReceiveData.Read(typeof(int)); } rea.Message.ReceiveData.Dispose(); lock (otherPlayers) { object playerObject = otherPlayers[senderID]; if (null == playerObject) { return; } RemotePlayer player = (RemotePlayer)playerObject; Shot[] shotArray = new Shot[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotArray[i] = new Shot(); shotArray[i].Position = shotUpdate.ShotPosition[i]; shotArray[i].Age = shotUpdate.ShotAge[i]; } player.Ship.ShotHandler.SetShotArray(shotArray); player.Ship.Position = update.ShipPosition; player.Ship.Outline = update.Outline; player.Ship.Velocity = update.ShipVelocity; player.Ship.State = update.State; player.Ship.WaitCount = update.WaitCount; player.Ship.DeathCount = update.DeathCount; player.Ship.FlameIndex = update.FlameIndex; player.Ship.Sounds = (Sounds)update.Sounds; player.Ship.Score = update.Score; player.UpdateTime = DateTime.Now; player.Active = true; otherPlayers[senderID] = player; } break; } case MessageType.GameParamUpdateID: { GameParamUpdate update = (GameParamUpdate)rea.Message. ReceiveData.Read(typeof(GameParamUpdate)); rea.Message.ReceiveData.Dispose(); gravity = update.Gravity; gameSpeed = update.GameSpeed; if (update.BounceBack != 0) { bounceBack = true; } else { bounceBack = false; } if (update.InverseGravity != 0) { inverseGravity = true; } else { inverseGravity = false; } if (update.BlackHole != 0) { blackHole = true; } else { blackHole = false; } Size newWindowSize = update.WindowSize; Rectangle newBounds = new Rectangle(this.windowBounds.Location, newWindowSize); //Initialize(newBounds); break; } case MessageType.Add1ToScore: { rea.Message.ReceiveData.Dispose(); ship.Score += 1; break; } case MessageType.Add2ToScore: { rea.Message.ReceiveData.Dispose(); ship.Score += 2; break; } case MessageType.GamePaused: { rea.Message.ReceiveData.Dispose(); gameState = GameStates.Paused; break; } case MessageType.GameRunning: { rea.Message.ReceiveData.Dispose(); if (gameState == GameStates.Paused) { gameState = GameStates.Running; } break; } } }
public void DataReceived(object sender, ReceiveEventArgs rea) { int senderID = rea.Message.SenderID; //Ignore messages received before we are initialized if ((gameState == GameStates.Loading) || (gameState == GameStates.Config)) { rea.Message.ReceiveData.Dispose(); return; } byte mType = (byte)rea.Message.ReceiveData.Read(typeof(byte)); MessageType messageType = (MessageType)mType; switch (messageType) { case MessageType.PlayerUpdateID: { PlayerUpdate update = (PlayerUpdate)rea.Message.ReceiveData.Read(typeof(PlayerUpdate)); ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotPosition = new Vector3[Constants.NumShots]; shotUpdate.ShotAge = new float[Constants.NumShots]; shotUpdate.ShotAlive = new bool[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = (Vector3)rea.Message.ReceiveData.Read(typeof(Vector3)); shotUpdate.ShotAge[i] = (int)rea.Message.ReceiveData.Read(typeof(int)); shotUpdate.ShotAlive[i] = (bool)rea.Message.ReceiveData.Read(typeof(bool)); } rea.Message.ReceiveData.Dispose(); lock (opponentShip) { opponentShip.Position.WorldMatrix = update.WorldMatrix; opponentShip.Score = update.Score; opponentShip.Sounds = (Sounds)update.Sounds; opponentShip.WaitCount = update.WaitCount; opponentShip.SetState((ShipState)update.State); Photon[] shotArray = opponentShip.ShotHandler.GetShotArray(); for (int i = 0; i < Constants.NumShots; i++) { shotArray[i].Location = shotUpdate.ShotPosition[i]; shotArray[i].Age = shotUpdate.ShotAge[i]; shotArray[i].Alive = shotUpdate.ShotAlive[i]; } opponentShip.ShotHandler.SetShotArray(shotArray); } break; } case MessageType.Add1ToScore: { rea.Message.ReceiveData.Dispose(); playerShip.Score += 1; break; } case MessageType.GamePaused: { rea.Message.ReceiveData.Dispose(); gameState = GameStates.Paused; break; } case MessageType.GameRunning: { rea.Message.ReceiveData.Dispose(); if (gameState == GameStates.Paused) { gameState = GameStates.Running; } break; } } }
public void SendMyPlayerUpdate() { if (!networkEnabled) return; PlayerUpdate update = new PlayerUpdate(); update.WorldMatrix = playerShip.Position.WorldMatrix; update.State = (int)playerShip.State; update.WaitCount = playerShip.WaitCount; update.DeathCount = playerShip.DeathCount; update.Sounds = (int)playerShip.Sounds; update.Score = playerShip.Score; ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotAge = new float[Constants.NumShots]; shotUpdate.ShotPosition = new Vector3[Constants.NumShots]; shotUpdate.ShotAlive = new bool[Constants.NumShots]; Photon[] shotArray = playerShip.ShotHandler.GetShotArray(); for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = shotArray[i].Location; shotUpdate.ShotAge[i] = shotArray[i].Age; shotUpdate.ShotAlive[i] = shotArray[i].Alive; } peer.SendPlayerUpdate(update, shotUpdate); }
public void DataReceived(object sender, ReceiveEventArgs rea) { int senderID = rea.Message.SenderID; //Ignore messages received before we are initialized if ((gameState == GameStates.Loading) || (gameState == GameStates.Config)) { rea.Message.ReceiveData.Dispose(); return; } byte mType = (byte)rea.Message.ReceiveData.Read(typeof(byte)); MessageType messageType = (MessageType)mType; switch (messageType) { case MessageType.PlayerUpdateID: { PlayerUpdate update = (PlayerUpdate)rea.Message.ReceiveData.Read(typeof(PlayerUpdate)); ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotPosition = new Vector3[Constants.NumShots]; shotUpdate.ShotAge = new float[Constants.NumShots]; shotUpdate.ShotAlive = new bool[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = (Vector3)rea.Message.ReceiveData.Read(typeof(Vector3)); shotUpdate.ShotAge[i] = (int)rea.Message.ReceiveData.Read(typeof(int)); shotUpdate.ShotAlive[i] = (bool)rea.Message.ReceiveData.Read(typeof(bool)); } rea.Message.ReceiveData.Dispose(); lock(opponentShip) { opponentShip.Position.WorldMatrix = update.WorldMatrix; opponentShip.Score = update.Score; opponentShip.Sounds = (Sounds)update.Sounds; opponentShip.WaitCount = update.WaitCount; opponentShip.SetState((ShipState)update.State); Photon[] shotArray = opponentShip.ShotHandler.GetShotArray(); for (int i = 0; i < Constants.NumShots; i++) { shotArray[i].Location = shotUpdate.ShotPosition[i]; shotArray[i].Age = shotUpdate.ShotAge[i]; shotArray[i].Alive = shotUpdate.ShotAlive[i]; } opponentShip.ShotHandler.SetShotArray(shotArray); } break; } case MessageType.Add1ToScore: { rea.Message.ReceiveData.Dispose(); playerShip.Score += 1; break; } case MessageType.GamePaused: { rea.Message.ReceiveData.Dispose(); gameState = GameStates.Paused; break; } case MessageType.GameRunning: { rea.Message.ReceiveData.Dispose(); if (gameState == GameStates.Paused) gameState = GameStates.Running; break; } } }
public void DataReceived(object sender, ReceiveEventArgs rea) { int senderID = rea.Message.SenderID; //Ignore messages received before we are initialized if ((gameState == GameStates.Loading) || (gameState == GameStates.Config)) { rea.Message.ReceiveData.Dispose(); return; } byte mType = (byte)rea.Message.ReceiveData.Read(typeof(byte)); MessageType messageType = (MessageType)mType; switch (messageType) { case MessageType.PlayerUpdateID: { PlayerUpdate update = (PlayerUpdate)rea. Message.ReceiveData.Read(typeof(PlayerUpdate)); ShotUpdate shotUpdate = new ShotUpdate(); shotUpdate.ShotPosition = new Vector2[Constants.NumShots]; shotUpdate.ShotAge = new int[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotUpdate.ShotPosition[i] = (Vector2)rea.Message.ReceiveData.Read(typeof(Vector2)); shotUpdate.ShotAge[i] = (int)rea.Message.ReceiveData.Read(typeof(int)); } rea.Message.ReceiveData.Dispose(); lock (otherPlayers) { object playerObject = otherPlayers[senderID]; if (null == playerObject) return; RemotePlayer player = (RemotePlayer) playerObject; Shot[] shotArray = new Shot[Constants.NumShots]; for (int i = 0; i < Constants.NumShots; i++) { shotArray[i] = new Shot(); shotArray[i].Position = shotUpdate.ShotPosition[i]; shotArray[i].Age = shotUpdate.ShotAge[i]; } player.Ship.ShotHandler.SetShotArray(shotArray); player.Ship.Position = update.ShipPosition; player.Ship.Outline = update.Outline; player.Ship.Velocity = update.ShipVelocity; player.Ship.State = update.State; player.Ship.WaitCount = update.WaitCount; player.Ship.DeathCount = update.DeathCount; player.Ship.FlameIndex = update.FlameIndex; player.Ship.Sounds = (Sounds)update.Sounds; player.Ship.Score = update.Score; player.UpdateTime = DateTime.Now; player.Active = true; otherPlayers[senderID] = player; } break; } case MessageType.GameParamUpdateID: { GameParamUpdate update = (GameParamUpdate)rea.Message. ReceiveData.Read(typeof(GameParamUpdate)); rea.Message.ReceiveData.Dispose(); gravity = update.Gravity; gameSpeed = update.GameSpeed; if (update.BounceBack != 0) bounceBack = true; else bounceBack = false; if (update.InverseGravity != 0) inverseGravity = true; else inverseGravity = false; if (update.BlackHole != 0) blackHole = true; else blackHole = false; Size newWindowSize = update.WindowSize; Rectangle newBounds = new Rectangle(this.windowBounds.Location, newWindowSize); //Initialize(newBounds); break; } case MessageType.Add1ToScore: { rea.Message.ReceiveData.Dispose(); ship.Score += 1; break; } case MessageType.Add2ToScore: { rea.Message.ReceiveData.Dispose(); ship.Score += 2; break; } case MessageType.GamePaused: { rea.Message.ReceiveData.Dispose(); gameState = GameStates.Paused; break; } case MessageType.GameRunning: { rea.Message.ReceiveData.Dispose(); if (gameState == GameStates.Paused) gameState = GameStates.Running; break; } } }