private void LocateUnit(SrpgLayer layer, SrpgUnit unit, int x, int y) { unit.SetZIndex(CalcZIndex(layer, y)); unit.gameObject.name = string.Format("[{0}, {1}] {2}", x, y, unit.gameObject.name); unit.x = x; unit.y = y; float unitX = _tileWidth * x + unit.offsetX; float unitY = _tileHeight * y + unit.offsetY; unit.transform.position = new Vector3(unitX, unitY); }
public void LocateUnitFirst(SrpgLayer layer, SrpgUnit unit, int x, int y, float fitRatio = 1f, SrpgBattle.UnitFitMode scaleMode = SrpgBattle.UnitFitMode.Inside) { unit.boundWidth = _tileWidth; unit.boundHeight = _tileHeight; Vector3 unitSize = unit.GetSize(); float unitScale = 0f; if (scaleMode == SrpgBattle.UnitFitMode.Inside) { float widthScale = _tileWidth * fitRatio / unitSize.x; float heightScale = _tileHeight * fitRatio / unitSize.y; if (widthScale >= 1f && heightScale >= 1f) // tile width >= unit width && tile height >= unit height { unitScale = Mathf.Min(widthScale, heightScale); } else if (widthScale < 1f && heightScale < 1f) // tile width < unit width && tile height < unit height { unitScale = Mathf.Max(widthScale, heightScale); } else if (widthScale >= 1f && heightScale < 1f) // tile width >= unit width && tile height < unit height { unitScale = heightScale; } else if (widthScale < 1f && heightScale >= 1f) // tile width < unit width && tile height >= unit height { unitScale = widthScale; } } else if (scaleMode == SrpgBattle.UnitFitMode.Width) { unitScale = _tileWidth * fitRatio / unitSize.x; } else if (scaleMode == SrpgBattle.UnitFitMode.Height) { unitScale = _tileHeight * fitRatio / unitSize.y; } unit.Scale(unitScale); Vector3 unitCenter = unit.GetCenter(); unit.offsetX = _tileWidth / 2 + unitCenter.x; unit.offsetY = _tileHeight / 2 - unitCenter.y; LocateUnit(layer, unit, x, y); }
public virtual bool MakeUnitMovePath(SrpgUnit unit, int destX, int destY, out List <SrpgNode> path) { path = new List <SrpgNode>(); List <SrpgAStarNode> closedNodes = null; if (!MakeUnitMovePath_AStar(unit, destX, destY, out closedNodes)) { return(false); } path = closedNodes.Cast <SrpgNode>().ToList(); return(true); }
public void AddUnit(SrpgUnit unit, int x, int y, float fitRatio, bool player) { _map.LocateUnitFirst(_unitLayer, unit, x, y, fitRatio, _unitFitMode); unit.player = player; unit.clicked += OnUnitClicked; if (player) { _playerUnits.Add(unit); } else { _enemyUnits.Add(unit); } }
public override bool MakeUnitMovePath(SrpgUnit unit, int destX, int destY, out List <SrpgNode> path) { path = new List <SrpgNode>(); Example1Unit myUnit = (Example1Unit)unit; List <SrpgAStarNode> closedNodes = null; if (!MakeUnitMovePath_AStar(myUnit, destX, destY, out closedNodes, myUnit.GetMaximumRange())) { return(false); } path = closedNodes.Cast <SrpgNode>().ToList(); return(true); }
public bool IsThereEnemyUnitAt(int x, int y, SrpgUnit self = null) { foreach (SrpgUnit unit in _enemyUnits) { if (self == unit) { continue; } if (unit.x == x && unit.y == y) { return(true); } } return(false); }
protected bool MakeUnitMovePath_AStar(SrpgUnit unit, int destX, int destY, out List <SrpgAStarNode> closedNodes, int limitRange = 10000) { closedNodes = new List <SrpgAStarNode>(); if (_groundLayer == null) { Debug.LogError("groundLayer is null"); return(false); } int point = 10; List <SrpgAStarNode> openNodes = new List <SrpgAStarNode>(); // 1) Add the starting square (or node) to the open list. openNodes.Add(new SrpgAStarNode(unit.x, unit.y, 0)); // 2) Repeat the following: while (openNodes.Count > 0) { // a) Look for the lowest F cost square on the open list. We refer to this as the current square. openNodes.Sort(); SrpgAStarNode currNode = openNodes[openNodes.Count - 1]; openNodes.RemoveAt(openNodes.Count - 1); // b) Switch it to the closed list. closedNodes.Add(currNode); // check out of unit-range if (closedNodes.Count > limitRange + 1) { return(false); } // check found path if (currNode.x == destX && currNode.y == destY) { return(true); } // get adjacent tiles List <SrpgTile> adjacentTiles = _map.GetAdjacentTiles(_groundLayer, currNode.x, currNode.y); // c) For each of the adjacent to this current square … foreach (SrpgTile adjaTile in adjacentTiles) { SrpgAStarNode adjaNode = new SrpgAStarNode(adjaTile.x, adjaTile.y); // If it is not walkable or if it is on the closed list, ignore it. Otherwise do the following. if (closedNodes.Contains(adjaNode)) { continue; } if (unit.player) { if (IsThereEnemyUnitAt(adjaTile.x, adjaTile.y)) { continue; } } else { if (IsTherePlayerUnitAt(adjaTile.x, adjaTile.y)) { continue; } } if (!IsTileWalkable(adjaTile)) { continue; } int newGCost = currNode.GCost + point; SrpgAStarNode openNode = openNodes.Find(oi => (oi.x == adjaNode.x && oi.y == adjaNode.y)); if (openNode != null) { // If it is on the open list already, check to see if this path to that square is better, using G cost as the measure. A lower G cost means that this is a better path. If so, change the parent of the square to the current square, and recalculate the G and F scores of the square. If you are keeping your open list sorted by F score, you may need to resort the list to account for the change. if (newGCost < openNode.GCost) { openNode.GCost = newGCost; openNode.Parent = currNode; } } else { // If it isn’t on the open list, add it to the open list. Make the current square the parent of this square. Record the F, G, and H costs of the square. adjaNode.GCost = newGCost; adjaNode.HCost = Math.Abs(adjaNode.x - destX) * point + Math.Abs(adjaNode.y - destY) * point; adjaNode.Parent = currNode; openNodes.Add(adjaNode); } } } return(false); }
public bool IsThereEnemyUnitAt(SrpgCell cell, SrpgUnit self = null) { return(IsThereEnemyUnitAt(cell.x, cell.y)); }
public bool IsTherePlayerUnitAt(SrpgCell cell, SrpgUnit self = null) { return(IsTherePlayerUnitAt(cell.x, cell.y, self)); }
public virtual void ClearActiveUnit() { _activeUnit = null; ClearActiveUnitCells(); }
static public int CalcSimpleDistance(SrpgUnit unit1, SrpgUnit unit2) { return(CalcSimpleDistance(unit1.x, unit1.y, unit2.x, unit2.y)); }