Browse Source

(WIP) Unity Netcode Part 3

master
laurids 1 year ago
parent
commit
620165d1ae
16 changed files with 77 additions and 60 deletions
  1. +15
    -5
      Assets/GWConquest/Scripts/Battle.cs
  2. +14
    -0
      Assets/GWConquest/Scripts/District.cs
  3. +12
    -0
      Assets/GWConquest/Scripts/Planet.cs
  4. +10
    -12
      Assets/GWConquest/Scripts/PlanetPlacement.cs
  5. +3
    -3
      Assets/GWConquest/Scripts/UI/BalanceUI.cs
  6. +4
    -11
      Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs
  7. +1
    -3
      Assets/GWConquest/Scripts/UI/BattleFlankUI.cs
  8. +1
    -1
      Assets/GWConquest/Scripts/UI/BattleFormationUI.cs
  9. +3
    -6
      Assets/GWConquest/Scripts/UI/BattleUI.cs
  10. +1
    -1
      Assets/GWConquest/Scripts/UI/BattleUnitIcon.cs
  11. +1
    -1
      Assets/GWConquest/Scripts/UI/DebugUI.cs
  12. +1
    -1
      Assets/GWConquest/Scripts/UI/DistrictIcon.cs
  13. +4
    -6
      Assets/GWConquest/Scripts/UI/ItemMoveTooltip.cs
  14. +3
    -3
      Assets/GWConquest/Scripts/UI/PlanetViewUI.cs
  15. +3
    -3
      Assets/GWConquest/Scripts/UI/StickToFormation.cs
  16. +1
    -4
      Assets/GWConquest/Scripts/UI/TransportUIUnitIcon.cs

+ 15
- 5
Assets/GWConquest/Scripts/Battle.cs View File

@ -128,6 +128,10 @@ namespace GWConquest
get => PreparingCooldown > 0;
}
public Player Victor {
get => Player.GetPlayerById(victorID.Value);
}
private void OnZoneChanged(int oldID, int newID)
{
if(newID != -1)
@ -499,11 +503,7 @@ namespace GWConquest
}
}
var evnt = UnitAttackingEvent.Create(action.Unit.entity);
evnt.Target = target.entity;
evnt.Hit = hits;
evnt.WeaponType = (int)weaponType;
evnt.Send();
action.Unit.PlayUnitAttackingAnimation(target, hits, weaponType);
}
private Unit FindTargetForCombat(UnitAction action)
@ -664,6 +664,11 @@ namespace GWConquest
}
}
public void MoveUnitToFlankRpc(NetworkBehaviourReference unit, NetworkBehaviourReference flank, int flankIndex)
{
MoveUnitToFlank(unit.GetBehaviour<Unit>(), flank.GetBehaviour<BattleFlank>(), flankIndex);
}
public void MoveUnitToReserve(Unit unit)
{
unit.BattleState = BattleUnitState.MovingToReserve;
@ -671,6 +676,11 @@ namespace GWConquest
unit.SetActionCooldown(GameManager.Instance.MoveToReserveCooldown / movement);
}
public void MoveUnitToReserveRpc(NetworkBehaviourReference unit)
{
MoveUnitToReserve(unit.GetBehaviour<Unit>());
}
public static bool Check(float chance) {
return Random.Range(0f, 1f) <= chance;
}


+ 14
- 0
Assets/GWConquest/Scripts/District.cs View File

@ -88,6 +88,10 @@ namespace GWConquest
set => storageCapacity.Value = value;
}
public float CaptureCooldown {
get => captureCooldown.Value;
}
public override void OnNetworkSpawn()
{
@ -156,6 +160,16 @@ namespace GWConquest
InitConnections();
}
public void AddConnection(District other)
{
connectedDistricts.Add(other);
}
public void SetPlanet(Planet p)
{
planet.Value = p;
}
public void InitConnections()
{
foreach(var reference in connectedDistricts)


+ 12
- 0
Assets/GWConquest/Scripts/Planet.cs View File

@ -54,6 +54,7 @@ namespace GWConquest
public string PlanetName {
get => planetNameVar.ToString();
set => planetNameVar.Value = value;
}
public string BackgroundMusic;
@ -80,6 +81,10 @@ namespace GWConquest
get => PlanetRegistry.GetPlanetData(gameObject.name);
}
public ushort PrefabID => planetPrefab.Value;
public Quaternion PrefabRotation => prefabRotation.Value;
public float PrefabScale => prefabScale.Value;
public PlanetIndicatorUI indicatorUI {get; private set;}
public List<Formation> InTransitFormations = new List<Formation>();
@ -527,6 +532,13 @@ namespace GWConquest
return CircleTransform.lossyScale.x * 0.5f;
}
public void SetPrefabProperties(ushort id, Quaternion rotation, float scale)
{
planetPrefab.Value = id;
prefabRotation.Value = rotation;
prefabScale.Value = scale;
}
#if UNITY_EDITOR
[ContextMenu("Setup Zones From Database")]


+ 10
- 12
Assets/GWConquest/Scripts/PlanetPlacement.cs View File

@ -124,14 +124,12 @@ namespace GWConquest
var planet = planetGO.GetComponent<Planet>();
var planetName = DrawName();
planet.State.PlanetName = planetName;
planet.PlanetName = planetName;
planetGO.name = planetName;
int prefabId = Random.Range(0, PlanetPrefabs.Length);
planet.State.PlanetPrefab = prefabId;
planet.State.PrefabRotation = Random.rotationUniform;
planet.State.PrefabScale = Random.Range(MinPlanetScale, MaxPlanetScale);
planet.SetPrefabProperties((ushort) prefabId, Random.rotationUniform, Random.Range(MinPlanetScale, MaxPlanetScale));
InitPlanetPrefab(planet);
@ -152,13 +150,13 @@ namespace GWConquest
public void InitPlanetPrefab(Planet planet)
{
var prefab = PlanetPrefabs[planet.State.PlanetPrefab];
var prefab = PlanetPrefabs[planet.PrefabID];
var sphereGO = Instantiate(prefab);
sphereGO.transform.SetParent(planet.transform);
sphereGO.transform.localPosition = Vector3.zero;
sphereGO.transform.localRotation = planet.State.PrefabRotation;
sphereGO.transform.localRotation = planet.PrefabRotation;
float scale = planet.State.PrefabScale * prefab.transform.localScale.x;
float scale = planet.PrefabScale * prefab.transform.localScale.x;
sphereGO.transform.localScale = scale * Vector3.one;
planet.PrefabObject = sphereGO;
@ -228,7 +226,7 @@ namespace GWConquest
string nameCandidate;
if(type == DistrictType.Civil)
{
nameCandidate = planet.State.PlanetName + " " + PlanetRegistry.CivilNames[i];
nameCandidate = planet.PlanetName + " " + PlanetRegistry.CivilNames[i];
}
else
{
@ -252,7 +250,7 @@ namespace GWConquest
zone.zoneType = ZoneType.Ground;
nextZoneId++;
district.State.Planet = BoltEntityCache.Set(planet);
district.SetPlanet(planet);
var otherDistricts = planet.GetComponentsInChildren<District>().ToList();
@ -260,8 +258,8 @@ namespace GWConquest
{
var otherDistrict = otherDistricts[0];
district.ConnectionList.Add(otherDistrict.entity);
otherDistrict.ConnectionList.Add(district.entity);
district.AddConnection(otherDistrict);
otherDistrict.AddConnection(district);
otherDistricts.RemoveAt(0);
@ -399,7 +397,7 @@ namespace GWConquest
{
float connLength = conn.GetLength();
int planetCount = Mathf.RoundToInt( Random.Range(connLength / MaxPlanetDistance, connLength / MinPlanetDistance) ) - 1;
BoltLog.Info("Spawning {0} planets between {1} and {2}", planetCount, conn.planet1.planetName, conn.planet2.planetName);
BoltLog.Info("Spawning {0} planets between {1} and {2}", planetCount, conn.planet1.PlanetName, conn.planet2.PlanetName);
Vector3 start = conn.planet1.transform.position;
Vector3 dir = conn.planet2.transform.position - start;


+ 3
- 3
Assets/GWConquest/Scripts/UI/BalanceUI.cs View File

@ -33,9 +33,9 @@ namespace GWConquest
{
if(d.ControllingPlayer == currentPlayer)
{
totalFood += d.Inventory.GetItemAmount("Food");
totalSupplies += d.Inventory.GetItemAmount("Supplies");
totalFuel += d.Inventory.GetItemAmount("Fuel");
totalFood += d.Inventory.GetItemAmount(ItemRegistry.kFood);
totalSupplies += d.Inventory.GetItemAmount(ItemRegistry.kSupplies);
totalFuel += d.Inventory.GetItemAmount(ItemRegistry.kFuel);
foreach(var (item, amount) in d.ItemProductionList)
{


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

@ -44,12 +44,12 @@ namespace GWConquest {
if(IsOwnPlayer)
{
return Battle.FormationList.Select((e,i) => e.Entity.GetComponent<Formation>()).Where(f => f.Player == Player.CurrentPlayer );
return Battle.Formations.Where(f => f.Player == Player.CurrentPlayer );
}
else {
return Battle.FormationList.Select((e,i) => e.Entity.GetComponent<Formation>()).Where(f => f.Player != Player.CurrentPlayer );
return Battle.Formations.Where(f => f.Player != Player.CurrentPlayer );
}
}
@ -237,10 +237,7 @@ namespace GWConquest {
var fui = icon.GetComponentInParent<BattleFormationUI>();
if(fui != null && target == fui.LeaderIcon)
{
var ev = AssignLeaderEvent.Create(GlobalTargets.OnlyServer);
ev.Unit = icon.Unit.entity;
ev.Formation = icon.Unit.Formation.entity;
ev.Send();
icon.Unit.Formation.AssignLeaderRpc(icon.Unit);
}
}
else if(icon.Unit.BattleState == BattleUnitState.InReserve && icon.Unit.CanGoToFlank)
@ -253,11 +250,7 @@ namespace GWConquest {
if(flankUI.BattleFlank.GetUnit(flankIndex) == null)
{
var ev = MoveUnitToFlankEvent.Create(GlobalTargets.OnlyServer);
ev.Unit = icon.Unit.entity;
ev.Flank = flankUI.BattleFlank.entity;
ev.FlankIndex = flankIndex;
ev.Send();
Battle.MoveUnitToFlankRpc(icon.Unit, flankUI.BattleFlank, flankIndex);
}


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

@ -107,9 +107,7 @@ namespace GWConquest {
{
if(icon.Unit.BattleState == BattleUnitState.OnFlank)
{
MoveUnitToReserveEvent ev = MoveUnitToReserveEvent.Create(GlobalTargets.OnlyServer);
ev.Unit = icon.Unit.entity;
ev.Send();
battleUI.Battle.MoveUnitToReserveRpc(icon.Unit);
}
}


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

@ -50,7 +50,7 @@ namespace GWConquest {
public int GetSortingKey()
{
return (int) Formation.entity.NetworkId.PackedValue;
return (int) Formation.NetworkObjectId;
}
public float GetHeight()


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

@ -128,7 +128,7 @@ namespace GWConquest
uiRT.localScale = Vector3.one;
var ui = go.GetComponent<BattleFlankUI>();
ui.Init(Battle.GetFlank(i));
ui.Init(Battle.GetFlank((ushort) i));
FlankUIs[i] = ui;
@ -480,10 +480,7 @@ namespace GWConquest
if (hasTarget)
{
var ev = StartShellingEvent.Create(GlobalTargets.OnlyServer);
ev.Unit = SelectingTargetUnit.entity;
ev.BattleFlank = flank?.entity;
ev.Send();
SelectingTargetUnit.StartShellingRpc(flank);
}
EndTargetSelection();
@ -602,7 +599,7 @@ namespace GWConquest
FlankPanel.gameObject.SetActive(false);
VictoryPanel.SetActive(true);
if(Battle.State.VictorID == Player.CurrentPlayer.PlayerId)
if(Battle.Victor == Player.CurrentPlayer)
{
VictoryText.SetActive(true);
DefeatText.SetActive(false);


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

@ -68,7 +68,7 @@ namespace GWConquest {
return string.Compare(a.Class.ShortName, b.Class.ShortName);
}
else {
return System.Math.Sign((long)a.entity.NetworkId.PackedValue - (long)b.entity.NetworkId.PackedValue);
return System.Math.Sign((long)a.NetworkObjectId - (long)b.NetworkObjectId);
}
}


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

@ -82,7 +82,7 @@ namespace GWConquest
Zone zone = planet.GetMainZone(ZoneType.Ground);
zone.GetComponent<District>().Inventory.AddItem(item, amount);
zone.GetComponent<District>().Inventory.AddItem(ItemRegistry.Instance.IDFromName(item), amount);
}
}


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

@ -160,7 +160,7 @@ namespace GWConquest
animator.SetBool("Battle", hasBattle);
}
var captureCooldown = district.State.CaptureCooldown;
var captureCooldown = district.CaptureCooldown;
var isCapturing = captureCooldown > 0;
if (animator.GetBool("Capturing") != isCapturing)
{


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

@ -4,6 +4,7 @@ using UnityEngine;
using UnityEngine.UI;
using Photon.Bolt;
using Photon.Bolt.Utils;
using Unity.Netcode;
namespace GWConquest
{
@ -73,12 +74,9 @@ namespace GWConquest
int transferable = GetMaxTransferable();
if(current <= transferable)
{
var ev = MoveItemEvent.Create(GlobalTargets.OnlyServer);
ev.ItemName = MovedItem.ItemName;
ev.ItemAmount = current;
ev.Origin = Origin.Type == TransportUIElement.ObjectType.District ? Origin.District.entity : Origin.Formation.entity;
ev.Target = Target.Type == TransportUIElement.ObjectType.District ? Target.District.entity : Target.Formation.entity;
ev.Send();
NetworkBehaviour originBeh = Origin.Type == TransportUIElement.ObjectType.District ? Origin.District : Origin.Formation;
NetworkBehaviour targetBeh = Target.Type == TransportUIElement.ObjectType.District ? Target.District : Target.Formation;
ServerCallbacks.Instance.MoveItemRpc(originBeh, targetBeh, MovedItem.ItemName, current);
}
else {
BoltLog.Error("Amount {0} of item {1} is not transferable ({2})", current, MovedItem.ItemName, transferable);


+ 3
- 3
Assets/GWConquest/Scripts/UI/PlanetViewUI.cs View File

@ -120,7 +120,7 @@ namespace GWConquest
public void SpawnLandingLine(Formation f)
{
BoltLog.Info("Spawning landing line");
if(Zone.GetFromId(f.State.CurrentTransition.OriginZone).planet == selectedPlanet)
if(f.CurrentTransition.OriginZone.planet == selectedPlanet)
{
bool isLanding = f.MovementOriginFormation != null;
Transform startTrans;
@ -128,12 +128,12 @@ namespace GWConquest
if (isLanding)
{
startTrans = FleetIcons.FleetIcons.FirstOrDefault(i => i.formation == f.MovementOriginFormation).transform;
endTrans = DistrictIconList.FirstOrDefault(i => Zone.GetZoneId(i.zone) == f.State.CurrentTransition.TargetZone).transform;
endTrans = DistrictIconList.FirstOrDefault(i => i.zone == f.CurrentTransition.TargetZone).transform;
}
else
{
endTrans = FleetIcons.FleetIcons.FirstOrDefault(i => i.formation == f.MovementTargetFormation).transform;
startTrans = DistrictIconList.FirstOrDefault(i => Zone.GetZoneId(i.zone) == f.State.CurrentTransition.OriginZone).transform;
startTrans = DistrictIconList.FirstOrDefault(i => i.zone == f.CurrentTransition.OriginZone).transform;
}
GameObject lineGO = Instantiate(LandingLinePrefab);


+ 3
- 3
Assets/GWConquest/Scripts/UI/StickToFormation.cs View File

@ -20,10 +20,10 @@ namespace GWConquest
}
else {
Vector3 dir;
if(formation.MovementState == FormationMovementState.Moving && formation.State.CurrentTransition.IsCurved)
if(formation.MovementState == FormationMovementState.Moving && formation.CurrentTransition.IsCurved)
{
var originZone = Zone.GetFromId(formation.State.CurrentTransition.OriginZone);
var targetZone = Zone.GetFromId(formation.State.CurrentTransition.TargetZone);
var originZone = formation.CurrentTransition.OriginZone;
var targetZone = formation.CurrentTransition.TargetZone;
var districtIcons = IngameUI.Instance.PlanetView.DistrictIconList;
var fleetIcons = IngameUI.Instance.PlanetView.FleetIcons.FleetIcons;


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

@ -41,10 +41,7 @@ namespace GWConquest
var LeaderIcon = ParentElement.DragTransform;
if (RectTransformUtility.RectangleContainsScreenPoint(LeaderIcon, Input.mousePosition))
{
AssignLeaderEvent ev = AssignLeaderEvent.Create(GlobalTargets.OnlyServer);
ev.Formation = ParentElement.Formation.entity;
ev.Unit = Unit.entity;
ev.Send();
ParentElement.Formation.AssignLeaderRpc(Unit);
}
}


Loading…
Cancel
Save