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 6.0 kB

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