public bool IsValidAttackTarget(HexCell attackingUnit, HexCell target, int playerId) { if (!(attackingUnit is UnitCell unit)) { return(false); } if (attackingUnit == null || attackingUnit.PlayerId != playerId) { return(false); } if (target == null || target.PlayerId == playerId) { return(false); } return(unit.GetStats().Range >= HexCoordinates.Distance(attackingUnit.Coordinates, target.Coordinates)); }
public void UpdateAttackCursor(UnitCell unit, HexCoordinates coords) { var target = cells.GetCellAtPosition(coords); if (target == null) { ui.SetGameCursor(HexCursor.Invalid); } else if (cells.IsValidAttackTarget(unit, target, unit.PlayerId)) { ui.SetGameCursor(HexCursor.Attack); } else { ui.SetGameCursor(HexCursor.Invalid); } }
public void CmdMoveCell(Int16 playerID, Int16 cellID, Int16 targetX, Int16 targetZ) { if (!game.IsCurrentPlayer(playerID)) { return; } var cell = game.Cells.GetCell(cellID); var target = new HexCoordinates(targetX, targetZ); var path = game.Cells.GetPathWithValidityCheck(cell, target, ID); if (path != null) { game.MoveCell(cell, target); } }
public void UpdateBuildCursor(HexCoordinates coords) { if (projectedCell == null) { return; } projectedCell.SetCoordinates(coords); if (IsProjectedCellInPlacementGrid()) { projectedCell.ShowAsValid(); } else { projectedCell.ShowAsInvalid(); } }
private void HandleBoardClick(HexCoordinates coords) { Clear(); var cell = game.Cells.GetCellAtPosition(coords); if (cell != null) { if (cell is UnitCell u && u.PlayerId == player.ID) { SetOwnUnitSelected(u); } selectedCell = cell; cell.SetSelected(); game.UI.ShowCellInfo(cell); } }
public void Update() { input.HandleInput(); var boardPosition = input.GetMousePositionOnBoard(); var coords = HexCoordinates.FromPosition(boardPosition); if (coords.Equals(currentCoords)) { return; } currentCoords = coords; if (isPlayerTurn) { UpdateCursor(coords); } }
private static List <Vector3> ConstructPath(PathCell target, int maxDistance) { var path = new Stack <Vector3>(); var cell = target; while (cell != null) { if (path.Count - 1 >= maxDistance) { return(null); } path.Push(HexCoordinates.ToPosition(cell.X, cell.Z)); cell = cell.PreviousCell; } return(path.ToList()); }
private bool MoveIfValid(UnitCell unit, HexCoordinates coords) { if (unit.HasMoved) { return(false); } var path = game.Cells.GetPathWithValidityCheck(unit, coords, player.ID); var isValidPath = path != null; if (isValidPath) { state = States.None; indicator.Clear(); player.CmdMoveCell( (Int16)player.ID, (Int16)unit.ID, (Int16)coords.X, (Int16)coords.Z); } return(isValidPath); }
public List <Vector3> FindPath(HexCoordinates origin, HexCoordinates target, int maxDistance) { if (cells.IsPieceInCell(target)) { return(null); } var distance = HexCoordinates.Distance(origin, target); var pathableCells = GetCellsWithinDistance(origin, distance); foreach (var cell in pathableCells) { if (cell.X == target.X && cell.Z == target.Z) { return(ConstructPath(cell, maxDistance)); } } return(null); }
private void UpdateCursor(HexCoordinates coords) { switch (state) { case States.Building: indicator.UpdateBuildCursor(coords); break; case States.OwnUnitSelected: indicator.UpdateUnitCursor(unit, coords); break; case States.Moving: indicator.UpdateMoveCursor(unit, coords); break; case States.Attacking: indicator.UpdateAttackCursor(unit, coords); break; } }
public void UpdateUnitCursor(UnitCell unit, HexCoordinates coords) { if (unit == null) { return; } if (!unit.HasAttacked) { var target = cells.GetCellAtPosition(coords); if (target != null) { cells.HidePath(); if (target.PlayerId == unit.PlayerId) { ui.SetGameCursor(HexCursor.Default); } else if (cells.IsValidAttackTarget(unit, target, unit.PlayerId)) { ui.SetGameCursor(HexCursor.Attack); } else { ui.SetGameCursor(HexCursor.Invalid); } return; } } if (unit.HasMoved) { ui.SetGameCursor(HexCursor.Default); } else { UpdateMoveCursor(unit, coords); } }
public static bool IsValidPlacementDistance(int radius, HexCoordinates cellCoords, HexCoordinates origin) { var distance = (Mathf.Abs(cellCoords.X - origin.X) + Mathf.Abs(cellCoords.Y - origin.Y) + Mathf.Abs(cellCoords.Z - origin.Z)) / 2; return(distance <= radius); }
public bool IsPieceInCell(HexCoordinates coordinates) { return(AllCells.Any(c => c.IsPlaced && c.Coordinates.Equals(coordinates))); }
public HexCell GetCellAtPosition(HexCoordinates coordinates) { return(AllCells.FirstOrDefault(c => c.IsPlaced && c.Coordinates.Equals(coordinates))); }
public List <Vector3> GetPath(UnitCell unit, HexCoordinates targetCoords) { return(pathfinder.FindPath(unit.Coordinates, targetCoords, unit.GetStats().Speed)); }
public void MoveCell(HexCell cell, HexCoordinates to) { Grid.RpcMoveCell((Int16)cell.ID, (Int16)to.X, (Int16)to.Z); }
public virtual void MoveTo(HexCoordinates coords) { SetCoordinates(coords); }
public static Vector3 ToPosition(HexCoordinates coordinates) { return(ToPosition(coordinates.X, coordinates.Z)); }
public static int Distance(HexCoordinates c1, HexCoordinates c2) { return((Mathf.Abs(c1.X - c2.X) + Mathf.Abs(c1.Y - c2.Y) + Mathf.Abs(c1.Z - c2.Z)) / 2); }
public void SetCoordinates(HexCoordinates coords) { Coordinates = coords; transform.position = HexCoordinates.ToPosition(coords); }