GeorgeGularte
[email protected]
  • Home
  • Blog
  • Projects
  • Resume

Blue Wings 

usercontrolledsprite.cs
File Size: 7 kb
File Type: cs
Download File

User Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace BlueWings
{
    class UserControlledSprite : Sprite
    {
        protected float fireRate;
        enum state { SE, S, SW, W, NW, N, NE, E };
        state e_lookingDirection;
        float dt = 0f;
        bool isItTimeToIncreaseSpeed = false;
        public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset,
           Point currentFrame, Point SheetSize, Vector2 speed, float fireRate)
            : base(textureImage, position, frameSize, collisionOffset, currentFrame, SheetSize, speed, 0)
        {
            this.fireRate = fireRate;
            spriteAnimation.GetScale = 1f;
        }
        public float GetFireRate
        {
            get { return fireRate; }
            set { fireRate = value; }
        }
        public Vector2 GetDirection()
        {
            Vector2 inputDirection = Vector2.Zero;
            if (Keyboard.GetState().IsKeyDown(Keys.A))
                inputDirection.X -= 1;
            if (Keyboard.GetState().IsKeyDown(Keys.D))
                inputDirection.X += 1;
            if (Keyboard.GetState().IsKeyDown(Keys.W))
                inputDirection.Y -= 1;
            if (Keyboard.GetState().IsKeyDown(Keys.S))
                inputDirection.Y += 1;

            GamePadState gamepadSate = GamePad.GetState(PlayerIndex.One);
            if (gamepadSate.ThumbSticks.Left.X != 0)
                inputDirection.X += gamepadSate.ThumbSticks.Left.X;
            if (gamepadSate.ThumbSticks.Left.Y != 0)
                inputDirection.Y -= gamepadSate.ThumbSticks.Left.Y;

            return inputDirection * speed;
        }

        public Vector2 LookingDirection
        {
            get
            {
                Vector2 inputDirection = Vector2.Zero;
                if (Keyboard.GetState().IsKeyDown(Keys.Left))
                    inputDirection.X -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Right))
                    inputDirection.X += 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Up))
                    inputDirection.Y -= 1;
                if (Keyboard.GetState().IsKeyDown(Keys.Down))
                    inputDirection.Y += 1;

                GamePadState gamepadSate = GamePad.GetState(PlayerIndex.One);
                if (gamepadSate.ThumbSticks.Right.X != 0)
                    inputDirection.X += gamepadSate.ThumbSticks.Right.X;
                if (gamepadSate.ThumbSticks.Right.Y != 0)
                    inputDirection.Y -= gamepadSate.ThumbSticks.Right.Y;

                float angle;
                angle = (float)Math.Atan2(inputDirection.Y, inputDirection.X);
                angle = angle - 22.5f / 45;

                if (inputDirection.LengthSquared() > 0.25f * 0.25f)
                {
                    angle = (float)Math.Atan2(inputDirection.Y, inputDirection.X);

                    angle = MathHelper.ToDegrees(angle);
                    angle = angle - 22.5f / 45;

                    if (angle < -135 - 22.5)
                        e_lookingDirection = state.W;
                    else if (angle < -90 - 22.5)
                        e_lookingDirection = state.NW;
                    else if (angle < -45 - 22.5)
                        e_lookingDirection = state.N;
                    else if (angle < -22.5)
                        e_lookingDirection = state.NE;
                    else if (angle < 22.5)
                        e_lookingDirection = state.E;
                    else if (angle < 45 + 22.5)
                        e_lookingDirection = state.SE;
                    else if (angle < 90 + 22.5)
                        e_lookingDirection = state.S;
                    else if (angle < 135 + 22.5)
                        e_lookingDirection = state.SW;
                    else
                        e_lookingDirection = state.W;
                }
                return inputDirection;
            }
        }
        public void Update(GameTime gameTime, Rectangle clientBounds, Texture2D background)
        {
            direction = GetDirection();
            position += direction;

            dt += (float)gameTime.ElapsedGameTime.TotalMinutes;

            float midPoint = 1.20F;
            if (dt >= midPoint)
            {
                fireRate = 50f;
            }

            while (!isItTimeToIncreaseSpeed)
            {
                if (dt > 2f)
                {
                    isItTimeToIncreaseSpeed = true;
                    if (isItTimeToIncreaseSpeed == true)
                    {
                        speed.X += 3;
                        speed.Y += 3;
                    }
                }
                break;
            }
            
            //If User, Window Clamp
            if (position.X < 0)
                position.X = 0;
            if (position.Y < 0)
                position.Y = 0;
            if (position.X > background.Width - spriteAnimation.FrameSize.X)
                position.X = background.Width - spriteAnimation.FrameSize.X;
            if (position.Y > background.Height - spriteAnimation.FrameSize.Y)
                position.Y = background.Height - spriteAnimation.FrameSize.Y;

            switch (e_lookingDirection)
            {
                case state.N:
                    North();
                    break;
                case state.NE:
                    NorthEast();
                    break;
                case state.E:
                    East();
                    break;
                case state.SE:
                    SouthEast();
                    break;
                case state.S:
                    South();
                    break;
                case state.SW:
                    SouthWest();
                    break;
                case state.W:
                    West();
                    break;
                case state.NW:
                    NorthWest();
                    break;
            }
            base.Update(gameTime, clientBounds);
        }
        void North()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 6);
        }
        void NorthEast()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 7);
        }
        void East()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 0);
        }
        void SouthEast()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 1);
        }
        void South()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 2);
        }
        void SouthWest()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 3);
        }
        void West()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 4);
        }
        void NorthWest()
        {
            spriteAnimation.GetOriginalFrame = new Point(0, 5);
        }
    }
}
Powered by Create your own unique website with customizable templates.