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

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