Skip to content

Commit

Permalink
Merge pull request #138 from Panxuc/dev
Browse files Browse the repository at this point in the history
refactor: ✨ modify the way that adds player (Part 2)
  • Loading branch information
asdawej authored Mar 23, 2024
2 parents c94fdaf + 13cc805 commit dad1d65
Show file tree
Hide file tree
Showing 12 changed files with 143 additions and 68 deletions.
2 changes: 2 additions & 0 deletions dependency/proto/Message2Server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@ message BuildSweeperMsg
{
SweeperType sweeper_type = 1;
int64 team_id = 2;
int64 player_id = 3;
int32 birthpoint_index = 4;
}
11 changes: 10 additions & 1 deletion logic/GameClass/GameObj/Areas/Construction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public bool Construct(int constructSpeed, ConstructionType constructionType, Shi
return false;
}
}
return HP.AddV(constructSpeed) > 0;
var addHP = HP.GetMaxV() - HP > constructSpeed ? constructSpeed : HP.GetMaxV() - HP;
if (ship.MoneyPool.Money < addHP / 10)
{
return false;
}
return ship.MoneyPool.SubMoney(HP.AddV(addHP) / 10) > 0;
}
public void BeAttacked(Bullet bullet)
{
Expand All @@ -63,6 +68,10 @@ public void BeAttacked(Bullet bullet)
long subHP = bullet.AP;
HP.SubPositiveV(subHP);
}
if (HP == 0)
{
constructionType = ConstructionType.Null;
}
}
public void AddConstructNum(int add = 1)
{
Expand Down
7 changes: 6 additions & 1 deletion logic/GameClass/GameObj/Areas/Wormhole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public class Wormhole(XY initPos, List<XY> grids)
public AtomicInt RepairNum { get; } = new AtomicInt(0);
public bool Repair(int constructSpeed, Ship ship)
{
return HP.AddV(constructSpeed) > 0;
var addHP = HP.GetMaxV() - HP > constructSpeed ? constructSpeed : HP.GetMaxV() - HP;
if (ship.MoneyPool.Money < addHP / 10)
{
return false;
}
return ship.MoneyPool.SubMoney(HP.AddV(addHP) / 10) > 0;
}
public void BeAttacked(Bullet bullet)
{
Expand Down
7 changes: 3 additions & 4 deletions logic/GameClass/GameObj/MoneyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ public MoneyPool()
}
public long AddMoney(long add)
{
Money.Add(add);
return add;
return Money.Add(add);
}
public void SubMoney(long sub)
public long SubMoney(long sub)
{
Money.Sub(sub);
return Money.Sub(sub);
}
}
33 changes: 28 additions & 5 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,37 @@ public GameObj? WhatInteractingWith
}
}
}
public void AddMoney(long add)
public long AddMoney(long add)
{
MoneyPool.Money.Add(add);
MoneyPool.Score.Add(add);
return MoneyPool.AddMoney(add);
}
public void SubMoney(long sub)
public long SubMoney(long sub)
{
MoneyPool.Money.Sub(sub);
return MoneyPool.SubMoney(sub);
}
public long GetCost()
{
var cost = 0;
switch (ShipType)
{
case ShipType.CivilShip:
cost += GameData.CivilShipCost;
break;
case ShipType.WarShip:
cost += GameData.WarShipCost;
break;
case ShipType.FlagShip:
cost += GameData.FlagShipCost;
break;
default:
return 0;
}
cost += producer.Cost;
cost += constructor.Cost;
cost += armor.Cost;
cost += shield.Cost;
cost += weapon.Cost;
return cost;
}
private long ChangeShipState(RunningStateType running, ShipStateType value = ShipStateType.Null, GameObj? gameObj = null)
{
Expand Down
11 changes: 0 additions & 11 deletions logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,16 +152,5 @@ public int GetNewFlagShipIndex()
}
return -1;
}

public long[] GetShipIDs()
{
long[] shipIDs = new long[shipList.Count];
int i = 0;
foreach (Ship ship in shipList)
{
shipIDs[i++] = ship.PlayerID;
}
return shipIDs;
}
}
}
15 changes: 12 additions & 3 deletions logic/Gaming/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ public long AddPlayer(PlayerInitInfo playerInitInfo)
return playerInitInfo.playerID;
}
}
public bool ActivateShip(long teamID, ShipType shipType, int birthPointIndex = 0)
public bool ActivateShip(long teamID, long playerID, ShipType shipType, int birthPointIndex = 0)
{
Ship? ship = teamList[(int)teamID].GetNewShip(shipType);
Ship? ship = teamList[(int)teamID].GetShip(playerID);
if (ship == null)
return false;
else if (ship.IsRemoved == false)
return false;
if (birthPointIndex < 0)
birthPointIndex = 0;
if (birthPointIndex >= teamList[(int)teamID].BirthPointList.Count)
Expand Down Expand Up @@ -109,7 +111,14 @@ public bool Construct(long teamID, long shipID, ConstructionType constructionTyp
return false;
Ship? ship = gameMap.FindShipInPlayerID(teamID, shipID);
if (ship != null)
return actionManager.Construct(ship, constructionType);
{
var flag = actionManager.Construct(ship, constructionType);
if (constructionType == ConstructionType.Community && flag)
{
UpdateBirthPoint();
}
return flag;
}
return false;
}
public bool InstallModule(long teamID, long shipID, ModuleType moduleType)
Expand Down
13 changes: 13 additions & 0 deletions logic/Gaming/ShipManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ private class ShipManager(Map gameMap)
}
public bool ActivateShip(Ship ship, XY pos)
{
var activateCost = ship.ShipType switch
{
ShipType.CivilShip => GameData.CivilShipCost,
ShipType.WarShip => GameData.WarShipCost,
ShipType.FlagShip => GameData.FlagShipCost,
_ => int.MaxValue
};
if (activateCost > ship.MoneyPool.Money)
{
return false;
}
if (ship.ShipState != ShipStateType.Deceased)
{
return false;
Expand Down Expand Up @@ -51,6 +62,8 @@ public void BeAttacked(Ship ship, Bullet bullet)
}
if (ship.HP == 0)
{
var money = ship.GetCost();
bullet.Parent.AddMoney(money);
Remove(ship);
}
}
Expand Down
2 changes: 1 addition & 1 deletion logic/Preparation/Interface/IMoneyPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public interface IMoneyPool
public AtomicLongOnlyAddScore Money { get; }
public AtomicLong Score { get; }
public long AddMoney(long add);
public void SubMoney(long sub);
public long SubMoney(long sub);
}
}
2 changes: 2 additions & 0 deletions logic/Preparation/Interface/IShip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public interface IShip : IMovable, IPlayer
public ShipType ShipType { get; }
public ShipStateType ShipState { get; }
public IntNumUpdateEachCD BulletNum { get; }
public long AddMoney(long add);
public long SubMoney(long sub);
public long SetShipState(RunningStateType running, ShipStateType value = ShipStateType.Null, IGameObj? obj = null);
public bool ResetShipState(long state, RunningStateType running = RunningStateType.Null, ShipStateType value = ShipStateType.Null, IGameObj? obj = null);
}
Expand Down
84 changes: 42 additions & 42 deletions logic/Preparation/Utility/GameData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static class GameData
public const int MapRows = 50; // 行数
public const int MapCols = 50; // 列数

public const int InitialMoney = 50; // 初始金钱
public const int InitialMoney = 500; // 初始金钱

public static bool IsGameObjMap(GameObjType gameObjType) => (uint)gameObjType > 3;
public static bool NeedCopy(GameObjType gameObjType)
Expand Down Expand Up @@ -105,73 +105,73 @@ public static bool ApproachToInteractInACross(XY pos1, XY pos2)
public const int ArcCastTime = 600; // 电弧前摇时间
public const int ArcSwingTime = 600; // 电弧后摇时间

public const int CivilShipCost = 40;
public const int CivilShipCost = 400;
public const int CivilShipMaxHP = 3000;
public const int CivilShipMoveSpeed = 3000;
public const int CivilShipViewRange = 8000;
public const int CivilShipBaseArmor = 0;
public const int CivilShipBaseShield = 0;
public const int CivilShipProducer1Cost = 0;
public const int CivilShipProducer2Cost = 40;
public const int CivilShipProducer3Cost = 80;
public const int CivilShipProducer2Cost = 400;
public const int CivilShipProducer3Cost = 800;
public const int CivilShipConstructor1Cost = 0;
public const int CivilShipConstructor2Cost = 40;
public const int CivilShipConstructor3Cost = 80;
public const int CivilShipArmor1Cost = 60;
public const int CivilShipShield1Cost = 60;
public const int CivilShipLaserGunCost = 100;
public const int CivilShipConstructor2Cost = 400;
public const int CivilShipConstructor3Cost = 800;
public const int CivilShipArmor1Cost = 600;
public const int CivilShipShield1Cost = 600;
public const int CivilShipLaserGunCost = 1000;

public const int WarShipCost = 120;
public const int WarShipCost = 1200;
public const int WarShipMaxHP = 4000;
public const int WarShipMoveSpeed = 2800;
public const int WarShipViewRange = 8000;
public const int WarShipBaseArmor = 400;
public const int WarShipBaseShield = 400;
public const int WarShipArmor1Cost = 60;
public const int WarShipArmor2Cost = 120;
public const int WarShipArmor3Cost = 180;
public const int WarShipShield1Cost = 60;
public const int WarShipShield2Cost = 120;
public const int WarShipShield3Cost = 180;
public const int WarShipArmor1Cost = 600;
public const int WarShipArmor2Cost = 1200;
public const int WarShipArmor3Cost = 1800;
public const int WarShipShield1Cost = 600;
public const int WarShipShield2Cost = 1200;
public const int WarShipShield3Cost = 1800;
public const int WarShipLaserGunCost = 0;
public const int WarShipPlasmaGunCost = 120;
public const int WarShipShellGunCost = 130;
public const int WarShipMissileGunCost = 180;
public const int WarShipArcGunCost = 240;
public const int WarShipPlasmaGunCost = 1200;
public const int WarShipShellGunCost = 1300;
public const int WarShipMissileGunCost = 1800;
public const int WarShipArcGunCost = 2400;

public const int FlagShipCost = 500;
public const int FlagShipCost = 5000;
public const int FlagShipMaxHP = 12000;
public const int FlagShipMoveSpeed = 2700;
public const int FlagShipViewRange = 8000;
public const int FlagShipBaseArmor = 800;
public const int FlagShipBaseShield = 800;
public const int FlagShipProducer1Cost = 40;
public const int FlagShipConstructor1Cost = 40;
public const int FlagShipArmor1Cost = 60;
public const int FlagShipArmor2Cost = 120;
public const int FlagShipArmor3Cost = 180;
public const int FlagShipShield1Cost = 60;
public const int FlagShipShield2Cost = 120;
public const int FlagShipShield3Cost = 180;
public const int FlagShipProducer1Cost = 400;
public const int FlagShipConstructor1Cost = 400;
public const int FlagShipArmor1Cost = 600;
public const int FlagShipArmor2Cost = 1200;
public const int FlagShipArmor3Cost = 1800;
public const int FlagShipShield1Cost = 600;
public const int FlagShipShield2Cost = 1200;
public const int FlagShipShield3Cost = 1800;
public const int FlagShipLaserGunCost = 0;
public const int FlagShipPlasmaGunCost = 120;
public const int FlagShipShellGunCost = 130;
public const int FlagShipMissileGunCost = 180;
public const int FlagShipArcGunCost = 240;

public const int ScoreHomePerSecond = 1;
public const int ScoreFactoryPerSecond = 3;
public const int ScoreProducer1PerSecond = 5;
public const int ScoreProducer2PerSecond = 7;
public const int ScoreProducer3PerSecond = 10;
public const int FlagShipPlasmaGunCost = 1200;
public const int FlagShipShellGunCost = 1300;
public const int FlagShipMissileGunCost = 1800;
public const int FlagShipArcGunCost = 2400;

public const int ScoreHomePerSecond = 20;
public const int ScoreFactoryPerSecond = 50;
public const int ScoreProducer1PerSecond = 20;
public const int ScoreProducer2PerSecond = 30;
public const int ScoreProducer3PerSecond = 40;
public const int ScoreConstructionDamaged = 200;
public static int ScoreShipKilled(int totalScore) => totalScore / 5;
public static int ScoreShipRecovered(int totalRecovery) => totalRecovery * 6 / 5;
public static int ScoreShipRecycled(int remainingHP) => remainingHP / 2;

public const int Constructor1Speed = 500;
public const int Constructor2Speed = 750;
public const int Constructor3Speed = 1000;
public const int Constructor1Speed = 300;
public const int Constructor2Speed = 400;
public const int Constructor3Speed = 500;
public const int Armor1 = 2000;
public const int Armor2 = 3000;
public const int Armor3 = 4000;
Expand Down
24 changes: 24 additions & 0 deletions logic/Server/RpcServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,30 @@ public override Task<BoolRes> Recycle(IDMsg request, ServerCallContext context)
return Task.FromResult(boolRes);
}

public override Task<BoolRes> BuildSweeper(BuildSweeperMsg request, ServerCallContext context)
{
#if DEBUG
Console.WriteLine($"TRY BuildSweeper: SweeperType {request.SweeperType} from Team {request.TeamId}");
#endif
BoolRes boolRes = new();

if (game.TeamList[(int)request.TeamId].GetShip(request.PlayerId) == null)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
else if (game.TeamList[(int)request.TeamId].GetShip(request.PlayerId).IsRemoved == false)
{
boolRes.ActSuccess = false;
return Task.FromResult(boolRes);
}
boolRes.ActSuccess = game.ActivateShip(request.TeamId, request.PlayerId, Transformation.ShipTypeFromProto(request.SweeperType), request.BirthpointIndex);
#if DEBUG
Console.WriteLine("END BuildSweeper");
#endif
return Task.FromResult(boolRes);
}

public override Task<BoolRes> EndAllAction(IDMsg request, ServerCallContext context)
{
#if DEBUG
Expand Down

0 comments on commit dad1d65

Please sign in to comment.