Skip to content

Commit

Permalink
Merge pull request #131 from asdawej/dev-xy
Browse files Browse the repository at this point in the history
Applying CellXY & replace out-dated code
  • Loading branch information
panxuc authored Mar 16, 2024
2 parents 672fbad + 85b3ab9 commit 4719cc1
Show file tree
Hide file tree
Showing 37 changed files with 498 additions and 549 deletions.
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/Asteroid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace GameClass.GameObj.Areas;

public class Asteroid : Immovable
public class Asteroid(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Asteroid)
{
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
public Asteroid(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Asteroid)
{
}
}
22 changes: 9 additions & 13 deletions logic/GameClass/GameObj/Areas/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@

namespace GameClass.GameObj.Areas;

public class Construction : Immovable
public class Construction(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Construction)
{
public AtomicLong TeamID { get; } = new(long.MaxValue);
public LongInTheVariableRange HP { get; } = new LongInTheVariableRange(0, GameData.CommunityHP);
public LongInTheVariableRange HP { get; } = new(0, GameData.CommunityHP);
public override bool IsRigid => constructionType == ConstructionType.Community;
public override ShapeType Shape => ShapeType.Square;
private ConstructionType constructionType = ConstructionType.Null;
Expand All @@ -18,13 +19,13 @@ public bool Construct(int constructSpeed, ConstructionType constructionType, Shi
{
return false;
}
if (this.constructionType != ConstructionType.Null && this.constructionType != constructionType && this.HP > 0)
if (this.constructionType != ConstructionType.Null && this.constructionType != constructionType && HP > 0)
{
return false;
}
if (this.constructionType == ConstructionType.Null || this.HP == 0)
if (this.constructionType == ConstructionType.Null || HP == 0)
{
this.TeamID.SetReturnOri(ship.TeamID);
TeamID.SetReturnOri(ship.TeamID);
this.constructionType = constructionType;
switch (constructionType)
{
Expand All @@ -45,12 +46,11 @@ public bool Construct(int constructSpeed, ConstructionType constructionType, Shi
}
public void BeAttacked(Bullet bullet)
{
if (bullet!.Parent!.TeamID == this.TeamID)
if (bullet!.Parent!.TeamID != TeamID)
{
return;
long subHP = bullet.AP;
HP.SubPositiveV(subHP);
}
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
}
public void AddConstructNum(int add = 1)
{
Expand All @@ -60,8 +60,4 @@ public void SubConstructNum(int sub = 1)
{
ConstructNum.Sub(sub);
}
public Construction(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Construction)
{
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public class Home(XY initPos, long id)

public void BeAttacked(Bullet bullet)
{
if (bullet!.Parent!.TeamID == this.TeamID)
{
return;
}
this.HP.SubPositiveV(bullet.AP);
if (bullet!.Parent!.TeamID != TeamID)
HP.SubPositiveV(bullet.AP);
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/NullArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@

namespace GameClass.GameObj.Areas;

public class NullArea : Immovable
public class NullArea(XY initPos)
: Immovable(initPos, int.MaxValue, GameObjType.Null)
{
public override bool IsRigid => false;
public override ShapeType Shape => ShapeType.Null;
public NullArea(XY initPos)
: base(initPos, int.MaxValue, GameObjType.Null)
{
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/OutOfBoundBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ namespace GameClass.GameObj.Areas;
/// <summary>
/// 逻辑墙
/// </summary>
public class OutOfBoundBlock : Immovable, IOutOfBound
public class OutOfBoundBlock(XY initPos)
: Immovable(initPos, int.MaxValue, GameObjType.OutOfBoundBlock), IOutOfBound
{
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
public OutOfBoundBlock(XY initPos)
: base(initPos, int.MaxValue, GameObjType.OutOfBoundBlock)
{
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/Resource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace GameClass.GameObj.Areas;

public class Resource : Immovable
public class Resource(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
{
public LongInTheVariableRange HP { get; } = new LongInTheVariableRange(GameData.ResourceHP);
public override bool IsRigid => true;
Expand All @@ -22,8 +23,4 @@ public void SubProduceNum(int sub = 1)
{
ProduceNum.Sub(sub);
}
public Resource(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
{
}
}
8 changes: 2 additions & 6 deletions logic/GameClass/GameObj/Areas/Ruin.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using Preparation.Utility;
using System;

namespace GameClass.GameObj.Areas;

public class Ruin : Immovable
public class Ruin(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Ruin)
{
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
public Ruin(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Ruin)
{
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Areas/Shadow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

namespace GameClass.GameObj.Areas;

public class Shadow : Immovable
public class Shadow(XY initPos)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Shadow)
{
public override bool IsRigid => false;
public override ShapeType Shape => ShapeType.Square;
public Shadow(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Shadow)
{
}
}
14 changes: 5 additions & 9 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@

namespace GameClass.GameObj.Areas;

public class Wormhole : Immovable, IWormhole
public class Wormhole(XY initPos, List<XY> grids)
: Immovable(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Wormhole), IWormhole
{
public LongInTheVariableRange HP = new LongInTheVariableRange(GameData.WormholeHP);
private readonly List<XY> grids = new();
public LongInTheVariableRange HP = new(GameData.WormholeHP);
private readonly List<XY> grids = grids;
public List<XY> Grids => grids;
public override bool IsRigid => HP > GameData.WormholeHP / 2;
public override ShapeType Shape => ShapeType.Square;
Expand All @@ -20,7 +21,7 @@ public bool Repair(int constructSpeed, Ship ship)
public void BeAttacked(Bullet bullet)
{
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
HP.SubPositiveV(subHP);
}
public void AddRepairNum(int add = 1)
{
Expand All @@ -30,9 +31,4 @@ 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;
}
}
17 changes: 5 additions & 12 deletions logic/GameClass/GameObj/BombedBullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
namespace GameClass.GameObj
{
// 为方便界面组做子弹爆炸特效,现引入“爆炸中的子弹”,在每帧发送给界面组
public sealed class BombedBullet : Immovable
public sealed class BombedBullet(Bullet bullet)
: Immovable(bullet.Position, bullet.Radius, GameObjType.BombedBullet)
{
public override ShapeType Shape => ShapeType.Circle;
public override bool IsRigid => false;
public long MappingID { get; }
public readonly Bullet bulletHasBombed;
public readonly XY facingDirection;

public BombedBullet(Bullet bullet) :
base(bullet.Position, bullet.Radius, GameObjType.BombedBullet)
{
this.bulletHasBombed = bullet;
this.MappingID = bullet.ID;
this.facingDirection = bullet.FacingDirection;
}
public long MappingID { get; } = bullet.ID;
public readonly Bullet bulletHasBombed = bullet;
public readonly XY facingDirection = bullet.FacingDirection;
}
}
6 changes: 3 additions & 3 deletions logic/GameClass/GameObj/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public Bullet(Ship ship, int radius, XY Position) :
base(Position, radius, GameObjType.Bullet)
{
this.CanMove.SetReturnOri(true);
this.MoveSpeed.SetReturnOri(this.Speed);
this.Parent = ship;
CanMove.SetReturnOri(true);
MoveSpeed.SetReturnOri(Speed);
Parent = ship;
}
}
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Bullets/Arc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public Arc(Ship ship, XY pos, int radius = GameData.BulletRadius) :
base(ship, radius, pos)
{
Random random = new();
this.AP.SetReturnOri(random.Next(GameData.ArcDamageMin, GameData.ArcDamageMax));
AP.SetReturnOri(random.Next(GameData.ArcDamageMin, GameData.ArcDamageMax));
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.ArcRange;
Expand Down
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Bullets/Laser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal sealed class Laser : Bullet
public Laser(Ship ship, XY pos, int radius = GameData.BulletRadius) :
base(ship, radius, pos)
{
this.AP.SetReturnOri(GameData.LaserDamage);
AP.SetReturnOri(GameData.LaserDamage);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.LaserRange;
Expand Down
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Bullets/Missile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed class Missile : Bullet
public Missile(Ship ship, XY pos, int radius = GameData.BulletRadius) :
base(ship, radius, pos)
{
this.AP.SetReturnOri(GameData.MissileDamage);
AP.SetReturnOri(GameData.MissileDamage);
}
public override double BulletBombRange => GameData.MissileBombRange;
public override double AttackDistance => GameData.MissileRange;
Expand Down
3 changes: 2 additions & 1 deletion logic/GameClass/GameObj/Bullets/NullBullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace GameClass.GameObj.Bullets;

internal sealed class NullBullet(Ship ship, XY Position, int radius = GameData.BulletRadius) : Bullet(ship, radius, Position)
internal sealed class NullBullet(Ship ship, XY Position, int radius = GameData.BulletRadius)
: Bullet(ship, radius, Position)
{
public override double BulletBombRange => 0;
public override double AttackDistance => 0;
Expand Down
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Bullets/Plasma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal sealed class Plasma : Bullet
public Plasma(Ship ship, XY pos, int radius = GameData.BulletRadius) :
base(ship, radius, pos)
{
this.AP.SetReturnOri(GameData.PlasmaDamage);
AP.SetReturnOri(GameData.PlasmaDamage);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.PlasmaRange;
Expand Down
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Bullets/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal sealed class Shell : Bullet
public Shell(Ship ship, XY pos, int radius = GameData.BulletRadius) :
base(ship, radius, pos)
{
this.AP.SetReturnOri(GameData.ShellDamage);
AP.SetReturnOri(GameData.ShellDamage);
}
public override double BulletBombRange => 0;
public override double AttackDistance => GameData.ShellRange;
Expand Down
21 changes: 7 additions & 14 deletions logic/GameClass/GameObj/GameObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,30 @@ namespace GameClass.GameObj
/// <summary>
/// 一切游戏元素的总基类,与THUAI4不同,继承IMoveable接口(出于一切物体其实都是可运动的指导思想)——LHR
/// </summary>
public abstract class GameObj : IGameObj
public abstract class GameObj(XY initPos, int initRadius, GameObjType initType) : IGameObj
{
private readonly ReaderWriterLockSlim gameObjReaderWriterLock = new();
public ReaderWriterLockSlim GameObjReaderWriterLock => gameObjReaderWriterLock;
protected readonly object gameObjLock = new();
public object GameLock => gameObjLock;
protected readonly XY birthPos;
private readonly GameObjType type;
protected readonly XY birthPos = initPos;
private readonly GameObjType type = initType;
public GameObjType Type => type;
private static long currentMaxID = 0; // 目前游戏对象的最大ID
public const long invalidID = long.MaxValue; // 无效的ID
public long ID { get; }
protected XY position;
public long ID { get; } = Interlocked.Increment(ref currentMaxID);
protected XY position = initPos;
public abstract XY Position { get; }
public abstract bool IsRigid { get; }
public abstract ShapeType Shape { get; }

private AtomicBool isRemoved = new(false);
private readonly AtomicBool isRemoved = new(false);
public AtomicBool IsRemoved { get => isRemoved; }
public virtual bool TryToRemove()
{
return IsRemoved.TrySet(true);
}
public int Radius { get; }
public int Radius { get; } = initRadius;
public virtual bool IgnoreCollideExecutor(IGameObj targetObj) => false;
public GameObj(XY initPos, int initRadius, GameObjType initType)
{
this.position = this.birthPos = initPos;
this.Radius = initRadius;
this.type = initType;
ID = Interlocked.Increment(ref currentMaxID);
}
}
}
7 changes: 2 additions & 5 deletions logic/GameClass/GameObj/Immovable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

namespace GameClass.GameObj;

public abstract class Immovable : GameObj
public abstract class Immovable(XY initPos, int initRadius, GameObjType initType)
: GameObj(initPos, initRadius, initType)
{
public override XY Position => position;
public Immovable(XY initPos, int initRadius, GameObjType initType)
: base(initPos, initRadius, initType)
{
}
}
Loading

0 comments on commit 4719cc1

Please sign in to comment.