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

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