Skip to content

Commit

Permalink
Merge pull request #127 from Panxuc/dev
Browse files Browse the repository at this point in the history
refactor: ✨ modify the way that adds player
  • Loading branch information
panxuc authored Mar 19, 2024
2 parents cac23a2 + 1f49ca9 commit 7d66318
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 139 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# .vs, .vscode must be ignored
.vs/
.vscode/
.idea/
6 changes: 3 additions & 3 deletions dependency/proto/Message2Clients.proto
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ message MessageOfTeam
{
int64 team_id = 1;
int64 player_id = 2;
int32 score = 3;
int32 energy = 4;
int64 guid=5;
int64 score = 3;
int64 energy = 4;
// int64 guid=5;
}

message MessageOfObj
Expand Down
7 changes: 7 additions & 0 deletions dependency/proto/Message2Server.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ message PlayerMsg
SweeperType sweeper_type = 3;
}

message ActivateMsg
{
int64 player_id = 1;
int64 team_id = 2;
SweeperType sweeper_type = 3;
}

message MoveMsg
{
int64 player_id = 1;
Expand Down
1 change: 1 addition & 0 deletions dependency/proto/Services.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ service AvailableService
rpc GetMap(NullRequest) returns (MessageOfMap);
// 游戏过程中玩家执行操作的服务
// 船
rpc Activate(ActivateMsg) returns (BoolRes); // 激活
rpc Move(MoveMsg) returns (MoveRes); // 移动
rpc Recover(RecoverMsg) returns (BoolRes); // 回复
rpc Produce(IDMsg) returns (BoolRes); // 开采
Expand Down
4 changes: 2 additions & 2 deletions logic/GameClass/GameObj/Map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public IOutOfBound GetOutOfBound(XY pos)
{
return (Ship?)GameObjDict[GameObjType.Ship].Find(gameObj => (ID == ((Ship)gameObj).ID));
}
public Ship? FindShipInShipID(long shipID)
public Ship? FindShipInPlayerID(long teamID, long playerID)
{
return (Ship?)GameObjDict[GameObjType.Ship].Find(gameObj => (shipID == ((Ship)gameObj).ShipID));
return (Ship?)GameObjDict[GameObjType.Ship].Find(gameObj => (teamID == ((Ship)gameObj).TeamID) && playerID == ((Ship)gameObj).PlayerID);
}

public static bool WormholeInteract(Wormhole gameObj, XY Pos)
Expand Down
9 changes: 5 additions & 4 deletions logic/GameClass/GameObj/Ship.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace GameClass.GameObj;
public class Ship : Movable, IShip
{
public AtomicLong TeamID { get; } = new(long.MaxValue);
public AtomicLong ShipID { get; } = new(long.MaxValue);
public AtomicLong PlayerID { get; } = new(long.MaxValue);
public override bool IsRigid => true;
public override ShapeType Shape => ShapeType.Circle;
public int ViewRange { get; }
Expand Down Expand Up @@ -382,10 +382,11 @@ public bool Commandable()
&& shipState != ShipStateType.Attacking);
}
}
public Ship(XY initPos, int initRadius, ShipType shipType, MoneyPool moneyPool) :
base(initPos, initRadius, GameObjType.Ship)
public Ship(int initRadius, ShipType shipType, MoneyPool moneyPool) :
base(GameData.PosNotInGame, initRadius, GameObjType.Ship)
{
this.CanMove.SetReturnOri(true);
this.CanMove.SetReturnOri(false);
this.IsRemoved.SetReturnOri(true);
this.Occupation = OccupationFactory.FindIOccupation(this.ShipType = shipType);
this.ViewRange = this.Occupation.ViewRange;
this.HP = new(this.Occupation.MaxHp);
Expand Down
74 changes: 66 additions & 8 deletions logic/GameClass/GameObj/Team.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Threading;
using Preparation.Interface;

namespace GameClass.GameObj
{
public class Team(Home home)
public class Team(Home home) : IPlayer
{
public long TeamID { get; } = home.TeamID;
public AtomicLong TeamID { get; } = new(home.TeamID);
public AtomicLong PlayerID { get; } = new(0);
public const long invalidTeamID = long.MaxValue;
public const long noneTeamID = long.MinValue;
private readonly List<Ship> shipList = new(GameData.MaxShipNum);
Expand All @@ -20,11 +22,28 @@ public class Team(Home home)
{
foreach (Ship ship in shipList)
{
if (ship.ShipID == shipID)
if (ship.PlayerID == shipID)
return ship;
}
return null;
}
public Ship? GetNewShip(ShipType shipType)
{
switch (shipType)
{
case ShipType.CivilShip:
var civilIndex = GetNewCivilShipIndex();
return civilIndex == -1 ? null : shipList[civilIndex];
case ShipType.WarShip:
var warIndex = GetNewWarShipIndex();
return warIndex == -1 ? null : shipList[warIndex];
case ShipType.FlagShip:
var flagIndex = GetNewFlagShipIndex();
return flagIndex == -1 ? null : shipList[flagIndex];
default:
return null;
}
}
public bool AddShip(Ship ship)
{
switch (ship.ShipType)
Expand Down Expand Up @@ -62,38 +81,77 @@ public void SetHome(Home home)
}
public int GetShipNum()
{
return shipList.Count;
int num = 0;
foreach (Ship ship in shipList)
{
if (!ship.IsRemoved)
num++;
}
return num;
}
public int GetCivilShipNum()
{
int num = 0;
foreach (Ship ship in shipList)
{
if (ship.ShipType == ShipType.CivilShip)
if (ship.ShipType == ShipType.CivilShip && !ship.IsRemoved)
num++;
}
return num;
}
public int GetNewCivilShipIndex()
{
var from = 1;
var to = 1 + GameData.MaxCivilShipNum;
for (int index = from; index < to; index++)
{
if (shipList[index].IsRemoved)
return index;
}
return -1;
}
public int GetWarShipNum()
{
int num = 0;
foreach (Ship ship in shipList)
{
if (ship.ShipType == ShipType.WarShip)
if (ship.ShipType == ShipType.WarShip && !ship.IsRemoved)
num++;
}
return num;
}
public int GetNewWarShipIndex()
{
var from = 1 + GameData.MaxCivilShipNum;
var to = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum;
for (int index = from; index < to; index++)
{
if (shipList[index].IsRemoved)
return index;
}
return -1;
}
public int GetFlagShipNum()
{
int num = 0;
foreach (Ship ship in shipList)
{
if (ship.ShipType == ShipType.FlagShip)
if (ship.ShipType == ShipType.FlagShip && !ship.IsRemoved)
num++;
}
return num;
}
public int GetNewFlagShipIndex()
{
var from = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum;
var to = 1 + GameData.MaxCivilShipNum + GameData.MaxWarShipNum + GameData.MaxFlagShipNum;
for (int index = from; index < to; index++)
{
if (shipList[index].IsRemoved)
return index;
}
return -1;
}
public void RemoveShip(Ship ship)
{
int i;
Expand All @@ -111,7 +169,7 @@ public long[] GetShipIDs()
int i = 0;
foreach (Ship ship in shipList)
{
shipIDs[i++] = ship.ShipID;
shipIDs[i++] = ship.PlayerID;
}
return shipIDs;
}
Expand Down
Loading

0 comments on commit 7d66318

Please sign in to comment.