Skip to content

Instantly share code, notes, and snippets.

@kb10uy
Created March 20, 2013 07:51
Show Gist options
  • Save kb10uy/5203013 to your computer and use it in GitHub Desktop.
Save kb10uy/5203013 to your computer and use it in GitHub Desktop.
オワコンになりつつあるXNAですが、動きまわるスプライトのようなものと周辺機器を実装したので上げてみる。
/// <summary>
/// 動きまわるものを定義します。
/// </summary>
public class XSprite
{
/// <summary>
/// X位置
/// </summary>
public double X { get; set; }
/// <summary>
/// Y位置
/// </summary>
public double Y { get; set; }
/// <summary>
/// X速度
/// </summary>
public double XSpeed { get; set; }
/// <summary>
/// Y速度
/// </summary>
public double YSpeed { get; set; }
/// <summary>
/// 描画対象のXGraphic
/// </summary>
public XGraphic Base { get; set; }
/// <summary>
/// 描画基点X
/// </summary>
public double HomeX { get; set; }
/// <summary>
/// 描画基点Y
/// </summary>
public double HomeY { get; set; }
/// <summary>
/// 描画占領
/// </summary>
public bool DrawHook { get; set; }
/// <summary>
/// 更新占領
/// </summary>
public bool UpdateHook { get; set; }
/// <summary>
/// 経過フレーム数
/// </summary>
public int Frame { get; set; }
/// <summary>
/// 角度
/// </summary>
public double Angle { get; set; }
/// <summary>
/// スケール
/// </summary>
public double Scale { get; set; }
/// <summary>
/// 透明度(未対応not implemented)
/// </summary>
public double Alpha { get; set; }
/// <summary>
/// 移動残りフレーム
/// </summary>
public double MoveRest { get; set; }
/// <summary>
/// 無限移動フラグ
/// </summary>
public bool MoveInfinity { get; set; }
/// <summary>
/// 更新時イベント
/// </summary>
public event Action<XSprite, int> OnUpdate;
/// <summary>
/// 描画時イベント
/// </summary>
public event Action<XSprite, SpriteBatch> OnDraw;
public XSprite()
{
Base = new XGraphic();
Scale = 1.0;
}
public XSprite(XGraphic g, double x, double y)
{
Base = g;
X = x;
Y = y;
Scale = 1.0;
}
public XSprite(XGraphic g, double x, double y, double hx, double hy)
{
Base = g;
X = x;
Y = y;
HomeX = hx;
HomeY = hy;
Scale = 1.0;
}
/// <summary>
/// 更新
/// </summary>
public void Update()
{
Frame++;
if (!UpdateHook)
{
if (MoveInfinity || MoveRest>=1)
{
X += XSpeed;
Y += YSpeed;
MoveRest--;
}
if (MoveRest <= 0 && !MoveInfinity)
{
XSpeed = 0;
YSpeed = 0;
}
}
if (OnUpdate != null) OnUpdate(this, 1);
}
/// <summary>
/// 描画
/// </summary>
/// <param name="sb">SpriteBatch</param>
public void Draw(SpriteBatch sb)
{
if (!DrawHook)
{
sb.Draw(
Base.Texture,
new Vector2((float)(X + HomeX), (float)(Y + HomeY)),
Base.Rect,
Color.White,
(float)Angle,
new Vector2((float)HomeX, (float)HomeY),
(float)Scale,
SpriteEffects.None,
0);
}
if (OnDraw != null) OnDraw(this, sb);
}
/// <summary>
/// 相対移動
/// </summary>
/// <param name="x">X量</param>
/// <param name="y">Y量</param>
/// <param name="fr">時間</param>
public void MoveRelative(double x,double y,int fr) {
if (fr<=0) {
X=x;
Y=y;
return;
}
XSpeed = x / fr;
YSpeed = y / fr;
MoveRest = fr;
}
/// <summary>
/// 絶対移動
/// </summary>
/// <param name="x">X位置</param>
/// <param name="y">Y位置</param>
/// <param name="fr">時間</param>
public void MoveAbsolute(double x, double y, int fr)
{
if (fr <= 0)
{
X = x;
Y = y;
return;
}
XSpeed = (x-X) / fr;
YSpeed = (y-Y) / fr;
MoveRest = fr;
}
}
public struct XGraphic
{
public Texture2D Texture;
public Rectangle Rect;
public static XGraphic FromTexture2D(Texture2D tex)
{
return new XGraphic { Texture = tex, Rect = new Rectangle(0, 0, tex.Width, tex.Height) };
}
public static XGraphic FromXGraphic(XGraphic x, Rectangle rec)
{
return new XGraphic { Texture = x.Texture, Rect = rec };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment