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

feat: ✨ Resource & Attack #20

Closed
wants to merge 2 commits into from
Closed
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
42 changes: 42 additions & 0 deletions logic/GameClass/GameObj/Areas/Resource.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,54 @@
using Preparation.Utility;
using System;
using System.Threading;

namespace GameClass.GameObj.Areas;

public class Resource : Immovable
{
public LongInTheVariableRange HP => throw new NotImplementedException();
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Square;
private int producingNum = 0;
public int ProducingNum
{
get => Interlocked.CompareExchange(ref producingNum, 0, 0);
}
public void AddProducingNum()
{
Interlocked.Increment(ref producingNum);
}
public void SubProducingNum()
{
Interlocked.Decrement(ref producingNum);
}
public bool Produce(int produceSpeed, Ship ship)
{
long orgHP, value;
lock (gameObjLock)
{
if (HP == 0)
{
return false;
}
orgHP = HP.GetValue();
HP.SubV(produceSpeed);
if (HP > HP.GetMaxV())
{
HP.SetV(HP.GetMaxV());
}
else if (HP < 0)
{
HP.SetV(0);
}
value = HP.GetValue();
}
if (value < orgHP)
{
if (value == 0) return true;
}
return false;
}
public Resource(XY initPos)
: base(initPos, GameData.NumOfPosGridPerCell / 2, GameObjType.Resource)
{
Expand Down
22 changes: 22 additions & 0 deletions logic/GameClass/GameObj/BombedBullet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Preparation.Utility;

namespace GameClass.GameObj
{
// 为方便界面组做子弹爆炸特效,现引入“爆炸中的子弹”,在每帧发送给界面组
public sealed class BombedBullet : Immovable
{
public override ShapeType Shape => ShapeType.Circle;
public override bool IsRigid => false;
public long MappingID { get; }
public readonly Bullet bulletHasBombed;
public readonly XY facingDirection;

public BombedBullet(Bullet bullet) :
base(bullet.Position, bullet.Radius, GameObjType.BombedBullet)
{
this.bulletHasBombed = bullet;
this.MappingID = bullet.ID;
this.facingDirection = bullet.FacingDirection;
}
}
}
21 changes: 20 additions & 1 deletion 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.Threading;

namespace GameClass.GameObj
{
Expand All @@ -16,7 +17,16 @@ public class Team
private readonly Dictionary<uint, XY> birthPointList;
public Dictionary<uint, XY> BirthPointList => birthPointList;
private Home home;
public int Score { get; private set; } = 0;
private long score = 0;
public long Score
{
get => Interlocked.Read(ref score);
}
private long totalScore;
public long TotalScore
{
get => Interlocked.Read(ref totalScore);
}
public Ship? GetShip(long shipID)
{
foreach (Ship ship in shipList)
Expand Down Expand Up @@ -48,6 +58,15 @@ public bool AddShip(Ship ship)
shipList.Add(ship);
return true;
}
public void AddScore(long add)
{
Interlocked.Add(ref score, add);
Interlocked.Add(ref totalScore, add);
}
public void SubScore(long sub)
{
Interlocked.Add(ref score, -sub);
}
public void SetHome(Home home)
{
this.home = home;
Expand Down
79 changes: 79 additions & 0 deletions logic/Gaming/ActionManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Threading;
using GameClass.GameObj;
using GameClass.GameObj.Areas;
using GameEngine;
using Preparation.Utility;
using Protobuf;
Expand All @@ -13,7 +14,39 @@ public partial class Game
private readonly ActionManager actionManager;
private class ActionManager
{
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(
gameMap: gameMap,
OnCollision: (obj, collisionObj, moveVec) =>
{
Ship ship = (Ship)obj;
switch (collisionObj.Type)
{
case GameObjType.Bullet:
if (((Bullet)collisionObj).Parent != ship)
{
// TODO
gameMap.Remove((GameObj)collisionObj);
}
break;
default:
break;
}
return MoveEngine.AfterCollision.MoveMax;
},
EndMove: obj =>
{
obj.ThreadNum.Release();
}
);
this.shipManager = shipManager;
}
public bool MoveShip(Ship shipToMove, int moveTimeInMilliseconds, double moveDirection)
{
if (moveTimeInMilliseconds < 5)
Expand Down Expand Up @@ -57,6 +90,52 @@ public static bool Stop(Ship ship)
}
public bool Produce(Ship ship)
{
Resource? resource = (Resource?)gameMap.OneForInteract(ship.Position, GameObjType.Resource);
if (resource == null)
{
return false;
}
if (resource.HP == 0)
{
return false;
}
long stateNum = ship.SetShipState(RunningStateType.Waiting, ShipStateType.Producing);
if (stateNum == -1)
{
return false;
}
new Thread
(
() =>
{
ship.ThreadNum.WaitOne();
if (!ship.StartThread(stateNum, RunningStateType.RunningActively))
{
ship.ThreadNum.Release();
return;
}
resource.AddProducingNum();
Thread.Sleep(GameData.CheckInterval);
new FrameRateTaskExecutor<int>
(
loopCondition: () => stateNum == ship.StateNum && gameMap.Timer.IsGaming,
loopToDo: () =>
{
if (resource.HP == 0)
{
ship.ResetShipState(stateNum);
return false;
}
return true;
},
timeInterval: GameData.CheckInterval,
finallyReturn: () => 0
).Start();
ship.ThreadNum.Release();
resource.SubProducingNum();
}
)
{ IsBackground = true }.Start();
return false;
}
public bool Construct(Ship ship)
Expand Down
58 changes: 57 additions & 1 deletion logic/Gaming/AttackManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,69 @@
using System.Threading;
using System.Collections.Generic;
using GameClass.GameObj;
using GameClass.GameObj.Bullets;
using Preparation.Utility;
using GameEngine;
using Preparation.Interface;
using Timothy.FrameRateTask;

namespace Gaming
{
internal class AttackManager
public partial class Game
{
private readonly AttackManager attackManager;
private class AttackManager
{
readonly Map gameMap;
public readonly MoveEngine moveEngine;
readonly ShipManager shipManager;
public AttackManager(Map gameMap, ShipManager shipManager)
{
this.gameMap = gameMap;
moveEngine = new MoveEngine(
gameMap: gameMap,
OnCollision: (obj, collisionObj, moveVec) =>
{
return MoveEngine.AfterCollision.Destroyed;
},
EndMove: obj =>
{
obj.CanMove.SetReturnOri(false);
}
);
this.shipManager = shipManager;
}
public void ProduceBulletNaturally(BulletType bulletType, Ship ship, double angle, XY pos)
{
// 子弹如果没有和其他物体碰撞,将会一直向前直到超出人物的attackRange
if (bulletType == BulletType.Null) return;
Bullet? bullet = BulletFactory.GetBullet(ship, pos, bulletType);
if (bullet == null) return;
gameMap.Add(bullet);
moveEngine.MoveObj(bullet, (int)(bullet.AttackDistance * 1000 / bullet.MoveSpeed), angle, ++bullet.StateNum); // 这里时间参数除出来的单位要是ms
}
public bool TryRemoveBullet(Bullet bullet)
{
if (gameMap.Remove(bullet))
{
bullet.CanMove.SetReturnOri(false);
if (bullet.BulletBombRange > 0)
{
BombedBullet bombedBullet = new(bullet);
gameMap.Add(bombedBullet);
new Thread
(() =>
{
Thread.Sleep(GameData.FrameDuration * 5);
gameMap.RemoveJustFromMap(bombedBullet);
}
)
{ IsBackground = true }.Start();
}
return true;
}
else return false;
}
}
}
}
15 changes: 7 additions & 8 deletions logic/Preparation/Utility/EnumType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,13 @@ public enum GunType
public enum PlaceType
{
Null = 0,
BirthPoint = 1,
Home = 2,
Ruin = 3,
Shadow = 4,
Asteroid = 5,
Resource = 6,
Construction = 7,
Wormhole = 8,
Ruin = 1,
Shadow = 2,
Asteroid = 3,
Resource = 4,
Construction = 5,
Wormhole = 6,
Home = 7,
}
/// <summary>
/// 采集器类型
Expand Down
Loading