You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

GameObj.cs 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System.Threading;
  4. namespace GameClass.GameObj
  5. {
  6. /// <summary>
  7. /// 一切游戏元素的总基类,与THUAI4不同,继承IMoveable接口(出于一切物体其实都是可运动的指导思想)——LHR
  8. /// </summary>
  9. public abstract class GameObj : IMoveable
  10. {
  11. protected readonly object gameObjLock = new();
  12. /// <summary>
  13. /// 可移动物体专用锁
  14. /// </summary>
  15. public object MoveLock => gameObjLock;
  16. protected readonly XY birthPos;
  17. private GameObjType type;
  18. public GameObjType Type => type;
  19. private static long currentMaxID = 0; // 目前游戏对象的最大ID
  20. public const long invalidID = long.MaxValue; // 无效的ID
  21. public const long noneID = long.MinValue;
  22. public long ID { get; }
  23. private XY position;
  24. public XY Position
  25. {
  26. get => position;
  27. protected
  28. set
  29. {
  30. lock (gameObjLock)
  31. {
  32. position = value;
  33. }
  34. }
  35. }
  36. public abstract bool IsRigid { get; }
  37. private XY facingDirection = new(1, 0);
  38. public XY FacingDirection
  39. {
  40. get => facingDirection;
  41. set
  42. {
  43. lock (gameObjLock)
  44. facingDirection = value;
  45. }
  46. }
  47. public abstract ShapeType Shape { get; }
  48. private bool canMove;
  49. public bool CanMove
  50. {
  51. get => canMove;
  52. set
  53. {
  54. lock (gameObjLock)
  55. {
  56. canMove = value;
  57. }
  58. }
  59. }
  60. private bool isMoving;
  61. public bool IsMoving
  62. {
  63. get => isMoving;
  64. set
  65. {
  66. lock (gameObjLock)
  67. {
  68. isMoving = value;
  69. }
  70. }
  71. }
  72. private bool isResetting;
  73. public bool IsResetting
  74. {
  75. get => isResetting;
  76. set
  77. {
  78. lock (gameObjLock)
  79. {
  80. isResetting = value;
  81. }
  82. }
  83. }
  84. public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令
  85. public int Radius { get; }
  86. private PlaceType place;
  87. public PlaceType Place
  88. {
  89. get => place;
  90. set
  91. {
  92. lock (gameObjLock)
  93. {
  94. place = value;
  95. }
  96. }
  97. }
  98. protected int moveSpeed;
  99. /// <summary>
  100. /// 移动速度
  101. /// </summary>
  102. public int MoveSpeed
  103. {
  104. get => moveSpeed;
  105. set
  106. {
  107. lock (gameObjLock)
  108. {
  109. moveSpeed = value;
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 原初移动速度
  115. /// </summary>
  116. private int orgMoveSpeed;
  117. public int OrgMoveSpeed
  118. {
  119. get => orgMoveSpeed;
  120. protected
  121. set
  122. {
  123. orgMoveSpeed = value;
  124. }
  125. }
  126. // 移动,改变坐标
  127. public long Move(XY moveVec)
  128. {
  129. lock (gameObjLock)
  130. {
  131. FacingDirection = moveVec;
  132. this.Position += moveVec;
  133. }
  134. return (long)(moveVec * moveVec);
  135. }
  136. /// <summary>
  137. /// 设置位置
  138. /// </summary>
  139. /// <param name="newpos">新位置</param>
  140. public void SetPosition(XY newpos)
  141. {
  142. Position = newpos;
  143. }
  144. /// <summary>
  145. /// 设置移动速度
  146. /// </summary>
  147. /// <param name="newMoveSpeed">新速度</param>
  148. public void SetMoveSpeed(int newMoveSpeed)
  149. {
  150. MoveSpeed = newMoveSpeed;
  151. }
  152. /// <summary>
  153. /// 复活时数据重置
  154. /// </summary>
  155. public virtual void Reset()
  156. {
  157. lock (gameObjLock)
  158. {
  159. facingDirection = new XY(1, 0);
  160. isMoving = false;
  161. canMove = false;
  162. isResetting = true;
  163. this.position = birthPos;
  164. }
  165. }
  166. /// <summary>
  167. /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable
  168. /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现
  169. /// </summary>
  170. /// <returns> 依具体类及该方法参数而定,默认为false </returns>
  171. protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  172. bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj);
  173. public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType)
  174. {
  175. this.Position = this.birthPos = initPos;
  176. this.Radius = initRadius;
  177. this.place = initPlace;
  178. this.type = initType;
  179. ID = Interlocked.Increment(ref currentMaxID);
  180. }
  181. }
  182. }