Browse Source

(WIP) Netcode Part 4: compiles!

master
laurids 1 year ago
parent
commit
253f3eaec7
21 changed files with 578 additions and 602 deletions
  1. +32
    -27
      Assets/GWConquest/Scripts/Battle.cs
  2. +11
    -8
      Assets/GWConquest/Scripts/BattleFlank.cs
  3. +12
    -12
      Assets/GWConquest/Scripts/BattleLog.cs
  4. +22
    -19
      Assets/GWConquest/Scripts/District.cs
  5. +14
    -10
      Assets/GWConquest/Scripts/DistrictFactory.cs
  6. +253
    -219
      Assets/GWConquest/Scripts/Formation.cs
  7. +163
    -0
      Assets/GWConquest/Scripts/GWNetworkManager.cs
  8. +1
    -1
      Assets/GWConquest/Scripts/GWNetworkManager.cs.meta
  9. +0
    -69
      Assets/GWConquest/Scripts/GlobalCallbacks.cs
  10. +5
    -6
      Assets/GWConquest/Scripts/Player.cs
  11. +0
    -124
      Assets/GWConquest/Scripts/ServerCallbacks.cs
  12. +0
    -11
      Assets/GWConquest/Scripts/ServerCallbacks.cs.meta
  13. +1
    -1
      Assets/GWConquest/Scripts/SpawnAIUnits.cs
  14. +2
    -2
      Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs
  15. +1
    -1
      Assets/GWConquest/Scripts/UI/BattleFlankUI.cs
  16. +1
    -1
      Assets/GWConquest/Scripts/UI/BattleUI.cs
  17. +1
    -1
      Assets/GWConquest/Scripts/UI/DebugUI.cs
  18. +1
    -1
      Assets/GWConquest/Scripts/UI/ItemMoveTooltip.cs
  19. +1
    -14
      Assets/GWConquest/Scripts/UI/ProductionMenu.cs
  20. +1
    -1
      Assets/GWConquest/Scripts/UI/TransportUIUnitIcon.cs
  21. +56
    -74
      Assets/GWConquest/Scripts/Unit.cs

+ 32
- 27
Assets/GWConquest/Scripts/Battle.cs View File

@ -223,40 +223,43 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsInPreparing)
if(IsServer)
{ {
PreparingCooldown -= BoltNetwork.FrameDeltaTime;
if(PreparingCooldown <= 0)
if(IsInPreparing)
{ {
PreparingCooldown = 0;
PreparingCooldown -= BoltNetwork.FrameDeltaTime;
if(PreparingCooldown <= 0)
{
PreparingCooldown = 0;
}
} }
}
else if(!IsOver)
{
var gm = GameManager.Instance;
else if(!IsOver)
{
var gm = GameManager.Instance;
var players = AllPlayers;
var playerCount = players.Count();
var players = AllPlayers;
var playerCount = players.Count();
if(playerCount <= 1)
{
if(playerCount <= 0)
if(playerCount <= 1)
{ {
BoltLog.Info("No players in Battle {0}!", this);
}
else {
var finalPlayer = players.First();
BoltLog.Info("Player {0} is only one left in Battle {1}!", finalPlayer, this);
if(playerCount <= 0)
{
BoltLog.Info("No players in Battle {0}!", this);
}
else {
var finalPlayer = players.First();
BoltLog.Info("Player {0} is only one left in Battle {1}!", finalPlayer, this);
}
Zone.CurrentBattle = null;
SetBattleOver();
//Destroy(gameObject);
} }
Zone.CurrentBattle = null;
SetBattleOver();
//Destroy(gameObject);
SimulateTurn();
} }
SimulateTurn();
} }
@ -664,7 +667,8 @@ namespace GWConquest
} }
} }
public void MoveUnitToFlankRpc(NetworkBehaviourReference unit, NetworkBehaviourReference flank, int flankIndex)
[ServerRpc]
public void MoveUnitToFlankServerRpc(NetworkBehaviourReference unit, NetworkBehaviourReference flank, int flankIndex)
{ {
MoveUnitToFlank(unit.GetBehaviour<Unit>(), flank.GetBehaviour<BattleFlank>(), flankIndex); MoveUnitToFlank(unit.GetBehaviour<Unit>(), flank.GetBehaviour<BattleFlank>(), flankIndex);
} }
@ -676,7 +680,8 @@ namespace GWConquest
unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement); unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement);
} }
public void MoveUnitToReserveRpc(NetworkBehaviourReference unit)
[ServerRpc]
public void MoveUnitToReserveServerRpc(NetworkBehaviourReference unit)
{ {
MoveUnitToReserve(unit.GetBehaviour<Unit>()); MoveUnitToReserve(unit.GetBehaviour<Unit>());
} }


+ 11
- 8
Assets/GWConquest/Scripts/BattleFlank.cs View File

@ -124,18 +124,21 @@ namespace GWConquest {
public void FixedUpdate() public void FixedUpdate()
{ {
for(int i = 0; i < MaxUnitCount; i++)
if(IsServer)
{ {
if(GetDeathCooldown(i) > 0)
for(int i = 0; i < MaxUnitCount; i++)
{ {
deathCooldowns[i] -= BoltNetwork.FrameDeltaTime;
if(GetDeathCooldown(i) <= 0)
if(GetDeathCooldown(i) > 0)
{ {
var unit = GetUnit(i);
if(unit != null)
deathCooldowns[i] -= BoltNetwork.FrameDeltaTime;
if(GetDeathCooldown(i) <= 0)
{ {
unit.CurrentFlank = null;
SetUnit(i, null);
var unit = GetUnit(i);
if(unit != null)
{
unit.CurrentFlank = null;
SetUnit(i, null);
}
} }
} }
} }


+ 12
- 12
Assets/GWConquest/Scripts/BattleLog.cs View File

@ -12,27 +12,27 @@ namespace GWConquest
public struct BattleLogUnitEntry public struct BattleLogUnitEntry
{ {
public NetworkId Id;
public string ClassName;
public NullableNetworkBehaviourReference Id;
public ushort ClassID;
public int PlayerID; public int PlayerID;
public UnitClass UnitClass { public UnitClass UnitClass {
get => UnitClass.FromName(ClassName);
get => UnitClass.FromID(ClassID);
} }
public Unit Unit { public Unit Unit {
get => BoltNetwork.FindEntity(Id)?.GetComponent<Unit>();
get => Id.GetBehaviour<Unit>();
} }
} }
public struct BattleLogBattleEntry public struct BattleLogBattleEntry
{ {
public NetworkId Id;
public NullableNetworkBehaviourReference Id;
public string BattleName; public string BattleName;
public string PlanetName; public string PlanetName;
public Battle Battle { public Battle Battle {
get => BoltNetwork.FindEntity(Id)?.GetComponent<Battle>();
get => Id.GetBehaviour<Battle>();
} }
} }
@ -51,16 +51,16 @@ namespace GWConquest
public BattleLogEntry(Unit unit, BattleLogAction action, Unit target = null, Battle battle = null) public BattleLogEntry(Unit unit, BattleLogAction action, Unit target = null, Battle battle = null)
{ {
Unit = new BattleLogUnitEntry() { Unit = new BattleLogUnitEntry() {
Id = unit.entity.NetworkId,
ClassName = unit.state.UnitClass,
Id = unit,
ClassID = unit.Class.ID,
PlayerID = unit.Player.PlayerId PlayerID = unit.Player.PlayerId
}; };
Action = action; Action = action;
if(target != null) if(target != null)
{ {
Target = new BattleLogUnitEntry() { Target = new BattleLogUnitEntry() {
Id = target.entity.NetworkId,
ClassName = target.state.UnitClass,
Id = target,
ClassID = target.Class.ID,
PlayerID = target.Player.PlayerId PlayerID = target.Player.PlayerId
}; };
} }
@ -70,7 +70,7 @@ namespace GWConquest
if(battle != null) if(battle != null)
{ {
Battle = new BattleLogBattleEntry() { Battle = new BattleLogBattleEntry() {
Id = battle.entity.NetworkId,
Id = battle,
BattleName = battle.name, BattleName = battle.name,
PlanetName = battle.Zone.planet.name PlanetName = battle.Zone.planet.name
}; };
@ -91,7 +91,7 @@ namespace GWConquest
public IEnumerable<BattleLogEntry> GetEntriesForBattle(Battle battle) public IEnumerable<BattleLogEntry> GetEntriesForBattle(Battle battle)
{ {
return Entries.Where(e => e.Battle.Id == battle.entity.NetworkId);
return Entries.Where(e => e.Battle.Battle == battle);
} }
} }
} }

+ 22
- 19
Assets/GWConquest/Scripts/District.cs View File

@ -298,31 +298,34 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
CheckControllingChange();
if(ControllingPlayer != null)
if(IsServer)
{ {
string producingItem = ProducingItem;
CheckControllingChange();
bool producesCredits = DistrictType == DistrictType.Civil;
if(ControllingPlayer != null)
{
string producingItem = ProducingItem;
if(producingItem != null || producesCredits) {
if(itemProductionCooldown.Value <= 0f)
{
if(producingItem != null)
{
Inventory.AddItem(ItemRegistry.Instance.IDFromName(producingItem), 1);
}
bool producesCredits = DistrictType == DistrictType.Civil;
if(producesCredits)
if(producingItem != null || producesCredits) {
if(itemProductionCooldown.Value <= 0f)
{ {
ControllingPlayer.Credits++;
}
if(producingItem != null)
{
Inventory.AddItem(ItemRegistry.Instance.IDFromName(producingItem), 1);
}
itemProductionCooldown.Value = ProducingCooldown;
}
else {
itemProductionCooldown.Value -= Time.fixedDeltaTime;
if(producesCredits)
{
ControllingPlayer.Credits++;
}
itemProductionCooldown.Value = ProducingCooldown;
}
else {
itemProductionCooldown.Value -= Time.fixedDeltaTime;
}
} }
} }
} }


+ 14
- 10
Assets/GWConquest/Scripts/DistrictFactory.cs View File

@ -43,6 +43,7 @@ namespace GWConquest
public bool StartsBroken = false; public bool StartsBroken = false;
public string[] SpecialUnits; public string[] SpecialUnits;
public int MaxQueueLength;
private District _district; private District _district;
public District District public District District
@ -152,19 +153,22 @@ namespace GWConquest
} }
int length = ProductionQueueLength; int length = ProductionQueueLength;
BoltLog.Info("Adding unit {0} to production queue on district {1}", buildable, gameObject.name);
productionQueue.Add(new ProductionQueueEntry() {
UnitClass = buildable.ID,
TimeProduced = 0f,
PlayerID = player.PlayerId,
IsUpgrade = buildable is DistrictUpgrade
});
if(length < MaxQueueLength)
{
BoltLog.Info("Adding unit {0} to production queue on district {1}", buildable, gameObject.name);
productionQueue.Add(new ProductionQueueEntry() {
UnitClass = buildable.ID,
TimeProduced = 0f,
PlayerID = player.PlayerId,
IsUpgrade = buildable is DistrictUpgrade
});
}
} }
[ServerRpc] [ServerRpc]
public void AddProductionQueueEntryRpc(ushort buildableID, bool isUpgrade, NetworkBehaviourReference player)
public void AddProductionQueueEntryServerRpc(ushort buildableID, bool isUpgrade, NetworkBehaviourReference player)
{ {
IBuildable buildable = isUpgrade ? DistrictUpgrade.FromID(buildableID) : UnitClass.FromID(buildableID); IBuildable buildable = isUpgrade ? DistrictUpgrade.FromID(buildableID) : UnitClass.FromID(buildableID);
AddProductionQueueEntry(buildable, player.GetBehaviour<Player>()); AddProductionQueueEntry(buildable, player.GetBehaviour<Player>());
@ -188,7 +192,7 @@ namespace GWConquest
Zone zone = GetComponent<Zone>(); Zone zone = GetComponent<Zone>();
if(zone != null) if(zone != null)
{ {
Unit.SpawnUnit(zone, buildable as UnitClass, productionQueue[0].Player);
GWNetworkManager.Instance.SpawnUnit(zone, buildable as UnitClass, productionQueue[0].Player);
} }
else else
{ {


+ 253
- 219
Assets/GWConquest/Scripts/Formation.cs View File

@ -9,17 +9,19 @@ using Unity.Netcode;
namespace GWConquest namespace GWConquest
{ {
public struct Transition : INetworkSerializeByMemcpy
public struct Transition : INetworkSerializeByMemcpy
{ {
public int OriginZoneID; public int OriginZoneID;
public int TargetZoneID; public int TargetZoneID;
public float TransitionLength; public float TransitionLength;
public bool IsCurved; public bool IsCurved;
public Zone OriginZone {
public Zone OriginZone
{
get => Zone.GetFromId(OriginZoneID); get => Zone.GetFromId(OriginZoneID);
} }
public Zone TargetZone {
public Zone TargetZone
{
get => Zone.GetFromId(TargetZoneID); get => Zone.GetFromId(TargetZoneID);
} }
} }
@ -66,25 +68,26 @@ namespace GWConquest
} }
private void OnZoneChanged(int previousValue, int newValue) private void OnZoneChanged(int previousValue, int newValue)
{
if(previousValue != newValue)
{
if (previousValue != newValue)
{ {
Zone previousZone = Zone.GetFromId(previousValue); Zone previousZone = Zone.GetFromId(previousValue);
Zone newZone = Zone.GetFromId(newValue); Zone newZone = Zone.GetFromId(newValue);
if(previousZone != null)
if (previousZone != null)
{ {
previousZone.OnFormationDeparting(this); previousZone.OnFormationDeparting(this);
} }
if(newZone != null)
if (newZone != null)
{ {
newZone.OnFormationArrived(this); newZone.OnFormationArrived(this);
} }
BoltLog.Info("Current zone changed from {0} to {1} on formation {2}", previousZone, newZone, this); BoltLog.Info("Current zone changed from {0} to {1} on formation {2}", previousZone, newZone, this);
}
}
} }
public ZoneType ZoneType {
public ZoneType ZoneType
{
get => isSpace.Value ? ZoneType.Space : ZoneType.Ground; get => isSpace.Value ? ZoneType.Space : ZoneType.Ground;
set => isSpace.Value = value == ZoneType.Space; set => isSpace.Value = value == ZoneType.Space;
} }
@ -98,7 +101,8 @@ namespace GWConquest
public string FormationName public string FormationName
{ {
get => formationName.Value; get => formationName.Value;
set {
set
{
formationName.Value = value; formationName.Value = value;
keepFormationName = true; keepFormationName = true;
} }
@ -110,23 +114,28 @@ namespace GWConquest
private Vector3 restingPosition; private Vector3 restingPosition;
public float AnimCompletion {
public float AnimCompletion
{
get => animDistanceCovered / CurrentTransition.TransitionLength; get => animDistanceCovered / CurrentTransition.TransitionLength;
} }
public float AnimTimeLeft {
public float AnimTimeLeft
{
get => (CurrentTransition.TransitionLength - animDistanceCovered) / movementSpeed; get => (CurrentTransition.TransitionLength - animDistanceCovered) / movementSpeed;
} }
public IEnumerable<Unit> Units {
public IEnumerable<Unit> Units
{
get => units.Select(r => r.GetBehaviour<Unit>()); get => units.Select(r => r.GetBehaviour<Unit>());
} }
public IEnumerable<Formation> SubFormations {
public IEnumerable<Formation> SubFormations
{
get => units.Select(r => r.GetBehaviour<Formation>()); get => units.Select(r => r.GetBehaviour<Formation>());
} }
public int UnitCount {
public int UnitCount
{
get => units.Count; get => units.Count;
} }
@ -141,16 +150,19 @@ namespace GWConquest
get => Units.SelectMany(u => u.Inventory); get => Units.SelectMany(u => u.Inventory);
} }
public IEnumerable<Zone> PathQueue {
public IEnumerable<Zone> PathQueue
{
get => pathQueue.Select(id => Zone.GetFromId(id)); get => pathQueue.Select(id => Zone.GetFromId(id));
} }
public Formation MovementTargetFormation {
public Formation MovementTargetFormation
{
get => movementTargetFormation.Value.GetBehaviour<Formation>(); get => movementTargetFormation.Value.GetBehaviour<Formation>();
set => movementTargetFormation.Value = value; set => movementTargetFormation.Value = value;
} }
public Formation MovementOriginFormation {
public Formation MovementOriginFormation
{
get => movementOriginFormation.Value.GetBehaviour<Formation>(); get => movementOriginFormation.Value.GetBehaviour<Formation>();
set => movementOriginFormation.Value = value; set => movementOriginFormation.Value = value;
} }
@ -165,61 +177,70 @@ namespace GWConquest
get => currentTransition.Value; get => currentTransition.Value;
} }
public bool IsEmbarked {
public bool IsEmbarked
{
get => isEmbarked.Value; get => isEmbarked.Value;
set => isEmbarked.Value = value; set => isEmbarked.Value = value;
} }
public Formation ParentFormation {
public Formation ParentFormation
{
get => parentFormation.Value.GetBehaviour<Formation>(); get => parentFormation.Value.GetBehaviour<Formation>();
set => parentFormation.Value = value; set => parentFormation.Value = value;
} }
public const float MoraleCap = 27 * 3 * 100; public const float MoraleCap = 27 * 3 * 100;
public float Morale {
public float Morale
{
get => morale.Value; get => morale.Value;
set => morale.Value = value; set => morale.Value = value;
} }
public float StartingMorale {
public float StartingMorale
{
get => startingMorale.Value; get => startingMorale.Value;
set => startingMorale.Value = value; set => startingMorale.Value = value;
} }
public FormationMovementState MovementState {
public FormationMovementState MovementState
{
get => movementState.Value; get => movementState.Value;
set => movementState.Value = value; set => movementState.Value = value;
} }
public bool IsMoving {
public bool IsMoving
{
get => MovementState == FormationMovementState.Moving || get => MovementState == FormationMovementState.Moving ||
MovementState == FormationMovementState.PreparingMovement || MovementState == FormationMovementState.PreparingMovement ||
MovementState == FormationMovementState.FinishingMovement; MovementState == FormationMovementState.FinishingMovement;
} }
public float ActionCooldown {
public float ActionCooldown
{
get => actionCooldown.Value; get => actionCooldown.Value;
} }
public float ActionCooldownPercent {
get =>actionCooldown.Value / actionCooldownMax.Value;
public float ActionCooldownPercent
{
get => actionCooldown.Value / actionCooldownMax.Value;
} }
public void SetActionCooldown(float value) public void SetActionCooldown(float value)
{ {
actionCooldown.Value = value; actionCooldown.Value = value;
actionCooldownMax.Value = value; actionCooldownMax.Value = value;
}
}
public GameObject movingArmyPrefab; public GameObject movingArmyPrefab;
public float movingArmyScale; public float movingArmyScale;
public GameObject movingArmyIcon {get; private set;}
public GameObject movingArmyIcon { get; private set; }
private Vector3 arrivalPosition; private Vector3 arrivalPosition;
public float movementSpeed {get => GameManager.Instance.DefaultMovementSpeed;}
public float movementSpeed { get => GameManager.Instance.DefaultMovementSpeed; }
public int FormationNumber {
public int FormationNumber
{
get => formationNumber.Value; get => formationNumber.Value;
set => formationNumber.Value = value; set => formationNumber.Value = value;
} }
@ -233,18 +254,20 @@ namespace GWConquest
get => !IsMoving; get => !IsMoving;
} }
public Planet CurrentPlanet {
get {
public Planet CurrentPlanet
{
get
{
var z = currentZone; var z = currentZone;
if(z != null)
if (z != null)
{ {
return z.planet; return z.planet;
} }
else if(CurrentTransition.OriginZoneID != -1)
else if (CurrentTransition.OriginZoneID != -1)
{ {
return CurrentTransition.OriginZone.planet; return CurrentTransition.OriginZone.planet;
} }
else return null;
else return null;
} }
} }
@ -252,7 +275,7 @@ namespace GWConquest
public void TryMoveToPlanet(Planet planet) public void TryMoveToPlanet(Planet planet)
{ {
if(currentZone.zoneType == ZoneType.Space)
if (currentZone.zoneType == ZoneType.Space)
{ {
var targetZone = planet.GetMainZone(ZoneType.Space); var targetZone = planet.GetMainZone(ZoneType.Space);
@ -265,27 +288,27 @@ namespace GWConquest
var pathfindingGraph = targetZone.zoneType == ZoneType.Space ? Planet.PlanetPathfindingGraph : targetZone.planet.pathfindingGraph; var pathfindingGraph = targetZone.zoneType == ZoneType.Space ? Planet.PlanetPathfindingGraph : targetZone.planet.pathfindingGraph;
var shortestPath = pathfindingGraph.FindShortestPath(this, currentZone, targetZone); var shortestPath = pathfindingGraph.FindShortestPath(this, currentZone, targetZone);
if(shortestPath != null)
if (shortestPath != null)
{ {
StartMovingOnPath(shortestPath.zones); StartMovingOnPath(shortestPath.zones);
} }
else
else
{ {
BoltLog.Warn("No path found between {0} and {1}", currentZone, targetZone); BoltLog.Warn("No path found between {0} and {1}", currentZone, targetZone);
} }
} }
public void StartMovingOnPath(List<Zone> path, Formation targetFormation=null)
public void StartMovingOnPath(List<Zone> path, Formation targetFormation = null)
{ {
if (path.Count > 1 && IsOwner && CanMove) if (path.Count > 1 && IsOwner && CanMove)
{ {
if(!CanMoveTo(path[0], path[1]))
if (!CanMoveTo(path[0], path[1]))
{ {
BoltLog.Info($"Formation {this} has not enough fuel for transition {path[0]} -> {path[1]}"); BoltLog.Info($"Formation {this} has not enough fuel for transition {path[0]} -> {path[1]}");
return; return;
} }
int[] zoneIDs = path.GetRange(1, path.Count-1).Select(z => Zone.GetZoneId(z)).ToArray();
int[] zoneIDs = path.GetRange(1, path.Count - 1).Select(z => Zone.GetZoneId(z)).ToArray();
BeginMovementServerRpc(zoneIDs, targetFormation); BeginMovementServerRpc(zoneIDs, targetFormation);
} }
else else
@ -297,22 +320,22 @@ namespace GWConquest
public void BeginMovementServer(int[] zoneIDs, Formation targetFormation) public void BeginMovementServer(int[] zoneIDs, Formation targetFormation)
{ {
if(zoneIDs == null)
if (zoneIDs == null)
{ {
throw new ArgumentNullException("Movement path is null!"); throw new ArgumentNullException("Movement path is null!");
} }
Zone firstZone = Zone.GetFromId(zoneIDs[0]); Zone firstZone = Zone.GetFromId(zoneIDs[0]);
if(!CanMoveTo(currentZone,firstZone))
if (!CanMoveTo(currentZone, firstZone))
{ {
BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {firstZone}"); BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {firstZone}");
return; return;
} }
if(IsEmbarked)
if (IsEmbarked)
{ {
if(ParentFormation != null)
if (ParentFormation != null)
{ {
MovementOriginFormation = ParentFormation; MovementOriginFormation = ParentFormation;
ParentFormation.RemoveSubFormation(this); ParentFormation.RemoveSubFormation(this);
@ -321,7 +344,7 @@ namespace GWConquest
pathQueue.SetEntries(zoneIDs); pathQueue.SetEntries(zoneIDs);
if(targetFormation != null)
if (targetFormation != null)
{ {
MovementTargetFormation = targetFormation; MovementTargetFormation = targetFormation;
} }
@ -330,9 +353,9 @@ namespace GWConquest
MovementState = FormationMovementState.PreparingMovement; MovementState = FormationMovementState.PreparingMovement;
} }
public void BeginMovementServer(Zone targetZone, Formation targetFormation=null)
public void BeginMovementServer(Zone targetZone, Formation targetFormation = null)
{ {
BeginMovementServer(new int[] {Zone.GetZoneId(targetZone)}, targetFormation: targetFormation);
BeginMovementServer(new int[] { Zone.GetZoneId(targetZone) }, targetFormation: targetFormation);
} }
[ServerRpc] [ServerRpc]
@ -343,27 +366,28 @@ namespace GWConquest
public void MoveToZone(Zone target) public void MoveToZone(Zone target)
{ {
if(IsOwner && MovementState == FormationMovementState.PreparingMovement)
if (IsOwner && MovementState == FormationMovementState.PreparingMovement)
{ {
if(!CanMoveTo(currentZone, target))
if (!CanMoveTo(currentZone, target))
{ {
BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {target}"); BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {target}");
return; return;
} }
int remainingFuel = 0; int remainingFuel = 0;
foreach(Unit u in Units)
foreach (Unit u in Units)
{ {
int fuelRequired = u.GetFuelCostForJump(currentZone, target); int fuelRequired = u.GetFuelCostForJump(currentZone, target);
int totalFuel = u.TotalFuel; int totalFuel = u.TotalFuel;
if(fuelRequired > 0)
if (fuelRequired > 0)
{ {
if(totalFuel >= fuelRequired)
if (totalFuel >= fuelRequired)
{ {
u.ConsumeTotalFuel(fuelRequired); u.ConsumeTotalFuel(fuelRequired);
} }
else {
int diff = fuelRequired -totalFuel;
else
{
int diff = fuelRequired - totalFuel;
u.ConsumeTotalFuel(totalFuel); u.ConsumeTotalFuel(totalFuel);
remainingFuel += diff; remainingFuel += diff;
} }
@ -371,32 +395,34 @@ namespace GWConquest
} }
//consume remaining fuel from other ships //consume remaining fuel from other ships
foreach(Unit u in Units)
foreach (Unit u in Units)
{ {
int totalFuel = u.TotalFuel; int totalFuel = u.TotalFuel;
if(totalFuel >= remainingFuel)
if (totalFuel >= remainingFuel)
{ {
u.ConsumeTotalFuel(remainingFuel); u.ConsumeTotalFuel(remainingFuel);
remainingFuel = 0; remainingFuel = 0;
break; break;
} }
else {
else
{
u.ConsumeTotalFuel(totalFuel); u.ConsumeTotalFuel(totalFuel);
remainingFuel -= totalFuel; remainingFuel -= totalFuel;
} }
} }
if(remainingFuel != 0)
if (remainingFuel != 0)
{ {
BoltLog.Warn($"There is {remainingFuel} required fuel remaining after consuming everything, this should not happen!"); BoltLog.Warn($"There is {remainingFuel} required fuel remaining after consuming everything, this should not happen!");
} }
currentTransition.Value = new Transition() {
currentTransition.Value = new Transition()
{
OriginZoneID = Zone.GetZoneId(currentZone), OriginZoneID = Zone.GetZoneId(currentZone),
TargetZoneID = Zone.GetZoneId(target), TargetZoneID = Zone.GetZoneId(target),
TransitionLength = Zone.GetTransitionLength(currentZone, target), TransitionLength = Zone.GetTransitionLength(currentZone, target),
IsCurved = target.zoneType == ZoneType.Ground || currentZone.zoneType == ZoneType.Ground IsCurved = target.zoneType == ZoneType.Ground || currentZone.zoneType == ZoneType.Ground
}; };
CoveredDistance = 0; CoveredDistance = 0;
currentZone = null; currentZone = null;
MovementState = FormationMovementState.Moving; MovementState = FormationMovementState.Moving;
@ -425,7 +451,7 @@ namespace GWConquest
private void OnTransitStateChanged(FormationMovementState previousValue, FormationMovementState newValue) private void OnTransitStateChanged(FormationMovementState previousValue, FormationMovementState newValue)
{ {
animDistanceCovered = 0; animDistanceCovered = 0;
if(newValue == FormationMovementState.Moving)
if (newValue == FormationMovementState.Moving)
{ {
var originZone = CurrentTransition.OriginZone; var originZone = CurrentTransition.OriginZone;
var targetZone = CurrentTransition.TargetZone; var targetZone = CurrentTransition.TargetZone;
@ -442,118 +468,120 @@ namespace GWConquest
arrivalPosition = targetZone.planet.GetFleetArrivalPosition(originZone); arrivalPosition = targetZone.planet.GetFleetArrivalPosition(originZone);
} }
if(originZone.planet == targetZone.planet)
if (originZone.planet == targetZone.planet)
{ {
originZone.planet.InTransitFormations.Add(this); originZone.planet.InTransitFormations.Add(this);
} }
} }
else else
{ {
var planet = currentZone.planet; var planet = currentZone.planet;
if(planet.InTransitFormations.Contains(this))
if (planet.InTransitFormations.Contains(this))
{ {
planet.InTransitFormations.Remove(this); planet.InTransitFormations.Remove(this);
} }
} }
} }
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsServer)
if (IsServer)
{ {
if(IsEmbarked)
{
if(ParentFormation != null)
if (IsEmbarked)
{ {
if(currentZone != ParentFormation.currentZone)
if (ParentFormation != null)
{ {
currentZone = ParentFormation.currentZone;
if (currentZone != ParentFormation.currentZone)
{
currentZone = ParentFormation.currentZone;
}
}
else
{
BoltLog.Error("Formation {0} lost its parent formation {1}!", this, ParentFormation);
ParentFormation = null;
IsEmbarked = false;
} }
} }
else {
BoltLog.Error("Formation {0} lost its parent formation {1}!", this, ParentFormation);
ParentFormation = null;
IsEmbarked = false;
}
}
if(MovementState == FormationMovementState.Moving)
{
CoveredDistance += movementSpeed * BoltNetwork.FrameDeltaTime;
if (CoveredDistance >= CurrentTransition.TransitionLength)
{
if (MovementState == FormationMovementState.Moving)
{
CoveredDistance += movementSpeed * BoltNetwork.FrameDeltaTime;
if (CoveredDistance >= CurrentTransition.TransitionLength)
{
currentZone = CurrentTransition.TargetZone;
currentZone = CurrentTransition.TargetZone;
if(currentZone.zoneType == ZoneType.Space)
{
fleetRestingPoint.Value = currentZone.planet.GetFleetArrivalPosition(CurrentTransition.OriginZone);
}
if (currentZone.zoneType == ZoneType.Space)
{
fleetRestingPoint.Value = currentZone.planet.GetFleetArrivalPosition(CurrentTransition.OriginZone);
}
currentTransition.Value = new Transition()
{
OriginZoneID = -1,
TargetZoneID = -1,
TransitionLength = 0
};
CoveredDistance = 0;
MovementState = FormationMovementState.FinishingMovement;
SetActionCooldown(GameManager.Instance.MovementFinishedCooldown);
}
}
currentTransition.Value = new Transition()
{
OriginZoneID = -1,
TargetZoneID = -1,
TransitionLength = 0
};
CoveredDistance = 0;
MovementState = FormationMovementState.FinishingMovement;
if(ActionCooldown <= 0)
{
actionCooldown.Value = 0;
SetActionCooldown(GameManager.Instance.MovementFinishedCooldown);
if(MovementState == FormationMovementState.PreparingMovement)
{
Zone z = PathQueue.First();
pathQueue.RemoveAt(0);
MoveToZone(z);
}
} }
else if(MovementState == FormationMovementState.FinishingMovement)
if (ActionCooldown <= 0)
{ {
if(pathQueue.Count > 0)
actionCooldown.Value = 0;
if (MovementState == FormationMovementState.PreparingMovement)
{ {
if(CanMoveTo(currentZone, PathQueue.First()))
{
SetActionCooldown(GameManager.Instance.MovementStartingCooldown);
MovementState = FormationMovementState.PreparingMovement;
}
else
{
BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {PathQueue.First()}");
pathQueue.Clear();
MovementState = FormationMovementState.Idle;
}
Zone z = PathQueue.First();
pathQueue.RemoveAt(0);
MoveToZone(z);
} }
else
else if (MovementState == FormationMovementState.FinishingMovement)
{ {
if(MovementTargetFormation != null)
if (pathQueue.Count > 0)
{ {
if(MovementTargetFormation.HasNetworkObject && MovementTargetFormation.currentZone == currentZone)
if (CanMoveTo(currentZone, PathQueue.First()))
{
SetActionCooldown(GameManager.Instance.MovementStartingCooldown);
MovementState = FormationMovementState.PreparingMovement;
}
else
{ {
MovementTargetFormation.AddSubFormation(this);
BoltLog.Info($"Formation {this} has not enough fuel for transition {currentZone} -> {PathQueue.First()}");
pathQueue.Clear();
MovementState = FormationMovementState.Idle;
} }
MovementTargetFormation = null;
} }
if(MovementOriginFormation != null)
else
{ {
MovementOriginFormation = null;
}
if (MovementTargetFormation != null)
{
if (MovementTargetFormation.HasNetworkObject && MovementTargetFormation.currentZone == currentZone)
{
MovementTargetFormation.AddSubFormation(this);
}
MovementTargetFormation = null;
}
if (MovementOriginFormation != null)
{
MovementOriginFormation = null;
}
MovementState = FormationMovementState.Idle;
MovementState = FormationMovementState.Idle;
}
} }
} }
}
else {
actionCooldown.Value -= Time.fixedDeltaTime;
}
else
{
actionCooldown.Value -= Time.fixedDeltaTime;
}
} }
} }
@ -584,11 +612,12 @@ namespace GWConquest
{ {
if (currentZone != null && movingArmyIcon == null) if (currentZone != null && movingArmyIcon == null)
{ {
if(currentZone.zoneType == ZoneType.Space)
if (currentZone.zoneType == ZoneType.Space)
{ {
MakeMovingIcon(IngameUI.Instance.MovingFleetsTransform); MakeMovingIcon(IngameUI.Instance.MovingFleetsTransform);
} }
else {
else
{
MakeMovingIcon(IngameUI.Instance.PlanetView.DistrictIcons); MakeMovingIcon(IngameUI.Instance.PlanetView.DistrictIcons);
} }
} }
@ -680,11 +709,11 @@ namespace GWConquest
if (movingArmyIcon != null) if (movingArmyIcon != null)
{ {
if(ZoneType == ZoneType.Space)
if (ZoneType == ZoneType.Space)
{ {
if (!IngameUI.PlanetViewEnabled) if (!IngameUI.PlanetViewEnabled)
{ {
if(GWCamera.Instance.ShowCircles || MovementState == FormationMovementState.Moving)
if (GWCamera.Instance.ShowCircles || MovementState == FormationMovementState.Moving)
{ {
movingArmyIcon.SetActive(true); movingArmyIcon.SetActive(true);
var stick = movingArmyIcon.GetComponent<StickToFormation>(); var stick = movingArmyIcon.GetComponent<StickToFormation>();
@ -698,10 +727,11 @@ namespace GWConquest
movingArmyIcon.SetActive(false); movingArmyIcon.SetActive(false);
} }
} }
else {
if(IngameUI.PlanetViewEnabled && IngameUI.Instance.PlanetView.selectedPlanet == CurrentPlanet)
else
{
if (IngameUI.PlanetViewEnabled && IngameUI.Instance.PlanetView.selectedPlanet == CurrentPlanet)
{ {
if(MovementState == FormationMovementState.Moving)
if (MovementState == FormationMovementState.Moving)
{ {
//movingArmyIcon.SetActive(true); //movingArmyIcon.SetActive(true);
var stick = movingArmyIcon.GetComponent<StickToFormation>(); var stick = movingArmyIcon.GetComponent<StickToFormation>();
@ -720,7 +750,7 @@ namespace GWConquest
private Vector3 GetRestingPosition() private Vector3 GetRestingPosition()
{ {
if(currentZone == null)
if (currentZone == null)
return Vector3.zero; return Vector3.zero;
var planet = currentZone.planet; var planet = currentZone.planet;
@ -737,7 +767,7 @@ namespace GWConquest
AllFormations.Add(this); AllFormations.Add(this);
if(IsOwner)
if (IsOwner)
{ {
currentZoneID.Value = -1; currentZoneID.Value = -1;
MovementState = FormationMovementState.Idle; MovementState = FormationMovementState.Idle;
@ -750,24 +780,27 @@ namespace GWConquest
currentZoneID.OnValueChanged += OnZoneChanged; currentZoneID.OnValueChanged += OnZoneChanged;
base.OnNetworkSpawn(); base.OnNetworkSpawn();
} }
public float GetFormationStrength() public float GetFormationStrength()
{ {
if(HasNetworkObject)
if (HasNetworkObject)
{ {
return Units.Sum(unit => {
if(unit != null && unit.HasNetworkObject)
return Units.Sum(unit =>
{
if (unit != null && unit.HasNetworkObject)
{ {
return unit.Class.UnitStrength; return unit.Class.UnitStrength;
} }
else {
else
{
return 0f; return 0f;
} }
}); });
} }
else {
else
{
return 0f; return 0f;
} }
} }
@ -776,31 +809,31 @@ namespace GWConquest
public void OnUnitAdded(Unit unit) public void OnUnitAdded(Unit unit)
{ {
BoltLog.Info("Unit {0} added to formation {1}", unit, this); BoltLog.Info("Unit {0} added to formation {1}", unit, this);
if(currentZone != null)
if (currentZone != null)
{ {
currentZone.OnFormationChanged(this); currentZone.OnFormationChanged(this);
} }
if(IsOwner)
if (IsOwner)
{ {
if(!keepFormationName)
if (!keepFormationName)
{ {
ConstructName(); ConstructName();
} }
}
}
OnUnitsChanged?.Invoke(); OnUnitsChanged?.Invoke();
} }
public void OnUnitRemoved(Unit unit) public void OnUnitRemoved(Unit unit)
{ {
OnUnitsChanged?.Invoke(); OnUnitsChanged?.Invoke();
if(IsOwner)
if (IsOwner)
{ {
if(!keepFormationName)
if (!keepFormationName)
{ {
ConstructName(); ConstructName();
} }
}
}
} }
public void AddSubFormation(Formation f) public void AddSubFormation(Formation f)
@ -825,7 +858,7 @@ namespace GWConquest
public event Action OnUnitsChanged; public event Action OnUnitsChanged;
public float GetModifierForTransition(Zone origin, Zone target) public float GetModifierForTransition(Zone origin, Zone target)
{ {
return 1f; return 1f;
@ -833,11 +866,12 @@ namespace GWConquest
public bool CanMoveTo(Zone origin, Zone target) public bool CanMoveTo(Zone origin, Zone target)
{ {
if(origin.zoneType == target.zoneType)
if (origin.zoneType == target.zoneType)
{ {
return Units.Sum(u => u.TotalFuel) >= GetFuelCostForJump(origin, target); return Units.Sum(u => u.TotalFuel) >= GetFuelCostForJump(origin, target);
} }
else {
else
{
//TODO Fuel für Landung?? //TODO Fuel für Landung??
return true; return true;
} }
@ -850,23 +884,25 @@ namespace GWConquest
public FormationCategory Category public FormationCategory Category
{ {
get {
get
{
var units = Units; var units = Units;
float totalUnits = units.Count(); float totalUnits = units.Count();
if(ZoneType == ZoneType.Space)
if (ZoneType == ZoneType.Space)
{ {
return FormationCategory.FleetBattle; return FormationCategory.FleetBattle;
} }
else {
if(units.Count(u => u.Class.UnitType == UnitType.Vehicle) / totalUnits >= 0.3)
else
{
if (units.Count(u => u.Class.UnitType == UnitType.Vehicle) / totalUnits >= 0.3)
{ {
return FormationCategory.ArmyArmoured; return FormationCategory.ArmyArmoured;
} }
else if(units.Count(u => u.Class.UnitType == UnitType.Artillery) / totalUnits >= 0.3)
else if (units.Count(u => u.Class.UnitType == UnitType.Artillery) / totalUnits >= 0.3)
{ {
return FormationCategory.ArmyArtillery; return FormationCategory.ArmyArtillery;
} }
else if(units.Count(u => u.Class.UnitType == UnitType.Infantry) / totalUnits >= 0.5)
else if (units.Count(u => u.Class.UnitType == UnitType.Infantry) / totalUnits >= 0.5)
{ {
return FormationCategory.ArmyInfantry; return FormationCategory.ArmyInfantry;
} }
@ -874,7 +910,7 @@ namespace GWConquest
{ {
return FormationCategory.ArmyCombined; return FormationCategory.ArmyCombined;
} }
}
}
} }
} }
@ -887,47 +923,48 @@ namespace GWConquest
var units = Units; var units = Units;
float totalUnits = units.Count(); float totalUnits = units.Count();
if(ZoneType == ZoneType.Ground)
if (ZoneType == ZoneType.Ground)
{ {
if(totalUnits < 5)
if (totalUnits < 5)
{ {
name += "Company"; name += "Company";
} }
else if(totalUnits < 10)
else if (totalUnits < 10)
{ {
name += "Battalion"; name += "Battalion";
} }
else if(totalUnits < 20)
else if (totalUnits < 20)
{ {
name += "Division"; name += "Division";
} }
else if(totalUnits < 40)
else if (totalUnits < 40)
{ {
name += "Corps"; name += "Corps";
} }
else if(totalUnits < 60)
else if (totalUnits < 60)
{ {
name += "Army"; name += "Army";
} }
} }
else {
if(totalUnits < 5)
else
{
if (totalUnits < 5)
{ {
name += "Squadron"; name += "Squadron";
} }
else if(totalUnits < 10)
else if (totalUnits < 10)
{ {
name += "Flotilla"; name += "Flotilla";
} }
else if(totalUnits < 20)
else if (totalUnits < 20)
{ {
name += "Force"; name += "Force";
} }
else if(totalUnits < 40)
else if (totalUnits < 40)
{ {
name += "Fleet"; name += "Fleet";
} }
else if(totalUnits < 60)
else if (totalUnits < 60)
{ {
name += "Armada"; name += "Armada";
} }
@ -943,13 +980,13 @@ namespace GWConquest
public void TakeMoraleDamage(float moraleDamage) public void TakeMoraleDamage(float moraleDamage)
{ {
if(Morale > 0)
if (Morale > 0)
{ {
if(DebugUI.LogBattleEvents)
if (DebugUI.LogBattleEvents)
BoltLog.Info("Formation {0} took {1} points of morale damage", this, moraleDamage); BoltLog.Info("Formation {0} took {1} points of morale damage", this, moraleDamage);
Morale -= moraleDamage; Morale -= moraleDamage;
if(Morale <= 0)
if (Morale <= 0)
{ {
Morale = 0; Morale = 0;
BoltLog.Info("Formation {0} is demoralized and will flee!", this); BoltLog.Info("Formation {0} is demoralized and will flee!", this);
@ -957,15 +994,15 @@ namespace GWConquest
BeginMovementServer(retreatZone); BeginMovementServer(retreatZone);
foreach(Unit u in Units)
foreach (Unit u in Units)
{ {
if(u.CurrentFlank != null)
if (u.CurrentFlank != null)
{ {
u.CurrentBattle.MoveUnitToReserve(u); u.CurrentBattle.MoveUnitToReserve(u);
} }
} }
PlayFormationDemoralizedAnimation();
PlayFormationDemoralizedAnimationClientRpc();
} }
} }
} }
@ -973,11 +1010,12 @@ namespace GWConquest
public Zone GetRetreatZone() public Zone GetRetreatZone()
{ {
IEnumerable<Zone> zones; IEnumerable<Zone> zones;
if(currentZone.zoneType == ZoneType.Ground)
if (currentZone.zoneType == ZoneType.Ground)
{ {
zones = currentZone.planet.pathfindingGraph.GetConnectedNodes(currentZone); zones = currentZone.planet.pathfindingGraph.GetConnectedNodes(currentZone);
} }
else {
else
{
zones = Planet.PlanetPathfindingGraph.GetConnectedNodes(currentZone); zones = Planet.PlanetPathfindingGraph.GetConnectedNodes(currentZone);
} }
var list = zones.ToList(); var list = zones.ToList();
@ -993,34 +1031,36 @@ namespace GWConquest
bool isArriving = MovementState == FormationMovementState.FinishingMovement; bool isArriving = MovementState == FormationMovementState.FinishingMovement;
GameManager gm = GameManager.Instance; GameManager gm = GameManager.Instance;
foreach(Unit u in Units)
foreach (Unit u in Units)
{ {
u.BattleState = isArriving ? BattleUnitState.Arriving : BattleUnitState.InReserve; u.BattleState = isArriving ? BattleUnitState.Arriving : BattleUnitState.InReserve;
u.ResetActionCooldown(); u.ResetActionCooldown();
if(!battle.IsSpaceBattle && !u.Class.IsHero)
if (!battle.IsSpaceBattle && !u.Class.IsHero)
{ {
if(Battle.Check(gm.StartRevealChance))
if (Battle.Check(gm.StartRevealChance))
{ {
if(Battle.Check(gm.StartFullRevealChance))
if (Battle.Check(gm.StartFullRevealChance))
{ {
u.RevealState = RevealState.Visible; u.RevealState = RevealState.Visible;
} }
else {
else
{
u.RevealState = RevealState.ClassHidden; u.RevealState = RevealState.ClassHidden;
} }
} }
else {
else
{
u.RevealState = RevealState.FullHidden; u.RevealState = RevealState.FullHidden;
} }
}
}
} }
} }
[ClientRpc] [ClientRpc]
public void PlayFormationDemoralizedAnimation()
public void PlayFormationDemoralizedAnimationClientRpc()
{ {
if(HeroUnit != null && HeroUnit.CurrentIcon != null)
if (HeroUnit != null && HeroUnit.CurrentIcon != null)
{ {
HeroUnit.CurrentIcon.PlayDemoralizedAnim(); HeroUnit.CurrentIcon.PlayDemoralizedAnim();
} }
@ -1028,10 +1068,10 @@ namespace GWConquest
public void RemoveDeadUnits() public void RemoveDeadUnits()
{ {
for(int i = units.Count-1; i >= 0; i--)
for (int i = units.Count - 1; i >= 0; i--)
{ {
var unit = units[i].GetBehaviour<Unit>(); var unit = units[i].GetBehaviour<Unit>();
if(unit.IsDead)
if (unit.IsDead)
{ {
units.RemoveAt(i); units.RemoveAt(i);
OnUnitRemoved(unit); OnUnitRemoved(unit);
@ -1047,14 +1087,7 @@ namespace GWConquest
units.Add(unit); units.Add(unit);
} }
public static Formation SpawnFormation(Zone zone, Player player)
{
BoltEntity formationEntity = BoltNetwork.Instantiate(BoltPrefabs.Formation);
player.AssignControl(formationEntity);
Formation playerFormation = formationEntity.GetComponent<Formation>();
playerFormation.Initialize(zone, player);
return playerFormation;
}
public void Initialize(Zone zone, Player player) public void Initialize(Zone zone, Player player)
{ {
@ -1063,7 +1096,7 @@ namespace GWConquest
currentZone = zone; currentZone = zone;
ZoneType = zone.zoneType; ZoneType = zone.zoneType;
if(ZoneType == ZoneType.Space)
if (ZoneType == ZoneType.Space)
{ {
Vector3 startPos = zone.planet.GetFleetRestingPosition(); Vector3 startPos = zone.planet.GetFleetRestingPosition();
fleetRestingPoint.Value = startPos; fleetRestingPoint.Value = startPos;
@ -1071,7 +1104,7 @@ namespace GWConquest
} }
[ServerRpc] [ServerRpc]
public void AssignLeaderRpc(NullableNetworkBehaviourReference unit)
public void AssignLeaderServerRpc(NullableNetworkBehaviourReference unit)
{ {
heroUnit.Value = unit; heroUnit.Value = unit;
} }
@ -1080,13 +1113,14 @@ namespace GWConquest
{ {
return FormationInventory; return FormationInventory;
} }
} }
public enum FormationMovementState {
public enum FormationMovementState
{
Idle, PreparingMovement, FinishingMovement, Moving Idle, PreparingMovement, FinishingMovement, Moving
} }
} }

+ 163
- 0
Assets/GWConquest/Scripts/GWNetworkManager.cs View File

@ -0,0 +1,163 @@
using UnityEngine;
using Unity.Netcode;
using System.Linq;
namespace GWConquest {
public class GWNetworkManager : NetworkBehaviour
{
private static GWNetworkManager _instance;
public static GWNetworkManager Instance {
get {
if(_instance == null)
{
_instance = FindObjectOfType<GWNetworkManager>();
}
return _instance;
}
}
public GameObject UnitPrefab;
public GameObject FormationPrefab;
public override void OnNetworkSpawn()
{
NetworkManager.SceneManager.OnSceneEvent += OnSceneEvent;
base.OnNetworkSpawn();
}
private void OnSceneEvent(SceneEvent sceneEvent)
{
if(IsServer)
{
if(sceneEvent.SceneEventType == SceneEventType.LoadComplete)
{
if(sceneEvent.ClientId == NetworkManager.ServerClientId)
{
ServerSceneLoadCompleteLocal(sceneEvent.SceneName);
}
else {
ServerSceneLoadCompleteRemote(sceneEvent.ClientId);
}
}
}
else {
if(sceneEvent.SceneEventType == SceneEventType.LoadComplete)
{
ClientSceneLoadComplete();
}
}
}
public void ServerSceneLoadCompleteLocal(string scene)
{
if(scene != "GalaxyMap")
return;
PlanetPlacement.Instance.PlacePlanets();
foreach(Planet planet in FindObjectsOfType<Planet>())
{
planet.FinishSetup();
}
Player serverPlayer = NetworkManager.SpawnManager.GetLocalPlayerObject().GetComponent<Player>();
serverPlayer.SetFaction((ushort) GameManager.HostFactionIndex);
serverPlayer.AssignStartingPlanets();
GameManager.Instance.SceneLoadLocalDone();
//foreach(GameObject go in FindObjectsOfType<GameObject>()) {
// go.SendMessage("OnSceneLoadLocalDone", SendMessageOptions.DontRequireReceiver);
//}
foreach(var spawner in FindObjectsOfType<SpawnAIUnits>())
{
spawner.SpawnUnits();
}
foreach(Zone z in Zone.AllZones)
{
z.CheckBattleStart();
}
Planet.SetupPlanetPathfinding();
GameManager.EntitiesLoaded = true;
GameOptions.ApplyOptions();
LoadingScreen.Dismiss();
Debug.Log("Server scene loading done.");
}
public void ServerSceneLoadCompleteRemote(ulong clientID)
{
Player clientPlayer = NetworkManager.SpawnManager.GetPlayerNetworkObject(clientID).GetComponent<Player>();
clientPlayer.SetFaction((ushort) ((GameManager.HostFactionIndex + 1) % 2));
clientPlayer.AssignStartingPlanets();
}
public void ClientSceneLoadComplete()
{
GameOptions.ApplyOptions();
}
[ServerRpc]
public void MoveItemServerRpc(NetworkBehaviourReference origin, NetworkBehaviourReference target, ushort itemID, int itemAmount)
{
NetworkBehaviour originBeh = origin.GetBehaviour();
NetworkBehaviour targetBeh = target.GetBehaviour();
if(originBeh is IHasInventory && targetBeh is IHasInventory)
{
IInventory originInv = (originBeh as IHasInventory).GetInventory();
IInventory targetInv = (targetBeh as IHasInventory).GetInventory();
Inventory.MoveItem(originInv, targetInv, itemID, itemAmount);
}
else {
Debug.LogError("One of the specified behaviours is not an inventory");
}
}
public Unit SpawnUnit(Zone zone, UnitClass uc, Player player)
{
Formation playerFormation = zone.Formations.FirstOrDefault(f => f.Player == player);
if (playerFormation == null)
{
playerFormation = SpawnFormation(zone, player);
}
GameObject go = Instantiate(UnitPrefab);
go.GetComponent<NetworkObject>().Spawn();
Unit unit = go.GetComponent<Unit>();
unit.InstantiateWithClass(uc);
unit.Formation = playerFormation;
playerFormation.AddUnit(unit);
Debug.LogFormat("Spawned unit {0} for player {1} in formation {2}", unit, player, playerFormation);
return unit;
}
[ServerRpc]
public void SpawnUnitServerRpc(int zoneID, ushort unitClassID, int playerID)
{
SpawnUnit(Zone.GetFromId(zoneID), UnitClass.FromID(unitClassID), Player.GetPlayerById(playerID));
}
public Formation SpawnFormation(Zone zone, Player player)
{
GameObject go = Instantiate(FormationPrefab);
Formation playerFormation = go.GetComponent<Formation>();
playerFormation.Initialize(zone, player);
return playerFormation;
}
}
}

Assets/GWConquest/Scripts/GlobalCallbacks.cs.meta → Assets/GWConquest/Scripts/GWNetworkManager.cs.meta View File

@ -1,5 +1,5 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 4cd2ea54c0e00384894169adbfdf6403
guid: 1741c174eb1458047aa5ab35a3b0aec4
MonoImporter: MonoImporter:
externalObjects: {} externalObjects: {}
serializedVersion: 2 serializedVersion: 2

+ 0
- 69
Assets/GWConquest/Scripts/GlobalCallbacks.cs View File

@ -1,69 +0,0 @@
using Photon.Bolt;
using Photon.Bolt.Utils;
using UdpKit;
using UnityEngine;
using System.Linq;
namespace GWConquest
{
[BoltGlobalBehaviour]
public class GlobalCallbacks : GlobalEventListener
{
public override void BoltStartBegin()
{
Debug.Log("Calling global callbacks...");
BoltNetwork.RegisterTokenClass<EntityListToken>();
BoltNetwork.RegisterTokenClass<ZoneListToken>();
BoltNetwork.RegisterTokenClass<InventoryToken>();
BoltNetwork.RegisterTokenClass<StringListToken>();
}
public override void SceneLoadLocalDone(string scene, IProtocolToken token)
{
//BoltLog.Info("scene load token: {0}", token);
GameOptions.ApplyOptions();
}
public override void SessionConnected(UdpSession session, IProtocolToken token)
{
BoltLog.Info("Session connected.");
}
public override void OnEvent(ServerConfigEvent evnt)
{
if(BoltNetwork.IsClient)
{
GameManager.InitialEntityCount = evnt.EntityCount;
BoltLog.Info("Initial entity count is {0}", GameManager.InitialEntityCount);
if(!GameManager.EntitiesLoaded && GameManager.InitialEntityCount <= BoltNetwork.Entities.Count())
{
BoltLog.Info("All entities retrieved!");
GameManager.Instance.SetEntitiesLoaded();
}
}
}
public override void EntityReceived(BoltEntity entity)
{
if(BoltNetwork.IsClient && !GameManager.EntitiesLoaded)
{
var ecount = BoltNetwork.Entities.Count();
if(ecount >= GameManager.InitialEntityCount)
{
BoltLog.Info("All entities retrieved!");
GameManager.Instance.SetEntitiesLoaded();
}
}
}
}
}

+ 5
- 6
Assets/GWConquest/Scripts/Player.cs View File

@ -86,6 +86,11 @@ namespace GWConquest
BoltLog.Info("Current player is {0} with connection ID {1}", this, OwnerClientId); BoltLog.Info("Current player is {0} with connection ID {1}", this, OwnerClientId);
} }
public void SetFaction(ushort ind)
{
factionIndex.Value = ind;
}
public NetworkClient Client public NetworkClient Client
{ {
get get
@ -161,12 +166,6 @@ namespace GWConquest
} }
} }
} }
[ServerRpc]
public void BuildUnit(int zoneID, ushort unitClassID)
{
Unit.SpawnUnit(Zone.GetFromId(zoneID), UnitClass.FromID(unitClassID), this);
}
} }
[System.Serializable] [System.Serializable]


+ 0
- 124
Assets/GWConquest/Scripts/ServerCallbacks.cs View File

@ -1,124 +0,0 @@
using System.Linq;
using UnityEngine;
using UdpKit;
using Photon.Bolt;
using Photon.Bolt.Utils;
using Unity.Netcode;
namespace GWConquest
{
[BoltGlobalBehaviour(BoltNetworkModes.Server)]
public class ServerCallbacks : NetworkBehaviour
{
private static ServerCallbacks _instance;
public static ServerCallbacks Instance {
get {
if(_instance == null)
{
_instance = FindObjectOfType<ServerCallbacks>();
}
return _instance;
}
}
public override void SceneLoadLocalDone(string scene, IProtocolToken token)
{
if(scene != "GalaxyMap")
return;
PlanetPlacement.Instance.PlacePlanets();
foreach(Planet planet in FindObjectsOfType<Planet>())
{
planet.FinishSetup();
}
BoltEntity playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player);
Debug.Log("Player entity instantiated");
IPlayerState playerState = playerEntity.GetState<IPlayerState>();
playerState.IsHost = true;
playerState.FactionIndex = GameManager.HostFactionIndex;
playerEntity.GetComponent<Player>().AssignStartingPlanets();
playerEntity.TakeControl();
GameManager.Instance.SceneLoadLocalDone();
//foreach(GameObject go in FindObjectsOfType<GameObject>()) {
// go.SendMessage("OnSceneLoadLocalDone", SendMessageOptions.DontRequireReceiver);
//}
foreach(var spawner in FindObjectsOfType<SpawnAIUnits>())
{
spawner.SpawnUnits();
}
foreach(Zone z in Zone.AllZones)
{
z.CheckBattleStart();
}
Planet.SetupPlanetPathfinding();
GameManager.EntitiesLoaded = true;
LoadingScreen.Dismiss();
BoltLog.Info("Server scene loading done.");
}
public override void SceneLoadRemoteDone(BoltConnection connection, IProtocolToken token)
{
BoltEntity playerEntity = BoltNetwork.Instantiate(BoltPrefabs.Player);
IPlayerState playerState = playerEntity.GetState<IPlayerState>();
playerState.ConnectionId = (int)connection.ConnectionId;
playerState.IsHost = false;
playerState.FactionIndex = (GameManager.HostFactionIndex + 1) % 2;
playerEntity.GetComponent<Player>().AssignStartingPlanets();
playerEntity.AssignControl(connection);
var ecount = BoltNetwork.Entities.Count();
var configEvnt = ServerConfigEvent.Create(connection);
configEvnt.EntityCount = ecount;
configEvnt.Send();
}
public override void ConnectRequest(UdpEndPoint endpoint, IProtocolToken token)
{
BoltLog.Info("Connect request recieved");
if(GameManager.EntitiesLoaded)
{
BoltLog.Info("Accepting connect request");
BoltNetwork.Accept(endpoint);
}
else {
BoltLog.Info("Refusing connect request");
BoltNetwork.Refuse(endpoint);
}
}
public override void Connected(BoltConnection connection)
{
BoltLog.Info("Client connected");
}
[ServerRpc]
public void MoveItemRpc(NetworkBehaviourReference origin, NetworkBehaviourReference target, ushort itemID, int itemAmount)
{
NetworkBehaviour originBeh = origin.GetBehaviour();
NetworkBehaviour targetBeh = target.GetBehaviour();
if(originBeh is IHasInventory && targetBeh is IHasInventory)
{
IInventory originInv = (originBeh as IHasInventory).GetInventory();
IInventory targetInv = (targetBeh as IHasInventory).GetInventory();
Inventory.MoveItem(originInv, targetInv, itemID, itemAmount);
}
else {
BoltLog.Error("One of the specified behaviours is not an inventory");
}
}
}
}

+ 0
- 11
Assets/GWConquest/Scripts/ServerCallbacks.cs.meta View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: b7ac2246de5adcc4eb245fff9adac55e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

+ 1
- 1
Assets/GWConquest/Scripts/SpawnAIUnits.cs View File

@ -23,7 +23,7 @@ namespace GWConquest {
{ {
for(int i = 0; i < unitCount; i++) for(int i = 0; i < unitCount; i++)
{ {
var unit = Unit.SpawnUnit(zone, UnitClass.FromName(unitName), player);
var unit = GWNetworkManager.Instance.SpawnUnit(zone, UnitClass.FromName(unitName), player);
if(assignAsLeader) if(assignAsLeader)
{ {
unit.Formation.HeroUnit = unit; unit.Formation.HeroUnit = unit;


+ 2
- 2
Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs View File

@ -237,7 +237,7 @@ namespace GWConquest {
var fui = icon.GetComponentInParent<BattleFormationUI>(); var fui = icon.GetComponentInParent<BattleFormationUI>();
if(fui != null && target == fui.LeaderIcon) if(fui != null && target == fui.LeaderIcon)
{ {
icon.Unit.Formation.AssignLeaderRpc(icon.Unit);
icon.Unit.Formation.AssignLeaderServerRpc(icon.Unit);
} }
} }
else if(icon.Unit.BattleState == BattleUnitState.InReserve && icon.Unit.CanGoToFlank) else if(icon.Unit.BattleState == BattleUnitState.InReserve && icon.Unit.CanGoToFlank)
@ -250,7 +250,7 @@ namespace GWConquest {
if(flankUI.BattleFlank.GetUnit(flankIndex) == null) if(flankUI.BattleFlank.GetUnit(flankIndex) == null)
{ {
Battle.MoveUnitToFlankRpc(icon.Unit, flankUI.BattleFlank, flankIndex);
Battle.MoveUnitToFlankServerRpc(icon.Unit, flankUI.BattleFlank, flankIndex);
} }


+ 1
- 1
Assets/GWConquest/Scripts/UI/BattleFlankUI.cs View File

@ -107,7 +107,7 @@ namespace GWConquest {
{ {
if(icon.Unit.BattleState == BattleUnitState.OnFlank) if(icon.Unit.BattleState == BattleUnitState.OnFlank)
{ {
battleUI.Battle.MoveUnitToReserveRpc(icon.Unit);
battleUI.Battle.MoveUnitToReserveServerRpc(icon.Unit);
} }
} }


+ 1
- 1
Assets/GWConquest/Scripts/UI/BattleUI.cs View File

@ -480,7 +480,7 @@ namespace GWConquest
if (hasTarget) if (hasTarget)
{ {
SelectingTargetUnit.StartShellingRpc(flank);
SelectingTargetUnit.StartShellingServerRpc(flank);
} }
EndTargetSelection(); EndTargetSelection();


+ 1
- 1
Assets/GWConquest/Scripts/UI/DebugUI.cs View File

@ -59,7 +59,7 @@ namespace GWConquest
for(int i = 0; i < amount; i++) for(int i = 0; i < amount; i++)
{ {
Unit.SpawnUnit(zone, uc, player);
GWNetworkManager.Instance.SpawnUnit(zone, uc, player);
} }
} }


+ 1
- 1
Assets/GWConquest/Scripts/UI/ItemMoveTooltip.cs View File

@ -76,7 +76,7 @@ namespace GWConquest
{ {
NetworkBehaviour originBeh = Origin.Type == TransportUIElement.ObjectType.District ? Origin.District : Origin.Formation; NetworkBehaviour originBeh = Origin.Type == TransportUIElement.ObjectType.District ? Origin.District : Origin.Formation;
NetworkBehaviour targetBeh = Target.Type == TransportUIElement.ObjectType.District ? Target.District : Target.Formation; NetworkBehaviour targetBeh = Target.Type == TransportUIElement.ObjectType.District ? Target.District : Target.Formation;
ServerCallbacks.Instance.MoveItemRpc(originBeh, targetBeh, MovedItem.ItemName, current);
GWNetworkManager.Instance.MoveItemServerRpc(originBeh, targetBeh, MovedItem.ItemName, current);
} }
else { else {
BoltLog.Error("Amount {0} of item {1} is not transferable ({2})", current, MovedItem.ItemName, transferable); BoltLog.Error("Amount {0} of item {1} is not transferable ({2})", current, MovedItem.ItemName, transferable);


+ 1
- 14
Assets/GWConquest/Scripts/UI/ProductionMenu.cs View File

@ -130,20 +130,7 @@ namespace GWConquest
{ {
if(DebugUI.FreeBuild || buildable.BuildCost == null || factory.District.CanBuild(buildable)) if(DebugUI.FreeBuild || buildable.BuildCost == null || factory.District.CanBuild(buildable))
{ {
AddProductionEvent ev = AddProductionEvent.Create(GlobalTargets.OnlyServer);
ev.Factory = factory.entity;
if(buildable is UnitClass)
{
ev.UnitClass = (buildable as UnitClass).ShortName;
ev.IsUpgrade = false;
}
else if(buildable is DistrictUpgrade)
{
ev.UnitClass = (buildable as DistrictUpgrade).ShortName;
ev.IsUpgrade = true;
}
ev.Player = Player.CurrentPlayer.entity;
ev.Send();
factory.AddProductionQueueEntryServerRpc(buildable.ID, buildable is DistrictUpgrade, Player.CurrentPlayer);
} }
} }


+ 1
- 1
Assets/GWConquest/Scripts/UI/TransportUIUnitIcon.cs View File

@ -41,7 +41,7 @@ namespace GWConquest
var LeaderIcon = ParentElement.DragTransform; var LeaderIcon = ParentElement.DragTransform;
if (RectTransformUtility.RectangleContainsScreenPoint(LeaderIcon, Input.mousePosition)) if (RectTransformUtility.RectangleContainsScreenPoint(LeaderIcon, Input.mousePosition))
{ {
ParentElement.Formation.AssignLeaderRpc(Unit);
ParentElement.Formation.AssignLeaderServerRpc(Unit);
} }
} }


+ 56
- 74
Assets/GWConquest/Scripts/Unit.cs View File

@ -223,7 +223,7 @@ namespace GWConquest
Inventory.StorageCapacity = newClass.InventorySlots; Inventory.StorageCapacity = newClass.InventorySlots;
Equipment.StorageCapacity = newClass.EquipmentSlots; Equipment.StorageCapacity = newClass.EquipmentSlots;
if(IsOwner)
if(IsServer)
{ {
Fuel = newClass.FuelCapacity; Fuel = newClass.FuelCapacity;
Food = newClass.FoodCapacity; Food = newClass.FoodCapacity;
@ -253,26 +253,6 @@ namespace GWConquest
Morale = uc.Morale; Morale = uc.Morale;
} }
public static Unit SpawnUnit(Zone zone, UnitClass uc, Player player)
{
Formation playerFormation = zone.Formations.FirstOrDefault(f => f.Player == player);
if (playerFormation == null)
{
playerFormation = Formation.SpawnFormation(zone, player);
}
BoltEntity unitEntity = BoltNetwork.Instantiate(BoltPrefabs.Unit);
player.AssignControl(unitEntity);
Unit unit = unitEntity.GetComponent<Unit>();
unit.InstantiateWithClass(uc);
unit.Formation = playerFormation;
playerFormation.AddUnit(unit);
BoltLog.Info("Spawned unit {0} for player {1} in formation {2}", unit, player, playerFormation);
return unit;
}
public void TakeDamage(int damage, Unit attacker, bool isGlancingHit=false, WeaponStats weapon=null, WeaponType weaponType=WeaponType.Light) public void TakeDamage(int damage, Unit attacker, bool isGlancingHit=false, WeaponStats weapon=null, WeaponType weaponType=WeaponType.Light)
{ {
int remainingDamage = damage; int remainingDamage = damage;
@ -365,7 +345,7 @@ namespace GWConquest
{ {
animType = DamageAnimationType.GlancingHit; animType = DamageAnimationType.GlancingHit;
} }
PlayUnitDamageAnimation(weaponType, animType, attacker);
PlayUnitDamageAnimationClientRpc(weaponType, animType, attacker);
if(Hitpoints <= 0) if(Hitpoints <= 0)
{ {
@ -383,7 +363,7 @@ namespace GWConquest
} }
[ClientRpc] [ClientRpc]
public void PlayUnitDamageAnimation(WeaponType weaponType, DamageAnimationType animationType, NullableNetworkBehaviourReference attacker)
public void PlayUnitDamageAnimationClientRpc(WeaponType weaponType, DamageAnimationType animationType, NullableNetworkBehaviourReference attacker)
{ {
if(IconEnabled) if(IconEnabled)
{ {
@ -453,77 +433,79 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
var battle = CurrentBattle;
if(!IsDead && battle != null && !battle.IsInPreparing)
if (IsServer)
{ {
if(BattleState == BattleUnitState.Arriving)
var battle = CurrentBattle;
if (!IsDead && battle != null && !battle.IsInPreparing)
{ {
if(!Formation.IsMoving)
if (BattleState == BattleUnitState.Arriving)
{ {
BattleState = BattleUnitState.InReserve;
}
}
if (ActionCooldown <= 0)
{
actionCooldown.Value = 0;
if (BattleState == BattleUnitState.MovingToFlank)
{
BattleState = BattleUnitState.OnFlank;
int requiredFuel = GetFuelCostForFlank();
if(requiredFuel <= TotalFuel)
if (!Formation.IsMoving)
{ {
ConsumeTotalFuel(requiredFuel);
BattleState = BattleUnitState.InReserve;
} }
} }
else if (BattleState == BattleUnitState.MovingToReserve)
if (ActionCooldown <= 0)
{ {
BattleState = BattleUnitState.InReserve;
CurrentFlank.RemoveUnit(this);
CurrentFlank = null;
actionCooldown.Value = 0;
if (BattleState == BattleUnitState.MovingToFlank)
{
BattleState = BattleUnitState.OnFlank;
int requiredFuel = GetFuelCostForFlank();
if(requiredFuel <= TotalFuel)
int requiredFuel = GetFuelCostForFlank();
if (requiredFuel <= TotalFuel)
{
ConsumeTotalFuel(requiredFuel);
}
}
else if (BattleState == BattleUnitState.MovingToReserve)
{ {
ConsumeTotalFuel(requiredFuel);
BattleState = BattleUnitState.InReserve;
CurrentFlank.RemoveUnit(this);
CurrentFlank = null;
int requiredFuel = GetFuelCostForFlank();
if (requiredFuel <= TotalFuel)
{
ConsumeTotalFuel(requiredFuel);
}
}
else if (BattleState == BattleUnitState.StartingShelling)
{
BattleState = BattleUnitState.Shelling;
}
else if (BattleState == BattleUnitState.StoppingShelling)
{
BattleState = BattleUnitState.InReserve;
FlankTarget = null;
} }
} }
else if(BattleState == BattleUnitState.StartingShelling)
{
BattleState = BattleUnitState.Shelling;
}
else if(BattleState == BattleUnitState.StoppingShelling)
else
{ {
BattleState = BattleUnitState.InReserve;
FlankTarget = null;
actionCooldown.Value -= Time.fixedDeltaTime;
} }
}
else
{
actionCooldown.Value -= Time.fixedDeltaTime;
}
if(ShieldsCooldown > 0)
{
ShieldsCooldown -= Time.fixedDeltaTime;
if(ShieldsCooldown <= 0)
if (ShieldsCooldown > 0)
{ {
ShieldsCooldown = 0;
ShieldsCooldown -= Time.fixedDeltaTime;
if (ShieldsCooldown <= 0)
{
ShieldsCooldown = 0;
}
} }
}
if(ShieldsCooldown == 0 && Shields < Class.Shields)
{
float regRate = Class.ShieldRegeneration / GameManager.Instance.BattleTurnLength;
Shields += regRate * BoltNetwork.FrameDeltaTime;
if(Shields > Class.Shields)
if (ShieldsCooldown == 0 && Shields < Class.Shields)
{ {
Shields = Class.Shields;
float regRate = Class.ShieldRegeneration / GameManager.Instance.BattleTurnLength;
Shields += regRate * BoltNetwork.FrameDeltaTime;
if (Shields > Class.Shields)
{
Shields = Class.Shields;
}
} }
} }
} }
} }
//TODO shelling auch auf Flanke! //TODO shelling auch auf Flanke!
@ -561,7 +543,7 @@ namespace GWConquest
} }
[ServerRpc] [ServerRpc]
public void StartShellingRpc(NetworkBehaviourReference target)
public void StartShellingServerRpc(NetworkBehaviourReference target)
{ {
StartShelling(target.GetBehaviour<BattleFlank>()); StartShelling(target.GetBehaviour<BattleFlank>());
} }


Loading…
Cancel
Save