From 6924e06fb8e1aaa63d09ada99400e0bc46a1ad15 Mon Sep 17 00:00:00 2001 From: Panxuc Date: Sat, 9 Dec 2023 14:58:12 +0800 Subject: [PATCH 1/3] feat: :sparkles: add game interface for various actions --- .../GameObj/Bullets/BulletFactory.cs | 9 +++ logic/GameClass/GameObj/Ship.cs | 24 +++++++- logic/GameClass/GameObj/Team.cs | 24 ++++++++ logic/Gaming/AttackManager.cs | 50 ++++++++++++++++ logic/Gaming/Game.cs | 57 +++++++++++++++++++ 5 files changed, 163 insertions(+), 1 deletion(-) diff --git a/logic/GameClass/GameObj/Bullets/BulletFactory.cs b/logic/GameClass/GameObj/Bullets/BulletFactory.cs index 2c11df48..53cf207e 100644 --- a/logic/GameClass/GameObj/Bullets/BulletFactory.cs +++ b/logic/GameClass/GameObj/Bullets/BulletFactory.cs @@ -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) + }; } \ No newline at end of file diff --git a/logic/GameClass/GameObj/Ship.cs b/logic/GameClass/GameObj/Ship.cs index 3c40e043..6222644c 100644 --- a/logic/GameClass/GameObj/Ship.cs +++ b/logic/GameClass/GameObj/Ship.cs @@ -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; @@ -65,6 +67,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; diff --git a/logic/GameClass/GameObj/Team.cs b/logic/GameClass/GameObj/Team.cs index 7d98d862..cbccc7cd 100644 --- a/logic/GameClass/GameObj/Team.cs +++ b/logic/GameClass/GameObj/Team.cs @@ -17,6 +17,30 @@ public class Team private readonly Dictionary birthPointList; public Dictionary BirthPointList => birthPointList; private Home home; + public long Money + { + get + { + long money = 0; + foreach (Ship ship in shipList) + { + money += ship.Money; + } + return money; + } + } + public long Score + { + get + { + long score = 0; + foreach (Ship ship in shipList) + { + score += ship.Score; + } + return score; + } + } public Ship? GetShip(long shipID) { foreach (Ship ship in shipList) diff --git a/logic/Gaming/AttackManager.cs b/logic/Gaming/AttackManager.cs index fab35347..2085c9dc 100644 --- a/logic/Gaming/AttackManager.cs +++ b/logic/Gaming/AttackManager.cs @@ -65,6 +65,56 @@ 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( + 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(); + } + } + } } } } diff --git a/logic/Gaming/Game.cs b/logic/Gaming/Game.cs index 0bbec139..860d6fd2 100644 --- a/logic/Gaming/Game.cs +++ b/logic/Gaming/Game.cs @@ -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) { gameMap = new Map(mapResource); From 021cb914b8c12dc9c0c2506bcc75aca609445e63 Mon Sep 17 00:00:00 2001 From: Panxuc Date: Sat, 9 Dec 2023 15:04:13 +0800 Subject: [PATCH 2/3] fix: :bug: fix attack return value --- logic/Gaming/AttackManager.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/logic/Gaming/AttackManager.cs b/logic/Gaming/AttackManager.cs index 2085c9dc..3acadc9a 100644 --- a/logic/Gaming/AttackManager.cs +++ b/logic/Gaming/AttackManager.cs @@ -114,6 +114,10 @@ public bool Attack(Ship ship, double angle) { IsBackground = true }.Start(); } } + if (bullet != null) + return true; + else + return false; } } } From 0413898cd4ebe754bc1f74692b5b222615ee848d Mon Sep 17 00:00:00 2001 From: Panxuc Date: Sat, 9 Dec 2023 17:25:08 +0800 Subject: [PATCH 3/3] feat: :sparkles: modify money & score --- logic/GameClass/GameObj/Ship.cs | 3 -- logic/GameClass/GameObj/Team.cs | 77 ++++----------------------------- 2 files changed, 8 insertions(+), 72 deletions(-) diff --git a/logic/GameClass/GameObj/Ship.cs b/logic/GameClass/GameObj/Ship.cs index 6222644c..ce8829cc 100644 --- a/logic/GameClass/GameObj/Ship.cs +++ b/logic/GameClass/GameObj/Ship.cs @@ -31,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; diff --git a/logic/GameClass/GameObj/Team.cs b/logic/GameClass/GameObj/Team.cs index cbccc7cd..76ff5052 100644 --- a/logic/GameClass/GameObj/Team.cs +++ b/logic/GameClass/GameObj/Team.cs @@ -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 @@ -17,30 +18,8 @@ public class Team private readonly Dictionary birthPointList; public Dictionary BirthPointList => birthPointList; private Home home; - public long Money - { - get - { - long money = 0; - foreach (Ship ship in shipList) - { - money += ship.Money; - } - return money; - } - } - public long Score - { - get - { - long score = 0; - foreach (Ship ship in shipList) - { - score += ship.Score; - } - return score; - } - } + public AtomicLong Money { get; } = new AtomicLong(0); + public AtomicLong Score { get; } = new AtomicLong(0); public Ship? GetShip(long shipID) { foreach (Ship ship in shipList) @@ -72,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) {