// see if the other ship has hit us, or if the other shots have hit us
        public void TestShip(RemotePlayer player)
        {
            Ship otherShip = player.Ship;

            // if we're not alive, don't do any tests...
            if (state != ShipState.Normal)
                return;

            // Test if their shots are close enough to us.
            if (otherShip.shots.TestShots(this)) {
                SetState(ShipState.Dying);
                game.SendTwoPointsToPlayer(player.PlayerID);	// give them 2 points
            }

            Vector2 delta = this.position - otherShip.position;
            if (Vector2.Length(delta) < Constants.ShipCollisionLimit) {
                SetState(ShipState.Dying);
                game.SendPointToAllPlayers(); // all others get a point
            }
        }
Esempio n. 2
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 PlayerCreated(object sender, PlayerCreatedEventArgs pcea)
        {
            Peer peer = (Peer) sender;
            int playerID = pcea.Message.PlayerID;
            PlayerInformation playerInfo = peer.GetPeerInformation(playerID);

            // See if the player that was just created is us
            if (playerInfo.Local) {
                localPlayer.ID = playerID;
                localPlayer.Name = playerInfo.Name;
            }
                // If not, create a remote player
            else {
                Ship newShip = new Ship(this);
                newShip.HostName = playerInfo.Name.ToUpper();
                newShip.State = (int)ShipState.Normal;
                newShip.ScreenBounds = this.windowBounds;
                RemotePlayer newPlayer = new RemotePlayer(playerID, playerInfo.Name, newShip);
                lock (otherPlayers) {
                    otherPlayers.Add(playerID, newPlayer);
                }
            }
        }