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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 set
  28. {
  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. {
  42. lock (gameObjLock)
  43. facingDirection = value;
  44. }
  45. }
  46. public abstract ShapeType Shape { get; }
  47. private bool canMove;
  48. public bool CanMove
  49. {
  50. get => canMove;
  51. set
  52. {
  53. lock (gameObjLock)
  54. {
  55. canMove = value;
  56. }
  57. }
  58. }
  59. private bool isMoving;
  60. public bool IsMoving
  61. {
  62. get => isMoving;
  63. set
  64. {
  65. lock (gameObjLock)
  66. {
  67. isMoving = value;
  68. }
  69. }
  70. }
  71. private bool isResetting;
  72. public bool IsResetting
  73. {
  74. get => isResetting;
  75. set
  76. {
  77. lock (gameObjLock)
  78. {
  79. isResetting = value;
  80. }
  81. }
  82. }
  83. public bool IsAvailable => !IsMoving && CanMove && !IsResetting; //是否能接收指令
  84. public int Radius { get; }
  85. private PlaceType place;
  86. public PlaceType Place
  87. {
  88. get => place;
  89. set
  90. {
  91. lock (gameObjLock)
  92. {
  93. place = value;
  94. }
  95. }
  96. }
  97. protected int moveSpeed;
  98. /// <summary>
  99. /// 移动速度
  100. /// </summary>
  101. public int MoveSpeed
  102. {
  103. get => moveSpeed;
  104. set
  105. {
  106. lock (gameObjLock)
  107. {
  108. moveSpeed = value;
  109. }
  110. }
  111. }
  112. /// <summary>
  113. /// 原初移动速度
  114. /// </summary>
  115. private int orgMoveSpeed;
  116. public int OrgMoveSpeed { get => orgMoveSpeed; protected set { orgMoveSpeed = value; } }
  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. }