Ejemplo n.º 1
0
    private void BuildPathTo(IntVector2 position)
    {
        // We aren't standing in the target, so find a path to it.
        AStarResolver resolver = new AStarResolver(BoardManager.Board);

        path = resolver.FindPath(this.position, position);
    }
Ejemplo n.º 2
0
        private string ResetPaths(string[] args)
        {
            if (path != null)
            {
                foreach (AiNode aiNode in path)
                {
                    if (aiNode.Walkable)
                    {
                        aiNode.Owner.RemoveComponent <MeshRendererComponent>();
                    }
                }

                path.Clear();
            }

            Random rnd       = new Random();
            AiNode startNode = nodes[rnd.Next(0, 64), rnd.Next(0, 64)];
            AiNode endNode   = nodes[rnd.Next(0, 64), rnd.Next(0, 64)];

            while (!startNode.Walkable)
            {
                startNode = nodes[rnd.Next(0, 64), rnd.Next(0, 64)];
            }

            while (!endNode.Walkable)
            {
                endNode = nodes[rnd.Next(0, 64), rnd.Next(0, 64)];
            }

            path = AStarResolver.FindPath(startNode, endNode, out bool found);

            for (int i = 0; i < path.Count; i++)
            {
                path[i].Owner
                .AddComponent(new LitMeshRendererComponent(DefaultFilepaths.DefaultLitShader, Prefabs.Cube, tex,
                                                           1));
            }

            startNode.Owner.GetComponent <LitMeshRendererComponent>().Textures[0] = beginTex;
            endNode.Owner.GetComponent <LitMeshRendererComponent>().Textures[0]   = endTex;
            return("Success: " + found);
        }
Ejemplo n.º 3
0
 public void Setup()
 {
     resolver = new AStarResolver();
 }
Ejemplo n.º 4
0
        protected override void Update(float deltaTime)
        {
            if (ObjectUnderMouse(Owner.LocalPosition, out KeyValuePair <Collider, RayHit> hit)
                ) //We Check where we clicked on
            {
                AiNode node = hit.Key.Owner.GetComponent <AiNode>();
                if (node != null)
                {
                    if (Input.GetKey(Key.S)) //Setting the Start Point
                    {
                        LitMeshRendererComponent lmr =
                            node.Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                        if (startNode != null && startNode != node)
                        {
                            ApplyTexture(startNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                         startNode.Walkable ? greenTex : redTex);
                        }

                        startNode = node;
                    }
                    else if (Input.GetKey(Key.E)) //Setting the End Point
                    {
                        LitMeshRendererComponent lmr =
                            node.Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                        if (endNode != null && endNode != node)
                        {
                            ApplyTexture(endNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                         endNode.Walkable ? greenTex : redTex);
                        }

                        endNode = node;
                    }
                }
            }

            //When Start and end Point is defined and space is pressed we calculate the path
            if (startNode != null && endNode != null && Input.GetKey(Key.Space))
            {
                if (path != null) //First Clean the Old path, and reset the textures
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, path[i].Walkable ? greenTex : redTex);
                    }
                }

                //This line is doing the A*
                path = AStarResolver.FindPath(startNode, endNode, out bool foundPath);


                if (foundPath) //If there exists a path from start to end, we will make it visible
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, purpleTex);
                    }
                }
            }

            //Escape Resets the Nodes color and removes the starting points
            if (Input.GetKey(Key.Escape))
            {
                if (startNode != null)
                {
                    ApplyTexture(startNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                 startNode.Walkable ? greenTex : redTex);
                }

                if (endNode != null)
                {
                    ApplyTexture(endNode.Owner.GetComponent <LitMeshRendererComponent>(),
                                 endNode.Walkable ? greenTex : redTex);
                }

                startNode = endNode = null;
                if (path != null)
                {
                    for (int i = 0; i < path.Count; i++)
                    {
                        LitMeshRendererComponent lmr =
                            (path[i] as AiNode).Owner.GetComponent <LitMeshRendererComponent>();
                        ApplyTexture(lmr, path[i].Walkable ? greenTex : redTex);
                    }
                }

                path = null;
            }
        }