Example #1
0
    // Update is called once per frame
    void Update()
    {
        if (mode == MouseMode.Normal)
        {
            lastMousePos = Input.mousePosition;

            Ray        ray;
            RaycastHit rayHit;
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
            {
                if (Physics.Raycast(ray, out rayHit) && rayHit.collider.transform.tag.Contains("Interactable"))
                {
                    if (lastHit != null)
                    {
                        lastHit.GetComponent <MeshRenderer>().material.color = Color.white;
                    }
                    lastHitTag = rayHit.collider.transform.tag;
                    lastHit    = rayHit.transform;
                    Material lastHitMat = lastHit.GetComponent <MeshRenderer>().material;
                    lastHitMat.color = Color.white;
                    //Set Inspect UI
                    ui.SetInspect(lastHit.name);
                    if (lastHitTag.Contains("Floor"))
                    {
                        floorGrid.HighlightUV((int)(hitpos.x + 0.5f), (int)(hitpos.z + 0.5f), Color.white);
                        hitpos = rayHit.point;
                        floorGrid.HighlightUV((int)(hitpos.x + 0.5f), (int)(hitpos.z + 0.5f), Color.cyan);
                    }
                    else //if it wasnt a floorboard
                    {
                        lastHitMat.color = Color.cyan;
                        floorGrid.HighlightUV((int)(hitpos.x + 0.5f), (int)(hitpos.z + 0.5f), Color.white);
                    }
                }
                else if (lastHit != null)
                {
                    ui.HideSubMenus();
                    if (lastHitTag.Contains("Floor"))
                    {
                        floorGrid.HighlightUV((int)(hitpos.x + 0.5f), (int)(hitpos.z + 0.5f), Color.white);
                    }
                    else  //if it wasnt a floorboard
                    {
                        Material lastHitMat = lastHit.GetComponent <MeshRenderer>().material;
                        lastHitMat.color = Color.white;
                        floorGrid.HighlightUV((int)(hitpos.x + 0.5f), (int)(hitpos.z + 0.5f), Color.white);
                    }
                }
            }
        }
        else if (mode == MouseMode.Build)
        {
            //Do build stuff
        }
    }
Example #2
0
    public void Search()
    {
        searched = true;
        //Reset Debug floor
        if (DebugMode)
        {
            foreach (JPSNode node in closed)
            {
                floor.HighlightUV((int)node.pos.x, (int)node.pos.z, Color.white);
            }
            foreach (JPSNode node in path)
            {
                floor.HighlightUV((int)node.pos.x, (int)node.pos.z, Color.white);
            }
        }
        //Prime search values
        searching        = true;
        destinationFound = false;
        placeInPath      = 0;
        open.Clear();
        closed.Clear();
        path.Clear();
        currentNode = new JPSNode(startingPosition, null, destination);

        //First node must have all directions
        currentNode.AddDirection(new Vector3(1, 0, 1));
        currentNode.AddDirection(new Vector3(1, 0, -1));
        currentNode.AddDirection(new Vector3(-1, 0, 1));
        currentNode.AddDirection(new Vector3(-1, 0, -1));
        currentNode.AddDirection(new Vector3(1, 0, 0));
        currentNode.AddDirection(new Vector3(-1, 0, 0));
        currentNode.AddDirection(new Vector3(0, 0, 1));
        currentNode.AddDirection(new Vector3(0, 0, -1));
        AddToOpen(currentNode);

        //begin main search loop
        while (!destinationFound && open.Count > 0)
        {
            currentNode = open[0];
            for (int i = 0; i < open.Count; i++)
            {
                if (currentNode.priority > open[i].priority)
                {
                    currentNode = open[i];
                }
            }

            open.Remove(currentNode);
            closed.Add(currentNode);
            for (int i = 0; i < currentNode.directions.Count; i++)
            {
                if (currentNode.directions[i].magnitude == 1)
                {
                    FindNodes(currentNode.pos + currentNode.directions[i], currentNode, currentNode.directions[i], true);
                }
                else
                {
                    FindNodes(currentNode.pos + currentNode.directions[i], currentNode, currentNode.directions[i], false);
                }
            }
        }
        //Prepare path for unit
        currentNode = null;
        for (int i = 0; i < closed.Count; i++)
        {
            if (closed[i].pos == destination)
            {
                currentNode = closed[i];
            }
        }
        if (DebugMode)
        {
            Debug.Log("Path Cost: " + currentNode.priority);
        }
        if (currentNode != null)
        {
            while (currentNode.parent != null)
            {
                if (DebugMode)
                {
                    floor.HighlightUV((int)currentNode.pos.x, (int)currentNode.pos.z, Color.green);
                }
                path.Add(currentNode);
                currentNode = currentNode.parent;
            }
            path.Reverse();
            if (destinationFound)
            {
                navigating = true;
            }
        }
        searching = false;
    }