📄 charactersprite.cs
字号:
using System;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Samples.DirectX.UtilityToolkit;
using System.Drawing;
namespace TankGogogo.GameRenderer
{
/// <summary>
/// 一个角色精灵,负责角色渲染(旋转,移动).
/// </summary>
public class CharacterSprite
{
public const int DOWN=0;
public const int UP=4;
public const int LEFT=2;
public const int RIGHT=6;
private int stepspassed=0;
private bool changeenable=false;//0则转,1则移
private Vector2 position;
private float screenWidth,screenHeight;
private Rectangle cellRect;
private float speed;
private float timeSinceTurn;
private float aliveTime;
/// <summary>
/// 物理位置
/// </summary>
int currdirection;
int targetdirection;
Texture tex;
int currentFrame;
int numFrames;
int width,height;
int startCell;
int numRows,numColumns;
/// <summary>
/// 创建一个角色精灵,负责角色渲染(旋转,移动)
/// </summary>
/// <param name="t">包含所有帧的纹理</param>
/// <param name="cellWidth">每个帧原子的宽度</param>
/// <param name="cellHeight">每个帧原子的高度</param>
/// <param name="rows">描述了完整纹理包含几行帧原子</param>
/// <param name="column">完整纹理包含几列帧原子</param>
/// <param name="numberOfFrames">帧原子总数(〈=rows*column)</param>
/// <param name="startingCell">旋转开始原子</param>
public CharacterSprite(Texture t,int cellWidth,int cellHeight,int rows,int columns,int numberOfFrames,int startingCell,
float movementspeed,int startdirection,Vector2 startposition,Rectangle celRec)
{
cellRect=celRec;
targetdirection=currdirection=startdirection;
position = startposition;
currentFrame = 0;
numFrames = numberOfFrames;
tex = t;
width = cellWidth;
height = cellHeight;
startCell = startingCell;
numRows = rows;
numColumns = columns;
speed = movementspeed;
}
/// <summary>
/// 获取及设置当前帧
/// </summary>
public int Frame
{
get{return currentFrame;}
set
{
if(value>numFrames) throw new IndexOutOfRangeException("Specified frame is out of rangs of frames specified!");
currentFrame = value;
}
}
public void SetScreenSize(int width, int height)
{
screenWidth = width;
screenHeight = height;
}
public void Move(int direction)
{
switch(direction)
{
case DOWN:
position.Add(new Vector2(0,(float)cellRect.Height/10));
stepspassed++;
break;
case UP:
position.Add(new Vector2(0,-(float)cellRect.Height/10));
stepspassed++;
break;
case LEFT:
position.Add(new Vector2(-(float)cellRect.Width/10,0));
stepspassed++;
break;
case RIGHT:
position.Add(new Vector2((float)cellRect.Height/10,0));
stepspassed++;
break;
}
if(stepspassed>=10)
{
changeenable=false;
stepspassed=0;
}
}
public void moveto(int dir)
{
targetdirection=dir;
changeenable=true;
}
public void MovebyFrame()
{
if(targetdirection>currdirection)
{
if((targetdirection-currdirection)<(numFrames/2))
AdvancedFrame();
else DraggledFrame();
}
else
{
if(targetdirection<currdirection)
{
if((currdirection-targetdirection)<(numFrames/2))
DraggledFrame();
else AdvancedFrame();
}
else
{
if(changeenable)
{
Move(targetdirection);
//changeenable=false;
}
}
}
}
/// <summary>
/// 当前帧向前进一步,出界则归0,即顺时针转
/// </summary>
public void AdvancedFrame()
{
currentFrame++;
currdirection++;
if(currdirection>=numFrames)
currdirection=0;
if(currentFrame>=numFrames)
currentFrame = 0;
}
/// <summary>
/// 当前帧后退一步,出界则归最大,即逆时针转
/// </summary>
public void DraggledFrame()
{
currentFrame--;
currdirection--;
if(currdirection<0)
currdirection=numFrames-1;
if(currentFrame<0)
currentFrame=numFrames-1;
}
/// <summary>
/// 绘制当前帧
/// </summary>
/// <param name="sprite"></param>
/// <param name="upperLeftPosition"></param>
/// <param name="color"></param>
public void Draw(Sprite sprite,Color color)
{
int currentRow=(currentFrame+startCell)/numColumns;
int currentColumn=(currentFrame+startCell) % numColumns;
sprite.Transform=Matrix.Identity;
Rectangle srcRect=new Rectangle(currentColumn*width,currentRow*height,width,height);
sprite.Draw(tex,srcRect,Vector3.Empty,new Vector3(position.X,position.Y,0),color);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -