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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. lock (gameObjLock)
  30. {
  31. position = value;
  32. }
  33. }
  34. }
  35. public abstract bool IsRigid { get; }
  36. private XY facingDirection = new(1, 0);
  37. public XY FacingDirection
  38. {
  39. get => facingDirection;
  40. set {
  41. lock (gameObjLock)
  42. facingDirection = value;
  43. }
  44. }
  45. public abstract ShapeType Shape { get; }
  46. private bool canMove;
  47. public bool CanMove
  48. {
  49. get => canMove;
  50. set {
  51. lock (gameObjLock)
  52. {
  53. canMove = value;
  54. }
  55. }
  56. }
  57. private bool isMoving;
  58. public bool IsMoving
  59. {
  60. get => isMoving;
  61. set {
  62. lock (gameObjLock)
  63. {
  64. isMoving = value;
  65. }
  66. }
  67. }
  68. private bool isResetting;
  69. public bool IsResetting
  70. {
  71. get => isResetting;
  72. set {
  73. lock (gameObjLock)
  74. {
  75. isResetting = value;
  76. }
  77. }
  78. }
  79. public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令
  80. public int Radius { get; }
  81. private PlaceType place;
  82. public PlaceType Place
  83. {
  84. get => place;
  85. set {
  86. lock (gameObjLock)
  87. {
  88. place = value;
  89. }
  90. }
  91. }
  92. protected int moveSpeed;
  93. /// <summary>
  94. /// 移动速度
  95. /// </summary>
  96. public int MoveSpeed
  97. {
  98. get => moveSpeed;
  99. set {
  100. lock (gameObjLock)
  101. {
  102. moveSpeed = value;
  103. }
  104. }
  105. }
  106. /// <summary>
  107. /// 原初移动速度
  108. /// </summary>
  109. private int orgMoveSpeed;
  110. public int OrgMoveSpeed
  111. {
  112. get => orgMoveSpeed;
  113. protected
  114. set {
  115. orgMoveSpeed = value;
  116. }
  117. }
  118. // 移动,改变坐标
  119. public long Move(XY moveVec)
  120. {
  121. lock (gameObjLock)
  122. {
  123. FacingDirection = moveVec;
  124. this.Position += moveVec;
  125. }
  126. return (long)(moveVec * moveVec);
  127. }
  128. /// <summary>
  129. /// 设置位置
  130. /// </summary>
  131. /// <param name="newpos">新位置</param>
  132. public void SetPosition(XY newpos)
  133. {
  134. Position = newpos;
  135. }
  136. /// <summary>
  137. /// 设置移动速度
  138. /// </summary>
  139. /// <param name="newMoveSpeed">新速度</param>
  140. public void SetMoveSpeed(int newMoveSpeed)
  141. {
  142. MoveSpeed = newMoveSpeed;
  143. }
  144. /// <summary>
  145. /// 复活时数据重置
  146. /// </summary>
  147. public virtual void Reset()
  148. {
  149. lock (gameObjLock)
  150. {
  151. facingDirection = new XY(1, 0);
  152. isMoving = false;
  153. canMove = false;
  154. isResetting = true;
  155. this.position = birthPos;
  156. }
  157. }
  158. /// <summary>
  159. /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable
  160. /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现
  161. /// </summary>
  162. /// <returns> 依具体类及该方法参数而定,默认为false </returns>
  163. protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  164. bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj);
  165. public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType)
  166. {
  167. this.Position = this.birthPos = initPos;
  168. this.Radius = initRadius;
  169. this.place = initPlace;
  170. this.type = initType;
  171. ID = Interlocked.Increment(ref currentMaxID);
  172. }
  173. }
  174. }