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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. //player.actionLock>其他.actionLock
  11. private readonly ReaderWriterLockSlim moveReaderWriterLock = new();
  12. public ReaderWriterLockSlim MoveReaderWriterLock => moveReaderWriterLock;
  13. //规定moveReaderWriterLock<actionLock
  14. public Semaphore ThreadNum { get; } = new(1, 1);
  15. protected long stateNum = 0;
  16. public long StateNum
  17. {
  18. get
  19. {
  20. lock (actionLock)
  21. return stateNum;
  22. }
  23. set
  24. {
  25. lock (actionLock) stateNum = value;
  26. }
  27. }
  28. protected RunningStateType runningState = RunningStateType.Null;
  29. public RunningStateType RunningState
  30. {
  31. get
  32. {
  33. lock (actionLock) return runningState;
  34. }
  35. set
  36. {
  37. lock (actionLock)
  38. runningState = value;
  39. }
  40. }
  41. public override XY Position
  42. {
  43. get
  44. {
  45. lock (actionLock)
  46. return position;
  47. }
  48. }
  49. public override XY FacingDirection
  50. {
  51. get
  52. {
  53. lock (actionLock)
  54. return facingDirection;
  55. }
  56. }
  57. private int isMoving = 0;
  58. public bool IsMoving
  59. {
  60. get => (Interlocked.CompareExchange(ref isMoving, 0, 0) == 1);
  61. set => Interlocked.Exchange(ref isMoving, value ? 1 : 0);
  62. }
  63. // 移动,改变坐标
  64. public long MovingSetPos(XY moveVec, long stateNo)
  65. {
  66. if (moveVec.x != 0 || moveVec.y != 0)
  67. {
  68. lock (actionLock)
  69. {
  70. if (!CanMove || IsRemoved) return -1;
  71. if (stateNo != stateNum) return -1;
  72. facingDirection = moveVec;
  73. this.position += moveVec;
  74. }
  75. }
  76. return moveVec * moveVec;
  77. }
  78. public void ReSetPos(XY position)
  79. {
  80. lock (actionLock)
  81. {
  82. this.position = position;
  83. }
  84. }
  85. private int canMove;
  86. public override bool CanMove
  87. {
  88. get => (Interlocked.CompareExchange(ref canMove, 0, 0) == 1);
  89. }
  90. public void ReSetCanMove(bool value)
  91. {
  92. Interlocked.Exchange(ref canMove, (value ? 1 : 0));
  93. }
  94. public bool IsAvailableForMove // 是否能接收移动指令
  95. {
  96. get
  97. {
  98. lock (actionLock)
  99. {
  100. return !IsMoving && CanMove && !IsRemoved;
  101. }
  102. }
  103. }
  104. protected int moveSpeed;
  105. /// <summary>
  106. /// 移动速度
  107. /// </summary>
  108. public int MoveSpeed
  109. {
  110. get => Interlocked.CompareExchange(ref moveSpeed, 0, 0);
  111. set => Interlocked.Exchange(ref moveSpeed, value);
  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. IsRemoved = 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. }