Exemplo n.º 1
0
 public virtual void Start()
 {
     m_BoxCollisionController2D = gameObject.GetComponent <BoxCollisionController2D>();
     PathFindingGrid            = new PathFindingGrid(LayerMask, width, height, cellSize, origin);
     PathFindingGrid.UpdateNodes();
     PathFinder = new PathFinder(PathFindingGrid);
 }
Exemplo n.º 2
0
 private void Awake()
 {
     target = FindObjectOfType <Tank>().transform;
     path   = new List <PathfindingNode>();
     grid   = FindObjectOfType <PathFindingGrid>();
     enemy  = GetComponent <Enemy>();
     previousTargetPosition = new Vector2(9999, 9999);
 }
Exemplo n.º 3
0
 void Start()
 {
     boxCollider     = GetComponent <BoxCollider2D>();
     pathFindingGrid = FindObjectOfType <PathFindingGrid>();
     bodyAnimator    = body.GetComponent <Animator>();
     turretAnimator  = turret.GetComponent <Animator>();
     body.GetComponent <TankBody>().TankHasDied += KillTank;
 }
Exemplo n.º 4
0
    // Use this for initialization
    private void Start()
    {
        cycleIndex        = 0;
        cyclesData        = new List <CycleData>();
        agentCollectables = new List <GameObject>();
        grid = GetComponent <PathFindingGrid>();  //Get the reference.

        Invoke("InitializeCollectables", 1f);     //Call this method in 2 seconds. Ensures all things are setup first.
        //There is a better way to do this but for our case this is fine.
        Invoke("LookForPath", 1f);
    }
Exemplo n.º 5
0
    public static void SetScene()
    {
        Debug.Log("Setting Scene");
        if (SceneAsset.FindObjectOfType <UIManager>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Game UI"));
        }
        if (SceneAsset.FindObjectOfType <EventSystem>() == null)
        {
            var eventSystem = new GameObject("EventSystem", typeof(EventSystem), typeof(StandaloneInputModule));
        }
        if (SceneAsset.FindObjectOfType <GameManager>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Game Manager"));
        }
        if (SceneAsset.FindObjectOfType <LevelManager>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Level Manager"));
        }
        if (SceneAsset.FindObjectOfType <Camera>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Camera Parent"));
        }
        else
        {
            if (SceneAsset.FindObjectOfType <CameraShake>() == null)
            {
                GameObject Camera = SceneAsset.FindObjectOfType <Camera>().gameObject;
                SceneAsset.DestroyImmediate(Camera);
                PrefabUtility.InstantiatePrefab(Resources.Load("Core/Camera Parent"));
            }
        }
        if (SceneAsset.FindObjectOfType <Grid>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Basic Tilemap Grid"));
        }

        if (SceneAsset.FindObjectOfType <SoundManager>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Sound Manager"));
        }
        if (SceneAsset.FindObjectOfType <Light2D>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/Global Light"));
        }
        if (SceneAsset.FindObjectOfType <PathFindingGrid>() == null)
        {
            PrefabUtility.InstantiatePrefab(Resources.Load("Core/PathFindingGrid"));
            PathFindingGrid grid = SceneAsset.FindObjectOfType <PathFindingGrid>();
            grid.UnBuild();
            grid.Build();
        }
    }
Exemplo n.º 6
0
    public List <PathFindingNode> FindPath(MapNode startMapNode, MapNode endMapNode)
    {
        PathFindingGrid pathFindingGrid = new PathFindingGrid();
        PathFindingNode startNode       = pathFindingGrid.GetGridNode(startMapNode);
        PathFindingNode endNode         = pathFindingGrid.GetGridNode(endMapNode);

        openList = new List <PathFindingNode> {
            startNode
        };
        closedList = new List <PathFindingNode>();

        startNode.gCost = 0;
        startNode.hCost = CalculateDistanceCost(startNode, endNode);
        startNode.CalculateFCost();

        while (openList.Count > 0)
        {
            PathFindingNode curNode = GetLowestFCostNode(openList);
            if (curNode == endNode)
            {
                return(CalculatePath(endNode));
            }
            openList.Remove(curNode);
            closedList.Add(curNode);

            foreach (PathFindingNode neighborNode in GetNeighborList(pathFindingGrid, curNode))
            {
                if (closedList.Contains(neighborNode))
                {
                    continue;
                }

                int tentativeCost = curNode.gCost + CalculateDistanceCost(curNode, neighborNode);
                if (tentativeCost < neighborNode.gCost)
                {
                    neighborNode.cameFrom = curNode;
                    neighborNode.gCost    = tentativeCost;
                    neighborNode.hCost    = CalculateDistanceCost(neighborNode, endNode);
                    neighborNode.CalculateFCost();

                    if (!openList.Contains(neighborNode))
                    {
                        openList.Add(neighborNode);
                    }
                }
            }
        }
        return(null);
    }
Exemplo n.º 7
0
 public override void Start()
 {
     base.Start();
     PathFindingGrid.UpdateNodes();
     startNode = PathFindingGrid.WorldPosToNode(transform.position);
     endNode   = PathFindingGrid.WorldPosToNode(goal.transform.position);
     m_Path    = PathFinder.GeneratePath(startNode, endNode, this);
     // if (!m_Path.IsEmpty)
     // {
     //     m_Path.Visualize(Mathf.Infinity, dropHeight * cellSize);
     // }
     // else
     // {
     //     print("empty");
     // }
 }
Exemplo n.º 8
0
    public virtual void Initialise(Transform[] transforms = null, PairTargets[] pteroGround = null, Transform[] pteroAir = null, Weapon[] weapons = null)
    {
        animator              = GetComponent <Animator>();
        perception            = GetComponent <Perception>();
        rigidbody             = GetComponent <Rigidbody2D>();
        spriteRenderer        = GetComponent <SpriteRenderer>();
        material              = spriteRenderer.material;
        pathFindingGrid       = FindObjectOfType <PathFindingGrid>();
        weaponsToSwawnOnDeath = weapons;

        animator.SetBool("CanAttack", true);
        if (SoundManager.instance != null)
        {
            SoundManager.instance.PlaySFX("SFX_DinasaurSpawn");
        }
    }
Exemplo n.º 9
0
    public void Config(PathFindingGrid grid, float size)
    {
        this.size = size;
        bool isWall = false;

        SetWall(isWall);

        RaycastHit hit;

        if (Physics.Raycast(transform.position + (Vector3.up * 10), -Vector3.up, out hit, Mathf.Infinity))
        {
            LayerMask layer = hit.transform.gameObject.layer;
            weight   = grid.GetWeight(layer);
            Walkable = grid.GetIsWalkable(layer);
            Debug.Log(weight);
        }
    }
Exemplo n.º 10
0
        public override void Update()
        {
            PathFindingGrid.UpdateNodes();


            startNode = PathFindingGrid.WorldPosToNode(transform.position);
            endNode   = PathFindingGrid.WorldPosToNode(goal.transform.position);


            if (m_BoxCollisionController2D.Collisions.Below)
            {
                m_Path = PathFinder.GeneratePath(startNode, endNode, this);
            }

            if (!m_Path.IsEmpty)
            {
                m_Path.Visualize(Time.deltaTime, dropHeight * cellSize);
            }
            else
            {
                print("empty");
            }
            base.Update();


            if (m_Path.PathNodes.Count <= 1)
            {
                return;
            }
            if (!m_BoxCollisionController2D.Collisions.Below && velocity.y != 0)
            {
                return;
            }
            if (Mathf.Abs(m_Path.GetNextNode().col - startNode.col) > 1)
            {
                velocity = Math.Abs(transform.position.x - startNode.CenterWorldPos.x) < 0.1
                    ? jumpTo(m_Path.GetNextNode().CenterWorldPos)
                    : new Vector2((startNode.CenterWorldPos.x - transform.position.x > 0 ? 1 : -1) * speed, velocity.y);
            }
            else
            {
                velocity = new Vector2((m_Path.GetNextNode().col - startNode.col) * speed, velocity.y);
            }
        }
Exemplo n.º 11
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        serializedObject.Update();

        PathFindingGrid pathFindingGrid = (PathFindingGrid)target;

        if (GUILayout.Button("Create Grid"))
        {
            pathFindingGrid.Create();
        }

        if (GUILayout.Button("Clear Grid"))
        {
            pathFindingGrid.Clear();
        }

        serializedObject.ApplyModifiedProperties();
    }
Exemplo n.º 12
0
    private List <PathFindingNode> GetNeighborList(PathFindingGrid grid, PathFindingNode curNode)
    {
        List <PathFindingNode> neighborList = new List <PathFindingNode>();

        // TODO: Take wall corner case into account, i.e. an object cannot pass
        // from (0,0) to (1,1) if (0,1) and (1,0) are walls
        for (int x = -1; x <= 1; x++)
        {
            for (int y = -1; y <= 1; y++)
            {
                if (x == 0 && y == 0)
                {
                    continue;
                }
                PathFindingNode neighborNode = grid.GetGridNode(curNode.x + x, curNode.y + y);
                if (neighborNode != null && !neighborNode.IsWall())
                {
                    neighborList.Add(neighborNode);
                }
            }
        }
        return(neighborList);
    }
Exemplo n.º 13
0
 public void Start()
 {
     pathFindingManager = GetComponent <PathFindingManager>();
     grid = GetComponent <PathFindingGrid>();
     InitTextures();
 }
Exemplo n.º 14
0
    private RequestManager rm;    //Reference to our request manager.

    private void Awake()
    {
        //Get the references when this object is instantiated.
        rm   = GetComponent <RequestManager>();
        grid = GetComponent <PathFindingGrid>();
    }
Exemplo n.º 15
0
 private void Start()
 {
     grid = GetComponent <PathFindingGrid>();
 }
Exemplo n.º 16
0
 public void Start()
 {
     grid = gameObject.GetComponent <PathFindingGrid>();
 }
Exemplo n.º 17
0
    public Stack <Pair <int, int> > PerformPathFinding()
    {
        #region Variables initialization
        int i, j;

        int highestX = RTS.PathFindingHelper.maxWidth;
        int highestY = RTS.PathFindingHelper.maxHeight;

        GameGrid grid = RTS.PathFindingHelper.grid;

        Debug.Log("HIghest are " + highestX + " " + highestY);
        Debug.Log("Start coord are " + startPosition.X + " " + startPosition.Y);
        Debug.Log("End coord are " + endPosition.X + " " + endPosition.Y);

        bool[][] closedList = new bool[highestX][];

        for (i = 0; i < highestX; ++i)
        {
            closedList[i] = new bool[highestY];
            for (j = 0; j < highestY; ++j)
            {
                closedList[i][j] = false;
            }
        }

        SortedSet <Pair <int, int> > openList = new SortedSet <Pair <int, int> >();

        Pair <int, int> startingGrid = new Pair <int, int>();
        startingGrid.SetValues(startPosition.X, startPosition.Y);

        openList.Add(0.0, startingGrid);

        pfGrid = new PathFindingGrid(RTS.PathFindingHelper.maxWidth, RTS.PathFindingHelper.maxHeight);

        pfGrid.InitializeGrid();

        pfGrid.InitializeStartingCell(startPosition.X, startPosition.Y);
        Stack <Pair <int, int> > emptyPath = new Stack <Pair <int, int> >();

        //if (count == 1) return emptyPath;

        #endregion
        while (openList.IsNotEmpty())
        {
            Pair <double, Pair <int, int> > currentCell = openList.GetFirstElement();

            openList.RemoveElement(currentCell.First, currentCell.Second);

            i = currentCell.Second.First;
            j = currentCell.Second.Second;

            closedList[i][j] = true;

            int index;
            for (index = 0; index < RTS.PathFindingHelper.neighborLength; ++index)
            {
                // To store the 'g', 'h' and 'f' of the 8 successors
                double gNew = 0, hNew = 0, fNew = 0;

                int neighborI, neighborJ;

                neighborI = i + RTS.PathFindingHelper.Neighbors[index, 0];
                neighborJ = j + RTS.PathFindingHelper.Neighbors[index, 1];

                if (RTS.PathFindingHelper.IsSearchValid(neighborI, neighborJ, endPosition.X, endPosition.Y))
                {
                    //ReachedDestination
                    if (RTS.PathFindingHelper.HasDestinationBeenReached(neighborI, neighborJ, endPosition.X, endPosition.Y))
                    {
                        pfGrid.UpdateParent(neighborI, neighborJ, i, j);
                        count++;
                        return(TracePath());
                    }
                    else if (closedList[neighborI][neighborJ] == false && RTS.PathFindingHelper.IsCellFree(grid, neighborI, neighborJ))
                    {
                        double gValue = 1.0;
                        if (neighborI != i && neighborJ != j)
                        {
                            gValue = 1.414;
                        }

                        gNew = pfGrid.GetG(i, j) + gValue;
                        hNew = RTS.PathFindingHelper.CaculateHValue(neighborI, neighborJ, endPosition.X, endPosition.Y);
                        fNew = gNew + hNew;

                        if (!pfGrid.CanAdd(neighborI, neighborJ, fNew))
                        {
                            openList.Add(fNew, new Pair <int, int>(neighborI, neighborJ));

                            pfGrid.UpdateValue(neighborI, neighborJ, gNew, hNew, fNew, i, j);
                        }
                    }
                }
            }
        }

        //If we got here we didn't find a path

        return(emptyPath);
    }