public Shots()
 {
     shots = new Shot[Constants.NumShots];
     for (int count = 0; count < Constants.NumShots; count++) {
         shots[count] = new Shot();
     }
 }
 public void SetShotArray(Shot[] shotArray)
 {
     this.shots = shotArray;
 }
예제 #3
0
        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 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;
                }
            }
        }