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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/其他Lock,应当避免两个player的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. protected XY facingDirection = new(1, 0);
  50. public XY FacingDirection
  51. {
  52. get
  53. {
  54. lock (actionLock)
  55. return facingDirection;
  56. }
  57. set
  58. {
  59. lock (actionLock)
  60. facingDirection = value;
  61. }
  62. }
  63. public AtomicBool IsMoving { get; } = new(false);
  64. // 移动,改变坐标
  65. public long MovingSetPos(XY moveVec, long stateNo)
  66. {
  67. if (moveVec.x != 0 || moveVec.y != 0)
  68. {
  69. lock (actionLock)
  70. {
  71. if (!CanMove || IsRemoved) return -1;
  72. if (stateNo != stateNum) return -1;
  73. facingDirection = moveVec;
  74. this.position += moveVec;
  75. }
  76. }
  77. return moveVec * moveVec;
  78. }
  79. public void ReSetPos(XY position)
  80. {
  81. lock (actionLock)
  82. {
  83. this.position = position;
  84. }
  85. }
  86. private AtomicBool canMove = new(false);
  87. public AtomicBool CanMove { get => canMove; }
  88. public bool IsAvailableForMove => !IsMoving && CanMove && !IsRemoved; // 是否能接收移动指令
  89. /// <summary>
  90. /// 移动速度
  91. /// </summary>
  92. private AtomicInt moveSpeed = new(0);
  93. public AtomicInt MoveSpeed { get => moveSpeed; }
  94. /// <summary>
  95. /// 原初移动速度
  96. /// </summary>
  97. protected int orgMoveSpeed;
  98. public int OrgMoveSpeed => orgMoveSpeed;
  99. /* /// <summary>
  100. /// 复活时数据重置
  101. /// </summary>
  102. public virtual void Reset(PlaceType place)
  103. {
  104. lock (gameObjLock)
  105. {
  106. this.FacingDirection = new XY(1, 0);
  107. isMoving = false;
  108. CanMove = false;
  109. IsRemoved = true;
  110. this.Position = birthPos;
  111. this.Place= place;
  112. }
  113. }*/
  114. public Moveable(XY initPos, int initRadius, GameObjType initType) : base(initPos, initRadius, initType)
  115. {
  116. }
  117. }
  118. }