Beispiel #1
0
        public void Update(GameTime gameTime)
        {
            KeyboardState kState = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (healthTime > 0)
            {
                healthTime -= dt;
            }

            anim = animations[(int)direction];

            /* //USE ENUM TO SIMPLIFY THE BELOW!
             * switch(direction)
             * {
             *  case Dir.Down:
             *      anim = animations[0];
             *      break;
             *  case Dir.Up:
             *      anim = animations[1];
             *      break;
             *  case Dir.Left:
             *      anim = animations[2];
             *      break;
             *  case Dir.Right:
             *      anim = animations[3];
             *      break;
             *  default:
             *      break;
             * }
             */

            if (isMoving)
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(1);
            }

            isMoving = false;

            if (kState.IsKeyDown(Keys.Right))
            {
                direction = Dir.Right;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Left))
            {
                direction = Dir.Left;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Up))
            {
                direction = Dir.Up;
                isMoving  = true;
            }

            if (kState.IsKeyDown(Keys.Down))
            {
                direction = Dir.Down;
                isMoving  = true;
            }



            if (isMoving)
            {
                Vector2 tempPos = position;

                switch (direction)
                {
                case Dir.Right:
                    tempPos.X += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.X += speed * dt;
                    }
                    break;

                case Dir.Left:
                    tempPos.X -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.X -= speed * dt;
                    }
                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.Y -= speed * dt;
                    }
                    break;

                case Dir.Down:
                    tempPos.Y += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius))
                    {
                        position.Y += speed * dt;
                    }
                    break;

                default:
                    break;
                }
            }

            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                Projectile.projectiles.Add(new Projectile(position, direction));
            }

            kStateOld = kState;
        }
Beispiel #2
0
        public void Update(GameTime gameTime, int mapW, int mapH)
        {
            KeyboardState kState = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (healthTimer > 0)
            {
                healthTimer -= dt;
            }

            anim = animations[(int)direction];
            if (isMoving)
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(1);
            }

            isMoving = false;

            if (kState.IsKeyDown(Keys.Right))
            {
                direction = Dir.Right;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Left))
            {
                direction = Dir.Left;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Up))
            {
                direction = Dir.Up;
                isMoving  = true;
            }
            if (kState.IsKeyDown(Keys.Down))
            {
                direction = Dir.Down;
                isMoving  = true;
            }
            if (isMoving)
            {
                Vector2 tempPos = position;
                switch (direction)
                {
                case Dir.Right:
                    tempPos.X += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.X < mapW)
                    {
                        position.X += speed * dt;
                    }
                    break;

                case Dir.Left:
                    tempPos.X -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.X > 0)
                    {
                        position.X -= speed * dt;
                    }
                    break;

                case Dir.Down:
                    tempPos.Y += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y < mapH)
                    {
                        position.Y += speed * dt;
                    }
                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y > 0)
                    {
                        position.Y -= speed * dt;
                    }
                    break;

                default:
                    break;
                }
            }
            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                Projectile.projectiles.Add(new Projectile(position, direction));
            }
            kStateOld = kState;
        }
Beispiel #3
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }
            if (player.Health > 0)
            {
                player.Update(gameTime, myMap.WidthInPixels, myMap.HeightInPixels);
            }

            float tempX = player.Position.X;
            float tempY = player.Position.Y;
            int   camW  = _graphics.PreferredBackBufferWidth;
            int   camH  = _graphics.PreferredBackBufferHeight;

            int mapW = myMap.WidthInPixels;
            int mapH = myMap.HeightInPixels;

            if (tempX < camW / 2)
            {
                tempX = camW / 2;
            }

            if (tempY < camH / 2)
            {
                tempY = camH / 2;
            }

            if (tempX > (mapW - (camW / 2)))
            {
                tempX = (mapW - (camW / 2));
            }
            if (tempY > (mapH - (camH / 2)))
            {
                tempY = (mapH - (camH / 2));
            }

            cam.LookAt(new Vector2(tempX, tempY));

            foreach (Projectile proj in Projectile.projectiles)
            {
                proj.Update(gameTime);
            }
            foreach (Enemy en in Enemy.enemies)
            {
                en.Update(gameTime, player.Position);
            }
            foreach (Projectile proj in Projectile.projectiles)
            {
                foreach (Enemy en in Enemy.enemies)
                {
                    int sum = proj.Radius + en.Radius;
                    if (Vector2.Distance(proj.Position, en.Position) < sum)
                    {
                        proj.Collided = true;
                        en.Health--;
                    }
                }
                if (Obstacle.didCollide(proj.Position, proj.Radius))
                {
                    proj.Collided = true;
                }
            }
            foreach (Enemy en in Enemy.enemies)
            {
                int sum = player.Radius + en.Radius;
                if (Vector2.Distance(player.Position, en.Position) < sum && player.HealthTimer <= 0)
                {
                    player.Health--;
                    player.HealthTimer = 1.5f;
                }
            }
            Projectile.projectiles.RemoveAll(p => p.Collided);
            Enemy.enemies.RemoveAll(e => e.Health <= 0);

            mapRenderer.Update(gameTime);
            base.Update(gameTime);
        }