diff --git a/logic/GameClass/GameObj/Areas/Home.cs b/logic/GameClass/GameObj/Areas/Home.cs index 53141a5d..b52da7cd 100644 --- a/logic/GameClass/GameObj/Areas/Home.cs +++ b/logic/GameClass/GameObj/Areas/Home.cs @@ -4,15 +4,11 @@ namespace GameClass.GameObj.Areas; -public class Home : Immovable, IHome +public class Home(XY initPos, long id) + : Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Home), IHome { - public AtomicLong TeamID { get; } - public LongInTheVariableRange HP => new LongInTheVariableRange(GameData.HomeHP); - + public long TeamID { get; } = id; + public LongInTheVariableRange HP => new(GameData.HomeHP); public override bool IsRigid => false; public override ShapeType Shape => ShapeType.Square; - public Home(XY initPos) - : base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Home) - { - } } diff --git a/logic/GameClass/GameObj/Map/Map.cs b/logic/GameClass/GameObj/Map/Map.cs index 116ba550..b760fe47 100644 --- a/logic/GameClass/GameObj/Map/Map.cs +++ b/logic/GameClass/GameObj/Map/Map.cs @@ -10,12 +10,21 @@ namespace GameClass.GameObj { public partial class Map : IMap { - private Dictionary> gameObjDict; - public Dictionary> GameObjDict => gameObjDict; - private Dictionary gameObjLockDict; - public Dictionary GameObjLockDict => gameObjLockDict; + public Dictionary> GameObjDict { get; } + public Dictionary GameObjLockDict { get; } public readonly uint[,] protoGameMap; public uint[,] ProtoGameMap => protoGameMap; + + #region 大本营相关 + public List Homes { get; } + private readonly long currentHomeNum = 0; + public bool TeamExists(long teamID) + { + return teamID < currentHomeNum; + } + #endregion + + #region GetPlaceType public PlaceType GetPlaceType(IGameObj obj) { try @@ -38,21 +47,23 @@ public PlaceType GetPlaceType(XY pos) return PlaceType.Null; } } + #endregion + public bool IsOutOfBound(IGameObj obj) { return obj.Position.x >= GameData.MapLength - obj.Radius || obj.Position.x <= obj.Radius || obj.Position.y >= GameData.MapLength - obj.Radius || obj.Position.y <= obj.Radius; } public IOutOfBound GetOutOfBound(XY pos) { - return new Areas.OutOfBoundBlock(pos); + return new OutOfBoundBlock(pos); } public Ship? FindShipInID(long ID) { Ship? ship = null; - gameObjLockDict[GameObjType.Ship].EnterReadLock(); + GameObjLockDict[GameObjType.Ship].EnterReadLock(); try { - foreach (Ship s in gameObjDict[GameObjType.Ship]) + foreach (Ship s in GameObjDict[GameObjType.Ship].Cast()) { if (s.ID == ID) { @@ -63,17 +74,17 @@ public IOutOfBound GetOutOfBound(XY pos) } finally { - gameObjLockDict[GameObjType.Ship].ExitReadLock(); + GameObjLockDict[GameObjType.Ship].ExitReadLock(); } return ship; } public Ship? FindShipInShipID(long shipID) { Ship? ship = null; - gameObjLockDict[GameObjType.Ship].EnterReadLock(); + GameObjLockDict[GameObjType.Ship].EnterReadLock(); try { - foreach (Ship s in gameObjDict[GameObjType.Ship]) + foreach (Ship s in GameObjDict[GameObjType.Ship].Cast()) { if (s.ShipID == shipID) { @@ -84,7 +95,7 @@ public IOutOfBound GetOutOfBound(XY pos) } finally { - gameObjLockDict[GameObjType.Ship].ExitReadLock(); + GameObjLockDict[GameObjType.Ship].ExitReadLock(); } return ship; } @@ -94,7 +105,7 @@ public IOutOfBound GetOutOfBound(XY pos) GameObjLockDict[gameObjType].EnterReadLock(); try { - foreach (GameObj gameObj in GameObjDict[gameObjType]) + foreach (GameObj gameObj in GameObjDict[gameObjType].Cast()) { if (gameObjType == GameObjType.Wormhole) { @@ -135,7 +146,7 @@ public IOutOfBound GetOutOfBound(XY pos) GameObjLockDict[gameObjType].EnterReadLock(); try { - foreach (GameObj gameObj in GameObjDict[gameObjType]) + foreach (GameObj gameObj in GameObjDict[gameObjType].Cast()) { if (GameData.IsInTheSameCell(gameObj.Position, Pos)) { @@ -156,7 +167,7 @@ public IOutOfBound GetOutOfBound(XY pos) GameObjLockDict[gameObjType].EnterReadLock(); try { - foreach (GameObj gameObj in GameObjDict[gameObjType]) + foreach (GameObj gameObj in GameObjDict[gameObjType].Cast()) { if (GameData.PartInTheSameCell(gameObj.Position, Pos)) { @@ -177,7 +188,7 @@ public IOutOfBound GetOutOfBound(XY pos) GameObjLockDict[gameObjType].EnterReadLock(); try { - foreach (GameObj gameObj in GameObjDict[gameObjType]) + foreach (GameObj gameObj in GameObjDict[gameObjType].Cast()) { if (GameData.ApproachToInteractInACross(gameObj.Position, Pos)) { @@ -245,7 +256,7 @@ public bool Remove(GameObj gameObj) GameObjLockDict[gameObj.Type].EnterWriteLock(); try { - foreach (GameObj obj in GameObjDict[gameObj.Type]) + foreach (GameObj obj in GameObjDict[gameObj.Type].Cast()) { if (gameObj.ID == obj.ID) { @@ -296,14 +307,14 @@ public void Add(IGameObj gameObj) } public Map(uint[,] mapResource) { - gameObjDict = []; - gameObjLockDict = []; + GameObjDict = []; + GameObjLockDict = []; foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType))) { if (idx != GameObjType.Null) { - gameObjDict.Add(idx, new List()); - gameObjLockDict.Add(idx, new ReaderWriterLockSlim()); + GameObjDict.Add(idx, new List()); + GameObjLockDict.Add(idx, new ReaderWriterLockSlim()); } } protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)]; @@ -353,11 +364,13 @@ public Map(uint[,] mapResource) } break; case PlaceType.Home: - Add(new Home(GameData.GetCellCenterPos(i, j))); + // 在生成地图时就把Home和TeamID获取, 生成Team时绑定上去即可 + Add(new Home(GameData.GetCellCenterPos(i, j), currentHomeNum++)); break; } } } + Homes = GameObjDict[GameObjType.Home].Cast().ToList(); } } } \ No newline at end of file diff --git a/logic/GameClass/GameObj/Map/MapReader.cs b/logic/GameClass/GameObj/Map/MapReader.cs index cb475916..679778e3 100644 --- a/logic/GameClass/GameObj/Map/MapReader.cs +++ b/logic/GameClass/GameObj/Map/MapReader.cs @@ -5,17 +5,11 @@ namespace MapGenerator; /// /// ͼṹ /// -public struct MapStruct +public struct MapStruct(uint height, uint width, uint[,] map) { - public MapStruct(uint height, uint width, uint[,] map) - { - this.height = height; - this.width = width; - this.map = map; - } - public uint height; - public uint width; - public uint[,] map; + public uint height = height; + public uint width = width; + public uint[,] map = map; } public static class MapReader diff --git a/logic/GameClass/GameObj/Team.cs b/logic/GameClass/GameObj/Team.cs index 42fccb48..66470e9f 100644 --- a/logic/GameClass/GameObj/Team.cs +++ b/logic/GameClass/GameObj/Team.cs @@ -6,18 +6,15 @@ namespace GameClass.GameObj { - public class Team + public class Team(Home home) { - private static long currentMaxTeamID = 0; - public static long CurrentMaxTeamID => currentMaxTeamID; - private readonly long teamID; - public long TeamID => teamID; + public long TeamID { get; } = home.TeamID; public const long invalidTeamID = long.MaxValue; public const long noneTeamID = long.MinValue; - private readonly List shipList; - private readonly Dictionary birthPointList; + private readonly List shipList = new(GameData.MaxShipNum); + private readonly Dictionary birthPointList = []; public Dictionary BirthPointList => birthPointList; - private Home home; + private Home home = home; public AtomicLong Money { get; } = new AtomicLong(0); public AtomicLong Score { get; } = new AtomicLong(0); public Ship? GetShip(long shipID) @@ -119,19 +116,7 @@ public long[] GetShipIDs() } return shipIDs; } - public static bool TeamExists(long teamID) - { - return teamID < currentMaxTeamID; - } public void UpdateBirthPoint() { } - public Team(Home home) - { - this.teamID = currentMaxTeamID++; - this.shipList = new(GameData.MaxShipNum); - this.birthPointList = []; - this.home = home; - this.home.TeamID.SetReturnOri(teamID); - } } } diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index fb593007..1905401a 100644 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -22,7 +22,7 @@ public struct ShipInitInfo(long teamID, long playerID, uint birthPoint, ShipType public Map GameMap => gameMap; public long AddShip(ShipInitInfo shipInitInfo) { - if (!Team.TeamExists(shipInitInfo.teamID)) + if (!gameMap.TeamExists(shipInitInfo.teamID)) { return GameObj.invalidID; } @@ -118,13 +118,13 @@ public bool Attack(long shipID, double angle) } public long GetTeamMoney(long teamID) { - if (!Team.TeamExists(teamID)) + if (!gameMap.TeamExists(teamID)) return -1; return teamList[(int)teamID].Money; } public long GetTeamScore(long teamID) { - if (!Team.TeamExists(teamID)) + if (!gameMap.TeamExists(teamID)) return -1; return teamList[(int)teamID].Score; } diff --git a/logic/Preparation/Interface/IHome.cs b/logic/Preparation/Interface/IHome.cs index f97c0ce3..b21ec621 100644 --- a/logic/Preparation/Interface/IHome.cs +++ b/logic/Preparation/Interface/IHome.cs @@ -4,7 +4,7 @@ namespace Preparation.Interface { public interface IHome { - public AtomicLong TeamID { get; } + public long TeamID { get; } public LongInTheVariableRange HP { get; } } }