//TODO: need to move to another component
    #region Moving logic
    public void MoveTo(Vector3 position, Action <bool> movingOverCallback)
    {
        SettlersEngine.Point startPoint = new SettlersEngine.Point()
        {
            X = (int)this.transform.position.x,
            Y = (int)this.transform.position.z
        };
        SettlersEngine.Point endPoint = new SettlersEngine.Point()
        {
            X = (int)position.x,
            Y = (int)position.z
        };
        IEnumerable <AStarPathNode> path = AStarManager.GetInstance().Search(startPoint, endPoint);

        if (path != null)
        {
            if (currentMoveEnumerator != null)
            {
                StopCoroutine(currentMoveEnumerator);
            }
            currentMoveEnumerator = StartCoroutine(StartMoveTo(path, movingOverCallback));
        }
        else
        {
            movingOverCallback(false);
        }
    }
Exemplo n.º 2
0
    private void Start()
    {
        GridAgent[] gridAgents = FindObjectsOfType <GridAgent>();
        int         size       = (int)Mathf.Sqrt(gridAgents.Length);

        globalTilesList = new GridAgent[size, size];
        for (int i = 0; i < gridAgents.Length; i++)
        {
            globalTilesList[gridAgents[i].position.x, gridAgents[i].position.y] = gridAgents[i];
        }

        UpdateGrid();
        AStarManager.GetInstance().CreateNewSolver(currentGrid);
    }
 public ActionInfo GetActionInfo(Vector2Int position)
 {
     if ((possibleActions & ActionType.Walk) == ActionType.Walk)
     {
         LinkedList <AStarPathNode> path = AStarManager.GetInstance().Search(GridTransform.Position, position);
         if (path == null || path.Count <= 1)
         {
             Debug.LogWarning("Couldn't find path to " + position.ToString());
             return(null);
         }
         if (path.Count - 1 > Stat.MovePoints.Value)
         {
             Debug.LogWarning("Not enough move points");
             return(null);
         }
         ActionInfo actionInfo = new ActionInfo();
         actionInfo.ActionList.Add(new MovingAction(path.Count - 1, path));
         return(actionInfo);
     }
     Debug.LogWarning("Can't walk");
     return(null);
 }
    public ActionInfo GetActionInfo(Actor selectedActor)
    {
        if (selectedActor == null)
        {
            Debug.LogError("Invalid param");
            return(null);
        }
        LinkedList <AStarPathNode> path = AStarManager.GetInstance().SearchClosest(GridTransform.Position, selectedActor.GridTransform.Position);

        if (path == null || path.Count <= 0)
        {
            Debug.LogWarning("Couldn't find path to " + selectedActor.GridTransform.Position.ToString());
            return(null);
        }
        if (path.Count - 1 + 2 > Stat.MovePoints.Value)
        {
            Debug.LogWarning("Not enough move points");
            return(null);
        }
        ActionInfo actionInfo = new ActionInfo();

        if (path.Count >= 2)
        {
            if ((possibleActions & ActionType.Walk) == ActionType.Walk)
            {
                actionInfo.ActionList.Add(new MovingAction(path.Count - 1, path));
                actionInfo.ActionList.Add(new AttackAction(2, ActionType.Melee, selectedActor));
                return(actionInfo);
            }
        }
        else
        {
            actionInfo.ActionList.Add(new AttackAction(2, ActionType.Melee, selectedActor));
            return(actionInfo);
        }
        return(null);
    }
Exemplo n.º 5
0
    public void ShowBattleGrid(int battleId)
    {
        if (globalTilesList == null)
        {
            return;
        }
        ClearBattleGrid();

        foreach (LinkedList <GridAgent> column in tilesList)
        {
            foreach (GridAgent ga in column)
            {
                if (ga.isDirty)
                {
                    ga.GetGrid();
                }
            }
        }

        Actor currentActor = BattleManager.GetInstance().GetCurrentActor(battleId);

        if (currentActor == null)
        {
            Debug.LogError("There is no current actor in battle manager");
            return;
        }
        Vector2Int startPos = currentActor.GridTransform.Position;

        int drawRange = currentActor.Stat.MovePoints.Value;

        Vector2Int gridMin = new Vector2Int(currentGrid[0, 0].X, currentGrid[0, 0].Y);
        Vector2Int gridMax = new Vector2Int(currentGrid[0, 0].X + currentGrid.GetLength(0) - 1, currentGrid[0, 0].Y + currentGrid.GetLength(1) - 1);

        currentBattleGrid.Clear();

        AStarManager aStarManager = AStarManager.GetInstance();

        int minX = startPos.x - drawRange;

        if (minX < gridMin.x)
        {
            if (minX < 0)
            {
                minX = gridMin.x;
            }
            else
            {
                MoveGrid(Direction.Left);
                ShowBattleGrid(battleId);
                return;
            }
        }

        int minY = startPos.y - drawRange;

        if (minY < gridMin.y)
        {
            if (minY < 0)
            {
                minY = gridMin.y;
            }
            else
            {
                MoveGrid(Direction.Down);
                ShowBattleGrid(battleId);
                return;
            }
        }

        int maxX = startPos.x + drawRange;

        if (maxX > gridMax.x)
        {
            if (maxX > GridMax.x)
            {
                maxX = gridMax.x;
            }
            else
            {
                MoveGrid(Direction.Right);
                ShowBattleGrid(battleId);
                return;
            }
        }

        int maxY = startPos.y + drawRange;

        if (maxY > gridMax.y)
        {
            if (maxY > GridMax.y)
            {
                maxY = gridMax.y;
            }
            else
            {
                MoveGrid(Direction.Up);
                ShowBattleGrid(battleId);
                return;
            }
        }

        int index = 0;

        for (int x = minX; x <= maxX; x++)
        {
            currentBattleGrid.Add(new List <Vector2Int>());
            for (int y = minY; y <= maxY; y++)
            {
                LinkedList <AStarPathNode> path = aStarManager.Search(startPos, new Vector2Int(x, y));
                int tileType = 0;
                if (path == null)
                {
                    tileType = 3;
                }
                if (path != null && path.Count - 1 <= drawRange)
                {
                    tileType = ((float)(path.Count - 1) / (float)drawRange) < 0.7f ? 2 : 1;
                }
                if (tileType == 0)
                {
                    continue;
                }
                tilemap.SetTile(new Vector3Int(x, y, 0), tileBases[tileType]);
                currentBattleGrid[index].Add(new Vector2Int(x, y));
            }
            index++;
        }
    }