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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 int isMoving = 0;
  45. public int IsMoving
  46. {
  47. get => Interlocked.CompareExchange(ref isMoving, 0, 0);
  48. set => Interlocked.Exchange(ref isMoving, value);
  49. }
  50. // 移动,改变坐标
  51. public long MovingSetPos(XY moveVec, long stateNo)
  52. {
  53. if (moveVec.x != 0 || moveVec.y != 0)
  54. {
  55. lock (actionLock)
  56. {
  57. moveReaderWriterLock.EnterReadLock();
  58. try
  59. {
  60. if (!canMove || isRemoved) return -1;
  61. }
  62. finally
  63. {
  64. moveReaderWriterLock.ExitReadLock();
  65. }
  66. if (stateNo != stateNum) return -1;
  67. facingDirection = moveVec;
  68. this.position += moveVec;
  69. }
  70. }
  71. return moveVec * moveVec;
  72. }
  73. public void ReSetPos(XY position)
  74. {
  75. lock (actionLock)
  76. {
  77. this.position = position;
  78. }
  79. }
  80. public override bool CanMove
  81. {
  82. get
  83. {
  84. moveReaderWriterLock.EnterReadLock();
  85. try
  86. {
  87. return canMove;
  88. }
  89. finally
  90. {
  91. moveReaderWriterLock.ExitReadLock();
  92. }
  93. }
  94. }
  95. public void ReSetCanMove(bool value)
  96. {
  97. moveReaderWriterLock.EnterWriteLock();
  98. try
  99. {
  100. canMove = value;
  101. }
  102. finally
  103. {
  104. moveReaderWriterLock.ExitWriteLock();
  105. }
  106. }
  107. public override 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. lock (actionLock)
  127. {
  128. moveReaderWriterLock.EnterReadLock();
  129. try
  130. {
  131. return isMoving == 0 && canMove && !isRemoved;
  132. }
  133. finally
  134. {
  135. moveReaderWriterLock.ExitReadLock();
  136. }
  137. }
  138. }
  139. }
  140. protected int moveSpeed;
  141. /// <summary>
  142. /// 移动速度
  143. /// </summary>
  144. public int MoveSpeed
  145. {
  146. get => Interlocked.CompareExchange(ref moveSpeed, 0, 0);
  147. set => Interlocked.Exchange(ref moveSpeed, value);
  148. }
  149. /// <summary>
  150. /// 原初移动速度
  151. /// </summary>
  152. public int OrgMoveSpeed { get; protected set; }
  153. /* /// <summary>
  154. /// 复活时数据重置
  155. /// </summary>
  156. public virtual void Reset(PlaceType place)
  157. {
  158. lock (gameObjLock)
  159. {
  160. this.FacingDirection = new XY(1, 0);
  161. isMoving = false;
  162. CanMove = false;
  163. IsRemoved = true;
  164. this.Position = birthPos;
  165. this.Place= place;
  166. }
  167. }*/
  168. public Moveable(XY initPos, int initRadius, GameObjType initType) : base(initPos, initRadius, initType)
  169. {
  170. }
  171. }
  172. }