Skip to content

Commit

Permalink
Merge pull request #32 from Panxuc/dev
Browse files Browse the repository at this point in the history
feat: ✨ improvement on Areas
  • Loading branch information
panxuc authored Nov 19, 2023
2 parents 5d50304 + f91e97d commit 08672c9
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 74 deletions.
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, GameData.CommunityHP);
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
22 changes: 18 additions & 4 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ 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 AtomicInt RepairNum { get; } = new AtomicInt(0);
public bool Repair(int constructSpeed, Ship ship)
{
return HP.AddV(constructSpeed) > 0;
}
public void AddRepairNum(int add = 1)
{
RepairNum.Add(add);
}
public void SubRepairNum(int sub = 1)
{
RepairNum.Sub(sub);
}
public Wormhole(XY initPos, List<XY> grids)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Wormhole)
{
this.grids = grids;
}
}
75 changes: 71 additions & 4 deletions 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 @@ -94,10 +96,30 @@ public IOutOfBound GetOutOfBound(XY pos)
{
foreach (GameObj gameObj in GameObjDict[gameObjType])
{
if (GameData.ApproachToInteract(gameObj.Position, Pos))
if (gameObjType == GameObjType.Wormhole)
{
GameObjForInteract = gameObj;
break;
bool flag = false;
foreach (XY xy in ((Wormhole)gameObj).Grids)
{
if (GameData.ApproachToInteract(xy, Pos))
{
GameObjForInteract = gameObj;
flag = true;
break;
}
}
if (flag)
{
break;
}
}
else
{
if (GameData.ApproachToInteract(gameObj.Position, Pos))
{
GameObjForInteract = gameObj;
break;
}
}
}
}
Expand Down Expand Up @@ -286,11 +308,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
3 changes: 3 additions & 0 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public IWeapon WeaponModule => weapon;
#endregion

public int ProduceSpeed => producer.ProduceSpeed;
public int ConstructSpeed => constructor.ConstructSpeed;

private GameObj? whatInteractingWith = null;
public GameObj? WhatInteractingWith
{
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
Loading

0 comments on commit 08672c9

Please sign in to comment.