This repository was archived by the owner on Jul 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRandomPath.cs
More file actions
100 lines (84 loc) · 3.13 KB
/
RandomPath.cs
File metadata and controls
100 lines (84 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using CubeCoordinates;
using UnityEngine;
public class RandomPath : MonoBehaviour
{
private Coordinates coordinates;
private void Awake()
{
Coordinates.Instance.SetCoordinateType(Coordinate.Type.GenerateMesh);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space)) BuildMap();
}
private void BuildMap()
{
// Clear all Coordinates and GameObjects
Coordinates.Instance.Clear();
// Cube coordinate radius of 10 from Vector3.zero
List<Vector3> cubes = Cubes.GetNeighbors(Vector3.zero, 10);
// Randomly remove some of the coordinates
for (int i = cubes.Count - 1; i >= 0; i--)
{
// Exclude Vector3.zero, our starting point
if (cubes[i] == Vector3.zero) continue;
if (Random.Range(0.0f, 100.0f) < 30.0f) cubes.RemoveAt(i);
}
// Create the Coordinate objects, they're necessary for `GetExpand` to work
Coordinates.Instance.CreateCoordinates (cubes);
// Get the origin Coordinate
Coordinate origin =
Coordinates.Instance.GetContainer().GetCoordinate(Vector3.zero);
// Use `GetExpand` to find all Coordinates connected to Vector3.zero, removing others
Coordinates
.Instance
.GetContainer()
.RemoveCoordinates(Coordinates
.Instance
.BooleanDifference(Coordinates
.Instance
.GetContainer()
.GetAllCoordinates(),
Coordinates.Instance.GetExpand(origin, 10)));
// Build instantiates the GameObjects, connecting them to Coordinate objects
Coordinates.Instance.Build();
int steps = 10;
bool found = false;
do
{
if (steps <= 0) return;
// Get a Ring (circle) of Coordinates at "steps" from origin, from existing Coordinates
List<Coordinate> ring = Coordinates.Instance.GetRing(origin, steps);
if (ring.Count > 0)
{
// Get a Path of Coordinates from 'origin' to random Coordinate in Ring results
List<Coordinate> path =
Coordinates
.Instance
.GetPath(origin, ring[Random.Range(0, ring.Count - 1)]);
if (path.Count >= 2)
{
// Add Coordinates to a custom "path" container
Coordinates
.Instance
.GetContainer("path")
.AddCoordinates(path);
found = true;
}
}
// Reduce steps if Ring results in no Coordinates
steps--;
}
while (!found);
// Get the path Coordinates from the Container and add some color
foreach (Coordinate
c
in
Coordinates.Instance.GetContainer("path").GetAllCoordinates()
)
c.go.GetComponent<Renderer>().material.color = Color.red;
}
}