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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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=>Interlocked.Read(ref stateNum);
  16. }
  17. //规定moveReaderWriterLock>actionLock
  18. public override XY Position
  19. {
  20. get
  21. {
  22. lock (actionLock)
  23. return position;
  24. }
  25. }
  26. public override XY FacingDirection
  27. {
  28. get
  29. {
  30. lock (actionLock)
  31. return facingDirection;
  32. }
  33. }
  34. private bool isMoving;
  35. public bool IsMoving
  36. {
  37. get
  38. {
  39. lock (actionLock)
  40. return isMoving;
  41. }
  42. set
  43. {
  44. lock (actionLock)
  45. {
  46. isMoving = value;
  47. }
  48. }
  49. }
  50. // 移动,改变坐标
  51. public long MovingSetPos(XY moveVec)
  52. {
  53. if (moveVec.x != 0 || moveVec.y != 0)
  54. lock (actionLock)
  55. {
  56. facingDirection = moveVec;
  57. this.position += moveVec;
  58. }
  59. return moveVec * moveVec;
  60. }
  61. public void ReSetPos(XY position)
  62. {
  63. lock (actionLock)
  64. {
  65. this.position = position;
  66. }
  67. }
  68. public override bool CanMove
  69. {
  70. get
  71. {
  72. moveReaderWriterLock.EnterReadLock();
  73. try
  74. {
  75. return canMove;
  76. }
  77. finally
  78. {
  79. moveReaderWriterLock.ExitReadLock();
  80. }
  81. }
  82. }
  83. public void ReSetCanMove(bool value)
  84. {
  85. moveReaderWriterLock.EnterWriteLock();
  86. try
  87. {
  88. lock (actionLock)
  89. {
  90. canMove = value;
  91. }
  92. }
  93. finally
  94. {
  95. moveReaderWriterLock.ExitWriteLock();
  96. }
  97. }
  98. protected bool isResetting;
  99. public bool IsResetting
  100. {
  101. get
  102. {
  103. moveReaderWriterLock.EnterReadLock();
  104. try
  105. {
  106. return isResetting;
  107. }
  108. finally
  109. {
  110. moveReaderWriterLock.ExitReadLock();
  111. }
  112. }
  113. }
  114. public bool IsAvailable => !IsMoving && CanMove && !IsResetting; // 是否能接收指令
  115. protected int moveSpeed;
  116. /// <summary>
  117. /// 移动速度
  118. /// </summary>
  119. public int MoveSpeed
  120. {
  121. get
  122. {
  123. moveReaderWriterLock.EnterReadLock();
  124. try
  125. {
  126. return moveSpeed;
  127. }
  128. finally
  129. {
  130. moveReaderWriterLock.ExitReadLock();
  131. }
  132. }
  133. set
  134. {
  135. moveReaderWriterLock.EnterWriteLock();
  136. try
  137. {
  138. lock (actionLock)
  139. {
  140. moveSpeed = value;
  141. }
  142. }
  143. finally
  144. {
  145. moveReaderWriterLock.ExitWriteLock();
  146. }
  147. }
  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. IsResetting = 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. }