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.1 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. {
  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. public int OrgMoveSpeed { get; protected set; }
  117. // 移动,改变坐标
  118. public long Move(XY moveVec)
  119. {
  120. lock (gameObjLock)
  121. {
  122. FacingDirection = moveVec;
  123. this.Position += moveVec;
  124. }
  125. return (long)(moveVec * moveVec);
  126. }
  127. /// <summary>
  128. /// 设置位置
  129. /// </summary>
  130. /// <param name="newpos">新位置</param>
  131. public void SetPosition(XY newpos)
  132. {
  133. Position = newpos;
  134. }
  135. /// <summary>
  136. /// 设置移动速度
  137. /// </summary>
  138. /// <param name="newMoveSpeed">新速度</param>
  139. public void SetMoveSpeed(int newMoveSpeed)
  140. {
  141. MoveSpeed = newMoveSpeed;
  142. }
  143. /// <summary>
  144. /// 复活时数据重置
  145. /// </summary>
  146. public virtual void Reset()
  147. {
  148. lock (gameObjLock)
  149. {
  150. facingDirection = new XY(1, 0);
  151. isMoving = false;
  152. canMove = false;
  153. isResetting = true;
  154. this.position = birthPos;
  155. }
  156. }
  157. /// <summary>
  158. /// 为了使IgnoreCollide多态化并使GameObj能不报错地继承IMoveable
  159. /// 在xfgg点播下设计了这个抽象辅助方法,在具体类中实现
  160. /// </summary>
  161. /// <returns> 依具体类及该方法参数而定,默认为false </returns>
  162. protected virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
  163. bool IMoveable.IgnoreCollide(IGameObj targetObj) => IgnoreCollideExecutor(targetObj);
  164. public GameObj(XY initPos, int initRadius, PlaceType initPlace, GameObjType initType)
  165. {
  166. this.Position = this.birthPos = initPos;
  167. this.Radius = initRadius;
  168. this.place = initPlace;
  169. this.type = initType;
  170. ID = Interlocked.Increment(ref currentMaxID);
  171. }
  172. }
  173. }