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

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