@ -1,3 +1,4 @@
using System ;
using System.Collections.Generic ;
using System.Linq ;
using Unity.Netcode ;
@ -14,7 +15,8 @@ namespace GWConquest
private NetworkVariable < int > rowCount = new NetworkVariable < int > ( ) ;
private GWNetworkList < NullableNetworkBehaviourReference > units = new GWNetworkList < NullableNetworkBehaviourReference > ( ) ;
private GWNetworkList < float > deathCooldowns = new GWNetworkList < float > ( ) ;
private NetworkVariable < float > defenderStartingCols = new ( ) ;
private NetworkVariable < float > attackerStartingCols = new ( ) ;
public HexGrid hexGrid ;
@ -27,7 +29,6 @@ namespace GWConquest
public Battle Battle
{
get = > battle . Value . GetBehaviour < Battle > ( ) ;
set = > battle . Value = value ;
}
public ushort FlankId
@ -41,13 +42,30 @@ namespace GWConquest
public int MaxUnitCount
{
get = > maxUnitCount . Value ;
set = > maxUnitCount . Value = value ;
}
public int RowCount
{
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 ;
}
public Unit GetUnit ( int index )
@ -159,6 +177,8 @@ namespace GWConquest
private void ResetGrid ( int _maxUnitCount , int _rowCount )
{
if ( _maxUnitCount = = 0 | | _rowCount = = 0 ) return ;
units . Clear ( ) ;
deathCooldowns . Clear ( ) ;
@ -170,59 +190,124 @@ namespace GWConquest
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 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 rowCount ;
private float columnCount ;
private Vector2 [ ] positions ;
private Vector2Int [ ] coordinates ;
private Dictionary < Vector2Int , int > indexMap ;
public HexGrid ( int _fieldCount , int _rowCount )
{
if ( _fieldCount = = 0 | | _rowCount = = 0 | | _rowCount > _fieldCount )
{
throw new ArgumentException ( $"Invalid field / row count: {_fieldCount} / {_rowCount}" ) ;
}
fieldCount = _fieldCount ;
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 )
{
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 ;