|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- using Preparation.Interface;
- using Preparation.Utility;
- using System;
-
- namespace GameClass.GameObj
- {
- /// <summary>
- /// 门
- /// </summary>
- public class Door : Immovable
- {
- public Door(XY initPos, PlaceType placeType) :
- base(initPos, GameData.numOfPosGridPerCell / 2, GameObjType.Door)
- {
- keyType = placeType switch
- {
- PlaceType.Door3 => PropType.Key3,
- PlaceType.Door5 => PropType.Key5,
- _ => PropType.Key6,
- };
- }
-
- private readonly PropType keyType;
- public PropType KeyType => keyType;
-
- public override bool IsRigid => !isOpen;
- public override ShapeType Shape => ShapeType.Square;
-
- private Character? whoLockOrOpen = null;
- public Character? WhoLockOrOpen
- {
- get
- {
- lock (gameObjLock)
- return whoLockOrOpen;
- }
- }
-
- private bool isOpen = true;
- public bool IsOpen
- {
- get
- {
- lock (gameObjLock)
- return isOpen;
- }
- }
-
- private AtomicInt lockDegree = new AtomicInt(0);
- public AtomicInt LockDegree { get => lockDegree; }
-
- private long openStartTime = 0;
- public long OpenStartTime
- {
- get
- {
- lock (gameObjLock)
- return openStartTime;
- }
- }
-
- public bool TryOpen(Character character)
- {
- lock (gameObjLock)
- {
- if (isOpen) return false;
- if (whoLockOrOpen != null) return false;
- openStartTime = Environment.TickCount64;
- whoLockOrOpen = character;
- return true;
- }
- }
- public void StopOpen()
- {
- lock (gameObjLock)
- {
- if (whoLockOrOpen != null)
- {
- if ((Environment.TickCount64 - openStartTime) >= GameData.degreeOfLockingOrOpeningTheDoor / whoLockOrOpen.SpeedOfOpeningOrLocking)
- //现在框架没有问题,但是调用可变的SpeedOfOpeningOrLocking可能死锁
- isOpen = true;
- whoLockOrOpen = null;
- }
- }
- }
- public void FinishOpen()
- {
- lock (gameObjLock)
- {
- isOpen = true;
- whoLockOrOpen = null;
- }
- }
-
- public bool TryLock(Character character)
- {
- lock (gameObjLock)
- {
- if (!isOpen) return false;
- if (whoLockOrOpen != null) return false;
- LockDegree.SetReturnOri(0);
- whoLockOrOpen = character;
- return true;
- }
- }
- public void StopLock()
- {
- lock (gameObjLock)
- {
- if (LockDegree >= GameData.degreeOfLockingOrOpeningTheDoor)
- isOpen = false;
- whoLockOrOpen = null;
- }
- }
- public void FinishLock()
- {
- lock (gameObjLock)
- {
- isOpen = false;
- whoLockOrOpen = null;
- }
- }
-
- public void ForceToOpen()
- {
- Character? character;
- lock (gameObjLock)
- {
- character = whoLockOrOpen;
- whoLockOrOpen = null;
- isOpen = true;
- }
- if (character != null)
- {
- lock (character.ActionLock)
- {
- if (character.PlayerState == PlayerStateType.OpeningTheDoor)
- {
- character.ReleaseTool(KeyType);
- character.SetPlayerStateNaturally();
- }
- else if (character.PlayerState == PlayerStateType.LockingTheDoor)
- {
- character.SetPlayerStateNaturally();
- }
- }
- }
- }
- }
- }
|