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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. protected bool isRemoved;
  118. public bool IsRemoved
  119. {
  120. get
  121. {
  122. moveReaderWriterLock.EnterReadLock();
  123. try
  124. {
  125. return isRemoved;
  126. }
  127. finally
  128. {
  129. moveReaderWriterLock.ExitReadLock();
  130. }
  131. }
  132. }
  133. public bool IsAvailableForMove // 是否能接收移动指令
  134. {
  135. get
  136. {
  137. lock (actionLock)
  138. {
  139. moveReaderWriterLock.EnterReadLock();
  140. try
  141. {
  142. return !isMoving && canMove && !isRemoved;
  143. }
  144. finally
  145. {
  146. moveReaderWriterLock.ExitReadLock();
  147. }
  148. }
  149. }
  150. }
  151. protected long moveSpeed;
  152. /// <summary>
  153. /// 移动速度
  154. /// </summary>
  155. public long MoveSpeed
  156. {
  157. get => Interlocked.Read(ref moveSpeed);
  158. set => Interlocked.Exchange(ref moveSpeed, value);
  159. }
  160. /// <summary>
  161. /// 原初移动速度
  162. /// </summary>
  163. public int OrgMoveSpeed { get; protected set; }
  164. /* /// <summary>
  165. /// 复活时数据重置
  166. /// </summary>
  167. public virtual void Reset(PlaceType place)
  168. {
  169. lock (gameObjLock)
  170. {
  171. this.FacingDirection = new XY(1, 0);
  172. isMoving = false;
  173. CanMove = false;
  174. IsRemoved = true;
  175. this.Position = birthPos;
  176. this.Place= place;
  177. }
  178. }*/
  179. public Moveable(XY initPos, int initRadius, GameObjType initType) : base(initPos, initRadius, initType)
  180. {
  181. }
  182. }
  183. }