Example #1
0
        /// <summary>
        /// Updates player and frame moving logic
        /// </summary>
        /// <param name="gameTime">Snapshot of times</param>
        public new void Update(GameTime gameTime)
        {
            MouseState    mouse        = Mouse.GetState();
            KeyboardState keyboard     = Keyboard.GetState();
            KeyboardState lastKeyboard = CurrentArea.LastKeyboard;

            if (keyboard.IsKeyUp(Keys.Escape) && lastKeyboard.IsKeyDown(Keys.Escape))              //toggle main menu with escape key
            {
                CurrentArea.AreaMenu.isShowing = !CurrentArea.AreaMenu.isShowing;
                CurrentArea.Paused             = CurrentArea.AreaMenu.isShowing;
                //inventoryMenu.isShowing = false;
            }
            if (lastKeyboard.IsKeyDown(Keys.E) && keyboard.IsKeyUp(Keys.E) && !CurrentArea.AreaMenu.isShowing)
            {
                //inventoryMenu.isShowing = !inventoryMenu.isShowing;
                CurrentArea.Paused = CurrentArea.AreaMenu.isShowing; // || inventoryMenu.isShowing;
            }

            RPG.MouseVisible = CurrentArea.AreaMenu.isShowing; // || inventoryMenu.isShowing; //enable mouse if menu is shown
            if (sprite.State != SpriteState.Die && !CurrentArea.Paused)
            {
                Vector2 moveDirection = Vector2.Zero;
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    Attack();
                }

                if (keyboard.IsKeyDown(Keys.F1) && !lastKeyboard.IsKeyDown(Keys.F1))
                {
                    RPG.DebugMode = !RPG.DebugMode;
                }

                //movement vectors
                if (keyboard.IsKeyDown(Keys.W))
                {
                    Turn(SpriteDirection.North);
                    moveDirection.Y--;
                }
                if (keyboard.IsKeyDown(Keys.A))
                {
                    Turn(SpriteDirection.West);
                    moveDirection.X--;
                }
                if (keyboard.IsKeyDown(Keys.S))
                {
                    Turn(SpriteDirection.South);
                    moveDirection.Y++;
                }
                if (keyboard.IsKeyDown(Keys.D))
                {
                    Turn(SpriteDirection.East);
                    moveDirection.X++;
                }
                //keep speed constant
                moveDirection.Normalize();
                moveDirection *= 2;

                if (keyboard.IsKeyDown(Keys.LeftShift))
                {
                    moveDirection *= 3;             //sprint or speed key
                }
                if (moveDirection.Length() > 0 && CurrentArea.CheckForCollisions(this, moveDirection))
                {
                    Move(moveDirection);
                }

                else if (moveDirection.Length() > 0 && keyboard.IsKeyDown(Keys.Space) && RPG.DebugMode)
                {
                    Move(moveDirection);            //debug override key
                }
                this.Position = new Vector2((int)Position.X, (int)Position.Y);
            }

            if (sprite.State == SpriteState.Die && sprite.CurrentFrame == sprite.MaxFrame && deathCooldown == 120)
            {
                RPG.LoadNewGame();
            }
            else if (sprite.State == SpriteState.Die &&
                     sprite.CurrentFrame == sprite.MaxFrame && deathCooldown < 120)
            {
                deathCooldown++;
            }

            base.Update(gameTime);
            //inventoryMenu.Update (gameTime);
        }
Example #2
0
//		public new void Draw(SpriteBatch spriteBatch)
//		{
//			SpriteFont font = RPG.ContentManager.Load<SpriteFont>("fonts/Arial");
//			Vector2 size = font.MeasureString(this.Position.ToString());
//			Vector2 pos = new Vector2(Bounds.Center.X - size.X/2, Position.Y - size.Y);
//			spriteBatch.DrawString(font, this.Position.ToString(), pos, Color.White);
//			base.Draw(spriteBatch);
//		}

        private void MoveUpdate(GameTime gameTime)
        {
            if (sprite.State != SpriteState.Die)
            {
                //set bounds
                RectangleF tempBounds = Bounds;
                RectangleF tempPlayer = CurrentArea.User.Bounds;

                tempBounds.Inflate(2f, 2f);
                tempBounds.Offset(-1f, -1f);

                Vector2 moveAmount = CurrentArea.User.Position - this.Position;
                moveAmount.Normalize();
                moveAmount = Vector2.Multiply(moveAmount, .8f);

                bool turn = true;

                //if player is in bounds, isn't too close to enemy and there are no collisions
                //move towards the player
                if (!(tempPlayer.Intersects(tempBounds)) &&
                    CurrentArea.CheckForCollisions(this, moveAmount) &&
                    walkLimit.Contains(tempBounds) && walkLimit.Contains(tempPlayer))
                {
                    Move(moveAmount);
                }
                //if player is out of bounds return to start
                else if (!walkLimit.Contains(tempPlayer))
                {
                    moveAmount = InitialPosition - Position;
                    moveAmount.Normalize();
                    tempBounds.Offset(moveAmount);
                    if (walkLimit.Contains(tempBounds) && CurrentArea.CheckForCollisions(this, moveAmount) && (Position - InitialPosition).Length() > 1)
                    {
                        Move(moveAmount);
                    }
                    if ((Position - InitialPosition).Length() <= 1)
                    {
                        turn = false;
                        Turn(startDirection);
                    }
                }
                else if (tempPlayer.Intersects(tempBounds) && sprite.State != SpriteState.Attack)
                {
                    if (currentCD == 0)
                    {
                        Attack();
                        currentCD++;
                    }
                }

                if (turn)
                {
                    Turn(moveAmount);
                }
                if (currentCD != 0 && currentCD != cooldown)
                {
                    currentCD++;
                }
                else if (currentCD == cooldown)
                {
                    currentCD = 0;
                }
            }
        }