Cell[] FindPath() { Cell[] tilesWhereUnitCanAttackFrom = Map.GetExtendedArea(new Cell[1] { Map.UnitTile(Target) }, AtkData); List <Cell> attackArea = new List <Cell>(); foreach (Cell c in tilesWhereUnitCanAttackFrom) { attackArea.Add(c); } Cell[] tilesWhereTargetCanCounterFrom = Map.GetExtendedArea(new Cell[1] { Map.UnitTile(Target) }, Target.Unit.inventory.GetAttackData(Target)); List <Cell> counterArea = new List <Cell>(); foreach (Cell c in tilesWhereTargetCanCounterFrom) { counterArea.Add(c); } List <Cell> preferenceArea = new List <Cell>(); foreach (Cell c in attackArea) { if (!counterArea.Contains(c)) { preferenceArea.Add(c); } } List <Cell> area = preferenceArea.Count == 0 ? attackArea : preferenceArea; Cell targetTile = area[UnityEngine.Random.Range(0, tilesWhereUnitCanAttackFrom.Length)]; foreach (Cell c in tilesWhereUnitCanAttackFrom) { if (c == Map.UnitTile(Fighter)) { targetTile = Map.UnitTile(Fighter); } } List <Cell> range = new List <Cell>(); Cell[] path = QPath.FindPath(Fighter, Map.UnitTile(Fighter), targetTile, Cell.EstimateDistance); foreach (Cell c in path) { if (MoveArea.Contains(c) && area.Contains(c)) { range.Add(c); } } return(range.ToArray()); }
public void DrawPath() { Cell destination = null; if (CurrentRange.Contains(MouseToCell())) { if (arrow != null) { ClearDrawnPath(); } arrow = new List <Cell>(); destination = MouseToCell(); } else { return; } CurrentPath = QPath.FindPath(Fighter, Map.UnitTile(Fighter), destination, Cell.EstimateDistance); if (CurrentPath.Length == 1) { return; } foreach (Cell c in CurrentPath) { GameObject a = null; if (!ObjectPool.ContainsKey("arrow") || ObjectPool.GetFirstInactiveObject("arrow") == null) { a = Instantiate(arrowPrefab, new Vector3(c.position.x, c.position.y), Quaternion.identity); } else { a = ObjectPool.GetFirstInactiveObject("arrow"); } ArrowDisplay display = a.GetComponent <ArrowDisplay>(); arrow.Add(display); display.layer = arrowLayer; Map.Add(display, arrowLayer, true); a.GetComponent <SpriteRenderer>().enabled = false; a.transform.position = new Vector3(c.position.x, c.position.y); a.SetActive(true); display.AssignSprite(); a.GetComponent <SpriteRenderer>().enabled = true; ObjectPool.AddToPool("arrow", a); } }
public static Cell[] GetMoveArea(Fighter f, int movement, bool weighted = true) { Dictionary <Cell, int> depths = GetTileDepths(f, movement + 1); Cell[] tiles = new Cell[depths.Count]; depths.Keys.CopyTo(tiles, 0); if (weighted) { IQPathTile startCell = UnitTile(f); List <Cell> range = new List <Cell> { (Cell)startCell }; foreach (Cell c in tiles) { Fighter unitInTile = c.unitInTile; IQPathTile[] path = QPath.FindPath(f, startCell, c, Cell.EstimateDistance); if (!c.impassible && AggregateCost(path, f.Unit) <= movement && c.MoveCost(f.Unit) >= 0 && (unitInTile == null || unitInTile.Unit.alignment == f.Unit.alignment)) { range.Add(c); c.Display_Debug(AggregateCost(path, f.Unit).ToString()); } } List <Cell> all = range; range = new List <Cell>(); foreach (Cell c in all) { if (c.unitInTile == null || c.unitInTile == f) { range.Add(c); } } return(range.ToArray()); } return(tiles); }
void OnLeftClick() { if (ActionMenu.Selection == ActionMenu.SelectionState.closed) { if (Fighter == null) { Cell cell = MouseToCell(); Selected = cell; startCell = Selected; if (cell != null && cell.unitInTile != null && cell.unitInTile.Unit.alignment == Unit.Alignment.Player && !cell.unitInTile.TurnOver) { Fighter = Map.GetCellData(Selected.position).unitInTile; //ClearHighlightedArea(); DoHighlights(Fighter, CurrentRange.ToArray(), true, 0.01f); } } else if (Fighter != null && CurrentRange.Contains(MouseToCell())) { pathApplied = true; Selected = MouseToCell(); MovementUsed = Map.AggregateCost(QPath.FindPath(Fighter, startCell, Selected, Cell.EstimateDistance), Fighter.Unit); } } else if (ActionMenu.Selection == ActionMenu.SelectionState.target) { if (ActionMenu.CanSelect) { SelectTarget(); } else { ActionMenu.CanSelect = true; } } }
private IEnumerator OnSelectedChange() { ClearDrawnPath(); ClearHighlightedArea(); yield return(StartCoroutine(Fighter.ApplyPath(QPath.FindPath(Fighter, startCell, Selected, Cell.EstimateDistance), GameSettings.DoMoveAnimation))); AttackArea = new Cell[0]; StaffArea = new Cell[0]; if (Fighter.Unit.inventory.GetWeapons(Fighter, true).Length > 0) { Weapon.AttackData atkData = Fighter.Unit.inventory.GetAttackData(Fighter); if (atkData != null) { AttackArea = Map.GetExtendedArea(new Cell[] { Map.UnitTile(Fighter) }, atkData.range, atkData.closedSet, atkData.closedSetMin); } } if (Fighter.Unit.inventory.GetStaves(Fighter, true).Length > 0) { Weapon.AttackData stfData = Fighter.Unit.inventory.GetStaffData(Fighter); if (stfData != null) { StaffArea = Map.GetExtendedArea(new Cell[] { Map.UnitTile(Fighter) }, stfData.range, stfData.closedSet, stfData.closedSetMin); } } CurrentRange = new List <Cell> { Map.GetCellData(Fighter.Position()) }; DoHighlights(Fighter, CurrentRange.ToArray(), false); ActionMenu.Open(Fighter); ActionMenu.ActionDone = false; }
public static int DistBtwn(Cell start, Cell end, Fighter f) { Cell[] path = QPath.FindPath(f, start, end, (IQPathTile a, IQPathTile b) => { return(1); }); return(path.Length); }