Skip to content

Commit

Permalink
Merge pull request #42 from asdawej/Map-Constructor
Browse files Browse the repository at this point in the history
Revision for Home & Team
  • Loading branch information
panxuc authored Dec 13, 2023
2 parents cbb75a8 + ff1fba6 commit 0c2d4ec
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 63 deletions.
12 changes: 4 additions & 8 deletions logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}
}
55 changes: 34 additions & 21 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ namespace GameClass.GameObj
{
public partial class Map : IMap
{
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 Dictionary<GameObjType, IList<IGameObj>> GameObjDict { get; }
public Dictionary<GameObjType, ReaderWriterLockSlim> GameObjLockDict { get; }
public readonly uint[,] protoGameMap;
public uint[,] ProtoGameMap => protoGameMap;

#region 大本营相关
public List<Home> Homes { get; }
private readonly long currentHomeNum = 0;
public bool TeamExists(long teamID)
{
return teamID < currentHomeNum;
}
#endregion

#region GetPlaceType
public PlaceType GetPlaceType(IGameObj obj)
{
try
Expand All @@ -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<Ship>())
{
if (s.ID == ID)
{
Expand All @@ -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<Ship>())
{
if (s.ShipID == shipID)
{
Expand All @@ -84,7 +95,7 @@ public IOutOfBound GetOutOfBound(XY pos)
}
finally
{
gameObjLockDict[GameObjType.Ship].ExitReadLock();
GameObjLockDict[GameObjType.Ship].ExitReadLock();
}
return ship;
}
Expand All @@ -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<GameObj>())
{
if (gameObjType == GameObjType.Wormhole)
{
Expand Down Expand Up @@ -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<GameObj>())
{
if (GameData.IsInTheSameCell(gameObj.Position, Pos))
{
Expand All @@ -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<GameObj>())
{
if (GameData.PartInTheSameCell(gameObj.Position, Pos))
{
Expand All @@ -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<GameObj>())
{
if (GameData.ApproachToInteractInACross(gameObj.Position, Pos))
{
Expand Down Expand Up @@ -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<GameObj>())
{
if (gameObj.ID == obj.ID)
{
Expand Down Expand Up @@ -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<IGameObj>());
gameObjLockDict.Add(idx, new ReaderWriterLockSlim());
GameObjDict.Add(idx, new List<IGameObj>());
GameObjLockDict.Add(idx, new ReaderWriterLockSlim());
}
}
protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
Expand Down Expand Up @@ -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<Home>().ToList();
}
}
}
14 changes: 4 additions & 10 deletions logic/GameClass/GameObj/Map/MapReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ namespace MapGenerator;
/// <summary>
/// 地图结构体
/// </summary>
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
Expand Down
25 changes: 5 additions & 20 deletions logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ship> shipList;
private readonly Dictionary<uint, XY> birthPointList;
private readonly List<Ship> shipList = new(GameData.MaxShipNum);
private readonly Dictionary<uint, XY> birthPointList = [];
public Dictionary<uint, XY> 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)
Expand Down Expand Up @@ -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);
}
}
}
6 changes: 3 additions & 3 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Interface/IHome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Preparation.Interface
{
public interface IHome
{
public AtomicLong TeamID { get; }
public long TeamID { get; }
public LongInTheVariableRange HP { get; }
}
}

0 comments on commit 0c2d4ec

Please sign in to comment.