示例#1
0
        // UPDATE BEGIN<===============================================================
        protected override void Update(GameTime gameTime)
        {
            var           aliveMonsters = (Monster.enemies.FindAll(en => !en.Dead));
            KeyboardState keys          = Keyboard.GetState();

            if (aliveMonsters.Count == 0)
            {
                winScreenOn = true;
                if (keys.IsKeyDown(Keys.Enter))
                {
                    foreach (Monster en in Monster.enemies)
                    {
                        en.Update(gameTime, player.Pos);
                    }
                    winScreenOn = false;
                    endScreenOn = false;

                    player.Health = +5;
                }
            }



            //++++++++++++++++++++++++STARTSCREEN+++++++++++++++++++++++


            if (startScreenOn || endScreenOn)
            {
                if (keys.IsKeyDown(Keys.Enter))
                {
                    startScreenOn       = false;
                    MediaPlayer.IsMuted = true;
                }
            }
            if (player.Health <= 0)
            {
                endScreenOn = true;
                if (keys.IsKeyDown(Keys.Enter))
                {
                    endScreenOn = false;

                    player.Health = +5;
                }
            }
            if (startScreenOn == false && endScreenOn == false)
            {
                elapsed += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
                if (elapsed >= delay)
                {
                    if (frames >= 3)
                    {
                        frames = 0;
                    }
                    else
                    {
                        frames++;
                    }
                    elapsed = 0;
                }
                sourceRect = new Rectangle(96 * frames, 0, 96, 96);

                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    Exit();
                }
                if (player.Health > 0)
                {
                    player.Update(gameTime);
                }

                playerCam.LookAt(player.Pos);

                foreach (Shooting bullet in Shooting.bullets)
                {
                    bullet.Update(gameTime);
                }

                foreach (Monster en in Monster.enemies)
                {
                    en.Update(gameTime, player.Pos);
                }

                foreach (Shooting bullet in Shooting.bullets)
                {
                    foreach (Monster en in Monster.enemies)
                    {
                        int sum = bullet.Radius + en.Rad;
                        if (Vector2.Distance(bullet.Position, en.Pos) < sum)
                        {
                            bullet.Collision = true;
                            en.Health--;
                        }
                    }
                    // Collision between bullet and the obstacles
                    if (Obstacle._collided(bullet.Position, bullet.Radius))
                    {
                        bullet.Collision = true;
                    }
                }

                foreach (Monster en in Monster.enemies)
                {
                    int sum = player.Rad + en.Rad;
                    if (Vector2.Distance(player.Pos, en.Pos) < sum && player.Healthdelay <= 0)
                    {
                        player.Health--;
                        player.Healthdelay = 1.5f;
                    }
                }
                Shooting.bullets.RemoveAll(p => p.Collision);
                Monster.enemies.RemoveAll(e => e.Health <= 0);

                base.Update(gameTime);
            }

            //++++++++++++++++++++++++STARTSCREEN+++++++++++++++++++++++
        }
示例#2
0
        public void Update(GameTime gt)
        {
            KeyboardState currentKeyState = Keyboard.GetState();
            float         dt = (float)gt.ElapsedGameTime.TotalSeconds;

            anim = animations[(int)direction];
            if (healthDelay > 0)
            {
                healthDelay -= dt;
            }

            if (isWalking)
            {
                anim.Update(gt);
            }
            else
            {
                anim.setFrame(1);
            }

            isWalking = false;

            if (currentKeyState.IsKeyDown(Keys.Right))
            {
                direction = Dir.Right;
                isWalking = true;
            }
            if (currentKeyState.IsKeyDown(Keys.Left))
            {
                direction = Dir.Left;
                isWalking = true;
            }
            if (currentKeyState.IsKeyDown(Keys.Up))
            {
                direction = Dir.Up;
                isWalking = true;
            }
            if (currentKeyState.IsKeyDown(Keys.Down))
            {
                direction = Dir.Down;
                isWalking = true;
            }
            if (isWalking)
            {
                Vector2 tempPos = pos;
                switch (direction)
                {
                case Dir.Right:
                    tempPos.X += speed * dt;
                    if (!Obstacle._collided(tempPos, rad))
                    {
                        pos.X += speed * dt;
                    }
                    break;

                case Dir.Left:
                    tempPos.X -= speed * dt;
                    if (!Obstacle._collided(tempPos, rad))
                    {
                        pos.X -= speed * dt;
                    }
                    break;

                case Dir.Down:
                    tempPos.Y += speed * dt;
                    if (!Obstacle._collided(tempPos, rad))
                    {
                        pos.Y += speed * dt;
                    }
                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Obstacle._collided(tempPos, rad))
                    {
                        pos.Y -= speed * dt;
                    }
                    break;

                default:
                    break;
                }
            }

            if (currentKeyState.IsKeyDown(Keys.Space) && previousKeyState.IsKeyUp(Keys.Space))
            {
                Shooting.bullets.Add(new Shooting(pos, direction));
            }
            previousKeyState = currentKeyState;
        }