using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
namespace ShootingGame
{
///
/// This is a game component that implements IUpdateable.
///
public class Sprite : Microsoft.Xna.Framework.DrawableGameComponent
{
private Texture2D t2d; //--------------- ภาพ Texture2D ที่จะมาทำ Sprite
private ContentManager content;
private SpriteRange spriteRange; //----------- Frame ที่ใช้ Draw ตั้งแต่ Frameเริ่มต้น จน สิ้นสุด ตามที่กำหนด
private int framesPerSecond;//--------FPS ที่กำหนดให้ Sprite
private float timePerFrame; //-------------- เวลาที่จะใช้ ต่อ Frame
public float TotalElapsedTime = 0.0f; //--------- เวลาที่ใช้ไป
public int spriteWidth; //-----------ความกว้างของแต่ละ Sprite
public int spriteHeight; //------ความยาวของแต่ละ Sprite
public int NumberOfFrames=1; //---------จำนวน Frames ที่ใช้ในการ draw
public int spritesPerRow; //------------- จำนวน Frame ที่มี 1 แถว
public int Frame; //Frame ปัจจุบัน
public Sprite(Game game)
: base(game)
{
content = new ContentManager(game.Services);
//------------------ โหลด content Mangage
}
///
/// Allows the game component to perform any initialization it needs to before starting
/// to run. This is where it can query for any required services and load content.
///
public override void Initialize()
{
// TODO: Add your initialization code here
base.Initialize();
}
public void Load(string textureName, SpriteRange spriteRange, int spriteWidth, int spriteHeight, int NumberOfFrames, int framesPerSecond)
{
t2d = content.Load(@"Content\Textures\" + textureName);
//-------------------- เป็นการโหลด Texture2D มาใช้ในเกม
//--------------กำหนดค่าเบื้องต้นของ Sprite
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
this.NumberOfFrames = NumberOfFrames;
this.spritesPerRow = t2d.Width / this.spriteWidth;
this.spriteRange = spriteRange;
this.framesPerSecond = framesPerSecond;
this.timePerFrame = 1.0f / (float)framesPerSecond;
this.Frame = 0;
//--------------กำหนดค่าเบื้องต้นของ Sprite
}
///
/// Update Sprite
///
/// Provides a snapshot of timing values.
public override void Update(GameTime gameTime)
{ // ใช้เป็นตัวคุม ให้ Sprite Drawn ตาม Frame Rate ที่เรากำหนดไว้
this.TotalElapsedTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (this.TotalElapsedTime > this.timePerFrame)
{
this.Frame++;
this.Frame = this.Frame % (this.NumberOfFrames);
this.TotalElapsedTime -= this.timePerFrame;
}
base.Update(gameTime);
}
public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 pos)
{
Draw(gameTime, spriteBatch, pos, Color.White, 0.0f, new Vector2(0, 0),new Vector2(1,1), SpriteEffects.None, 0.0f);
}
public void Draw(GameTime gameTime,SpriteBatch spriteBatch, Vector2 pos,Color c,float rotation,Vector2 origin,Vector2 scale,SpriteEffects spriteEffects,float layerDepth)
{ //--------------การ Draw Sprite
int xincrease = this.Frame + this.spriteRange.StartAniX - 1;
int xwrapped = xincrease % this.spritesPerRow;
int x = xwrapped * this.spriteWidth;
int yincrease = xincrease / this.spritesPerRow;
int y = (yincrease + this.spriteRange.StartAniY - 1) * this.spriteHeight;
//--------ส่วนการคำนวณ Frame ที่จะใช้วาด
Rectangle rect = new Rectangle(x, y, this.spriteWidth, this.spriteHeight);
spriteBatch.Draw(t2d, pos, rect, c, rotation, origin,scale,spriteEffects, layerDepth);
//------------------------ สั่งวาด Sprite
base.Draw(gameTime);
}
public struct SpriteFrameCount
{ //---------------- เป็นการะบุจำนวน Frame ทั้งหมด
public int NumCols;
public int NumRows;
public SpriteFrameCount(int numCols, int numRows)
{
NumCols = numCols;
NumRows = numRows;
}
public int getCount()
{
return NumCols* NumRows;
}
}
}
public struct SpriteRange
{ //----------------- เป็นการระบุFrame เริ่ม - Frame สุดท้าย
public int StartAniX;
public int StartAniY;
public int EndAniX;
public int EndAniY;
public SpriteRange(int startAniX, int startAniY, int endAniX, int endAniY)
{
StartAniX = startAniX;
StartAniY = startAniY;
EndAniX = endAniX;
EndAniY = endAniY;
}
}
}