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.

Moveable.cs 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System.Threading;
  4. namespace GameClass.GameObj
  5. {
  6. public abstract class Moveable : GameObj, IMoveable
  7. {
  8. protected readonly object actionLock = new();
  9. public object ActionLock => actionLock;
  10. private readonly ReaderWriterLockSlim moveReaderWriterLock = new();
  11. public ReaderWriterLockSlim MoveReaderWriterLock => moveReaderWriterLock;
  12. protected long stateNum = 0;
  13. public long StateNum
  14. {
  15. get
  16. {
  17. lock (actionLock)
  18. return stateNum;
  19. }
  20. set
  21. {
  22. lock (actionLock) stateNum = value;
  23. }
  24. }
  25. //规定moveReaderWriterLock>actionLock
  26. public override XY Position
  27. {
  28. get
  29. {
  30. lock (actionLock)
  31. return position;
  32. }
  33. }
  34. public override XY FacingDirection
  35. {
  36. get
  37. {
  38. lock (actionLock)
  39. return facingDirection;
  40. }
  41. }
  42. private bool isMoving;
  43. public bool IsMoving
  44. {
  45. get
  46. {
  47. lock (actionLock)
  48. return isMoving;
  49. }
  50. set
  51. {
  52. lock (actionLock)
  53. {
  54. isMoving = value;
  55. }
  56. }
  57. }
  58. // 移动,改变坐标
  59. public long MovingSetPos(XY moveVec)
  60. {
  61. if (moveVec.x != 0 || moveVec.y != 0)
  62. lock (actionLock)
  63. {
  64. facingDirection = moveVec;
  65. this.position += moveVec;
  66. }
  67. return moveVec * moveVec;
  68. }
  69. public void ReSetPos(XY position)
  70. {
  71. lock (actionLock)
  72. {
  73. this.position = position;
  74. }
  75. }
  76. public override bool CanMove
  77. {
  78. get
  79. {
  80. moveReaderWriterLock.EnterReadLock();
  81. try
  82. {
  83. return canMove;
  84. }
  85. finally
  86. {
  87. moveReaderWriterLock.ExitReadLock();
  88. }
  89. }
  90. }
  91. public void ReSetCanMove(bool value)
  92. {
  93. moveReaderWriterLock.EnterWriteLock();
  94. try
  95. {
  96. lock (actionLock)
  97. {
  98. canMove = value;
  99. }
  100. }
  101. finally
  102. {
  103. moveReaderWriterLock.ExitWriteLock();
  104. }
  105. }
  106. protected bool isRemoved;
  107. public bool IsRemoved
  108. {
  109. get
  110. {
  111. moveReaderWriterLock.EnterReadLock();
  112. try
  113. {
  114. return isRemoved;
  115. }
  116. finally
  117. {
  118. moveReaderWriterLock.ExitReadLock();
  119. }
  120. }
  121. }
  122. public bool IsAvailableForMove // 是否能接收移动指令
  123. {
  124. get
  125. {
  126. moveReaderWriterLock.EnterReadLock();
  127. try
  128. {
  129. lock (actionLock)
  130. return !isMoving && canMove && !isRemoved;
  131. }
  132. finally
  133. {
  134. moveReaderWriterLock.ExitReadLock();
  135. }
  136. }
  137. }
  138. protected int moveSpeed;
  139. /// <summary>
  140. /// 移动速度
  141. /// </summary>
  142. public int MoveSpeed
  143. {
  144. get
  145. {
  146. moveReaderWriterLock.EnterReadLock();
  147. try
  148. {
  149. return moveSpeed;
  150. }
  151. finally
  152. {
  153. moveReaderWriterLock.ExitReadLock();
  154. }
  155. }
  156. set
  157. {
  158. moveReaderWriterLock.EnterWriteLock();
  159. try
  160. {
  161. lock (actionLock)
  162. {
  163. moveSpeed = value;
  164. }
  165. }
  166. finally
  167. {
  168. moveReaderWriterLock.ExitWriteLock();
  169. }
  170. }
  171. }
  172. /// <summary>
  173. /// 原初移动速度
  174. /// </summary>
  175. public int OrgMoveSpeed { get; protected set; }
  176. /* /// <summary>
  177. /// 复活时数据重置
  178. /// </summary>
  179. public virtual void Reset(PlaceType place)
  180. {
  181. lock (gameObjLock)
  182. {
  183. this.FacingDirection = new XY(1, 0);
  184. isMoving = false;
  185. CanMove = false;
  186. IsRemoved = true;
  187. this.Position = birthPos;
  188. this.Place= place;
  189. }
  190. }*/
  191. public Moveable(XY initPos, int initRadius, GameObjType initType) : base(initPos, initRadius, initType)
  192. {
  193. }
  194. }
  195. }