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

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