Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ improvement on Areas #32

Merged
merged 12 commits into from
Nov 19, 2023
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Areas/AreaFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ public static class AreaFactory
{
public static Immovable GetArea(XY pos, PlaceType placeType) => placeType switch
{
PlaceType.Home => new Home(pos),
//PlaceType.Home => new Home(pos),
PlaceType.Ruin => new Ruin(pos),
PlaceType.Shadow => new Shadow(pos),
PlaceType.Asteroid => new Asteroid(pos),
PlaceType.Resource => new Resource(pos),
PlaceType.Construction => new Construction(pos),
PlaceType.Wormhole => new Wormhole(pos),
//PlaceType.Wormhole => new Wormhole(pos),
_ => new NullArea(pos)
};
public static OutOfBoundBlock GetOutOfBoundBlock(XY pos) => new(pos);
Expand Down
41 changes: 40 additions & 1 deletion logic/GameClass/GameObj/Areas/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,50 @@ namespace GameClass.GameObj.Areas;

public class Construction : Immovable
{
public LongInTheVariableRange HP => throw new NotImplementedException();
public LongInTheVariableRange HP { get; } = new LongInTheVariableRange(0);
public override bool IsRigid => constructionType == ConstructionType.Community;
public override ShapeType Shape => ShapeType.Square;
private ConstructionType constructionType = ConstructionType.Null;
public ConstructionType ConstructionType => constructionType;
public AtomicInt ConstructNum { get; } = new AtomicInt(0);
public bool Construct(int constructSpeed, ConstructionType constructionType, Ship ship)
{
if (constructionType == ConstructionType.Null)
{
return false;
}
if (this.constructionType != ConstructionType.Null && this.constructionType != constructionType && this.HP > 0)
{
return false;
}
if (this.constructionType == ConstructionType.Null || this.HP == 0)
{
this.constructionType = constructionType;
switch (constructionType)
{
case ConstructionType.Community:
HP.SetMaxV(GameData.CommunityHP);
break;
case ConstructionType.Factory:
HP.SetMaxV(GameData.FactoryHP);
break;
case ConstructionType.Fort:
HP.SetMaxV(GameData.FortHP);
break;
default:
break;
}
}
return HP.AddV(constructSpeed) > 0;
}
public void AddConstructNum(int add = 1)
{
ConstructNum.Add(add);
}
public void SubConstructNum(int sub = 1)
{
ConstructNum.Sub(sub);
}
public Construction(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Construction)
{
Expand Down
12 changes: 7 additions & 5 deletions logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ namespace GameClass.GameObj.Areas;

public class Home : Immovable, IHome
{
public AtomicLong TeamID => throw new NotImplementedException();
public LongInTheVariableRange HP => throw new NotImplementedException();
public long Score => throw new NotImplementedException();
private long teamID;
public long TeamID => teamID;
public LongInTheVariableRange HP => new LongInTheVariableRange(GameData.HomeHP);
public AtomicLong Score => new AtomicLong(0);
public override bool IsRigid => false;
public override ShapeType Shape => ShapeType.Square;
public void AddScore(long add)
{
throw new NotImplementedException();
Score.Add(add);
}
public Home(XY initPos)
public Home(XY initPos, long teamID)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Home)
{
this.teamID = teamID;
}
}
44 changes: 9 additions & 35 deletions logic/GameClass/GameObj/Areas/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,22 @@ namespace GameClass.GameObj.Areas;

public class Resource : Immovable
{
public LongInTheVariableRange HP => throw new NotImplementedException();
public LongInTheVariableRange HP { get; } = new LongInTheVariableRange(GameData.ResourceHP);
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
private int producingNum = 0;
public int ProducingNum
{
get => Interlocked.CompareExchange(ref producingNum, 0, 0);
}
public void AddProducingNum()
public AtomicInt ProduceNum { get; } = new AtomicInt(0);
public bool Produce(int produceSpeed, Ship ship)
{
Interlocked.Increment(ref producingNum);
// TODO: Add Money
return HP.SubV(produceSpeed) > 0;
}
public void SubProducingNum()
public void AddProduceNum(int add = 1)
{
Interlocked.Decrement(ref producingNum);
ProduceNum.Add(add);
}
public bool Produce(int produceSpeed, Ship ship)
public void SubProduceNum(int sub = 1)
{
long orgHP, value;
lock (gameObjLock)
{
if (HP == 0)
{
return false;
}
orgHP = HP.GetValue();
HP.SubV(produceSpeed);
if (HP > HP.GetMaxV())
{
HP.SetV(HP.GetMaxV());
}
else if (HP < 0)
{
HP.SetV(0);
}
value = HP.GetValue();
}
if (value < orgHP)
{
if (value == 0) return true;
}
return false;
ProduceNum.Sub(sub);
}
public Resource(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
Expand Down
9 changes: 5 additions & 4 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ namespace GameClass.GameObj.Areas;

public class Wormhole : Immovable, IWormhole
{
public LongInTheVariableRange HP => throw new NotImplementedException();
public List<XY> Entrance => throw new NotImplementedException();
public List<XY> Content => throw new NotImplementedException();
public LongInTheVariableRange HP = new LongInTheVariableRange(GameData.WormholeHP);
private List<XY> grids = new();
public List<XY> Grids => grids;
public override bool IsRigid => HP > GameData.WormholeHP / 2;
public override ShapeType Shape => ShapeType.Square;
public Wormhole(XY initPos)
public Wormhole(XY initPos, List<XY> grids)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Wormhole)
{
this.grids = grids;
}
}
49 changes: 48 additions & 1 deletion logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Preparation.Interface;
using Preparation.Utility;
using System;
using GameClass.GameObj.Areas;
using System.Linq;

namespace GameClass.GameObj
{
Expand Down Expand Up @@ -286,11 +288,56 @@ public Map(uint[,] mapResource)
}
protoGameMap = new uint[mapResource.GetLength(0), mapResource.GetLength(1)];
Array.Copy(mapResource, protoGameMap, mapResource.Length);
long teamID = 0;
for (int i = 0; i < GameData.MapRows; ++i)
{
for (int j = 0; j < GameData.MapCols; ++j)
{
Add(Areas.AreaFactory.GetArea(GameData.GetCellCenterPos(i, j), (PlaceType)mapResource[i, j]));
bool hasWormhole = false;
switch (mapResource[i, j])
{
case (uint)PlaceType.Resource:
Add(new Resource(GameData.GetCellCenterPos(i, j)));
break;
case (uint)PlaceType.Construction:
Add(new Construction(GameData.GetCellCenterPos(i, j)));
break;
case (uint)PlaceType.Wormhole:
foreach (Wormhole wormhole in GameObjDict[GameObjType.Wormhole].Cast<Wormhole>())
{
if (wormhole.Grids.Contains(new XY(i, j)))
{
hasWormhole = true;
break;
}
else
{
foreach (XY xy in wormhole.Grids)
{
if (Math.Abs(xy.x - i) <= 1 && Math.Abs(xy.y - j) <= 1)
{
wormhole.Grids.Add(new XY(i, j));
hasWormhole = true;
break;
}
}
if (hasWormhole)
{
break;
}
}
}
if (!hasWormhole)
{
List<XY> grids = new();
grids.Add(new XY(i, j));
Add(new Wormhole(GameData.GetCellCenterPos(i, j), grids));
}
break;
case (uint)PlaceType.Home:
Add(new Home(GameData.GetCellCenterPos(i, j), teamID++));
break;
}
}
}
}
Expand Down
22 changes: 7 additions & 15 deletions logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@ public class Team
private readonly Dictionary<uint, XY> birthPointList;
public Dictionary<uint, XY> BirthPointList => birthPointList;
private Home home;
private long score = 0;
public long Score
{
get => Interlocked.Read(ref score);
}
private long totalScore;
public long TotalScore
{
get => Interlocked.Read(ref totalScore);
}
public AtomicLong Money { get; } = new AtomicLong(0);
public AtomicLong Score { get; } = new AtomicLong(0);
public Ship? GetShip(long shipID)
{
foreach (Ship ship in shipList)
Expand Down Expand Up @@ -58,14 +50,14 @@ public bool AddShip(Ship ship)
shipList.Add(ship);
return true;
}
public void AddScore(long add)
public void AddMoney(long add)
{
Interlocked.Add(ref score, add);
Interlocked.Add(ref totalScore, add);
Money.Add(add);
Score.Add(add);
}
public void SubScore(long sub)
public void SubMoney(long sub)
{
Interlocked.Add(ref score, -sub);
Money.Sub(sub);
}
public void SetHome(Home home)
{
Expand Down
4 changes: 2 additions & 2 deletions logic/Gaming/ActionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public bool Produce(Ship ship)
ship.ThreadNum.Release();
return;
}
resource.AddProducingNum();
resource.AddProduceNum();
Thread.Sleep(GameData.CheckInterval);
new FrameRateTaskExecutor<int>
(
Expand All @@ -132,7 +132,7 @@ public bool Produce(Ship ship)
finallyReturn: () => 0
).Start();
ship.ThreadNum.Release();
resource.SubProducingNum();
resource.SubProduceNum();
}
)
{ IsBackground = true }.Start();
Expand Down
4 changes: 2 additions & 2 deletions logic/Preparation/Interface/IHome.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Preparation.Interface
{
public interface IHome
{
public AtomicLong TeamID { get; }
public long TeamID { get; }
public LongInTheVariableRange HP { get; }
public long Score { get; }
public AtomicLong Score { get; }
public void AddScore(long add);
}
}
3 changes: 1 addition & 2 deletions logic/Preparation/Interface/IWormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Preparation.Interface
{
public interface IWormhole : IGameObj
{
public List<XY> Entrance { get; }
public List<XY> Content { get; }
public List<XY> Grids { get; }
}
}
Loading