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

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