예제 #1
0
        public void Update(GameTime gameTime)
        {
            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;
        }
예제 #2
0
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            base.Update(gameTime);

            // player movement
            if (player.Health > 0)
            {
                player.Update(gameTime, myMap.WidthInPixels, myMap.HeightInPixels);
            }

            // for CAMERA to follow player but to stop when you get near the edge
            float tempX = player.Position.X;
            float tempY = player.Position.Y;
            int   camW  = graphics.PreferredBackBufferWidth;  // LEFT side of the map
            int   camH  = graphics.PreferredBackBufferHeight; // TOP side of the map
            int   mapW  = myMap.WidthInPixels;                // RIGHT side of the map
            int   mapH  = myMap.HeightInPixels;               // BOTTOM side of the map

            if (tempX < camW / 2)                             // if the player is too close to the LEFT side of the screen edge
            {
                tempX = camW / 2;                             // set cam.LookAt to be graphics.PreferredBackBufferWidth
            }
            if (tempY < camH / 2)                             // if the player is too close to the TOP side of the screen edge
            {
                tempY = camH / 2;                             // set cam.LookAt to be graphics.PreferredBackBufferHeight
            }
            if (tempX > (mapW - (camW / 2)))                  // RIGHT
            {
                tempX = mapW - (camW / 2);
            }
            if (tempY > (mapH - (camH / 2))) // RIGHT
            {
                tempY = mapH - (camH / 2);   // BOTTOM
            }


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


            // projectiles
            foreach (Projectile proj in Projectile.projectiles)
            {
                proj.Update(gameTime);
            }

            // enemies
            foreach (Enemy en in Enemy.enemies)
            {
                en.Update(gameTime, player.Position);
            }

            // hit detection between projectile and enemy and obstacle
            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) // if the distance of the projectle and enemy is less than sum, then theres a collision
                    {
                        proj.Collided = true;                               // projectile disappers when hitting enemy
                        en.Health--;                                        // take 1 health away
                    }
                }

                // collision between projectile and obstacle
                if (Obstacle.didCollide(proj.Position, proj.Radius))
                {
                    proj.Collided = true; // projectile disappers when hitting obstacle
                }
            }

            // check for collision between player and enemy
            foreach (Enemy en  in Enemy.enemies)
            {
                int sum = en.Radius + player.Radius;
                // if player and enemy are too close
                if (Vector2.Distance(player.Position, en.Position) < sum && player.HealthTimer <= 0) // check distancing
                {
                    player.Health--;                                                                 // reduce player health by 1
                    player.HealthTimer = 1.5F;                                                       // 1.5 second delay between hits
                }
            }

            // remove all projectiles that have collided
            Projectile.projectiles.RemoveAll(p => p.Collided);

            // remove enemies with health at 0
            Enemy.enemies.RemoveAll(e => e.Health <= 0);
        }
예제 #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);
            }

            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);

            base.Update(gameTime);
        }
예제 #4
0
        // Methods
        // player movement
        public void Update(GameTime gameTime, int mapW, int mapH) // int mapW, int mapH is for the right and bottom camera edges
        {
            KeyboardState kstate = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

            // so players health doesnt decline too fast when hit
            if (healthTimer > 0)
            {
                healthTimer -= dt;
            }

            // set the animation based on direction player is facing
            anim = animations[(int)direction];

            // this cycles through the player animations
            if (isMoving)
            {
                anim.Update(gameTime);
            }
            else
            {
                anim.setFrame(1); // return animation to standing still frame
            }
            // this means every frame the player stops moving
            isMoving = false;
            // however if the keys are held, player will move
            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;
            }

            // movement speed
            if (isMoving)
            {
                Vector2 tempPos = position; // for obstable collision

                switch (direction)
                {
                case Dir.Down:
                    tempPos.Y += speed * dt;                                       // put next move into variable and pass to Obstacle.didCollide
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y < mapH) // if collision is NOT made and not at edge of screen
                    {
                        position.Y += speed * dt;                                  // do the actual movement
                    }
                    break;

                case Dir.Up:
                    tempPos.Y -= speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.Y > 0)
                    {
                        position.Y -= 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.Right:
                    tempPos.X += speed * dt;
                    if (!Obstacle.didCollide(tempPos, radius) && tempPos.X < mapW)
                    {
                        position.X += speed * dt;
                    }
                    break;

                default:
                    break;
                }
            }

            // when spacebar is pressed, create ONE projectile
            if (kstate.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                Projectile.projectiles.Add(new Projectile(position, direction));
                MySounds.projectileSound.Play(1f, 0.5f, 0f);
            }

            kStateOld = kstate;
        }
예제 #5
0
        public void Update(GameTime gameTime)
        {
            KeyboardState kState = Keyboard.GetState();
            float         dt     = (float)gameTime.ElapsedGameTime.TotalSeconds;

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

            /// <summary>
            /// this one liner is a better version of switch statement below.
            /// Because "directions" is enum, so basically
            /// it resolves to [0, 1, 2, 3]
            /// </summary>
            animSprite = animations[(int)direction];
            //switch (direction)
            //{
            //    case Dir.Down:
            //        animSprite = animations[0];
            //        break;
            //    case Dir.Up:
            //        animSprite = animations[1];
            //        break;
            //    case Dir.Left:
            //        animSprite = animations[2];
            //        break;
            //    case Dir.Right:
            //        animSprite = animations[3];
            //        break;
            //    default:
            //        break;
            //}

            // for stopping character to always moving
            if (isMoving)
            {
                animSprite.Update(gameTime);
            }
            else
            {
                animSprite.SetFrame(1);
            }

            #region Changing direction
            // every frame will be false and only if keys are down, it will be true
            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;
            }
            #endregion

            if (isMoving)
            {
                // holding current player position at this particular moment
                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;
                }
            }

            // this makes projectile shoot once only when Space key is pressed down
            if (kState.IsKeyDown(Keys.Space) && kStateOld.IsKeyUp(Keys.Space))
            {
                Projectile.projectiles.Add(new Projectile(position, direction));
            }

            kStateOld = kState;
        }
예제 #6
0
        //=============
        // ===UPDATE===
        //=============
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            {
                Exit();
            }

            // TODO: Add your update logic here
            if (player.Health > 0)
            {
                player.Update(gameTime);
            }

            // shooting projectiles
            foreach (Projectile proj in Projectile.projectiles)
            {
                proj.Update(gameTime);
            }
            // executing all enemies updates
            foreach (Enemy en in Enemy.enemies)
            {
                en.Update(gameTime, player.Position);
            }

            // checking for enemy collision with projectiles.
            // On collision destroying enemies and projectiles
            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;
                }
            }

            // checking if enemies colliding with the player
            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;
                }
            }

            // actually destroying enemies and projectiles
            Projectile.projectiles.RemoveAll(p => p.Collided == true);
            Enemy.enemies.RemoveAll(e => e.Health <= 0);

            base.Update(gameTime);
        }// end of UPDATE