Skip to content

Commit

Permalink
Merge pull request #49 from Panxuc/dev
Browse files Browse the repository at this point in the history
feat: ✨ realize attack
  • Loading branch information
asdawej authored Dec 17, 2023
2 parents 28af3f5 + 5a6ed16 commit fdcaa2c
Show file tree
Hide file tree
Showing 13 changed files with 203 additions and 99 deletions.
9 changes: 9 additions & 0 deletions logic/GameClass/GameObj/Areas/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public bool Construct(int constructSpeed, ConstructionType constructionType, Shi
}
return HP.AddV(constructSpeed) > 0;
}
public void BeAttacked(Bullet bullet)
{
if (bullet!.Parent!.TeamID == this.TeamID)
{
return;
}
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
}
public void AddConstructNum(int add = 1)
{
ConstructNum.Add(add);
Expand Down
10 changes: 10 additions & 0 deletions logic/GameClass/GameObj/Areas/Home.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,14 @@ public class Home(XY initPos, long id)
public LongInTheVariableRange HP => new(GameData.HomeHP);
public override bool IsRigid => false;
public override ShapeType Shape => ShapeType.Square;

public void BeAttacked(Bullet bullet)
{
if (bullet!.Parent!.TeamID == this.TeamID)
{
return;
}
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
}
}
5 changes: 5 additions & 0 deletions logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public bool Repair(int constructSpeed, Ship ship)
{
return HP.AddV(constructSpeed) > 0;
}
public void BeAttacked(Bullet bullet)
{
long subHP = bullet.AP;
this.HP.SubPositiveV(subHP);
}
public void AddRepairNum(int add = 1)
{
RepairNum.Add(add);
Expand Down
13 changes: 10 additions & 3 deletions logic/GameClass/GameObj/Bullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@ public abstract class Bullet : ObjOfShip
public abstract int Speed { get; }
public abstract int CastTime { get; }
public abstract int SwingTime { get; }
public abstract int CD { get; }
public abstract int MaxBulletNum { get; }
public abstract double ArmorModifier { get; }
public abstract double ShieldModifier { get; }
public override bool IsRigid => true; // 默认为true
public override ShapeType Shape => ShapeType.Circle; // 默认为圆形
public abstract BulletType TypeOfBullet { get; }
public abstract bool CanAttack(GameObj target);
public abstract bool CanBeBombed(GameObjType gameObjType);
public virtual bool CanBeBombed(GameObjType gameObjType) => gameObjType switch
{
GameObjType.Ship => true,
GameObjType.Construction => true,
GameObjType.Wormhole => true,
GameObjType.Home => true,
_ => false
};
public override bool IgnoreCollideExecutor(IGameObj targetObj)
{
if (targetObj == Parent) return true;
Expand Down
20 changes: 3 additions & 17 deletions logic/GameClass/GameObj/Bullets/Arc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,8 @@ public Arc(Ship ship, XY pos, int radius = GameData.BulletRadius) :
public override int Speed => GameData.ArcSpeed;
public override int CastTime => GameData.ArcCastTime;
public override int SwingTime => GameData.ArcSwingTime;
private const int cd = GameData.ArcSwingTime;
public override int CD => cd;
public const int maxBulletNum = 1;
public override int MaxBulletNum => maxBulletNum;
public override double ArmorModifier => GameData.ArcArmorModifier;
public override double ShieldModifier => GameData.ArcShieldModifier;
public override BulletType TypeOfBullet => BulletType.Arc;
public override bool CanAttack(GameObj target)
{
//if (target.Type == GameObjType.Ship
// || target.Type == GameObjType.Construction
// || target.Type == GameObjType.Wormhole
// || target.Type == GameObjType.Home)
// return true;
return false;
}
public override bool CanBeBombed(GameObjType gameObjType)
{
return false;
}
public override bool CanAttack(GameObj target) => false;
}
19 changes: 3 additions & 16 deletions logic/GameClass/GameObj/Bullets/Laser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,8 @@ public Laser(Ship ship, XY pos, int radius = GameData.BulletRadius) :
public override int Speed => GameData.LaserSpeed;
public override int CastTime => GameData.LaserCastTime;
public override int SwingTime => GameData.LaserSwingTime;
private const int cd = GameData.LaserSwingTime;
public override int CD => cd;
public override int MaxBulletNum => 1;
public override double ArmorModifier => GameData.LaserArmorModifier;
public override double ShieldModifier => GameData.LaserShieldModifier;
public override BulletType TypeOfBullet => BulletType.Laser;
public override bool CanAttack(GameObj target)
{
//if (target.Type == GameObjType.Ship
// || target.Type == GameObjType.Construction
// || target.Type == GameObjType.Wormhole
// || target.Type == GameObjType.Home)
// return true;
return false;
}
public override bool CanBeBombed(GameObjType gameObjType)
{
return false;
}
public override bool CanAttack(GameObj target) => false;
}
25 changes: 6 additions & 19 deletions logic/GameClass/GameObj/Bullets/Missile.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Preparation.Utility;
using System;
using Preparation.Utility;

namespace GameClass.GameObj.Bullets;

Expand All @@ -14,23 +15,9 @@ public Missile(Ship ship, XY pos, int radius = GameData.BulletRadius) :
public override int Speed => GameData.MissileSpeed;
public override int CastTime => GameData.MissileCastTime;
public override int SwingTime => GameData.ShellSwingTime;
private const int cd = GameData.ShellSwingTime;
public override int CD => cd;
public const int maxBulletNum = 1;
public override int MaxBulletNum => maxBulletNum;
public override double ArmorModifier => GameData.MissileArmorModifier;
public override double ShieldModifier => Double.MaxValue;
public override BulletType TypeOfBullet => BulletType.Missile;
public override bool CanAttack(GameObj target)
{
//if (target.Type == GameObjType.Ship
// || target.Type == GameObjType.Construction
// || target.Type == GameObjType.Wormhole
// || target.Type == GameObjType.Home)
// return true;
return false;
}
public override bool CanBeBombed(GameObjType gameObjType)
{
//return true;
return false;
}
public override bool CanAttack(GameObj target) =>
XY.DistanceFloor3(target.Position, this.Position) <= GameData.MissileBombRange;
}
10 changes: 3 additions & 7 deletions logic/GameClass/GameObj/Bullets/NullBullet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@

namespace GameClass.GameObj.Bullets;

internal sealed class NullBullet : Bullet
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;
public override int Speed => 0;
public override int CastTime => 0;
public override int SwingTime => 0;
public override int CD => 0;
public override int MaxBulletNum => 0;
public override double ArmorModifier => 0;
public override double ShieldModifier => 0;
public override BulletType TypeOfBullet => BulletType.Null;
public override bool CanAttack(GameObj target) => false;
public override bool CanBeBombed(GameObjType gameObjType) => false;
public NullBullet(Ship ship, XY Position, int radius = GameData.BulletRadius)
: base(ship, radius, Position)
{
}
}
20 changes: 3 additions & 17 deletions logic/GameClass/GameObj/Bullets/Plasma.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,8 @@ public Plasma(Ship ship, XY pos, int radius = GameData.BulletRadius) :
public override int Speed => GameData.PlasmaSpeed;
public override int CastTime => GameData.PlasmaCastTime;
public override int SwingTime => GameData.PlasmaSwingTime;
private const int cd = GameData.PlasmaSwingTime;
public override int CD => cd;
public const int maxBulletNum = 1;
public override int MaxBulletNum => maxBulletNum;
public override double ArmorModifier => GameData.PlasmaArmorModifier;
public override double ShieldModifier => GameData.PlasmaShieldModifier;
public override BulletType TypeOfBullet => BulletType.Plasma;
public override bool CanAttack(GameObj target)
{
//if (target.Type == GameObjType.Ship
// || target.Type == GameObjType.Construction
// || target.Type == GameObjType.Wormhole
// || target.Type == GameObjType.Home)
// return true;
return false;
}
public override bool CanBeBombed(GameObjType gameObjType)
{
return false;
}
public override bool CanAttack(GameObj target) => false;
}
20 changes: 3 additions & 17 deletions logic/GameClass/GameObj/Bullets/Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,8 @@ public Shell(Ship ship, XY pos, int radius = GameData.BulletRadius) :
public override int Speed => GameData.ShellSpeed;
public override int CastTime => GameData.ShellCastTime;
public override int SwingTime => GameData.ShellSwingTime;
private const int cd = GameData.ShellSwingTime;
public override int CD => cd;
public const int maxBulletNum = 1;
public override int MaxBulletNum => maxBulletNum;
public override double ArmorModifier => GameData.ShellArmorModifier;
public override double ShieldModifier => GameData.ShellShieldModifier;
public override BulletType TypeOfBullet => BulletType.Shell;
public override bool CanAttack(GameObj target)
{
//if (target.Type == GameObjType.Ship
// || target.Type == GameObjType.Construction
// || target.Type == GameObjType.Wormhole
// || target.Type == GameObjType.Home)
// return true;
return false;
}
public override bool CanBeBombed(GameObjType gameObjType)
{
return false;
}
public override bool CanAttack(GameObj target) => false;
}
76 changes: 74 additions & 2 deletions logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using GameClass.GameObj;
using System.Collections.Generic;
using System.Threading;
using GameClass.GameObj;
using GameClass.GameObj.Areas;
using GameClass.GameObj.Bullets;
using GameEngine;
using Preparation.Utility;
using System.Threading;
using Preparation.Interface;
using Timothy.FrameRateTask;

namespace Gaming
Expand All @@ -29,6 +32,26 @@ public void ProduceBulletNaturally(BulletType bulletType, Ship ship, double angl
gameMap.Add(bullet);
moveEngine.MoveObj(bullet, (int)(bullet.AttackDistance * 1000 / bullet.MoveSpeed), angle, ++bullet.StateNum); // 这里时间参数除出来的单位要是ms
}
private void BombObj(Bullet bullet, GameObj objBeingShot)
{
switch (objBeingShot.Type)
{
case GameObjType.Ship:
shipManager.BeAttacked((Ship)objBeingShot, bullet);
break;
case GameObjType.Construction:
((Construction)objBeingShot).BeAttacked(bullet);
break;
case GameObjType.Wormhole:
((Wormhole)objBeingShot).BeAttacked(bullet);
break;
case GameObjType.Home:
((Home)objBeingShot).BeAttacked(bullet);
break;
default:
break;
}
}
public bool TryRemoveBullet(Bullet bullet)
{
if (gameMap.Remove(bullet))
Expand All @@ -51,6 +74,55 @@ public bool TryRemoveBullet(Bullet bullet)
}
else return false;
}
private void BulletBomb(Bullet bullet, GameObj? objBeingShot)
{
if (!TryRemoveBullet(bullet))
{
return;
}
if (bullet.BulletBombRange == 0)
{
if (objBeingShot == null)
{
shipManager.BackSwing((Ship)bullet.Parent!, bullet.SwingTime);
return;
}
BombObj(bullet, objBeingShot);
shipManager.BackSwing((Ship)bullet.Parent!, bullet.SwingTime);
return;
}
else
{
var beAttackedList = new List<IGameObj>();
foreach (var kvp in gameMap.GameObjDict)
{
if (bullet.CanBeBombed(kvp.Key))
{
gameMap.GameObjLockDict[kvp.Key].EnterReadLock();
try
{
foreach (var item in gameMap.GameObjDict[kvp.Key])
{
if (bullet.CanAttack((GameObj)item))
{
beAttackedList.Add(item);
}
}
}
finally
{
gameMap.GameObjLockDict[kvp.Key].ExitReadLock();
}
}
}
foreach (GameObj beAttackedObj in beAttackedList)
{
BombObj(bullet, beAttackedObj);
}
beAttackedList.Clear();
shipManager.BackSwing((Ship)bullet.Parent!, bullet.SwingTime);
}
}
public bool Attack(Ship ship, double angle)
{
if (!ship.Commandable())
Expand Down
Loading

0 comments on commit fdcaa2c

Please sign in to comment.