Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRR: Ship.cs & Gaming #41

Merged
merged 4 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ public void Add(IGameObj gameObj)
}
public Map(uint[,] mapResource)
{
gameObjDict = new Dictionary<GameObjType, IList<IGameObj>>();
gameObjLockDict = new Dictionary<GameObjType, ReaderWriterLockSlim>();
gameObjDict = [];
gameObjLockDict = [];
foreach (GameObjType idx in Enum.GetValues(typeof(GameObjType)))
{
if (idx != GameObjType.Null)
Expand All @@ -313,15 +313,15 @@ public Map(uint[,] mapResource)
for (int j = 0; j < GameData.MapCols; ++j)
{
bool hasWormhole = false;
switch (mapResource[i, j])
switch ((PlaceType)mapResource[i, j])
{
case (uint)PlaceType.Resource:
case PlaceType.Resource:
Add(new Resource(GameData.GetCellCenterPos(i, j)));
break;
case (uint)PlaceType.Construction:
case PlaceType.Construction:
Add(new Construction(GameData.GetCellCenterPos(i, j)));
break;
case (uint)PlaceType.Wormhole:
case PlaceType.Wormhole:
foreach (Wormhole wormhole in GameObjDict[GameObjType.Wormhole].Cast<Wormhole>())
{
if (wormhole.Grids.Contains(new XY(i, j)))
Expand All @@ -348,12 +348,11 @@ public Map(uint[,] mapResource)
}
if (!hasWormhole)
{
List<XY> grids = new();
grids.Add(new XY(i, j));
List<XY> grids = [new XY(i, j)];
Add(new Wormhole(GameData.GetCellCenterPos(i, j), grids));
}
break;
case (uint)PlaceType.Home:
case PlaceType.Home:
Add(new Home(GameData.GetCellCenterPos(i, j)));
break;
}
Expand Down
11 changes: 5 additions & 6 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
private ShipStateType shipState = ShipStateType.Null;
public ShipStateType ShipState => shipState;
public IOccupation Occupation { get; }
public IntNumUpdateEachCD BulletNum { get; }
/// <summary>
/// 子弹数上限, THUAI7为无穷
/// </summary>
public IntNumUpdateEachCD BulletNum => new(int.MaxValue, 1);
#region Producer
private ProducerType producerType = ProducerType.Null;
public ProducerType ProducerModuleType => producerType;
Expand Down Expand Up @@ -71,11 +74,7 @@ public override bool IgnoreCollideExecutor(IGameObj targetObj)
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))
);
XY res = Position + new XY(angle, Radius + GameData.BulletRadius);
Bullet? bullet = BulletFactory.GetBullet(this, res, weaponType);
if (bullet == null) return null;
FacingDirection = new XY(angle, bullet.AttackDistance);
Expand Down
3 changes: 2 additions & 1 deletion logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public void UpdateBirthPoint()
public Team(Home home)
{
this.teamID = currentMaxTeamID++;
this.shipList = new List<Ship>(GameData.MaxShipNum);
this.shipList = new(GameData.MaxShipNum);
this.birthPointList = [];
this.home = home;
this.home.TeamID.SetReturnOri(teamID);
}
Expand Down
41 changes: 17 additions & 24 deletions logic/Gaming/ActionManager.cs
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
using System;
using System.Threading;
using GameClass.GameObj;
using GameClass.GameObj;
using GameClass.GameObj.Areas;
using GameEngine;
using Preparation.Utility;
using System.Threading;
using Timothy.FrameRateTask;

namespace Gaming
{
public partial class Game
{
private readonly ActionManager actionManager;
private class ActionManager
private class ActionManager(Map gameMap, ShipManager shipManager)
{
private readonly Map gameMap;
private readonly ShipManager shipManager;
public readonly MoveEngine moveEngine;
public ActionManager(Map gameMap, ShipManager shipManager)
{
this.gameMap = gameMap;
this.shipManager = shipManager;
this.moveEngine = new MoveEngine(
private readonly Map gameMap = gameMap;
private readonly ShipManager shipManager = shipManager;
public readonly MoveEngine moveEngine = new(
gameMap: gameMap,
OnCollision: (obj, collisionObj, moveVec) =>
{
Expand All @@ -44,8 +38,7 @@ public ActionManager(Map gameMap, ShipManager shipManager)
obj.ThreadNum.Release();
}
);
this.shipManager = shipManager;
}

public bool MoveShip(Ship shipToMove, int moveTimeInMilliseconds, double moveDirection)
{
if (moveTimeInMilliseconds < 5)
Expand All @@ -59,18 +52,18 @@ public bool MoveShip(Ship shipToMove, int moveTimeInMilliseconds, double moveDir
}
new Thread
(
() =>
{
shipToMove.ThreadNum.WaitOne();
if (!shipToMove.StartThread(stateNum, RunningStateType.RunningActively))
() =>
{
shipToMove.ThreadNum.Release();
return;
shipToMove.ThreadNum.WaitOne();
if (!shipToMove.StartThread(stateNum, RunningStateType.RunningActively))
{
shipToMove.ThreadNum.Release();
return;
}
moveEngine.MoveObj(shipToMove, moveTimeInMilliseconds, moveDirection, shipToMove.StateNum);
Thread.Sleep(moveTimeInMilliseconds);
shipToMove.ResetShipState(stateNum);
}
moveEngine.MoveObj(shipToMove, moveTimeInMilliseconds, moveDirection, shipToMove.StateNum);
Thread.Sleep(moveTimeInMilliseconds);
shipToMove.ResetShipState(stateNum);
}
)
{ IsBackground = true }.Start();
return true;
Expand Down
34 changes: 10 additions & 24 deletions logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
using System;
using System.Threading;
using System.Collections.Generic;
using GameClass.GameObj;
using GameClass.GameObj;
using GameClass.GameObj.Bullets;
using Preparation.Utility;
using GameEngine;
using Preparation.Interface;
using Preparation.Utility;
using System.Threading;
using Timothy.FrameRateTask;

namespace Gaming
{
public partial class Game
{
private readonly AttackManager attackManager;
private class AttackManager
private class AttackManager(Map gameMap, ShipManager shipManager)
{
readonly Map gameMap;
public readonly MoveEngine moveEngine;
readonly ShipManager shipManager;
public AttackManager(Map gameMap, ShipManager shipManager)
{
this.gameMap = gameMap;
moveEngine = new MoveEngine(
private readonly Map gameMap = gameMap;
private readonly ShipManager shipManager = shipManager;
public readonly MoveEngine moveEngine = new(
gameMap: gameMap,
OnCollision: (obj, collisionObj, moveVec) =>
{
return MoveEngine.AfterCollision.Destroyed;
},
EndMove: obj =>
{
obj.CanMove.SetReturnOri(false);
}
OnCollision: (obj, collisionObj, moveVec) => MoveEngine.AfterCollision.Destroyed,
EndMove: obj => obj.CanMove.SetReturnOri(false)
);
this.shipManager = shipManager;
}

public void ProduceBulletNaturally(BulletType bulletType, Ship ship, double angle, XY pos)
{
// 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
Expand Down
38 changes: 17 additions & 21 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
using System;
using System.Threading;
using System.Collections.Generic;
using Preparation.Utility;
using Preparation.Interface;
using GameClass.GameObj;
using GameClass.GameObj;
using GameClass.GameObj.Areas;
using Preparation.Utility;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace Gaming
{
public partial class Game
{
public struct ShipInitInfo
public struct ShipInitInfo(long teamID, long playerID, uint birthPoint, ShipType shipType)
{
public long teamID;
public long playerID;
public uint birthPoint;
public ShipType shipType;
public ShipInitInfo(long teamID, long playerID, uint birthPoint, ShipType shipType)
{
this.teamID = teamID;
this.playerID = playerID;
this.birthPoint = birthPoint;
this.shipType = shipType;
}
public long teamID = teamID;
public long playerID = playerID;
public uint birthPoint = birthPoint;
public ShipType shipType = shipType;
}
private readonly List<Team> teamList;
public List<Team> TeamList => teamList;
Expand Down Expand Up @@ -138,9 +130,13 @@ public long GetTeamScore(long teamID)
}
public Game(uint[,] mapResource, int numOfTeam)
{
gameMap = new Map(mapResource);
teamList = new List<Team>();
foreach (GameObj gameObj in gameMap.GameObjDict[GameObjType.Home])
gameMap = new(mapResource);
shipManager = new(gameMap);
moduleManager = new();
actionManager = new(gameMap, shipManager);
attackManager = new(gameMap, shipManager);
teamList = [];
foreach (GameObj gameObj in gameMap.GameObjDict[GameObjType.Home].Cast<GameObj>())
{
if (gameObj.Type == GameObjType.Home)
{
Expand Down
6 changes: 3 additions & 3 deletions logic/Gaming/ModuleManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using Preparation.Utility;
using GameClass.GameObj;
using GameClass.GameObj;
using Preparation.Utility;

namespace Gaming
{
public partial class Game
{
private readonly ModuleManager moduleManager;
private partial class ModuleManager
private class ModuleManager
{
public bool PurchaseProducer(Ship ship, ProducerType producerType, int parameter)
{
Expand Down
12 changes: 3 additions & 9 deletions logic/Gaming/ShipManager.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
using System.Threading;
using GameClass.GameObj;
using GameClass.GameObj;
using Preparation.Utility;
using Preparation.Interface;

namespace Gaming
{
public partial class Game
{
private readonly ShipManager shipManager;
private partial class ShipManager
private class ShipManager(Map gameMap)
{
readonly Map gameMap;
readonly Map gameMap = gameMap;
public Ship? AddShip(XY pos, long teamID, long shipID, ShipType shipType)
{
Ship newShip = new(pos, GameData.ShipRadius, shipType);
Expand All @@ -19,10 +17,6 @@ private partial class ShipManager
newShip.ShipID.SetReturnOri(shipID);
return newShip;
}
public ShipManager(Map gameMap)
{
this.gameMap = gameMap;
}
}
}
}
Loading