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

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