Exemplo n.º 1
0
 private void DrawBaseCircle()
 {
     //Draw Radius
     DebugDrawers.DrawCircle(V3.zero, 1, Axis.Z, C.white, 0, 64);
     //Construct a triangle
     G.color = C.blue;
     G.DrawLine(V3.zero, new V3(cosine, sine));
     G.color = C.red;
     G.DrawLine(V3.zero, V3.right * cosine);
     G.color = C.green;
     G.DrawRay(V3.zero + V3.right * cosine, V3.up * sine);
 }
Exemplo n.º 2
0
    void LateUpdate()
    {
        //
        if (Input.GetKey(KeyCode.L))
        {
            loops += Time.deltaTime;
        }
        if (Input.GetKeyDown(KeyCode.K))
        {
            resolution++;
        }
        //
        if (Input.GetKeyDown(KeyCode.A))
        {
            foreach (var n in nodes)
            {
                n.RotateCCW();
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            foreach (var n in nodes)
            {
                n.RotateCW();
            }
        }

        nodes.Visualize(Color.yellow * .5f, .4f);
        nodes.Visualize(Color.red, .35f);



        DebugDrawers.DrawArc(transform.position, Vector3.zero, Color.yellow, 1);

        //rotate = Time.time * 180;

        Debug.DrawRay(seeker.position, seeker.forward);
        Debug.DrawRay(target.position, target.forward);

        DebugDrawers.DrawCircle(seeker.position, 3, Axis.Y);
        Collider[] c = Physics.OverlapSphere(seeker.position, 3, seekablesLayer);

        if (c.Length > 0)
        {
            print("Found " + target.name);

            Debug.DrawLine(seeker.position, target.position);

            angle = Vector3.Angle(seeker.forward, target.position - seeker.position);

            angle = Vector3.SignedAngle(seeker.forward, target.position - seeker.position, Vector3.up);
            var a = angle;
            print(state);
            if (a > -45 && a < 45)
            {
                state = State.Forward;
            }
            else if (a > 45 && a < 135)
            {
                state = State.Right;
            }
            else if (a > -135 && a < -45)
            {
                state = State.Left;
            }
            else if (a > 135 || a < -135)
            {
                state = State.Back;
            }
            else
            {
                state = State.Nothing;
            }
        }
        else
        {
            state = State.Nothing;
        }
    }
Exemplo n.º 3
0
 void OnDrawGizmos()
 {
     DebugDrawers.OnDrawGizmos();
 }
Exemplo n.º 4
0
 public static void Visualize(this Node n, Color color, float radius, int xOffset = 0, int yOffset = 0)
 {
     DebugDrawers.DrawCircle(new Vector2(n.x + xOffset, n.y + yOffset), radius, Axis.Z, color, 0);
 }
Exemplo n.º 5
0
    public PathingResult FindPath(object fromObject, object toObject, bool drawDebug = false)
    {
        PathingResult result = new PathingResult();

        result.isValid = false;

        float       debugDelay = 0f;
        const float debugTime  = 20000000f;

        if (drawDebug)
        {
            DebugDrawers.Clear();
        }

        if (
            fromObject == null ||
            toObject == null ||
            fromObject == toObject
            )
        {
            return(result);
        }

        // Result node variables.
        foreach (DictionaryEntry entry in this.nodes)
        {
            Node node = (Node)entry.Value;
            node.isQueued    = false;
            node.minDistance = Mathf.Infinity;
        }

        Node fromNode = (Node)this.nodes[fromObject];
        Node toNode   = (Node)this.nodes[toObject];

        if (fromNode == null || toNode == null || !fromNode.isEnabled || !toNode.isEnabled)
        {
            return(result);
        }

        fromNode.minDistance = 0f;

        Queue <Node> nodesToTest = new Queue <Node>();

        nodesToTest.Enqueue(fromNode);

        Node currentNode;

        while (true)
        {
            // Tested all nodes we can reach.
            if (nodesToTest.Count == 0)
            {
                break;
            }

            currentNode = nodesToTest.Dequeue();

            foreach (Connection connection in currentNode.connections)
            {
                Node node = connection.node;

                if (node.isEnabled == false)
                {
                    continue;
                }

                node.minDistance = Mathf.Min(
                    node.minDistance,
                    currentNode.minDistance + connection.distance
                    );

                if (node.isQueued == false)
                {
                    nodesToTest.Enqueue(node);
                    node.isQueued = true;
                }

                if (drawDebug)
                {
                    DebugDrawers.SpawnLine(
                        currentNode.pos,
                        node.pos,
                        Color.blue,
                        debugTime - debugDelay,
                        debugDelay
                        );
                    debugDelay += 0.02f;
                }
            }

            if (currentNode == toNode)
            {
                break;
            }
        }

        // No path.
        if (toNode.isQueued == false)
        {
            return(result);
        }

        result.distance = toNode.minDistance;
        result.isValid  = true;
        result.nodes    = new List <object>();

        result.nodes.Add(toNode.o);

        // Trace toNode to fromNode, following the shortest path.
        currentNode = toNode;
        Node lastNode = null;

        while (true)
        {
            float minMinDistance = Mathf.Infinity;
            Node  bestNode       = null;
            foreach (Connection connection in currentNode.connections)
            {
                if (connection.node.isEnabled == false)
                {
                    continue;
                }

                if (connection.node.minDistance < minMinDistance)
                {
                    minMinDistance = connection.node.minDistance;
                    bestNode       = connection.node;
                }
            }

            if (drawDebug)
            {
                foreach (Connection connection in currentNode.connections)
                {
                    if (connection.node.isEnabled == false)
                    {
                        continue;
                    }

                    if (connection.node != bestNode && connection.node != lastNode)
                    {
                        DebugDrawers.SpawnLine(
                            currentNode.pos,
                            connection.node.pos,
                            Color.yellow,
                            debugTime - debugDelay,
                            debugDelay
                            );
                    }
                }

                DebugDrawers.SpawnLine(
                    currentNode.pos,
                    bestNode.pos,
                    Color.green,
                    debugTime - debugDelay,
                    debugDelay
                    );

                debugDelay += 0.33f;
            }

            lastNode = currentNode;

            result.nodes.Add(bestNode.o);

            if (bestNode == fromNode)
            {
                break;
            }
            else
            {
                currentNode = bestNode;
            }
        }

        result.nodes.Reverse();

        return(result);
    }
Exemplo n.º 6
0
    void Update()
    {
        if (hardInput.GetKeyDown("Test LOS"))
        {
            // Draw a LOS test line from the selected tile to all other tiles.

            Debug.Log("Testing LOS");

            if (this.selectedTile == null)
            {
                return;
            }

            foreach (BattleTile tile in this.tiles)
            {
                bool canSee = this.TestLOS(this.selectedTile, tile);

                DebugDrawers.SpawnLine(
                    this.selectedTile.transform.position,
                    tile.transform.position,
                    canSee ? Color.green : Color.red,
                    2f
                    );
            }
        }
        else if (hardInput.GetKeyDown("Test pathing"))
        {
            Debug.Log("Testing pathing");

            if (this.selectedTile == null || this.hoveredTile == null)
            {
                return;
            }

            if (this.selectedTile.mech)
            {
                this.pathNetwork.SetNodeEnabled(this.selectedTile, true);
            }

            PathingResult result = this.pathNetwork.FindPath(
                this.selectedTile,
                this.hoveredTile,
                true
                );

            if (this.selectedTile.mech)
            {
                this.pathNetwork.SetNodeEnabled(this.selectedTile, false);
            }

            if (result.isValid)
            {
                Debug.Log(result.nodes.Count + " nodes in path " + result.distance + " units long.");
            }
            else
            {
                Debug.Log("Invalid path!");
            }
        }
        else if (hardInput.GetKeyDown("Test saving"))
        {
            Debug.Log("Testing saving");

            string jsonText = this.history.ToJSON();
            string path     = "TestSave.json";

            using (StreamWriter sw = new StreamWriter(path)){
                sw.WriteLine(jsonText);
            }

            Debug.Log("Wrote save to " + path);
        }
    }