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.

Door.cs 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using Preparation.Interface;
  2. using Preparation.Utility;
  3. using System;
  4. using System.Threading;
  5. namespace GameClass.GameObj
  6. {
  7. /// <summary>
  8. /// 门
  9. /// </summary>
  10. public class Door : Immovable
  11. {
  12. public Door(XY initPos, PlaceType placeType) :
  13. base(initPos, GameData.numOfPosGridPerCell / 2, GameObjType.Door)
  14. {
  15. keyType = placeType switch
  16. {
  17. PlaceType.Door3 => PropType.Key3,
  18. PlaceType.Door5 => PropType.Key5,
  19. _ => PropType.Key6,
  20. };
  21. }
  22. private readonly PropType keyType;
  23. public PropType KeyType => keyType;
  24. public override bool IsRigid => !isOpen;
  25. public override ShapeType Shape => ShapeType.Square;
  26. private Character? whoLockOrOpen = null;
  27. public Character? WhoLockOrOpen
  28. {
  29. get
  30. {
  31. lock (gameObjLock)
  32. return whoLockOrOpen;
  33. }
  34. }
  35. private bool isOpen = true;
  36. public bool IsOpen
  37. {
  38. get
  39. {
  40. lock (gameObjLock)
  41. return isOpen;
  42. }
  43. }
  44. private int lockDegree = 0;
  45. public int LockDegree
  46. {
  47. get => Interlocked.CompareExchange(ref lockDegree, -1, -1);
  48. }
  49. public int AddLockDegree(int add)
  50. {
  51. return Interlocked.Add(ref lockDegree, add);
  52. }
  53. private long openStartTime = 0;
  54. public long OpenStartTime
  55. {
  56. get
  57. {
  58. lock (gameObjLock)
  59. return openStartTime;
  60. }
  61. }
  62. public bool TryOpen(Character character)
  63. {
  64. lock (gameObjLock)
  65. {
  66. if (isOpen) return false;
  67. if (whoLockOrOpen != null) return false;
  68. openStartTime = Environment.TickCount64;
  69. whoLockOrOpen = character;
  70. return true;
  71. }
  72. }
  73. public void StopOpen()
  74. {
  75. lock (gameObjLock)
  76. {
  77. if (whoLockOrOpen != null)
  78. {
  79. if ((Environment.TickCount64 - openStartTime) >= GameData.degreeOfLockingOrOpeningTheDoor / whoLockOrOpen.SpeedOfOpeningOrLocking)
  80. //现在框架没有问题,但是调用可变的SpeedOfOpeningOrLocking可能死锁
  81. isOpen = true;
  82. whoLockOrOpen = null;
  83. }
  84. }
  85. }
  86. public void FinishOpen()
  87. {
  88. lock (gameObjLock)
  89. {
  90. isOpen = true;
  91. whoLockOrOpen = null;
  92. }
  93. }
  94. public bool TryLock(Character character)
  95. {
  96. lock (gameObjLock)
  97. {
  98. if (!isOpen) return false;
  99. if (whoLockOrOpen != null) return false;
  100. Interlocked.Exchange(ref lockDegree, 0);
  101. whoLockOrOpen = character;
  102. return true;
  103. }
  104. }
  105. public void StopLock()
  106. {
  107. lock (gameObjLock)
  108. {
  109. if (LockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
  110. isOpen = false;
  111. whoLockOrOpen = null;
  112. }
  113. }
  114. public void FinishLock()
  115. {
  116. lock (gameObjLock)
  117. {
  118. isOpen = false;
  119. whoLockOrOpen = null;
  120. }
  121. }
  122. public void ForceToOpen()
  123. {
  124. Character? character;
  125. lock (gameObjLock)
  126. {
  127. character = whoLockOrOpen;
  128. whoLockOrOpen = null;
  129. isOpen = true;
  130. }
  131. if (character != null)
  132. {
  133. lock (character.ActionLock)
  134. {
  135. if (character.PlayerState == PlayerStateType.OpeningTheDoor)
  136. {
  137. character.ReleaseTool(KeyType);
  138. character.SetPlayerStateNaturally();
  139. }
  140. else if (character.PlayerState == PlayerStateType.LockingTheDoor)
  141. {
  142. character.SetPlayerStateNaturally();
  143. }
  144. }
  145. }
  146. }
  147. }
  148. }