Skip to content

Commit

Permalink
Merge pull request #39 from Panxuc/dev
Browse files Browse the repository at this point in the history
feat: ✨ add some interfaces in game
  • Loading branch information
asdawej authored Dec 9, 2023
2 parents 2314c0b + 92cce32 commit 91bd54b
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 49 deletions.
9 changes: 9 additions & 0 deletions logic/GameClass/GameObj/Bullets/BulletFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@ public static class BulletFactory
BulletType.Arc => new Arc(ship, pos),
_ => new NullBullet(ship, pos)
};
public static Bullet? GetBullet(Ship ship, XY pos, WeaponType weaponType) => weaponType switch
{
WeaponType.LaserGun => new Laser(ship, pos),
WeaponType.PlasmaGun => new Plasma(ship, pos),
WeaponType.ShellGun => new Shell(ship, pos),
WeaponType.MissileGun => new Missile(ship, pos),
WeaponType.ArcGun => new Arc(ship, pos),
_ => new NullBullet(ship, pos)
};
}
27 changes: 23 additions & 4 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Preparation.Interface;
using System;
using GameClass.GameObj.Bullets;
using Preparation.Interface;
using Preparation.Utility;
using GameClass.GameObj.Modules;
using GameClass.GameObj.Occupations;
Expand Down Expand Up @@ -29,9 +31,6 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public ShipStateType ShipState => shipState;
public IOccupation Occupation { get; }
public IntNumUpdateEachCD BulletNum { get; }
public AtomicLong Money { get; } = new(0);
public AtomicLong Score { get; } = new(0);

#region Producer
private ProducerType producerType = ProducerType.Null;
public ProducerType ProducerModuleType => producerType;
Expand Down Expand Up @@ -65,6 +64,26 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
public WeaponType WeaponModuleType => weaponType;
private IWeapon weapon;
public IWeapon WeaponModule => weapon;
public Bullet? Attack(double angle)
{
lock (actionLock)
{
if (weaponType == WeaponType.Null) return null;
if (BulletNum.TrySub(1) == 1)
{
XY res = Position + new XY
(
(int)(Math.Abs((Radius + GameData.BulletRadius) * Math.Cos(angle))) * Math.Sign(Math.Cos(angle)),
(int)(Math.Abs((Radius + GameData.BulletRadius) * Math.Sin(angle))) * Math.Sign(Math.Sin(angle))
);
Bullet? bullet = BulletFactory.GetBullet(this, res, weaponType);
if (bullet == null) return null;
FacingDirection = new XY(angle, bullet.AttackDistance);
return bullet;
}
return null;
}
}
#endregion

public int ProduceSpeed => producer.ProduceSpeed;
Expand Down
53 changes: 8 additions & 45 deletions logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using GameClass.GameObj.Areas;
using Preparation.Utility;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Threading;

namespace GameClass.GameObj
Expand All @@ -17,6 +18,8 @@ public class Team
private readonly Dictionary<uint, XY> birthPointList;

Check warning on line 18 in logic/GameClass/GameObj/Team.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Field 'Team.birthPointList' is never assigned to, and will always have its default value null
public Dictionary<uint, XY> BirthPointList => birthPointList;
private Home home;
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 @@ -48,54 +51,14 @@ public bool AddShip(Ship ship)
//shipList.Add(ship);
return true;
}
public bool AddMoney(long shipID, long add)
public void AddMoney(long add)
{
foreach (Ship ship in shipList)
{
if (ship.ShipID == shipID)
{
ship.Money.Add(add);
ship.Score.Add(add);
return true;
}
}
return false;
Money.Add(add);
Score.Add(add);
}
public bool SubMoney(long shipID, long sub)
public void SubMoney(long sub)
{
foreach (Ship ship in shipList)
{
if (ship.ShipID == shipID && ship.Money >= sub)
{
ship.Money.Sub(sub);
return true;
}
}
return false;
}

public bool MoveMoney(long srcShipID, long dstShipID, long move)
{
Ship? srcShip = null;
Ship? dstShip = null;
foreach (Ship ship in shipList)
{
if (ship.ShipID == srcShipID)
{
srcShip = ship;
}
if (ship.ShipID == dstShipID)
{
dstShip = ship;
}
}
if (srcShip != null && dstShip != null && srcShip.Money >= move)
{
srcShip.Money.Sub(move);
dstShip.Money.Add(move);
return true;
}
return false;
Money.Sub(sub);
}
public void SetHome(Home home)
{
Expand Down
54 changes: 54 additions & 0 deletions logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,60 @@ public bool TryRemoveBullet(Bullet bullet)
}
else return false;
}
public bool Attack(Ship ship, double angle)
{
if (!ship.Commandable())
{
return false;
}
Bullet? bullet = ship.Attack(angle);
if (bullet != null)
{
gameMap.Add(bullet);
moveEngine.MoveObj(bullet, (int)(bullet.AttackDistance * 1000 / bullet.MoveSpeed), angle, ++bullet.StateNum); // 这里时间参数除出来的单位要是ms
if (bullet.CastTime > 0)
{
long stateNum = ship.SetShipState(RunningStateType.Waiting, ShipStateType.Attacking);
if (stateNum == -1)
{
TryRemoveBullet(bullet);
return false;
}
new Thread
(() =>
{
ship.ThreadNum.WaitOne();
if (!ship.StartThread(stateNum, RunningStateType.RunningActively))
{
TryRemoveBullet(bullet);
ship.ThreadNum.Release();
return;
}
new FrameRateTaskExecutor<int>(
loopCondition: () => stateNum == ship.StateNum && gameMap.Timer.IsGaming,
loopToDo: () => { },
timeInterval: GameData.CheckInterval,
finallyReturn: () => 0,
maxTotalDuration: bullet.CastTime
).Start();
ship.ThreadNum.Release();
if (gameMap.Timer.IsGaming)
{
if (!ship.ResetShipState(stateNum))
{
TryRemoveBullet(bullet);
}
}
}
)
{ IsBackground = true }.Start();
}
}
if (bullet != null)
return true;
else
return false;
}
}
}
}
57 changes: 57 additions & 0 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,63 @@ public bool MoveShip(long shipID, int moveTimeInMilliseconds, double angle)
return false;
}
}
public bool Produce(long shipID)
{
if (!gameMap.Timer.IsGaming)
return false;
Ship? ship = gameMap.FindShipInShipID(shipID);
if (ship != null)
return actionManager.Produce(ship);
return false;
}
public bool Construct(long shipID, ConstructionType constructionType)
{
if (!gameMap.Timer.IsGaming)
return false;
Ship? ship = gameMap.FindShipInShipID(shipID);
if (ship != null)
return actionManager.Construct(ship, constructionType);
return false;
}
public bool Repair(long ShipID)
{
if (!gameMap.Timer.IsGaming)
return false;
Ship? ship = gameMap.FindShipInShipID(ShipID);
if (ship != null)
return actionManager.Repair(ship);
return false;
}
public bool Stop(long shipID)
{
if (!gameMap.Timer.IsGaming)
return false;
Ship? ship = gameMap.FindShipInShipID(shipID);
if (ship != null)
return ActionManager.Stop(ship);
return false;
}
public bool Attack(long shipID, double angle)
{
if (!gameMap.Timer.IsGaming)
return false;
Ship? ship = gameMap.FindShipInShipID(shipID);
if (ship != null)
return attackManager.Attack(ship, angle);
return false;
}
public long GetTeamMoney(long teamID)
{
if (!Team.TeamExists(teamID))
return -1;
return teamList[(int)teamID].Money;
}
public long GetTeamScore(long teamID)
{
if (!Team.TeamExists(teamID))
return -1;
return teamList[(int)teamID].Score;
}
public Game(uint[,] mapResource, int numOfTeam)

Check warning on line 139 in logic/Gaming/Game.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Non-nullable field 'actionManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 139 in logic/Gaming/Game.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Non-nullable field 'attackManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 139 in logic/Gaming/Game.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Non-nullable field 'moduleManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

Check warning on line 139 in logic/Gaming/Game.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-logic

Non-nullable field 'shipManager' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
{
gameMap = new Map(mapResource);
Expand Down

0 comments on commit 91bd54b

Please sign in to comment.