📄 gameobject_sprite.cs
字号:
using System;
using System.Reflection;
using Microsoft.DirectX.DirectInput;
using DDGameHelper;
namespace ddt2
{
/// <summary>
/// GameObject_Player_Plane 的摘要说明。
/// </summary>
public class GameObject_Sprite : GameObject
{
/// <summary>
/// 同类型的GameObj计数器(主要作用于资源管理。当 counter > 0 时,即便调用 dispose 也不释放 resource)
/// </summary>
private static int _counter = 0;
/// <summary>
/// 指向 BmpRectCombo Collection 的 索引
/// </summary>
private static int _indexOfBmpRectComboCollection = 0;
private double _maxX = 0.0, _maxY = 0.0; //临时用用,越界判断
private double _moveX, _moveY , _moveTime; //控制随机移动相关
private bool _isForwardPlay; //是否向前 play , 决定 _currF 是 ++ 或 --
private AnimatePlayMethods _playmethods; //播放方式 ,决定 _currF<0 或 >_lastF 时的数值
private bool _god = true; //刚生成时的无敌状态
private double _godTime = 1000; //无敌状态维持时间
public override int IndexOfBmpRectComboCollection()
{
return _indexOfBmpRectComboCollection;
}
//private static string _name = "sprite";
public override string Name()
{
return _type.ToString();
}
public override object Properties()
{
return _god;
}
private static GameObject_Enum _type = GameObject_Enum.Sprite;
public override int Type()
{
return (int)_type;
}
public GameObject_Sprite(double x, double y, double velX, double velY, double velF, AnimatePlayMethods playmethods,bool isForwardPlay, ref GameLoopHandler gameLoopHandler) : base(ref gameLoopHandler)
{
BmpRectCombo brc=((BmpRectCombo)_bmpRectComboCollection[_indexOfBmpRectComboCollection]);
_velF = velF;
_x=x; _y=y;
_velX=velX; _velY=velY;
_maxX = GameSettings._screenWidth - brc.RectangleArray.Rectangles[0].Width;
_maxY = GameSettings._screenHeight - brc.RectangleArray.Rectangles[0].Height;
_lastF = brc.RectangleArray.Rectangles.Length - 1;
_currF = 0;
_currT = 0.0;
_isForwardPlay = isForwardPlay;
_playmethods=playmethods;
}
protected override void Init()
{
LoadResource();
}
protected override void LoadResource()
{
_counter++;
if(_counter==1)
{
BitmapObject bo = new BitmapObject(BitmapDisplayType.Transparent, GameSettings.GetResourceStream("ddt2.Resource.Picture.animate.png"));
RectangleArray ra = new RectangleArray(bo.Size.Width, bo.Size.Height, 32, 32, 0);
DDHandler d=_glh.DDHandler;
_indexOfBmpRectComboCollection = _bmpRectComboCollection.Add(new BmpRectCombo(ref bo, ref ra, ref d));
}
}
protected override void UnloadResource()
{
_counter--;
if(_counter==0)
{
_bmpRectComboCollection[_indexOfBmpRectComboCollection] = null;
}
}
public override void Process()
{
//随机移动
if(_moveTime <= 0)
{
_moveX=_glh.RandoM.Next(3)-1;
_moveY=_glh.RandoM.Next(3)-1;
_moveTime=_glh.RandoM.NextDouble()*1000+100;
}
else
{
_moveTime-=_glh.LoopDuration;
_x+=_moveX * (_glh.LoopDuration/_velX);
_y+=_moveY * (_glh.LoopDuration/_velY);
if(_x>_maxX || _x<0)_moveX=-_moveX;
if(_y>_maxY || _y<0)_moveY=-_moveY;
}
//frame播放
_currT+=_glh.LoopDuration;
if(_currT > _velF)
{
_currT-=_velF;
_currF+=(_isForwardPlay?1:-1);
if(_playmethods == AnimatePlayMethods.Repeat)
{
if(_currF<0)_currF=_lastF;
if(_currF>_lastF)_currF=0;
}
if(_playmethods == AnimatePlayMethods.Reverse)
{
if(_currF<0)
{
_currF=0;
_isForwardPlay=!_isForwardPlay;
}
if(_currF>_lastF)
{
_currF=_lastF;
_isForwardPlay=!_isForwardPlay;
}
}
if(_playmethods == AnimatePlayMethods.Stop)
{
if(_currF<0)_currF=0;
if(_currF>_lastF)_currF=_lastF;
}
}
//移动越界处理
if(_x>_maxX)_x=_maxX;
if(_x<0)_x=0;
if(_y>_maxY)_y=_maxY;
if(_y<0)_y=0;
if(_godTime>0)_godTime -= _glh.LoopDuration;
if(_godTime<0) _god = false;
_lastX = _x; _lastY = _y;
}
public override void Process(ref Microsoft.DirectX.DirectInput.KeyboardState ks)
{
}
public override void Draw()
{
if((_god && _glh.Counter%3==0) || !_god)
{
DDHelper.DrawBitmap(((BmpRectCombo)_bmpRectComboCollection[IndexOfBmpRectComboCollection()]),(int)_x,(int)_y,_currF);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -