Browse Source

Netcode is working!

master
laurids 1 year ago
parent
commit
6163db4889
10 changed files with 57 additions and 53 deletions
  1. +3
    -3
      Assets/GWConquest/Scripts/Battle.cs
  2. +1
    -1
      Assets/GWConquest/Scripts/BattleFlank.cs
  3. +1
    -1
      Assets/GWConquest/Scripts/District.cs
  4. +2
    -2
      Assets/GWConquest/Scripts/DistrictFactory.cs
  5. +16
    -12
      Assets/GWConquest/Scripts/Formation.cs
  6. +13
    -8
      Assets/GWConquest/Scripts/GWNetworkManager.cs
  7. +3
    -2
      Assets/GWConquest/Scripts/GameManager.cs
  8. +2
    -2
      Assets/GWConquest/Scripts/Planet.cs
  9. +13
    -19
      Assets/GWConquest/Scripts/Player.cs
  10. +3
    -3
      Assets/GWConquest/Scripts/Unit.cs

+ 3
- 3
Assets/GWConquest/Scripts/Battle.cs View File

@ -231,7 +231,7 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsServer)
if(IsSpawned && IsServer)
{ {
if(IsInPreparing) if(IsInPreparing)
{ {
@ -674,7 +674,7 @@ namespace GWConquest
} }
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void MoveUnitToFlankServerRpc(NetworkBehaviourReference unit, NetworkBehaviourReference flank, int flankIndex) 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);
@ -687,7 +687,7 @@ namespace GWConquest
unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement); unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement);
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void MoveUnitToReserveServerRpc(NetworkBehaviourReference unit) public void MoveUnitToReserveServerRpc(NetworkBehaviourReference unit)
{ {
MoveUnitToReserve(unit.GetBehaviour<Unit>()); MoveUnitToReserve(unit.GetBehaviour<Unit>());


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

@ -124,7 +124,7 @@ namespace GWConquest {
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsServer)
if(IsSpawned && IsServer)
{ {
for(int i = 0; i < MaxUnitCount; i++) for(int i = 0; i < MaxUnitCount; i++)
{ {


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

@ -298,7 +298,7 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsServer)
if(IsSpawned && IsServer)
{ {
CheckControllingChange(); CheckControllingChange();


+ 2
- 2
Assets/GWConquest/Scripts/DistrictFactory.cs View File

@ -167,7 +167,7 @@ namespace GWConquest
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void AddProductionQueueEntryServerRpc(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);
@ -181,7 +181,7 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if(IsProducing)
if(IsServer && IsSpawned && IsProducing)
{ {
productionQueue[0] = productionQueue[0].IncreaseTime(Time.fixedDeltaTime); productionQueue[0] = productionQueue[0].IncreaseTime(Time.fixedDeltaTime);
IBuildable buildable = GetProductionQueueEntry(0); IBuildable buildable = GetProductionQueueEntry(0);


+ 16
- 12
Assets/GWConquest/Scripts/Formation.cs View File

@ -300,7 +300,7 @@ namespace GWConquest
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 && Player == Player.CurrentPlayer && CanMove)
{ {
if (!CanMoveTo(path[0], path[1])) if (!CanMoveTo(path[0], path[1]))
{ {
@ -309,12 +309,13 @@ namespace GWConquest
} }
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();
Debug.Log($"[RPC] Sending RPC: Begin Movement Server with zone path {zoneIDs}, target {targetFormation}");
BeginMovementServerRpc(zoneIDs, targetFormation); BeginMovementServerRpc(zoneIDs, targetFormation);
} }
else else
{ {
Debug.LogFormat("Could not move formation {0} (controlled: {1}, movement state: {2})",
this, IsOwner, MovementState);
Debug.LogFormat("Could not move formation {0} (player: {1}, movement state: {2})",
this, Player, MovementState);
} }
} }
@ -344,6 +345,8 @@ namespace GWConquest
pathQueue.SetEntries(zoneIDs); pathQueue.SetEntries(zoneIDs);
Debug.Log($"First in path queue is now zone {PathQueue.First()}");
if (targetFormation != null) if (targetFormation != null)
{ {
MovementTargetFormation = targetFormation; MovementTargetFormation = targetFormation;
@ -358,15 +361,16 @@ namespace GWConquest
BeginMovementServer(new int[] { Zone.GetZoneId(targetZone) }, targetFormation: targetFormation); BeginMovementServer(new int[] { Zone.GetZoneId(targetZone) }, targetFormation: targetFormation);
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void BeginMovementServerRpc(int[] zoneIDs, NullableNetworkBehaviourReference targetFormation) public void BeginMovementServerRpc(int[] zoneIDs, NullableNetworkBehaviourReference targetFormation)
{ {
Debug.Log($"[RPC] Recieved RPC: Begin Movement Server with zone path {String.Join(',', zoneIDs)}, target {targetFormation}");
BeginMovementServer(zoneIDs, targetFormation: targetFormation.GetBehaviour<Formation>()); BeginMovementServer(zoneIDs, targetFormation: targetFormation.GetBehaviour<Formation>());
} }
public void MoveToZone(Zone target) public void MoveToZone(Zone target)
{ {
if (IsOwner && MovementState == FormationMovementState.PreparingMovement)
if (MovementState == FormationMovementState.PreparingMovement)
{ {
if (!CanMoveTo(currentZone, target)) if (!CanMoveTo(currentZone, target))
{ {
@ -487,7 +491,7 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if (IsServer)
if (IsSpawned && IsServer)
{ {
if (IsEmbarked) if (IsEmbarked)
{ {
@ -563,7 +567,7 @@ namespace GWConquest
{ {
if (MovementTargetFormation != null) if (MovementTargetFormation != null)
{ {
if (MovementTargetFormation.HasNetworkObject && MovementTargetFormation.currentZone == currentZone)
if (MovementTargetFormation.IsSpawned && MovementTargetFormation.currentZone == currentZone)
{ {
MovementTargetFormation.AddSubFormation(this); MovementTargetFormation.AddSubFormation(this);
} }
@ -606,7 +610,7 @@ namespace GWConquest
private void Update() private void Update()
{ {
if (HasNetworkObject)
if (IsSpawned)
{ {
if (GameManager.EntitiesLoaded) if (GameManager.EntitiesLoaded)
{ {
@ -785,11 +789,11 @@ namespace GWConquest
public float GetFormationStrength() public float GetFormationStrength()
{ {
if (HasNetworkObject)
if (IsSpawned)
{ {
return Units.Sum(unit => return Units.Sum(unit =>
{ {
if (unit != null && unit.HasNetworkObject)
if (unit != null && unit.IsSpawned)
{ {
return unit.Class.UnitStrength; return unit.Class.UnitStrength;
} }
@ -1057,7 +1061,7 @@ namespace GWConquest
} }
} }
[ClientRpc]
[ClientRpc(Delivery = RpcDelivery.Unreliable)]
public void PlayFormationDemoralizedAnimationClientRpc() public void PlayFormationDemoralizedAnimationClientRpc()
{ {
if (HeroUnit != null && HeroUnit.CurrentIcon != null) if (HeroUnit != null && HeroUnit.CurrentIcon != null)
@ -1102,7 +1106,7 @@ namespace GWConquest
} }
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void AssignLeaderServerRpc(NullableNetworkBehaviourReference unit) public void AssignLeaderServerRpc(NullableNetworkBehaviourReference unit)
{ {
heroUnit.Value = unit; heroUnit.Value = unit;


+ 13
- 8
Assets/GWConquest/Scripts/GWNetworkManager.cs View File

@ -5,6 +5,7 @@ using System.Linq;
namespace GWConquest { namespace GWConquest {
public class GWNetworkManager : NetworkBehaviour public class GWNetworkManager : NetworkBehaviour
{ {
public static bool SceneSynchronized = false;
private static GWNetworkManager _instance; private static GWNetworkManager _instance;
public static GWNetworkManager Instance { public static GWNetworkManager Instance {
@ -30,7 +31,7 @@ namespace GWConquest {
private void OnSceneEvent(SceneEvent sceneEvent) private void OnSceneEvent(SceneEvent sceneEvent)
{ {
Debug.LogFormat("GWNetworkManager: On Scene Event", sceneEvent);
Debug.LogFormat("GWNetworkManager: On Scene Event with type {0} and client id {1}", sceneEvent.SceneEventType, sceneEvent.ClientId);
if(IsServer) if(IsServer)
{ {
if(sceneEvent.SceneEventType == SceneEventType.LoadComplete) if(sceneEvent.SceneEventType == SceneEventType.LoadComplete)
@ -48,10 +49,14 @@ namespace GWConquest {
} }
else { else {
if(sceneEvent.SceneEventType == SceneEventType.LoadComplete)
if(sceneEvent.SceneEventType == SceneEventType.SynchronizeComplete)
{ {
ClientSceneLoadComplete();
GameManager.Instance.SceneLoadLocalDone(true);
SceneSynchronized = true;
if(Player.CurrentPlayer != null)
{
Debug.Log("Player is already synchronized, completing scene load now");
ClientSceneLoadComplete();
}
} }
} }
} }
@ -100,16 +105,16 @@ namespace GWConquest {
public void ServerSceneLoadCompleteRemote(ulong clientID) public void ServerSceneLoadCompleteRemote(ulong clientID)
{ {
Player clientPlayer = GameManager.Instance.SpawnPlayer((ushort) ((GameManager.HostFactionIndex + 1) % 2));
clientPlayer.NetworkObject.ChangeOwnership(clientID);
Player clientPlayer = GameManager.Instance.SpawnPlayer((ushort) ((GameManager.HostFactionIndex + 1) % 2), clientID);
} }
public void ClientSceneLoadComplete() public void ClientSceneLoadComplete()
{ {
GameOptions.ApplyOptions(); GameOptions.ApplyOptions();
GameManager.Instance.SceneLoadLocalDone(true);
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void MoveItemServerRpc(NetworkBehaviourReference origin, NetworkBehaviourReference target, ushort itemID, int itemAmount) public void MoveItemServerRpc(NetworkBehaviourReference origin, NetworkBehaviourReference target, ushort itemID, int itemAmount)
{ {
NetworkBehaviour originBeh = origin.GetBehaviour(); NetworkBehaviour originBeh = origin.GetBehaviour();
@ -148,7 +153,7 @@ namespace GWConquest {
return unit; return unit;
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void SpawnUnitServerRpc(int zoneID, ushort unitClassID, int playerID) public void SpawnUnitServerRpc(int zoneID, ushort unitClassID, int playerID)
{ {
SpawnUnit(Zone.GetFromId(zoneID), UnitClass.FromID(unitClassID), Player.GetPlayerById(playerID)); SpawnUnit(Zone.GetFromId(zoneID), UnitClass.FromID(unitClassID), Player.GetPlayerById(playerID));


+ 3
- 2
Assets/GWConquest/Scripts/GameManager.cs View File

@ -139,10 +139,10 @@ namespace GWConquest
IsLoaded = true; IsLoaded = true;
} }
public Player SpawnPlayer(ushort faction)
public Player SpawnPlayer(ushort faction, ulong clientID=0)
{ {
GameObject go = Instantiate(PlayerPrefab); GameObject go = Instantiate(PlayerPrefab);
go.GetComponent<NetworkObject>().Spawn();
go.GetComponent<NetworkObject>().SpawnWithOwnership(clientID);
Player player = go.GetComponent<Player>(); Player player = go.GetComponent<Player>();
player.SetFaction(faction); player.SetFaction(faction);
player.AssignStartingPlanets(); player.AssignStartingPlanets();
@ -151,6 +151,7 @@ namespace GWConquest
public void SceneLoadLocalDone(bool isClient) public void SceneLoadLocalDone(bool isClient)
{ {
Debug.LogFormat("Scene load local done in GameManager. Is client:{0}", isClient);
EntitiesLoaded = true; EntitiesLoaded = true;
if(isClient) if(isClient)


+ 2
- 2
Assets/GWConquest/Scripts/Planet.cs View File

@ -266,7 +266,7 @@ namespace GWConquest
public void FinishSetup() public void FinishSetup()
{ {
if(IsOwner)
if(IsServer)
{ {
UpdateConnectionsServer(); UpdateConnectionsServer();
} }
@ -353,7 +353,7 @@ namespace GWConquest
private void FixedUpdate() private void FixedUpdate()
{ {
if(GameManager.EntitiesLoaded && formationsChangedSinceLastFrame)
if(GameManager.EntitiesLoaded && IsSpawned && formationsChangedSinceLastFrame)
{ {
FormationsChanged?.Invoke(); FormationsChanged?.Invoke();


+ 13
- 19
Assets/GWConquest/Scripts/Player.cs View File

@ -64,13 +64,25 @@ namespace GWConquest
{ {
Debug.Log("Attaching player..."); Debug.Log("Attaching player...");
PlayerList.Add(this); PlayerList.Add(this);
if(IsOwner)
if(IsServer)
{ {
playerID.Value = (ushort) PlayerList.IndexOf(this); playerID.Value = (ushort) PlayerList.IndexOf(this);
} }
knownPlanetsList.OnListChanged += OnPlanetsUpdated; knownPlanetsList.OnListChanged += OnPlanetsUpdated;
visiblePlanetsList.OnListChanged += OnPlanetsUpdated; visiblePlanetsList.OnListChanged += OnPlanetsUpdated;
if(!IsServer)
{
CurrentPlayer = this;
Debug.LogFormat("Current player is {0} with connection ID {1}", this, OwnerClientId);
if(GWNetworkManager.SceneSynchronized)
{
Debug.Log("The rest of the scene is synchronized. Completing scene load...");
GWNetworkManager.Instance.ClientSceneLoadComplete();
}
}
} }
@ -80,15 +92,6 @@ namespace GWConquest
PlayerList.Remove(this); PlayerList.Remove(this);
} }
public override void OnGainedOwnership()
{
if(!IsServer)
{
CurrentPlayer = this;
Debug.LogFormat("Current player is {0} with connection ID {1}", this, OwnerClientId);
}
}
public void SetFaction(ushort ind) public void SetFaction(ushort ind)
{ {
factionIndex.Value = ind; factionIndex.Value = ind;
@ -114,15 +117,6 @@ namespace GWConquest
} }
} }
public void AssignControl(NetworkObject obj)
{
if(!IsAI)
{
obj.ChangeOwnership(OwnerClientId);
}
}
public void AssignStartingPlanets() public void AssignStartingPlanets()
{ {
if(Faction.StartingPlanets != null) if(Faction.StartingPlanets != null)


+ 3
- 3
Assets/GWConquest/Scripts/Unit.cs View File

@ -362,7 +362,7 @@ namespace GWConquest
Death, Damage, Demoralized, ShieldDamage, ShieldBroken, GlancingHit Death, Damage, Demoralized, ShieldDamage, ShieldBroken, GlancingHit
} }
[ClientRpc]
[ClientRpc(Delivery = RpcDelivery.Unreliable)]
public void PlayUnitDamageAnimationClientRpc(WeaponType weaponType, DamageAnimationType animationType, NullableNetworkBehaviourReference attacker) public void PlayUnitDamageAnimationClientRpc(WeaponType weaponType, DamageAnimationType animationType, NullableNetworkBehaviourReference attacker)
{ {
if(IconEnabled) if(IconEnabled)
@ -433,7 +433,7 @@ namespace GWConquest
public void FixedUpdate() public void FixedUpdate()
{ {
if (IsServer)
if (IsSpawned && IsServer)
{ {
var battle = CurrentBattle; var battle = CurrentBattle;
if (!IsDead && battle != null && !battle.IsInPreparing) if (!IsDead && battle != null && !battle.IsInPreparing)
@ -542,7 +542,7 @@ namespace GWConquest
SetActionCooldown(GameManager.Instance.ArtilleryCooldown / movement); SetActionCooldown(GameManager.Instance.ArtilleryCooldown / movement);
} }
[ServerRpc]
[ServerRpc(RequireOwnership = false)]
public void StartShellingServerRpc(NetworkBehaviourReference target) public void StartShellingServerRpc(NetworkBehaviourReference target)
{ {
StartShelling(target.GetBehaviour<BattleFlank>()); StartShelling(target.GetBehaviour<BattleFlank>());


Loading…
Cancel
Save