Home > .Net Framework, XNA > Kelas Sprite di XNA

Kelas Sprite di XNA


XNA adalah sebuah framework untuk menghasilkan sebuah Game berbasiskan .Net Framework, pada sebuah game tentunya kita butuh menampilkan sebuah gambar karakter yang biasa disebut Sprite, berikut ini sebuah kelas bantu untuk menampilkan sprite

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Strategy.Helper;
using Strategy.Exceptions;
namespace Strategy.Sprites
{
    public class Sprite
    {
        protected Texture2D texture;
        protected Texture2D selectionMark;
        protected SpriteVector coordinat;
        protected bool selected;
        protected bool selectable;
        protected Rectangle bound;
        protected MouseState mouseState;
        protected string assetName;
        protected bool onScreen;
        protected EnumDirection spriteDirection;
        protected bool active = true;
        protected ContentManager content;
        protected static Dictionary<EnumDirection, int> directions = new Dictionary<EnumDirection, int>()
        {
            {EnumDirection.EAST,(int)EnumDirection.EAST},
            {EnumDirection.NORTH,(int) EnumDirection.NORTH},
            {EnumDirection.NORTH_EAST,(int) EnumDirection.NORTH_EAST},
            {EnumDirection.NORTH_WEST,(int) EnumDirection.NORTH_WEST},
            {EnumDirection.SOUTH,(int) EnumDirection.SOUTH},
            {EnumDirection.SOUTH_EAST,(int) EnumDirection.SOUTH_EAST},
            {EnumDirection.SOUTH_WEST,(int) EnumDirection.SOUTH_WEST},
            {EnumDirection.WEST,(int) EnumDirection.WEST},
            //{EnumDirection.NOTHING,(int) EnumDirection.NOTHING}
        };
              

        public static void CostumDirectionsConfiguration(Dictionary<EnumDirection, int> customDirections)
        {
            directions = customDirections;
        }

        public Sprite(string assetName,ContentManager content)
        {

            
                this.assetName = assetName;
                texture = content.Load<Texture2D>(assetName);
                coordinat = new SpriteVector(0,0);
                bound = new Rectangle(
                    (int)coordinat.DrawLocation.X, (int)coordinat.DrawLocation.Y,
                    texture.Width, texture.Height);


                //selectionMark = content.Load<Texture2D>("selectedMark");
            
        }

        public bool Active
        {
            get { return active; }
            set { active = value; }
        }

        public bool OnScreen
        {
            get { return onScreen; }
            
        }
        public Texture2D Texture
        {
            get { return texture; }
            set { texture = value; }
        }
        public string AssetName
        {
            get { return assetName; }
            set { assetName = value; }
        }

        public virtual void draw(SpriteBatch spriteBatch,GameWindow gameWindow,
            Vector2 viewportPosition)
        {
            Vector2 pos = Vector2.Zero;
            pos.X = coordinat.DrawLocation.X - viewportPosition.X;
            pos.Y = coordinat.DrawLocation.Y - viewportPosition.Y;// -SpriteVector.IncreaseY;
            //onScreen= checkOnScreen(gameWindow,coordinat.DrawLocation);
            onScreen = checkOnScreen(gameWindow, pos);
            if (onScreen)
            {
                //spriteBatch.Draw(texture, coordinat.DrawLocation, Color.White);
                spriteBatch.Draw(texture, pos, Color.White);
            }
        }

        public static bool  checkOnScreen(GameWindow gameWindow,Vector2 location)
        {
            GameWindow window = gameWindow;
            bool tempOnScreen = (window.ClientBounds.Contains(
               (int)location.X + window.ClientBounds.Left,
               (int)location.Y + window.ClientBounds.Top));
            return tempOnScreen;
        }

        public SpriteVector SpriteCoordinat
        {
            get { return coordinat; }
            set { coordinat = value; }
        }
        public bool Selected
        {
            get { return selected; }
            set { selected = value; }
        }

        public bool Selectable
        {
            get { return selectable; }
            set { selectable = value; }
        }

        public Rectangle Bound
        {
            get { return bound; }
            set { bound = value; }
        }
        public EnumDirection SpriteDirection
        {
            get { return spriteDirection; }
            set { spriteDirection = value; }
        }
        public virtual void update(GameTime gameTime)
        {
            bound.X = (int)coordinat.DrawLocation.X;
            bound.Y = (int)coordinat.DrawLocation.Y;
            if (selectable)
            {
                mouseState = Mouse.GetState();
                selected = bound.Contains(mouseState.X, mouseState.Y);
            }
        }

        public virtual void update(GameTime gameTime, Vector2 viewportPosition)
        {
            this.update(gameTime);
            Vector2 temp = Vector2.Zero;
            temp.X = coordinat.DrawLocation.X - viewportPosition.X;
            temp.Y = coordinat.DrawLocation.Y - viewportPosition.Y - SpriteVector.IncreaseY;
            coordinat.DrawLocation = temp;
        }

        public virtual void processSpriteDirection(Vector2 destination)
        {
            spriteDirection = Direction.findDirection(
                coordinat.DrawLocation, destination);

        }

        public virtual bool moveTo(GameTime gameTime,Vector2 destination,float speed)
        {
            processSpriteDirection(destination);
            if (coordinat.DrawLocation.Equals(destination))
            {
                return true;
            }
            
            float angle = 90 + MathHelper.ToDegrees(
                (float)(Math.Atan2(destination.Y - coordinat.DrawLocation.Y,
                    destination.X - coordinat.DrawLocation.X)));
            double radians = (double)MathHelper.ToRadians(angle);
            //Console.WriteLine(gameTime.ElapsedGameTime.Seconds);
            float vx = (float)Math.Sin(radians) * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            float vy = (float)-Math.Cos(radians) * speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            //Console.WriteLine(angle.ToString() + ";" + radians.ToString() + ";" +
                //vx.ToString() + ";" + vy.ToString());
            bool arriveX = false;
            bool arriveY = false;
            // checking x coordinate
            if (vx != 0)
            {
                if (vx > 0)
                {
                    // moving right
                    if (coordinat.DrawLocation.X + vx >= destination.X)
                    {
                        vx = destination.X - coordinat.DrawLocation.X; // snap
                        arriveX = true;
                    }

                }
                else
                {
                    // moving left
                    if (coordinat.DrawLocation.X + vx <= destination.X)
                    {
                        vx = destination.X - coordinat.DrawLocation.X; // snap
                        arriveX = true;
                    }
                }

            }
            else if (coordinat.DrawLocation.X == destination.X)
            {
                arriveX = true;
            }

            // checking y coordinate
            if (vy != 0)
            {
                if (vy > 0)
                {
                    // moving down
                    if (coordinat.DrawLocation.Y + vy >= destination.Y)
                    {
                        vy = destination.Y - coordinat.DrawLocation.Y; // snap
                        arriveY = true;
                    }

                }
                else
                {
                    // moving up
                    if (coordinat.DrawLocation.Y + vy <= destination.Y)
                    {
                        vy = destination.Y - coordinat.DrawLocation.Y; // snap
                        arriveY = true;
                    }
                }

            }
            else if (coordinat.DrawLocation.Y == destination.Y)
            {
                arriveY = true;
            }

            coordinat.DrawLocation = new Vector2(
                coordinat.DrawLocation.X + vx,
                coordinat.DrawLocation.Y + vy);

            return (arriveX && arriveY);
        }
    }
}

Categories: .Net Framework, XNA Tags: , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: