Skip to content

Commit

Permalink
Merge pull request #24 from asdawej/dev
Browse files Browse the repository at this point in the history
Null Singleton & add interface methods
  • Loading branch information
shangfengh authored Nov 4, 2023
2 parents 4658048 + 8ef1a9c commit abfc0aa
Show file tree
Hide file tree
Showing 14 changed files with 136 additions and 82 deletions.
36 changes: 18 additions & 18 deletions logic/GameClass/GameObj/Modules/ModuleFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ public static class ModuleFactory
ProducerType.Producer1 => new CivilProducer1(),
ProducerType.Producer2 => new CivilProducer2(),
ProducerType.Producer3 => new CivilProducer3(),
_ => new NullProducer()
_ => NullProducer.Instance
},
ShipType.FlagShip => producerType switch
{
ProducerType.Producer1 => new FlagProducer1(),
_ => new NullProducer()
_ => NullProducer.Instance
},
_ => new NullProducer()
_ => NullProducer.Instance
};
public static IConstructor FindIConstructor(ShipType shipType, ConstructorType constructorType) => shipType switch
{
Expand All @@ -28,67 +28,67 @@ public static class ModuleFactory
ConstructorType.Constructor1 => new CivilConstructor1(),
ConstructorType.Constructor2 => new CivilConstructor2(),
ConstructorType.Constructor3 => new CivilConstructor3(),
_ => new NullConstructor()
_ => NullConstructor.Instance
},
ShipType.FlagShip => constructorType switch
{
ConstructorType.Constructor1 => new FlagConstructor1(),
_ => new NullConstructor()
_ => NullConstructor.Instance
},
_ => new NullConstructor()
_ => NullConstructor.Instance
};
public static IArmor FindIArmor(ShipType shipType, ArmorType armorType) => shipType switch
{
ShipType.CivilShip => armorType switch
{
ArmorType.Armor1 => new CivilArmor1(),
_ => new NullArmor()
_ => NullArmor.Instance
},
ShipType.WarShip => armorType switch
{
ArmorType.Armor1 => new WarArmor1(),
ArmorType.Armor2 => new WarArmor2(),
ArmorType.Armor3 => new WarArmor3(),
_ => new NullArmor()
_ => NullArmor.Instance
},
ShipType.FlagShip => armorType switch
{
ArmorType.Armor1 => new FlagArmor1(),
ArmorType.Armor2 => new FlagArmor2(),
ArmorType.Armor3 => new FlagArmor3(),
_ => new NullArmor()
_ => NullArmor.Instance
},
_ => new NullArmor()
_ => NullArmor.Instance
};
public static IShield FindIShield(ShipType shipType, ShieldType shieldType) => shipType switch
{
ShipType.CivilShip => shieldType switch
{
ShieldType.Shield1 => new CivilShield1(),
_ => new NullShield()
_ => NullShield.Instance
},
ShipType.WarShip => shieldType switch
{
ShieldType.Shield1 => new WarShield1(),
ShieldType.Shield2 => new WarShield2(),
ShieldType.Shield3 => new WarShield3(),
_ => new NullShield()
_ => NullShield.Instance
},
ShipType.FlagShip => shieldType switch
{
ShieldType.Shield1 => new FlagShield1(),
ShieldType.Shield2 => new FlagShield2(),
ShieldType.Shield3 => new FlagShield3(),
_ => new NullShield()
_ => NullShield.Instance
},
_ => new NullShield()
_ => NullShield.Instance
};
public static IWeapon FindIWeapon(ShipType shipType, WeaponType weaponType) => shipType switch
{
ShipType.CivilShip => weaponType switch
{
WeaponType.LaserGun => new CivilLaserGun(),
_ => new NullWeapon()
_ => NullWeapon.Instance
},
ShipType.WarShip => weaponType switch
{
Expand All @@ -97,7 +97,7 @@ public static class ModuleFactory
WeaponType.ShellGun => new WarShellGun(),
WeaponType.MissileGun => new WarMissileGun(),
WeaponType.ArcGun => new WarArcGun(),
_ => new NullWeapon()
_ => NullWeapon.Instance
},
ShipType.FlagShip => weaponType switch
{
Expand All @@ -106,8 +106,8 @@ public static class ModuleFactory
WeaponType.ShellGun => new FlagShellGun(),
WeaponType.MissileGun => new FlagMissileGun(),
WeaponType.ArcGun => new FlagArcGun(),
_ => new NullWeapon()
_ => NullWeapon.Instance
},
_ => new NullWeapon()
_ => NullWeapon.Instance
};
}
2 changes: 2 additions & 0 deletions logic/GameClass/GameObj/Modules/NullModules/NullArmor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace GameClass.GameObj.Modules;

public class NullArmor : IArmor
{
public static NullArmor Instance { get; } = new();
public int ArmorHP => 0;
public int Cost => 0;
private NullArmor() { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace GameClass.GameObj.Modules;

public class NullConstructor : IConstructor
{
public static NullConstructor Instance { get; } = new();
public int ConstructSpeed => 0;
public int Cost => 0;
private NullConstructor() { }
}
2 changes: 2 additions & 0 deletions logic/GameClass/GameObj/Modules/NullModules/NullProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace GameClass.GameObj.Modules;

public class NullProducer : IProducer
{
public static NullProducer Instance { get; } = new();
public int ProduceSpeed => 0;
public int Cost => 0;
private NullProducer() { }
}
2 changes: 2 additions & 0 deletions logic/GameClass/GameObj/Modules/NullModules/NullShield.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ namespace GameClass.GameObj.Modules;

public class NullShield : IShield
{
public static NullShield Instance { get; } = new();
public int ShieldHP => 0;
public int Cost => 0;
private NullShield() { }
}
2 changes: 2 additions & 0 deletions logic/GameClass/GameObj/Modules/NullModules/NullWeapon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ namespace GameClass.GameObj.Modules;

public class NullWeapon : IWeapon
{
public static NullWeapon Instance { get; } = new();
public BulletType BulletType => BulletType.Null;
public int Cost => 0;
private NullWeapon() { }
}
12 changes: 6 additions & 6 deletions logic/GameClass/GameObj/Occupations/CivilShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace GameClass.GameObj.Occupations;

public class CivilShip : IOccupation
{
private const int moveSpeed = GameData.CivilShipMoveSpeed;
public int MoveSpeed => moveSpeed;
private const int maxHp = GameData.CivilShipMaxHP;
public int MaxHp => maxHp;
private const int viewRange = GameData.CivilShipViewRange;
public int ViewRange => viewRange;
public int MoveSpeed { get; } = GameData.CivilShipMoveSpeed;
public int MaxHp { get; } = GameData.CivilShipMaxHP;
public int ViewRange { get; } = GameData.CivilShipViewRange;
public int Cost { get; } = GameData.CivilShipCost;
public int BaseArmor { get; } = GameData.CivilShipBaseArmor;
public int BaseShield { get; } = GameData.CivilShipBaseShield;
}
12 changes: 6 additions & 6 deletions logic/GameClass/GameObj/Occupations/FlagShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace GameClass.GameObj.Occupations;

public class FlagShip : IOccupation
{
private const int moveSpeed = GameData.FlagShipMoveSpeed;
public int MoveSpeed => moveSpeed;
private const int maxHp = GameData.FlagShipMaxHP;
public int MaxHp => maxHp;
private const int viewRange = GameData.FlagShipViewRange;
public int ViewRange => viewRange;
public int MoveSpeed { get; } = GameData.FlagShipMoveSpeed;
public int MaxHp { get; } = GameData.FlagShipMaxHP;
public int ViewRange { get; } = GameData.FlagShipViewRange;
public int Cost { get; } = GameData.FlagShipCost;
public int BaseArmor { get; } = GameData.FlagShipBaseArmor;
public int BaseShield { get; } = GameData.FlagShipBaseShield;
}
5 changes: 5 additions & 0 deletions logic/GameClass/GameObj/Occupations/NullOccupation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ namespace GameClass.GameObj.Occupations;

public class NullOccupation : IOccupation
{
public static NullOccupation Instance { get; } = new();
public int MoveSpeed => 0;
public int MaxHp => 0;
public int ViewRange => 0;
public int Cost => 0;
public int BaseArmor => 0;
public int BaseShield => 0;
private NullOccupation() { }
}
2 changes: 1 addition & 1 deletion logic/GameClass/GameObj/Occupations/OccupationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public static class OccupationFactory
ShipType.CivilShip => new CivilShip(),
ShipType.WarShip => new WarShip(),
ShipType.FlagShip => new FlagShip(),
_ => new NullOccupation()
_ => NullOccupation.Instance
};
}
12 changes: 6 additions & 6 deletions logic/GameClass/GameObj/Occupations/WarShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ namespace GameClass.GameObj.Occupations;

public class WarShip : IOccupation
{
private const int moveSpeed = GameData.WarShipMoveSpeed;
public int MoveSpeed => moveSpeed;
private const int maxHp = GameData.WarShipMaxHP;
public int MaxHp => maxHp;
private const int viewRange = GameData.WarShipViewRange;
public int ViewRange => viewRange;
public int MoveSpeed { get; } = GameData.WarShipMoveSpeed;
public int MaxHp { get; } = GameData.WarShipMaxHP;
public int ViewRange { get; } = GameData.WarShipViewRange;
public int Cost { get; } = GameData.WarShipCost;
public int BaseArmor { get; } = GameData.WarShipBaseArmor;
public int BaseShield { get; } = GameData.WarShipBaseShield;
}
123 changes: 78 additions & 45 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ namespace GameClass.GameObj;

public class Ship : Movable, IShip
{
public AtomicLong TeamID { get; } = new AtomicLong(long.MaxValue);
public AtomicLong ShipID { get; } = new AtomicLong(long.MaxValue);
public AtomicLong TeamID { get; } = new(long.MaxValue);
public AtomicLong ShipID { get; } = new(long.MaxValue);
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Circle;
private readonly int viewRange;
public int ViewRange => viewRange;
public int ViewRange { get; }
public override bool IgnoreCollideExecutor(IGameObj targetObj)
{
if (IsRemoved)
Expand All @@ -21,26 +20,51 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
return true;
return false;
}
// 属性值
public LongInTheVariableRange HP { get; }
public LongInTheVariableRange Armor { get; }
public LongInTheVariableRange Shield { get; }
private ShipType shipType = ShipType.Null;
public ShipType ShipType => shipType;
public ShipType ShipType { get; }
private ShipStateType shipState = ShipStateType.Null;
public ShipStateType ShipState => shipState;
private readonly IOccupation occupation;
public IOccupation Occupation => occupation;
public IOccupation Occupation { get; }
public IntNumUpdateEachCD BulletNum { get; }
private IProducer? producer = null;
public IProducer? ProducerModule => producer;
private IConstructor? constructor = null;
public IConstructor? ConstructorModule => constructor;
private IArmor? armor = null;
public IArmor? ArmorModule => armor;
private IShield? shield = null;
public IShield? ShieldModule => shield;
private IWeapon? weapon = null;
public IWeapon? WeaponModule => weapon;

#region Producer
private ProducerType producerType = ProducerType.Null;
public ProducerType ProducerModuleType => producerType;
private IProducer producer;
public IProducer ProducerModule => producer;
#endregion

#region Constructor
private ConstructorType constructorType = ConstructorType.Null;
public ConstructorType ConstructorModuleType => constructorType;
private IConstructor constructor;
public IConstructor ConstructorModule => constructor;
#endregion

#region Armor
private ArmorType armorType = ArmorType.Null;
public ArmorType ArmorModuleType => armorType;
private IArmor armor;
public IArmor ArmorModule;
#endregion

#region Shield
private ShieldType shieldType = ShieldType.Null;
public ShieldType ShieldModuleType => shieldType;
private IShield shield;
public IShield ShieldModule => shield;
#endregion

#region Weapon
private WeaponType weaponType = WeaponType.Null;
public WeaponType WeaponModuleType => weaponType;
private IWeapon weapon;
public IWeapon WeaponModule => weapon;
#endregion

private GameObj? whatInteractingWith = null;
public GameObj? WhatInteractingWith
{
Expand Down Expand Up @@ -172,34 +196,43 @@ public Ship(XY initPos, int initRadius, ShipType shipType) :
base(initPos, initRadius, GameObjType.Ship)
{
this.CanMove.SetReturnOri(true);
this.occupation = OccupationFactory.FindIOccupation(shipType);
this.viewRange = occupation.ViewRange;
this.HP = new(Occupation.MaxHp);
this.Occupation = OccupationFactory.FindIOccupation(this.ShipType = shipType);
this.ViewRange = this.Occupation.ViewRange;
this.HP = new(this.Occupation.MaxHp);
this.Armor = new(this.Occupation.BaseArmor);
this.Shield = new(this.Occupation.BaseShield);
this.MoveSpeed.SetReturnOri(this.orgMoveSpeed = Occupation.MoveSpeed);
this.shipType = shipType;
switch (shipType)
(this.producerType, this.constructorType, this.armorType, this.shieldType, this.weaponType) = this.ShipType switch
{
case ShipType.CivilShip:
this.producer = new CivilProducer1();
this.constructor = new CivilConstructor1();
this.armor = null;
this.shield = null;
this.weapon = null;
break;
case ShipType.WarShip:
this.producer = null;
this.constructor = null;
this.armor = null;
this.shield = null;
this.weapon = new WarLaserGun();
break;
case ShipType.FlagShip:
this.producer = null;
this.constructor = null;
this.armor = null;
this.shield = null;
this.weapon = new FlagLaserGun();
break;
}
ShipType.CivilShip => (
ProducerType.Producer1,
ConstructorType.Constructor1,
ArmorType.Null,
ShieldType.Null,
WeaponType.Null
),
ShipType.WarShip => (
ProducerType.Null,
ConstructorType.Null,
ArmorType.Null,
ShieldType.Null,
WeaponType.LaserGun
),
ShipType.FlagShip => (
ProducerType.Null,
ConstructorType.Null,
ArmorType.Null,
ShieldType.Null,
WeaponType.LaserGun
),
_ => (ProducerType.Null, ConstructorType.Null, ArmorType.Null, ShieldType.Null, WeaponType.Null)
};
(this.producer, this.constructor, this.armor, this.shield, this.weapon) = (
ModuleFactory.FindIProducer(this.ShipType, this.producerType),
ModuleFactory.FindIConstructor(this.ShipType, this.constructorType),
ModuleFactory.FindIArmor(this.ShipType, this.armorType),
ModuleFactory.FindIShield(this.ShipType, this.shieldType),
ModuleFactory.FindIWeapon(this.ShipType, this.weaponType)
);
}
}
Loading

0 comments on commit abfc0aa

Please sign in to comment.