|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543 |
- using System.Collections.Generic;
- using System.Threading;
- using Preparation.Interface;
- using Preparation.Utility;
- using System;
-
- namespace GameClass.GameObj
- {
- public partial class Map : IMap
- {
- private readonly Dictionary<uint, XY> birthPointList; // 出生点列表
- public Dictionary<uint, XY> BirthPointList => birthPointList;
-
- private uint numOfRepairedGenerators = 0;
- public uint NumOfRepairedGenerators
- {
- get => Interlocked.CompareExchange(ref numOfRepairedGenerators, 0, 0);
- }
- public void AddNumOfRepairedGenerators()
- {
- uint value = Interlocked.Increment(ref numOfRepairedGenerators);
- if (value == GameData.numOfGeneratorRequiredForEmergencyExit)
- {
- GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
- try
- {
- Random r = new(Environment.TickCount);
- EmergencyExit emergencyExit = (EmergencyExit)(GameObjDict[GameObjType.EmergencyExit][r.Next(0, GameObjDict[GameObjType.EmergencyExit].Count)]);
- emergencyExit.CanOpen.SetReturnOri(true);
- Preparation.Utility.Debugger.Output(emergencyExit, emergencyExit.Position.ToString());
- }
- finally
- {
- GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
- }
- }
- else
- if (value == GameData.numOfGeneratorRequiredForRepair)
- {
- GameObjLockDict[GameObjType.Doorway].EnterReadLock();
- try
- {
- foreach (Doorway doorway in GameObjDict[GameObjType.Doorway])
- doorway.PowerSupply.SetReturnOri(true);
- }
- finally
- {
- GameObjLockDict[GameObjType.Doorway].ExitReadLock();
- }
- }
- }
-
- private uint numOfDeceasedStudent = 0;
- public uint NumOfDeceasedStudent
- {
- get => Interlocked.CompareExchange(ref numOfDeceasedStudent, 0, 0);
- }
- private uint numOfEscapedStudent = 0;
- public uint NumOfEscapedStudent
- {
- get => Interlocked.CompareExchange(ref numOfEscapedStudent, 0, 0);
- }
- private uint numOfNoHpStudent = 0;
- public uint NumOfNoHpStudent
- {
- get => Interlocked.CompareExchange(ref numOfNoHpStudent, 0, 0);
- }
- private uint numOfRemovedStudent = 0;
- public uint NumOfRemovedStudent
- {
- get => Interlocked.CompareExchange(ref numOfRemovedStudent, 0, 0);
- }
-
- public void MapEscapeStudent()
- {
- if (Interlocked.Increment(ref numOfRemovedStudent) == GameData.numOfStudent - 1)
- OpenEmergencyExit();
- if (Interlocked.Increment(ref numOfNoHpStudent) == GameData.numOfStudent)
- {
- AddScoreFromAddict();
- Timer.IsGaming = false;
- return;
- }
- Interlocked.Increment(ref numOfEscapedStudent);
- }
- public void MapDieStudent()
- {
- if (Interlocked.Increment(ref numOfRemovedStudent) == GameData.numOfStudent - 1)
- OpenEmergencyExit();
- uint noHp = Interlocked.Increment(ref numOfNoHpStudent);
- ghost!.AddScore(GameData.TrickerScoreStudentDie);
- if (noHp == GameData.numOfStudent)
- {
- AddScoreFromAddict();
- Timer.IsGaming = false;
- return;
- }
- Interlocked.Increment(ref numOfDeceasedStudent);
- }
- public void MapAddictStudent()
- {
- if (Interlocked.Increment(ref numOfNoHpStudent) == GameData.numOfStudent)
- {
- AddScoreFromAddict();
- Timer.IsGaming = false;
- }
- }
- public void MapRescueStudent()
- {
- if (Timer.IsGaming)
- Interlocked.Decrement(ref numOfNoHpStudent);
- }
-
- private void OpenEmergencyExit()
- {
- GameObjLockDict[GameObjType.EmergencyExit].EnterReadLock();
- try
- {
- foreach (EmergencyExit emergencyExit in GameObjDict[GameObjType.EmergencyExit])
- if (emergencyExit.CanOpen)
- {
- emergencyExit.IsOpen = true;
- break;
- }
- }
- finally
- {
- GameObjLockDict[GameObjType.EmergencyExit].ExitReadLock();
- }
- }
- private void AddScoreFromAddict()
- {
- ghost!.AddScore(GameData.TrickerScoreStudentDie * (GameData.numOfStudent - NumOfRemovedStudent));
- }
-
-
- private Dictionary<GameObjType, IList<IGameObj>> gameObjDict;
- public Dictionary<GameObjType, IList<IGameObj>> GameObjDict => gameObjDict;
- private Dictionary<GameObjType, ReaderWriterLockSlim> gameObjLockDict;
- public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict => gameObjLockDict;
-
- public readonly uint[,] protoGameMap;
- public uint[,] ProtoGameMap => protoGameMap;
- public PlaceType GetPlaceType(IGameObj obj)
- {
- try
- {
- return (PlaceType)protoGameMap[obj.Position.x / GameData.numOfPosGridPerCell, obj.Position.y / GameData.numOfPosGridPerCell];
- }
- catch
- {
- return PlaceType.Null;
- }
- }
- public PlaceType GetPlaceType(XY pos)
- {
- try
- {
- return (PlaceType)protoGameMap[pos.x / GameData.numOfPosGridPerCell, pos.y / GameData.numOfPosGridPerCell];
- }
- catch
- {
- return PlaceType.Null;
- }
- }
-
- public bool IsOutOfBound(IGameObj obj)
- {
- return obj.Position.x >= GameData.lengthOfMap - obj.Radius || obj.Position.x <= obj.Radius || obj.Position.y >= GameData.lengthOfMap - obj.Radius || obj.Position.y <= obj.Radius;
- }
- public IOutOfBound GetOutOfBound(XY pos)
- {
- return new OutOfBoundBlock(pos);
- }
-
- public Character? FindPlayerInID(long playerID)
- {
- Character? player = null;
- gameObjLockDict[GameObjType.Character].EnterReadLock();
- try
- {
- foreach (Character person in gameObjDict[GameObjType.Character])
- {
- if (playerID == person.ID)
- {
- player = person;
- break;
- }
- }
- }
- finally
- {
- gameObjLockDict[GameObjType.Character].ExitReadLock();
- }
- return player;
- }
- public Character? FindPlayerInPlayerID(long playerID)
- {
- Character? player = null;
- gameObjLockDict[GameObjType.Character].EnterReadLock();
- try
- {
- foreach (Character person in gameObjDict[GameObjType.Character])
- {
- if (playerID == person.PlayerID)
- {
- player = person;
- break;
- }
- }
- }
- finally
- {
- gameObjLockDict[GameObjType.Character].ExitReadLock();
- }
- return player;
- }
- public Ghost? ghost = null;
- public Character? FindPlayerToAction(long playerID)
- {
- Character? player = null;
- gameObjLockDict[GameObjType.Character].EnterReadLock();
- try
- {
- foreach (Character person in gameObjDict[GameObjType.Character])
- {
- if (playerID == person.ID)
- {
- if (person.CharacterType == CharacterType.TechOtaku)
- {
- foreach (Character character in gameObjDict[GameObjType.Character])
- {
- if (((UseRobot)person.FindActiveSkill(ActiveSkillType.UseRobot)).NowPlayerID == character.PlayerID)
- {
- player = character;
- break;
- }
- }
- }
- else player = person;
- break;
- }
- }
- }
- finally
- {
- gameObjLockDict[GameObjType.Character].ExitReadLock();
- }
- return player;
- }
-
- public GameObj? OneForInteract(XY Pos, GameObjType gameObjType)
- {
- GameObj? GameObjForInteract = null;
- GameObjLockDict[gameObjType].EnterReadLock();
- try
- {
- foreach (GameObj gameObj in GameObjDict[gameObjType])
- {
- if (GameData.ApproachToInteract(gameObj.Position, Pos))
- {
- GameObjForInteract = gameObj;
- break;
- }
- }
- }
- finally
- {
- GameObjLockDict[gameObjType].ExitReadLock();
- }
- return GameObjForInteract;
- }
- public Student? StudentForInteract(Student AStudent)
- {
- GameObjLockDict[GameObjType.Character].EnterReadLock();
- try
- {
- foreach (Character character in GameObjDict[GameObjType.Character])
- {
- if (!character.IsGhost() && character != AStudent && GameData.ApproachToInteract(character.Position, AStudent.Position))
- {
- return (Student)character;
- }
- }
- }
- finally
- {
- GameObjLockDict[GameObjType.Character].ExitReadLock();
- }
- return null;
- }
- public GameObj? OneInTheSameCell(XY Pos, GameObjType gameObjType)
- {
- GameObj? GameObjForInteract = null;
- GameObjLockDict[gameObjType].EnterReadLock();
- try
- {
- foreach (GameObj gameObj in GameObjDict[gameObjType])
- {
- if (GameData.IsInTheSameCell(gameObj.Position, Pos))
- {
- GameObjForInteract = gameObj;
- break;
- }
- }
- }
- finally
- {
- GameObjLockDict[gameObjType].ExitReadLock();
- }
- return GameObjForInteract;
- }
- public GameObj? PartInTheSameCell(XY Pos, GameObjType gameObjType)
- {
- GameObj? GameObjForInteract = null;
- GameObjLockDict[gameObjType].EnterReadLock();
- try
- {
- foreach (GameObj gameObj in GameObjDict[gameObjType])
- {
- if (GameData.PartInTheSameCell(gameObj.Position, Pos))
- {
- GameObjForInteract = gameObj;
- break;
- }
- }
- }
- finally
- {
- GameObjLockDict[gameObjType].ExitReadLock();
- }
- return GameObjForInteract;
- }
- public GameObj? OneForInteractInACross(XY Pos, GameObjType gameObjType)
- {
- GameObj? GameObjForInteract = null;
- GameObjLockDict[gameObjType].EnterReadLock();
- try
- {
- foreach (GameObj gameObj in GameObjDict[gameObjType])
- {
- if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
- {
- GameObjForInteract = gameObj;
- break;
- }
- }
- }
- finally
- {
- GameObjLockDict[gameObjType].ExitReadLock();
- }
- return GameObjForInteract;
- }
-
- public bool CanSee(Character player, GameObj gameObj)
- {
- if ((gameObj.Type == GameObjType.Character) && ((Character)gameObj).HasInvisible) return false;
- XY pos1 = player.Position;
- XY pos2 = gameObj.Position;
- XY del = pos1 - pos2;
- if (del * del > player.ViewRange * player.ViewRange) return false;
- if (del.x > del.y)
- {
- if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
- {
- for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
- {
- if (GetPlaceType(pos1 + del * (x / del.x)) != PlaceType.Grass)
- return false;
- }
- }
- else
- {
- for (int x = GameData.PosGridToCellX(pos1) + GameData.numOfPosGridPerCell; x < GameData.PosGridToCellX(pos2); x += GameData.numOfPosGridPerCell)
- {
- if (GetPlaceType(pos1 + del * (x / del.x)) == PlaceType.Wall)
- return false;
- }
- }
- }
- else
- {
- if (GetPlaceType(pos1) == PlaceType.Grass && GetPlaceType(pos2) == PlaceType.Grass)
- {
- for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
- {
- if (GetPlaceType(pos1 + del * (y / del.y)) != PlaceType.Grass)
- return false;
- }
- }
- else
- {
- for (int y = GameData.PosGridToCellY(pos1) + GameData.numOfPosGridPerCell; y < GameData.PosGridToCellY(pos2); y += GameData.numOfPosGridPerCell)
- {
- if (GetPlaceType(pos1 + del * (y / del.y)) == PlaceType.Wall)
- return false;
- }
- }
- }
- return true;
- }
-
- public bool Remove(GameObj gameObj)
- {
- GameObj? ToDel = null;
- GameObjLockDict[gameObj.Type].EnterWriteLock();
- try
- {
- foreach (GameObj obj in GameObjDict[gameObj.Type])
- {
- if (gameObj.ID == obj.ID)
- {
- ToDel = obj;
- break;
- }
- }
- if (ToDel != null)
- {
- GameObjDict[gameObj.Type].Remove(ToDel);
- ToDel.TryToRemove();
- }
- }
- finally
- {
- GameObjLockDict[gameObj.Type].ExitWriteLock();
- }
- return ToDel != null;
- }
- public bool RemoveJustFromMap(GameObj gameObj)
- {
- GameObjLockDict[gameObj.Type].EnterWriteLock();
- try
- {
- if (GameObjDict[gameObj.Type].Remove(gameObj))
- {
- gameObj.TryToRemove();
- return true;
- }
- return false;
- }
- finally
- {
- GameObjLockDict[gameObj.Type].ExitWriteLock();
- }
- }
- public void Add(GameObj gameObj)
- {
- GameObjLockDict[gameObj.Type].EnterWriteLock();
- try
- {
- GameObjDict[gameObj.Type].Add(gameObj);
- }
- finally
- {
- GameObjLockDict[gameObj.Type].ExitWriteLock();
- }
- }
-
- public Map(uint[,] mapResource)
- {
- gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
- gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
- foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
- {
- if (idx != GameObjType.Null)
- {
- gameObjDict.Add(idx, new List<IGameObj>());
- gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
- }
- }
-
- protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
- Array.Copy(mapResource, protoGameMap, mapResource.Length);
-
- birthPointList = new Dictionary<uint, XY>(GameData.numOfBirthPoint);
-
- for (int i = 0; i < GameData.rows; ++i)
- {
- for (int j = 0; j < GameData.cols; ++j)
- {
- switch (mapResource[i, j])
- {
- case (uint)PlaceType.Wall:
- {
- Add(new Wall(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.Doorway:
- {
- Add(new Doorway(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.EmergencyExit:
- {
- Add(new EmergencyExit(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.Generator:
- {
- Add(new Generator(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.Chest:
- {
- Add(new Chest(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.Door3:
- {
- Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door3));
- break;
- }
- case (uint)PlaceType.Door5:
- {
- Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door5));
- break;
- }
- case (uint)PlaceType.Door6:
- {
- Add(new Door(GameData.GetCellCenterPos(i, j), PlaceType.Door6));
- break;
- }
- case (uint)PlaceType.Window:
- {
- Add(new Window(GameData.GetCellCenterPos(i, j)));
- break;
- }
- case (uint)PlaceType.BirthPoint1:
- case (uint)PlaceType.BirthPoint2:
- case (uint)PlaceType.BirthPoint3:
- case (uint)PlaceType.BirthPoint4:
- case (uint)PlaceType.BirthPoint5:
- {
- birthPointList.Add(mapResource[i, j], GameData.GetCellCenterPos(i, j));
- break;
- }
- }
- }
- }
- }
- }
- }
|