|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- using System;
- using System.Threading;
- using System.Collections.Generic;
- using Preparation.Utility;
- using Timothy.FrameRateTask;
- using Preparation.Interface;
- using GameClass.GameObj;
- using System.Numerics;
-
- namespace Gaming
- {
- public partial class Game
- {
- public struct PlayerInitInfo
- {
- public uint birthPointIndex;
- public long teamID;
- public long playerID;
- public CharacterType characterType;
- public PlayerInitInfo(uint birthPointIndex, long teamID, long playerID, CharacterType characterType)
- {
- this.birthPointIndex = birthPointIndex;
- this.teamID = teamID;
- this.characterType = characterType;
- this.playerID = playerID;
- }
- }
-
- private readonly List<Team> teamList;
- public List<Team> TeamList => teamList;
- private readonly Map gameMap;
- public Map GameMap => gameMap;
- // private readonly int numOfTeam;
- public long AddPlayer(PlayerInitInfo playerInitInfo)
- {
- if (!Team.teamExists(playerInitInfo.teamID))
- /* || !MapInfo.ValidBirthPointIdx(playerInitInfo.birthPointIdx)
- || gameMap.BirthPointList[playerInitInfo.birthPointIdx].Parent != null)*/
- return GameObj.invalidID;
-
- XY pos = gameMap.BirthPointList[playerInitInfo.birthPointIndex];
- // Console.WriteLine($"x,y: {pos.x},{pos.y}");
-
- Character newPlayer = (GameData.IsGhost(playerInitInfo.characterType)) ? new Ghost(pos, GameData.characterRadius, playerInitInfo.characterType) : new Student(pos, GameData.characterRadius, playerInitInfo.characterType);
- gameMap.Add(newPlayer);
-
- // Console.WriteLine($"GameObjDict[GameObjType.Character] length:{gameMap.GameObjDict[GameObjType.Character].Count}");
- teamList[(int)playerInitInfo.teamID].AddPlayer(newPlayer);
- newPlayer.TeamID = playerInitInfo.teamID;
- newPlayer.PlayerID = playerInitInfo.playerID;
- #region 人物装弹
- new Thread
- (
- () =>
- {
- while (!gameMap.Timer.IsGaming)
- Thread.Sleep(newPlayer.CD);
- long lastTime = Environment.TickCount64;
- new FrameRateTaskExecutor<int>(
- loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsResetting,
- loopToDo: () =>
- {
- long nowTime = Environment.TickCount64;
- if (newPlayer.BulletNum == newPlayer.MaxBulletNum)
- lastTime = nowTime;
- if (nowTime - lastTime >= newPlayer.CD)
- {
- _ = newPlayer.TryAddBulletNum();
- lastTime = nowTime;
- }
- },
- timeInterval: GameData.checkInterval,
- finallyReturn: () => 0
- )
- {
- AllowTimeExceed = true/*,
- MaxTolerantTimeExceedCount = 5,
- TimeExceedAction = exceedTooMuch =>
- {
- if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
- }*/
- }
- .Start();
- }
- )
- { IsBackground = true }.Start();
- #endregion
- #region BGM更新
- new Thread
- (
- () =>
- {
- while (!gameMap.Timer.IsGaming)
- Thread.Sleep((int)GameData.checkInterval);
- new FrameRateTaskExecutor<int>(
- loopCondition: () => gameMap.Timer.IsGaming && !newPlayer.IsResetting,
- loopToDo: () =>
- {
- gameMap.GameObjLockDict[GameObjType.Character].EnterReadLock();
- try
- {
- if (newPlayer.IsGhost())
- {
- double bgmVolume = 0;
- foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
- {
- if (!person.IsGhost() && XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
- {
- if ((double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position) > bgmVolume)
- bgmVolume = newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position);
- }
- }
- if (bgmVolume > 0)
- newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
- }
- else
- {
- foreach (Character person in gameMap.GameObjDict[GameObjType.Character])
- {
- if (person.IsGhost())
- {
- if (XY.Distance(newPlayer.Position, person.Position) <= (newPlayer.AlertnessRadius / person.Concealment))
- newPlayer.AddBgm(BgmType.GhostIsComing, (double)newPlayer.AlertnessRadius / XY.Distance(newPlayer.Position, person.Position));
- break;
- }
- }
- }
- }
- finally
- {
- gameMap.GameObjLockDict[GameObjType.Character].ExitReadLock();
- }
-
- gameMap.GameObjLockDict[GameObjType.Generator].EnterReadLock();
- try
- {
- double bgmVolume = 0;
- foreach (Generator generator in gameMap.GameObjDict[GameObjType.Generator])
- {
- if (XY.Distance(newPlayer.Position, generator.Position) <= newPlayer.AlertnessRadius)
- {
- if ((double)newPlayer.AlertnessRadius * generator.DegreeOfFRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position) > bgmVolume)
- bgmVolume = (double)newPlayer.AlertnessRadius * generator.DegreeOfFRepair / GameData.degreeOfFixedGenerator / XY.Distance(newPlayer.Position, generator.Position);
- }
- }
- if (bgmVolume > 0)
- newPlayer.AddBgm(BgmType.StudentIsApproaching, bgmVolume);
- }
-
-
- finally
- {
- gameMap.GameObjLockDict[GameObjType.Generator].ExitReadLock();
- }
- },
- timeInterval: GameData.checkInterval,
- finallyReturn: () => 0
- )
- {
- AllowTimeExceed = true/*,
- MaxTolerantTimeExceedCount = 5,
- TimeExceedAction = exceedTooMuch =>
- {
- if (exceedTooMuch) Console.WriteLine("The computer runs too slow that it cannot check the color below the player in time!");
- }*/
- }
- .Start();
- }
- )
- { IsBackground = true }.Start();
- #endregion
-
- return newPlayer.ID;
- }
- public bool StartGame(int milliSeconds)
- {
- if (gameMap.Timer.IsGaming)
- return false;
-
- propManager.StartProducing();
-
- // 开始游戏
- new Thread
- (
- () =>
- {
- if (!gameMap.Timer.StartGame(milliSeconds))
- return;
-
- EndGame(); // 游戏结束时要做的事
- }
- )
- { IsBackground = true }.Start();
-
- return true;
- }
-
- public void EndGame()
- {
- }
- public bool MovePlayer(long playerID, int moveTimeInMilliseconds, double angle)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- bool res = actionManager.MovePlayer(player, moveTimeInMilliseconds, angle);
- #if DEBUG
- Console.WriteLine($"PlayerID:{playerID} move to ({player.Position.x},{player.Position.y})!");
- #endif
- return res;
-
- }
- else
- {
- #if DEBUG
- Console.WriteLine($"PlayerID:{playerID} player does not exists!");
- #endif
- return false;
-
- }
- }
- public bool Treat(long playerID, long playerTreatedID)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- ICharacter? player = gameMap.FindPlayer(playerID);
- ICharacter? playerTreated = gameMap.FindPlayer(playerTreatedID);
- if (player != null && playerTreated != null)
- {
- if (!playerTreated.IsGhost() && !player.IsGhost())
- return actionManager.Treat((Student)player, (Student)playerTreated);
- }
- return false;
- }
- public bool Rescue(long playerID, long playerRescuedID)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- ICharacter? player = gameMap.FindPlayer(playerID);
- ICharacter? playerRescued = gameMap.FindPlayer(playerRescuedID);
- if (player != null && playerRescued != null)
- {
- if (!playerRescued.IsGhost() && !player.IsGhost())
- return actionManager.Treat((Student)player, (Student)playerRescued);
- }
- return false;
- }
- public bool Fix(long playerID)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- ICharacter? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- if (!player.IsGhost())
- return actionManager.Fix((Student)player);
- }
- return false;
- }
- public bool Escape(long playerID)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- ICharacter? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- if (!player.IsGhost())
- return actionManager.Escape((Student)player);
- }
- return false;
- }
- public bool Stop(long playerID)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- return actionManager.Stop(player);
- }
- return false;
- }
-
- public void Attack(long playerID, double angle)
- {
- if (!gameMap.Timer.IsGaming)
- return;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- _ = attackManager.Attack(player, angle);
- }
- }
- public void UseProp(long playerID, int indexing)
- {
- if (!gameMap.Timer.IsGaming)
- return;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- propManager.UseProp(player, indexing);
- }
- }
- public void ThrowProp(long playerID, int indexing)
- {
- if (!gameMap.Timer.IsGaming)
- return;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- propManager.ThrowProp(player, indexing);
- }
- }
- public bool PickProp(long playerID, PropType propType = PropType.Null)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- return propManager.PickProp(player, propType);
- }
- return false;
- }
-
- public bool UseActiveSkill(long playerID, ActiveSkillType activeSkillType)
- {
- if (!gameMap.Timer.IsGaming)
- return false;
- Character? player = gameMap.FindPlayer(playerID);
- if (player != null)
- {
- return skillManager.UseActiveSkill(player, activeSkillType);
- }
- else
- return false;
- }
-
- public void AllPlayerUsePassiveSkill()
- {
- if (!gameMap.Timer.IsGaming)
- return;
- gameMap.GameObjLockDict[GameObjType.Character].EnterWriteLock();
- try
- {
- foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
- {
- skillManager.UseAllPassiveSkill(player);
- }
- }
- finally
- {
- gameMap.GameObjLockDict[GameObjType.Character].ExitWriteLock();
- }
- }
-
- public void ClearLists(GameObjType[] objIdxes)
- {
- foreach (var idx in objIdxes)
- {
- if (idx != GameObjType.Null)
- {
- gameMap.GameObjLockDict[idx].EnterWriteLock();
- try
- {
- gameMap.GameObjDict[idx].Clear();
- }
- finally
- {
- gameMap.GameObjLockDict[idx].ExitWriteLock();
- }
- }
- }
- }
- public void ClearAllLists()
- {
- foreach (var keyValuePair in gameMap.GameObjDict)
- {
- if (!GameData.IsMap(keyValuePair.Key))
- {
- gameMap.GameObjLockDict[keyValuePair.Key].EnterWriteLock();
- try
- {
- if (keyValuePair.Key == GameObjType.Character)
- {
- foreach (Character player in gameMap.GameObjDict[GameObjType.Character])
- {
- player.CanMove = false;
- }
- }
- gameMap.GameObjDict[keyValuePair.Key].Clear();
- }
- finally
- {
- gameMap.GameObjLockDict[keyValuePair.Key].ExitWriteLock();
- }
- }
- }
- }
-
- public int GetTeamScore(long teamID)
- {
- return teamList[(int)teamID].Score;
- }
- public List<IGameObj> GetGameObj()
- {
- var gameObjList = new List<IGameObj>();
- foreach (var keyValuePair in gameMap.GameObjDict)
- {
- if (!GameData.IsMap(keyValuePair.Key))
- {
- gameMap.GameObjLockDict[keyValuePair.Key].EnterReadLock();
- try
- {
- gameObjList.AddRange(gameMap.GameObjDict[keyValuePair.Key]);
- }
- finally
- {
- gameMap.GameObjLockDict[keyValuePair.Key].ExitReadLock();
- }
- }
- }
- return gameObjList;
- }
- public Game(uint[,] mapResource, int numOfTeam)
- {
- // if (numOfTeam > maxTeamNum) throw new TeamNumOverFlowException();
-
- gameMap = new Map(mapResource);
-
- // 加入队伍
- // this.numOfTeam = numOfTeam;
- teamList = new List<Team>();
- for (int i = 0; i < numOfTeam; ++i)
- {
- teamList.Add(new Team());
- }
-
- attackManager = new AttackManager(gameMap);
- actionManager = new ActionManager(gameMap);
- propManager = new PropManager(gameMap);
- skillManager = new SkillManager(gameMap, actionManager, attackManager, propManager);
- }
- }
- }
|