예제 #1
0
    void Update()
    {
        // MOVEMENT
        float horizontal_input = Input.GetAxis("Horizontal");
        float vertical_input   = Input.GetAxis("Vertical");

        // Vertical is forwards/backwards
        //this.transform.Translate(Vector2.up * vertical_input * movement_speed * Time.deltaTime);

        //this.transform.Translate(Vector2.right * horizontal_input * movement_speed * Time.deltaTime);

        //currentPoint = getClosestNode(horizontal_input, vertical_input);

        if (horizontal_input < 0)
        {
            Debug.Log("Got horizontal input");
            foreach (Edge <GameObject> edge in pathCreater.GetGraph().Nodes[currentPoint].Edges)
            {
                Debug.Log("Getting weights " + edge.getWeight());

                if (edge.getWeight() > 0)
                {
                    Debug.Log("found edge greate than 0 edgde weight " + edge.getWeight() + "edge names are " + edge.GetNode(1).Value.name + edge.GetNode(0).Value.name);
                    this.transform.position = Vector3.MoveTowards(transform.position, edge.GetNode(0).Value.transform.position, Time.deltaTime * movement_speed * horizontal_input);
                }
            }
        }
    }
예제 #2
0
    /// <summary>
    /// Returns a path from the starting node position to
    /// the target nodes poisiton
    /// </summary>
    /// <param name="startPos">Parameter value to pass.</param>
    /// <param name="targetPos">Parameter value to pass.</param>
    /// <returns>Returns a list of nodes in the path between startPos to targetPos</returns>
    public List <GameObject> Path(GameObject startPos, GameObject targetPos)
    {
        //Create a list of gameobjects, the path
        List <GameObject> path = new List <GameObject>();

        //If startPos and targetPos are the same, return nothing
        if (startPos == targetPos)
        {
            return(path);
        }
        else
        {
            path.Add(startPos);

            //Node<GameObject> graphNode = pathCreater.GetGraph().Find(startPos);

            //if(graphNode != null){
            //    foreach(Node<GameObject> node in graphNode.Neighbors){
            //        path.Add(node.Value);
            //    }
            //}

            //Find the node equal to startPos in the graph
            Node <GameObject> start = createdPath.GetGraph().Find(startPos);

            //If start is no empty, search through neighbors to find node equal to targetPos
            if (start != null)
            {
                foreach (Node <GameObject> n in start.Neighbors)
                {
                    if (n.Value == targetPos)
                    {
                        path.Add(targetPos);
                    }
                }
            }
            return(path);
        }
    }