コード例 #1
0
ファイル: NpcPathfinding.cs プロジェクト: JonathanRN/Sufod3D
    public void FindPath(Tile target)
    {
        tacticsMove.ComputeWalkableAdjacencyLists(target);
        tacticsMove.GetCurrentTile();

        var openList   = new List <Tile>();
        var closedList = new List <Tile>();

        openList.Add(tacticsMove.CurrentTile);
        //currentTile.parent = ??
        tacticsMove.CurrentTile.H = Vector3.Distance(tacticsMove.CurrentTile.transform.position, target.transform.position);
        tacticsMove.CurrentTile.F = tacticsMove.CurrentTile.H;

        while (openList.Count > 0)
        {
            var t = FindLowestF(openList);

            closedList.Add(t);

            if (t == target)
            {
                ActualTargetTile = FindEndTile(t);
                tacticsMove.MoveToTile(ActualTargetTile);
                return;
            }

            foreach (var tile in t.AdjacencyList)
            {
                if (closedList.Contains(tile))
                {
                    //Do nothing, already processed
                }
                else if (openList.Contains(tile))
                {
                    var tempG = t.G + Vector3.Distance(tile.transform.position, t.transform.position);

                    if (tempG < tile.G)
                    {
                        tile.Parent = t;

                        tile.G = tempG;
                        tile.F = tile.G + tile.H;
                    }
                }
                else
                {
                    tile.Parent = t;

                    tile.G = t.G + Vector3.Distance(tile.transform.position, t.transform.position);
                    tile.H = Vector3.Distance(tile.transform.position, target.transform.position);
                    tile.F = tile.G + tile.H;

                    openList.Add(tile);
                }
            }
        }

        //todo - what do you do if there is no path to the target tile?
        Debug.Log("Path not found");
        //End turn for now
        turnManager.EndTurn();
    }
コード例 #2
0
    void CheckMouse()
    {
        if (Input.GetMouseButtonUp(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            //Mouse was clicked on an object with a collider
            if (Physics.Raycast(ray, out hit))
            {
                //A tile was clicked on
                if (hit.collider.tag == "Tile")
                {
                    Tile clickedTile = hit.collider.GetComponent <Tile>();
                    if (choosingAttackTargets)
                    {
                        if (clickedTile.selectable)
                        {
                            TacticsMove attackTarget = clickedTile.GetUnitOnTile();
                            if (attackTarget != null)
                            {
                                AttackEnemy(attackTarget);
                                hasAttacked = true;
                            }
                        }
                    }

                    //Tile was clicked on to move
                    else if (!choosingAttackTargets)
                    {
                        //Display GUI info on the current moving unit
                        classinfo.OnMouseDown();

                        //If the tile is a possible location to move to
                        if (clickedTile.selectable)
                        {
                            //Target to move to
                            MoveToTile(clickedTile);
                        }
                    }
                }

                //Player clicked on another unit of the same team
                else if (hit.collider.tag == "Player")
                {
                    TacticsMove player = hit.collider.GetComponent <TacticsMove>();
                    if (!player.hasMoved || !player.hasAttacked)
                    {
                        RemoveSelectableTiles();
                        RemoveAttackableTiles();
                        this.turn = false;
                        NewTurnManager.ExecuteTurn(player);
                    }
                }

                else if (hit.collider.tag == "NPC" && !hasAttacked)
                {
                    TacticsMove attackTarget = hit.collider.GetComponent <TacticsMove>();

                    //If our attack target exists and is within attack range
                    if (attackTarget != null && attackTarget.GetCurrentTile().attackable)
                    {
                        choosingAttackTargets = false;
                        enemy = attackTarget;
                        PlayAttackAnimation();
                    }
                    else
                    {
                        Debug.Log("This is the target tile " + attackTarget.GetCurrentTile());
                    }
                }
            }
        }
    }