2 Commits

Author SHA1 Message Date
  laurids a3a1a8de20 fix memory leaks 5 months ago
  laurids a71b7b40c7 flank flip; move only to first fields 5 months ago
13 changed files with 272 additions and 91 deletions
Unified View
  1. +16
    -16
      Assets/GWConquest/Scenes/GalaxyMap.unity
  2. +29
    -7
      Assets/GWConquest/Scripts/Battle.cs
  3. +124
    -33
      Assets/GWConquest/Scripts/BattleFlank.cs
  4. +10
    -3
      Assets/GWConquest/Scripts/District.cs
  5. +6
    -1
      Assets/GWConquest/Scripts/DistrictFactory.cs
  6. +10
    -3
      Assets/GWConquest/Scripts/Formation.cs
  7. +10
    -6
      Assets/GWConquest/Scripts/GWNetworkManager.cs
  8. +6
    -1
      Assets/GWConquest/Scripts/Planet.cs
  9. +8
    -2
      Assets/GWConquest/Scripts/Player.cs
  10. +10
    -6
      Assets/GWConquest/Scripts/UI/BattleArmyPanel.cs
  11. +16
    -2
      Assets/GWConquest/Scripts/UI/BattleFlankUI.cs
  12. +8
    -2
      Assets/GWConquest/Scripts/Unit.cs
  13. +19
    -9
      Assets/GWConquest/Scripts/Zone.cs

+ 16
- 16
Assets/GWConquest/Scenes/GalaxyMap.unity View File

@ -28409,7 +28409,7 @@ MonoBehaviour:
BetweenTransitionLengthFactor: 60 BetweenTransitionLengthFactor: 60
battleTurnLength: 30 battleTurnLength: 30
battleTurnDeviation: 10 battleTurnDeviation: 10
battleBeginPhaseLength: 0
battleBeginPhaseLength: 30
battleCommandPhaseLength: 20 battleCommandPhaseLength: 20
battleCombatPhaseLength: 20 battleCombatPhaseLength: 20
moveToFlankCooldown: 15 moveToFlankCooldown: 15
@ -33393,7 +33393,7 @@ PrefabInstance:
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 13
value: 12.88861
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -33514,7 +33514,7 @@ PrefabInstance:
- target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_AnchoredPosition.x propertyPath: m_AnchoredPosition.x
value: -65.8
value: -65.688614
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -46408,7 +46408,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &765894939 --- !u!224 &765894939
RectTransform: RectTransform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -61805,12 +61805,12 @@ PrefabInstance:
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
propertyPath: m_AnchorMax.x propertyPath: m_AnchorMax.x
value: 0
value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
propertyPath: m_AnchorMax.y propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
@ -74360,7 +74360,7 @@ PrefabInstance:
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 13
value: 12.88861
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4096209424460686483, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686483, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -74411,7 +74411,7 @@ PrefabInstance:
- target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_AnchoredPosition.x propertyPath: m_AnchoredPosition.x
value: 65.87012
value: 65.75873
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -119791,7 +119791,7 @@ PrefabInstance:
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 13
value: 12.88861
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -119917,7 +119917,7 @@ PrefabInstance:
- target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_AnchoredPosition.x propertyPath: m_AnchoredPosition.x
value: -65.8
value: -65.688614
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -124486,7 +124486,7 @@ RectTransform:
m_AnchorMin: {x: 0.5, y: 1} m_AnchorMin: {x: 0.5, y: 1}
m_AnchorMax: {x: 0.5, y: 1} m_AnchorMax: {x: 0.5, y: 1}
m_AnchoredPosition: {x: 0, y: -107.51172} m_AnchoredPosition: {x: 0, y: -107.51172}
m_SizeDelta: {x: 0, y: 40.225113}
m_SizeDelta: {x: 112.77534, y: 40.225113}
m_Pivot: {x: 0.5, y: 0.5} m_Pivot: {x: 0.5, y: 0.5}
--- !u!114 &1981011823 --- !u!114 &1981011823
MonoBehaviour: MonoBehaviour:
@ -125588,7 +125588,7 @@ GameObject:
m_Icon: {fileID: 0} m_Icon: {fileID: 0}
m_NavMeshLayer: 0 m_NavMeshLayer: 0
m_StaticEditorFlags: 0 m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!224 &1996625630 --- !u!224 &1996625630
RectTransform: RectTransform:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -133026,12 +133026,12 @@ PrefabInstance:
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
propertyPath: m_AnchorMax.x propertyPath: m_AnchorMax.x
value: 0
value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
propertyPath: m_AnchorMax.y propertyPath: m_AnchorMax.y
value: 0
value: 1
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9, - target: {fileID: 77748483005084366, guid: f69b4b94559d7a14d99e0d870647c1f9,
type: 3} type: 3}
@ -134072,7 +134072,7 @@ PrefabInstance:
- target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686482, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_SizeDelta.x propertyPath: m_SizeDelta.x
value: 13
value: 12.88861
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 4096209424460686483, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4096209424460686483, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
@ -134097,7 +134097,7 @@ PrefabInstance:
- target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 4411086135142167815, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}
propertyPath: m_AnchoredPosition.x propertyPath: m_AnchoredPosition.x
value: 65.87012
value: 65.75873
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d, - target: {fileID: 5945601791339210574, guid: ada9ee8cb263a4e4ba2f5874cfe9098d,
type: 3} type: 3}


+ 29
- 7
Assets/GWConquest/Scripts/Battle.cs View File

@ -8,14 +8,15 @@ namespace GWConquest
{ {
public class Battle : NetworkBehaviour public class Battle : NetworkBehaviour
{ {
private GWNetworkList<NetworkBehaviourReference> formations = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> formations;
private NetworkVariable<int> zoneID = new NetworkVariable<int>(); private NetworkVariable<int> zoneID = new NetworkVariable<int>();
private NetworkVariable<FixedString128Bytes> battleName = new NetworkVariable<FixedString128Bytes>(); private NetworkVariable<FixedString128Bytes> battleName = new NetworkVariable<FixedString128Bytes>();
private GWNetworkList<NullableNetworkBehaviourReference> flanks = new GWNetworkList<NullableNetworkBehaviourReference>();
private GWNetworkList<NullableNetworkBehaviourReference> flanks;
private NetworkVariable<int> flankCount = new NetworkVariable<int>(); private NetworkVariable<int> flankCount = new NetworkVariable<int>();
private NetworkVariable<int> victorID = new NetworkVariable<int>(); private NetworkVariable<int> victorID = new NetworkVariable<int>();
private NetworkVariable<float> phaseCooldown = new NetworkVariable<float>(); private NetworkVariable<float> phaseCooldown = new NetworkVariable<float>();
private NetworkVariable<BattlePhase> battlePhase = new NetworkVariable<BattlePhase>(); private NetworkVariable<BattlePhase> battlePhase = new NetworkVariable<BattlePhase>();
private NetworkVariable<int> defenderPlayerID = new();
public GameObject BattleFlankPrefab; public GameObject BattleFlankPrefab;
@ -111,7 +112,7 @@ namespace GWConquest
public Zone Zone public Zone Zone
{ {
get => Zone.GetFromId(zoneID.Value); get => Zone.GetFromId(zoneID.Value);
set
private set
{ {
zoneID.Value = Zone.GetZoneId(value); zoneID.Value = Zone.GetZoneId(value);
transform.position = value.transform.position; transform.position = value.transform.position;
@ -150,6 +151,11 @@ namespace GWConquest
get => Player.GetPlayerById(victorID.Value); get => Player.GetPlayerById(victorID.Value);
} }
public Player Defender
{
get => Player.GetPlayerById(defenderPlayerID.Value);
}
private void OnZoneChanged(int oldID, int newID) private void OnZoneChanged(int oldID, int newID)
{ {
if (newID != -1) if (newID != -1)
@ -165,20 +171,32 @@ namespace GWConquest
base.OnNetworkSpawn(); base.OnNetworkSpawn();
} }
private void Awake()
{
formations = new();
flanks = new();
}
private BattleFlank InstantiateNewFlank(int maxUnitCount, int rowCount) private BattleFlank InstantiateNewFlank(int maxUnitCount, int rowCount)
{ {
GameObject go = Instantiate(BattleFlankPrefab); GameObject go = Instantiate(BattleFlankPrefab);
go.GetComponent<NetworkObject>().Spawn(); go.GetComponent<NetworkObject>().Spawn();
var flank = go.GetComponent<BattleFlank>(); var flank = go.GetComponent<BattleFlank>();
flank.Battle = this;
flank.MaxUnitCount = maxUnitCount;
flank.RowCount = rowCount;
flank.Init(this, maxUnitCount, rowCount, 2.5f, 2.5f);
return flank; return flank;
} }
public void Init()
public void Init(Zone zone, Player defenderPlayer)
{ {
Zone = zone;
foreach(Formation f in zone.Formations)
{
AddFormation(f);
}
defenderPlayerID.Value = defenderPlayer.PlayerId;
Phase = BattlePhase.Begin; Phase = BattlePhase.Begin;
PhaseCooldown = GameManager.Instance.GetBattlePhaseLength(BattlePhase.Begin); PhaseCooldown = GameManager.Instance.GetBattlePhaseLength(BattlePhase.Begin);
@ -691,6 +709,10 @@ namespace GWConquest
{ {
Debug.LogErrorFormat("Tried to move unit {0} to flank index {1} on flank {2}, but it was already occupied by {3}", unit, flankIndex, flank, flank.GetUnit(flankIndex)); Debug.LogErrorFormat("Tried to move unit {0} to flank index {1} on flank {2}, but it was already occupied by {3}", unit, flankIndex, flank, flank.GetUnit(flankIndex));
} }
else if (!flank.CanPlayerMoveToField(flankIndex, unit.Player, Phase))
{
Debug.LogErrorFormat("Tried to move unit {0} to flank index {1} on flank {2}, but it is out of movement range", unit, flankIndex, flank);
}
else else
{ {
Debug.LogFormat("Moving unit {0} to flank index {1} on flank {2}", unit, flankIndex, flank); Debug.LogFormat("Moving unit {0} to flank index {1} on flank {2}", unit, flankIndex, flank);


+ 124
- 33
Assets/GWConquest/Scripts/BattleFlank.cs View File

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Unity.Netcode; using Unity.Netcode;
@ -12,9 +13,10 @@ namespace GWConquest
private NetworkVariable<ushort> flankID = new NetworkVariable<ushort>(); private NetworkVariable<ushort> flankID = new NetworkVariable<ushort>();
private NetworkVariable<int> maxUnitCount = new NetworkVariable<int>(); private NetworkVariable<int> maxUnitCount = new NetworkVariable<int>();
private NetworkVariable<int> rowCount = new NetworkVariable<int>(); private NetworkVariable<int> rowCount = new NetworkVariable<int>();
private GWNetworkList<NullableNetworkBehaviourReference> units = new GWNetworkList<NullableNetworkBehaviourReference>();
private GWNetworkList<float> deathCooldowns = new GWNetworkList<float>();
private GWNetworkList<NullableNetworkBehaviourReference> units;
private GWNetworkList<float> deathCooldowns;
private NetworkVariable<float> defenderStartingCols = new();
private NetworkVariable<float> attackerStartingCols = new();
public HexGrid hexGrid; public HexGrid hexGrid;
@ -27,7 +29,6 @@ namespace GWConquest
public Battle Battle public Battle Battle
{ {
get => battle.Value.GetBehaviour<Battle>(); get => battle.Value.GetBehaviour<Battle>();
set => battle.Value = value;
} }
public ushort FlankId public ushort FlankId
@ -41,13 +42,36 @@ namespace GWConquest
public int MaxUnitCount public int MaxUnitCount
{ {
get => maxUnitCount.Value; get => maxUnitCount.Value;
set => maxUnitCount.Value = value;
} }
public int RowCount public int RowCount
{ {
get => rowCount.Value; get => rowCount.Value;
set => rowCount.Value = value;
}
public float AttackerStartingCols
{
get => attackerStartingCols.Value;
}
public float DefenderStartingCols
{
get => defenderStartingCols.Value;
}
public void Init(Battle _battle, int _maxUnitCount, int _rowCount, float _attackerStartingCols, float _defenderStartingCols)
{
battle.Value = _battle;
maxUnitCount.Value = _maxUnitCount;
rowCount.Value = _rowCount;
attackerStartingCols.Value = _attackerStartingCols;
defenderStartingCols.Value = _defenderStartingCols;
}
private void Awake()
{
units = new();
deathCooldowns = new();
} }
public Unit GetUnit(int index) public Unit GetUnit(int index)
@ -159,6 +183,8 @@ namespace GWConquest
private void ResetGrid(int _maxUnitCount, int _rowCount) private void ResetGrid(int _maxUnitCount, int _rowCount)
{ {
if(_maxUnitCount == 0 || _rowCount == 0) return;
units.Clear(); units.Clear();
deathCooldowns.Clear(); deathCooldowns.Clear();
@ -170,59 +196,124 @@ namespace GWConquest
deathCooldowns.Add(0f); deathCooldowns.Add(0f);
} }
} }
public bool CanPlayerMoveToField(int index, Player player, BattlePhase phase)
{
float x = hexGrid.GetPosition(index).x;
float difference;
float allowed;
if(player == Battle.Defender)
{
difference = x + 1f;
allowed = phase == BattlePhase.Begin ? AttackerStartingCols : 1.5f;
}
else {
difference = hexGrid.ColumnCount - x;
allowed = phase == BattlePhase.Begin ? DefenderStartingCols : 1.5f;
}
return difference <= allowed;
}
}
public enum HexDirection
{
Left, Right, UpLeft, UpRight, DownLeft, DownRight
} }
public class HexGrid public class HexGrid
{ {
public static Vector2 uBase { get => Vector2.right; }
private static Vector2 _vBase = new(-0.5f, Mathf.Sqrt(3f) / 2f);
public static Vector2 vBase { get => _vBase; }
public static Vector2Int GetDirectionVector(HexDirection direction)
{
return direction switch
{
HexDirection.Left => new Vector2Int(-1, 0),
HexDirection.Right => new Vector2Int(1, 0),
HexDirection.UpLeft => new Vector2Int(-1, -1),
HexDirection.UpRight => new Vector2Int(0, -1),
HexDirection.DownLeft => new Vector2Int(0, 1),
HexDirection.DownRight => new Vector2Int(1, 1),
_ => new Vector2Int(0, 0),
};
}
private int fieldCount; private int fieldCount;
private int rowCount; private int rowCount;
private float columnCount; private float columnCount;
private Vector2[] positions;
private Vector2Int[] coordinates;
private Dictionary<Vector2Int, int> indexMap;
public HexGrid(int _fieldCount, int _rowCount) public HexGrid(int _fieldCount, int _rowCount)
{ {
if(_fieldCount == 0 || _rowCount == 0 || _rowCount > _fieldCount)
{
throw new ArgumentException($"Invalid field / row count: {_fieldCount} / {_rowCount}");
}
fieldCount = _fieldCount; fieldCount = _fieldCount;
rowCount = _rowCount; rowCount = _rowCount;
int currentColumn = 0;
int currentField = 0;
coordinates = new Vector2Int[_fieldCount];
indexMap = new Dictionary<Vector2Int, int>();
float triHeight = Mathf.Sqrt(3f) / 2f;
Vector2Int vec = new(0,0);
int index = 0;
coordinates[index] = vec;
indexMap[vec] = index;
index++;
positions = new Vector2[_fieldCount];
if(_fieldCount != 0 && _rowCount != 0)
int u = 1;
while(index < fieldCount)
{ {
while(currentField < _fieldCount)
for(int v = rowCount-1; v >= 0; v--)
{ {
for(int i = 0; i < _rowCount; i++)
{
if(i % 2 == currentColumn % 2)
{
positions[currentField] = new Vector2(currentColumn*0.5f, i*triHeight);
Debug.Log($"Placing field {currentField} at position {positions[currentField]}");
currentField++;
}
if(currentField >= _fieldCount)
{
break;
}
}
if(currentField < _fieldCount)
{
currentColumn++;
}
// endless loop reeeeeeeeeeeeeee
if(index >= fieldCount) break;
vec = new Vector2Int(u,v);
coordinates[index] = vec;
indexMap[vec] = index;
index++;
} }
u++;
} }
columnCount = currentColumn / 2f + 1f;
columnCount = u-1;
}
public Vector2Int GetCoordinates(int index)
{
return coordinates[index];
}
public int GetIndex(Vector2Int coords)
{
return indexMap[coords];
} }
public Vector2 GetPosition(int index) public Vector2 GetPosition(int index)
{ {
return positions[index];
Vector2Int coords = GetCoordinates(index);
return uBase * coords.x + vBase * coords.y;
}
public bool HasCoordinates(Vector2Int coords)
{
return indexMap.ContainsKey(coords);
}
public int GetIndexInDirection(int index, HexDirection direction)
{
Vector2Int vec = GetCoordinates(index) + GetDirectionVector(direction);
return HasCoordinates(vec) ? GetIndex(vec) : -1;
} }
public int RowCount => rowCount; public int RowCount => rowCount;


+ 10
- 3
Assets/GWConquest/Scripts/District.cs View File

@ -13,7 +13,7 @@ namespace GWConquest
private NetworkVariable<int> storageCapacity = new NetworkVariable<int>(); private NetworkVariable<int> storageCapacity = new NetworkVariable<int>();
private NetworkVariable<int> controllingPlayerID = new NetworkVariable<int>(); private NetworkVariable<int> controllingPlayerID = new NetworkVariable<int>();
public Inventory Inventory = new Inventory();
public Inventory Inventory;
private NetworkVariable<float> itemProductionCooldown = new NetworkVariable<float>(); private NetworkVariable<float> itemProductionCooldown = new NetworkVariable<float>();
private NetworkVariable<int> zoneID = new NetworkVariable<int>(); private NetworkVariable<int> zoneID = new NetworkVariable<int>();
private NetworkVariable<NetworkBehaviourReference> planet = new NetworkVariable<NetworkBehaviourReference>(); private NetworkVariable<NetworkBehaviourReference> planet = new NetworkVariable<NetworkBehaviourReference>();
@ -21,8 +21,8 @@ namespace GWConquest
private NetworkVariable<DistrictType> districtType = new NetworkVariable<DistrictType>(); private NetworkVariable<DistrictType> districtType = new NetworkVariable<DistrictType>();
private NetworkVariable<FixedString128Bytes> districtName = new NetworkVariable<FixedString128Bytes>(); private NetworkVariable<FixedString128Bytes> districtName = new NetworkVariable<FixedString128Bytes>();
private NetworkVariable<Vector3> relativePosition = new NetworkVariable<Vector3>(); private NetworkVariable<Vector3> relativePosition = new NetworkVariable<Vector3>();
private GWNetworkList<NetworkBehaviourReference> connectedDistricts = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<ushort> upgrades = new GWNetworkList<ushort>();
private GWNetworkList<NetworkBehaviourReference> connectedDistricts;
private GWNetworkList<ushort> upgrades;
private NetworkVariable<float> captureCooldown = new NetworkVariable<float>(); private NetworkVariable<float> captureCooldown = new NetworkVariable<float>();
public bool DistrictStatic; public bool DistrictStatic;
@ -98,6 +98,13 @@ namespace GWConquest
get => captureCooldown.Value; get => captureCooldown.Value;
} }
void Awake()
{
connectedDistricts = new();
upgrades = new();
Inventory = new();
}
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {


+ 6
- 1
Assets/GWConquest/Scripts/DistrictFactory.cs View File

@ -39,7 +39,7 @@ namespace GWConquest
} }
public class DistrictFactory : NetworkBehaviour public class DistrictFactory : NetworkBehaviour
{ {
public GWNetworkList<ProductionQueueEntry> productionQueue = new GWNetworkList<ProductionQueueEntry>();
public GWNetworkList<ProductionQueueEntry> productionQueue;
public bool StartsBroken = false; public bool StartsBroken = false;
public string[] SpecialUnits; public string[] SpecialUnits;
@ -83,6 +83,11 @@ namespace GWConquest
} }
} }
private void Awake()
{
productionQueue = new();
}
public IBuildable GetProductionQueueEntry(int i) public IBuildable GetProductionQueueEntry(int i)
{ {
if (productionQueue[i].IsUpgrade) if (productionQueue[i].IsUpgrade)


+ 10
- 3
Assets/GWConquest/Scripts/Formation.cs View File

@ -33,10 +33,10 @@ namespace GWConquest
private NetworkVariable<bool> isSpace = new NetworkVariable<bool>(); private NetworkVariable<bool> isSpace = new NetworkVariable<bool>();
private NetworkVariable<Transition> currentTransition = new NetworkVariable<Transition>(); private NetworkVariable<Transition> currentTransition = new NetworkVariable<Transition>();
private NetworkVariable<NetworkBehaviourReference> player = new NetworkVariable<NetworkBehaviourReference>(); private NetworkVariable<NetworkBehaviourReference> player = new NetworkVariable<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> units = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<int> pathQueue = new GWNetworkList<int>();
private GWNetworkList<NetworkBehaviourReference> units;
private GWNetworkList<int> pathQueue;
private NetworkVariable<NullableNetworkBehaviourReference> heroUnit = new NetworkVariable<NullableNetworkBehaviourReference>(); private NetworkVariable<NullableNetworkBehaviourReference> heroUnit = new NetworkVariable<NullableNetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> subFormations = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> subFormations;
private NetworkVariable<FixedString128Bytes> formationName = new NetworkVariable<FixedString128Bytes>(); private NetworkVariable<FixedString128Bytes> formationName = new NetworkVariable<FixedString128Bytes>();
private NetworkVariable<int> formationNumber = new NetworkVariable<int>(); private NetworkVariable<int> formationNumber = new NetworkVariable<int>();
private NetworkVariable<NullableNetworkBehaviourReference> movementTargetFormation = new NetworkVariable<NullableNetworkBehaviourReference>(); private NetworkVariable<NullableNetworkBehaviourReference> movementTargetFormation = new NetworkVariable<NullableNetworkBehaviourReference>();
@ -270,6 +270,13 @@ namespace GWConquest
public IInventory FormationInventory; public IInventory FormationInventory;
private void Awake()
{
units = new();
pathQueue = new();
subFormations = new();
}
public void TryMoveToPlanet(Planet planet) public void TryMoveToPlanet(Planet planet)
{ {
if (currentZone.zoneType == ZoneType.Space) if (currentZone.zoneType == ZoneType.Space)


+ 10
- 6
Assets/GWConquest/Scripts/GWNetworkManager.cs View File

@ -94,7 +94,10 @@ namespace GWConquest
foreach (Zone z in Zone.AllZones) foreach (Zone z in Zone.AllZones)
{ {
z.CheckBattleStart();
if(z.ShouldStartBattle())
{
SpawnBattle(z);
}
} }
Planet.SetupPlanetPathfinding(); Planet.SetupPlanetPathfinding();
@ -175,18 +178,19 @@ namespace GWConquest
return playerFormation; return playerFormation;
} }
public Battle SpawnBattle(Zone zone)
public Battle SpawnBattle(Zone zone, Player defender = null)
{ {
var go = Instantiate(BattlePrefab); var go = Instantiate(BattlePrefab);
go.GetComponent<NetworkObject>().Spawn(); go.GetComponent<NetworkObject>().Spawn();
Battle battle = go.GetComponent<Battle>(); Battle battle = go.GetComponent<Battle>();
battle.Zone = zone;
foreach (Formation f in zone.Formations)
if(defender == null)
{ {
battle.AddFormation(f);
defender = zone.Formations.FirstOrDefault().Player;
} }
battle.Init();
battle.Init(zone, defender);
Debug.LogFormat("Starting battle at zone {0}", this); Debug.LogFormat("Starting battle at zone {0}", this);


+ 6
- 1
Assets/GWConquest/Scripts/Planet.cs View File

@ -35,7 +35,7 @@ namespace GWConquest
private NetworkVariable<FixedString128Bytes> planetNameVar = new NetworkVariable<FixedString128Bytes>(); private NetworkVariable<FixedString128Bytes> planetNameVar = new NetworkVariable<FixedString128Bytes>();
private NetworkVariable<int> spaceZoneID = new NetworkVariable<int>(); private NetworkVariable<int> spaceZoneID = new NetworkVariable<int>();
private NetworkVariable<int> attackZoneID = new NetworkVariable<int>(); private NetworkVariable<int> attackZoneID = new NetworkVariable<int>();
private GWNetworkList<NetworkBehaviourReference> connectedPlanets = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> connectedPlanets;
private NetworkVariable<ushort> planetPrefab = new NetworkVariable<ushort>(); private NetworkVariable<ushort> planetPrefab = new NetworkVariable<ushort>();
private NetworkVariable<Quaternion> prefabRotation = new NetworkVariable<Quaternion>(); private NetworkVariable<Quaternion> prefabRotation = new NetworkVariable<Quaternion>();
private NetworkVariable<float> prefabScale = new NetworkVariable<float>(); private NetworkVariable<float> prefabScale = new NetworkVariable<float>();
@ -153,6 +153,11 @@ namespace GWConquest
{ {
get => connectedPlanets.Select(r => r.GetBehaviour<Planet>()); get => connectedPlanets.Select(r => r.GetBehaviour<Planet>());
} }
private void Awake()
{
connectedPlanets = new();
}
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {


+ 8
- 2
Assets/GWConquest/Scripts/Player.cs View File

@ -13,8 +13,8 @@ namespace GWConquest
private NetworkVariable<ushort> factionIndex = new NetworkVariable<ushort>(); private NetworkVariable<ushort> factionIndex = new NetworkVariable<ushort>();
private NetworkVariable<bool> isAI = new NetworkVariable<bool>(); private NetworkVariable<bool> isAI = new NetworkVariable<bool>();
private NetworkVariable<int> credits = new NetworkVariable<int>(); private NetworkVariable<int> credits = new NetworkVariable<int>();
private GWNetworkList<NetworkBehaviourReference> knownPlanetsList = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> visiblePlanetsList = new GWNetworkList<NetworkBehaviourReference>();
private GWNetworkList<NetworkBehaviourReference> knownPlanetsList;
private GWNetworkList<NetworkBehaviourReference> visiblePlanetsList;
public static List<Player> PlayerList = new List<Player>(); public static List<Player> PlayerList = new List<Player>();
@ -59,6 +59,12 @@ namespace GWConquest
} }
} }
private void Awake()
{
knownPlanetsList = new();
visiblePlanetsList = new();
}
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {
Debug.Log("Attaching player..."); Debug.Log("Attaching player...");


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

@ -241,25 +241,29 @@ namespace GWConquest
public void OnIconDragged(BattleUnitIcon icon, BattleUnitIcon target) public void OnIconDragged(BattleUnitIcon icon, BattleUnitIcon target)
{ {
if (icon.Unit.BattleState == BattleUnitState.InReserve && icon.Unit.Class.IsHero)
var unit = icon.Unit;
if (unit.BattleState == BattleUnitState.InReserve && unit.Class.IsHero)
{ {
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.AssignLeaderServerRpc(icon.Unit);
unit.Formation.AssignLeaderServerRpc(unit);
} }
} }
else if (icon.Unit.BattleState == BattleUnitState.InReserve && icon.Unit.CanGoToFlank)
else if (unit.BattleState == BattleUnitState.InReserve && unit.CanGoToFlank)
{ {
var unitTypeFits = Battle.IsSpaceBattle == (icon.Unit.Class.ZoneType == ZoneType.Space);
var unitTypeFits = Battle.IsSpaceBattle == (unit.Class.ZoneType == ZoneType.Space);
var flankUI = target.GetComponentInParent<BattleFlankUI>(); var flankUI = target.GetComponentInParent<BattleFlankUI>();
if (flankUI != null && unitTypeFits) if (flankUI != null && unitTypeFits)
{ {
var flankIndex = System.Array.IndexOf(flankUI.Icons, target); var flankIndex = System.Array.IndexOf(flankUI.Icons, target);
var flank = flankUI.BattleFlank;
if (flankUI.BattleFlank.GetUnit(flankIndex) == null)
var canMove = flank.CanPlayerMoveToField(flankIndex, unit.Player, BattleUI.Battle.Phase);
if (canMove && flank.GetUnit(flankIndex) == null)
{ {
Battle.MoveUnitToFlankServerRpc(icon.Unit, flankUI.BattleFlank, flankIndex);
Battle.MoveUnitToFlankServerRpc(unit, flank, flankIndex);
} }


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

@ -24,6 +24,8 @@ namespace GWConquest
private BattleUI battleUI; private BattleUI battleUI;
private bool isFlipped;
public RectTransform RectTransform public RectTransform RectTransform
{ {
get => GetComponent<RectTransform>(); get => GetComponent<RectTransform>();
@ -36,6 +38,8 @@ namespace GWConquest
BattleFlank = flank; BattleFlank = flank;
flank.CurrentUI = this; flank.CurrentUI = this;
isFlipped = flank.Battle.Defender != Player.CurrentPlayer;
var rt = RectTransform; var rt = RectTransform;
Icons = new BattleUnitIcon[flank.MaxUnitCount]; Icons = new BattleUnitIcon[flank.MaxUnitCount];
@ -44,11 +48,21 @@ namespace GWConquest
float iconHexSize = IconSize * triHeight; float iconHexSize = IconSize * triHeight;
float xOffset = (IconSize - iconHexSize) / 2f; float xOffset = (IconSize - iconHexSize) / 2f;
float xSize = flank.hexGrid.ColumnCount * iconHexSize;
for (int i = 0; i < flank.MaxUnitCount; i++) for (int i = 0; i < flank.MaxUnitCount; i++)
{ {
Vector2 gridPos = flank.hexGrid.GetPosition(i); Vector2 gridPos = flank.hexGrid.GetPosition(i);
float posx = iconHexSize * gridPos.x - xOffset;
float posx;
if(isFlipped)
{
posx = xSize - iconHexSize * (gridPos.x + 1) - xOffset;
}
else
{
posx = iconHexSize * gridPos.x - xOffset;
}
float posy = iconHexSize * gridPos.y; float posy = iconHexSize * gridPos.y;
var pos = new Vector2(posx, -posy); var pos = new Vector2(posx, -posy);
@ -72,7 +86,7 @@ namespace GWConquest
Icons[i] = icon; Icons[i] = icon;
} }
rt.sizeDelta = new Vector2(flank.hexGrid.ColumnCount * iconHexSize, flank.hexGrid.RowCount * IconSize);
rt.sizeDelta = new Vector2(xSize, flank.hexGrid.RowCount * IconSize);
} }
public void UpdateIcons() public void UpdateIcons()


+ 8
- 2
Assets/GWConquest/Scripts/Unit.cs View File

@ -20,8 +20,8 @@ namespace GWConquest
private NetworkVariable<BattleUnitState> battleState = new NetworkVariable<BattleUnitState>(); private NetworkVariable<BattleUnitState> battleState = new NetworkVariable<BattleUnitState>();
private NetworkVariable<float> actionCooldown = new NetworkVariable<float>(); private NetworkVariable<float> actionCooldown = new NetworkVariable<float>();
private NetworkVariable<float> actionCooldownMax = new NetworkVariable<float>(); private NetworkVariable<float> actionCooldownMax = new NetworkVariable<float>();
private Inventory inventory = new Inventory();
private Inventory equipment = new Inventory();
private Inventory inventory;
private Inventory equipment;
private NetworkVariable<int> flankTarget = new NetworkVariable<int>(); private NetworkVariable<int> flankTarget = new NetworkVariable<int>();
private NetworkVariable<float> morale = new NetworkVariable<float>(); private NetworkVariable<float> morale = new NetworkVariable<float>();
private NetworkVariable<int> supplies = new NetworkVariable<int>(); private NetworkVariable<int> supplies = new NetworkVariable<int>();
@ -255,6 +255,12 @@ namespace GWConquest
} }
} }
private void Awake()
{
inventory = new();
equipment = new();
}
public override void OnNetworkSpawn() public override void OnNetworkSpawn()
{ {
formation.OnValueChanged += OnFormationChanged; formation.OnValueChanged += OnFormationChanged;


+ 19
- 9
Assets/GWConquest/Scripts/Zone.cs View File

@ -126,21 +126,31 @@ namespace GWConquest
{ {
if (GameManager.Instance.IsLoaded) if (GameManager.Instance.IsLoaded)
{ {
CheckBattleStart();
if (ShouldStartBattle())
{
var defenders = formations.Where(f => f != formation).Select(f => f.Player).Distinct();
var defenderCount = defenders.Count();
if(defenderCount == 0)
{
Debug.LogWarning($"[Battle] No defender found when starting battle");
GWNetworkManager.Instance.SpawnBattle(this);
}
else
{
if(defenderCount > 1)
{
Debug.LogWarning($"[Battle] More than one defender found when starting battle");
}
GWNetworkManager.Instance.SpawnBattle(this, defenders.First());
}
}
} }
} }
} }
} }
public void CheckBattleStart()
{
if (ShouldStartBattle())
{
GWNetworkManager.Instance.SpawnBattle(this);
}
}
public bool ShouldStartBattle() public bool ShouldStartBattle()
{ {
return Formations.Select((f, i) => f.Player).Distinct().Count() > 1; return Formations.Select((f, i) => f.Player).Distinct().Count() > 1;


Loading…
Cancel
Save