Beispiel #1
0
 public PlayerX(GameplayObject player,Texture2D texture)
 {
     this.player=player;
     player.Texture=texture;
     player.Rotation = MathHelper.ToRadians(-90);
     positionX = 0;
     positionY = 0;
 }
Beispiel #2
0
 public Player(GameplayObject player, float ms, float fa, float ba, float fv, float mfv, float mbv, Texture2D texture,Vector2 position)
 {
     this.player = player;
     this.ms = ms;
     this.fa = fa;
     this.ba = ba;
     this.fv = fv;
     this.mfv = mfv;
     this.mbv = mbv;
     player.Texture = texture;
     player.Position = position;
     player.Rotation = MathHelper.ToRadians(-90);
     //use origin for coordinate system//
     origin = new GameplayObject();
     origin.Texture = texture;
     origin.Position = position;
 }
 /// <summary>
 /// Collision logic with another GameplayObject
 /// </summary>
 /// <param name="target"></param>
 public virtual void Collision(GameplayObject target)
 {
 }
        private void ProcessData(byte[] data)
        {
            readStream.SetLength(0);
            readStream.Position = 0;

            readStream.Write(data, 0, data.Length);
            readStream.Position = 0;

            Protocol p;

            try
            {
                p = (Protocol)reader.ReadByte();
                if (p == Protocol.Connected)
                {
                    byte id = reader.ReadByte();
                    string ip = reader.ReadString();
                    if (!enemyConnected)
                    {
                        enemyConnected = true;
                        enemy.Rotation = MathHelper.ToRadians(90);
                        enemy.Position = new Vector2(0 + enemy.Rectangle.X, 0 + enemy.Rectangle.Y);

                        writeStream.Position = 0;
                        writer.Write((byte)Protocol.Connected);
                        SendData(GetDataFromMemoryStream(writeStream));
                    }

                }
                else if (p == Protocol.Disconnected)
                {
                    enemyConnected = false;
                    byte id = reader.ReadByte();
                    string ip = reader.ReadString();
                    enemy = null;
                }
                else if (p == Protocol.PlayerMoved)
                {
                    float px = reader.ReadSingle();
                    float py = reader.ReadSingle();
                    byte id = reader.ReadByte();
                    string ip = reader.ReadString();
                    enemy.Position = new Vector2(enemy.Position.X + px, enemy.Position.Y - py);
                }
                else if (p == Protocol.BulletCreated)
                {
                    GameplayObject bullet = new GameplayObject();
                    bullet.Texture = bulletTexture;
                    bullet.Position = enemy.Position;
                    bullet.Rotation = enemy.Rotation;
                    bullet.Speed = 200;
                    bullet.Velocity = new Vector2(bullet.Speed * (float)Math.Cos(bullet.Rotation),
                        bullet.Speed * (float)Math.Sin(bullet.Rotation));
                    enemyBullets.Add(bullet);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            previous = current;
            current = Keyboard.GetState();
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == Microsoft.Xna.Framework.Input.ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            Vector2 iPosition = new Vector2(player.Position.X, player.Position.Y);

            Vector2 movement = Vector2.Zero;

            if (current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Left))
                player.Rotation += MathHelper.ToRadians(-ms);
            if (current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Right))
                player.Rotation += MathHelper.ToRadians(+ms);
            if (current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Up))
            {
                if (fv < mfv)
                    fv += fa;
            }
            if (current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Down))
            {
                if (fv > mbv)
                    fv -= ba;
            }

            movement.X = fv * (float)Math.Cos(player.Rotation);
            movement.Y = fv * (float)Math.Sin(player.Rotation);

            player.Velocity = movement;
            player.Update(gameTime);

            foreach (GameplayObject gameObject in playerBullets)
                gameObject.Update(gameTime);
            foreach (GameplayObject gameObject in enemyBullets)
                gameObject.Update(gameTime);

            Vector2 nPosition = new Vector2(player.Position.X, player.Position.Y);
            Vector2 dPosition = Vector2.Subtract(nPosition, iPosition);

            if (dPosition != Vector2.Zero)
            {
                writeStream.Position = 0;
                writer.Write((byte)Protocol.PlayerMoved);
                writer.Write(dPosition.X);
                writer.Write(dPosition.Y);
                SendData(GetDataFromMemoryStream(writeStream));
            }

            if (bulletTimer.TotalSeconds > 0) bulletTimer = bulletTimer.Subtract(gameTime.ElapsedGameTime);
            else
            {
                if (current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Space) && previous.IsKeyUp(Microsoft.Xna.Framework.Input.Keys.Space))
                {
                    GameplayObject bullet = new GameplayObject();
                    bullet.Texture = bulletTexture;
                    bullet.Position = player.Position;
                    bullet.Rotation = player.Rotation;
                    bullet.Speed = 200;
                    bullet.Velocity = new Vector2(bullet.Speed * (float)Math.Cos(bullet.Rotation),
                        bullet.Speed * (float)Math.Sin(bullet.Rotation));
                    writeStream.Position = 0;
                    writer.Write((byte)Protocol.BulletCreated);
                    SendData(GetDataFromMemoryStream(writeStream));
                    playerBullets.Add(bullet);
                    bulletTimer = TimeSpan.FromSeconds(shotseconds);
                }
            }

            base.Update(gameTime);
        }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            enemyConnected=false;
            readStream = new MemoryStream();
            reader = new BinaryReader(readStream);
            writeStream = new MemoryStream();
            writer = new BinaryWriter(writeStream);
            player = new GameplayObject();
            enemy = new GameplayObject();
            playerBullets = new List<GameplayObject>(10);
            enemyBullets = new List<GameplayObject>(10);

            current = previous = Keyboard.GetState();
            shotseconds = 5;
            bulletTimer = TimeSpan.FromSeconds(shotseconds);

            base.Initialize();
        }