-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObstacleZone.cs
112 lines (97 loc) · 3.01 KB
/
ObstacleZone.cs
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
101
102
103
104
105
106
107
108
109
110
111
112
using UnityEngine;
public class ObstacleZone : MonoBehaviour
{
public int numberOfObstacles = 20;
public Obstacle.Type type;
public Obstacle circular;
public Obstacle rectangular;
public Collider Collider => GetComponent<Collider>();
public GameObject AStarCollider;
public GameObject OfficeCollider;
// Start is called before the first frame update
private void Start()
{
Build();
AStarCollider.SetActive(false);
OfficeCollider.SetActive(false);
}
public void SetType(int type)
{
if (type < 2)
{
this.type = (Obstacle.Type)type;
Rebuild();
return;
}
Clear();
AStarCollider.SetActive(type == 2);
OfficeCollider.SetActive(type == 3);
if(type == 2)
{
Obstacle[] aStarBoxes = AStarCollider.GetComponentsInChildren<Obstacle>();
foreach(Obstacle b in aStarBoxes)
{
EntityMgr.inst.AddObstacle(b);
}
return;
}
Obstacle[] officeBoxes = OfficeCollider.GetComponentsInChildren<Obstacle>();
foreach (Obstacle b in officeBoxes)
{
EntityMgr.inst.AddObstacle(b);
}
}
public void SetCount(int count)
{
numberOfObstacles = 20;
if (count > 0) numberOfObstacles = 30;
if (count > 1) numberOfObstacles = 100;
if (!AStarCollider.activeSelf && !OfficeCollider.activeSelf)
{
Rebuild();
}
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.C))
{
Rebuild();
}
}
void Clear()
{
if (EntityMgr.inst != null) EntityMgr.inst.ClearObstacles();
else
{
foreach (Obstacle o in FindObjectsByType<Obstacle>(FindObjectsSortMode.None))
{
Destroy(o.gameObject);
}
}
}
void Rebuild()
{
Clear();
Build();
}
void Build()
{
for(int i = 0; i < numberOfObstacles; i++)
{
float xPos = Random.Range(Collider.bounds.min.x, Collider.bounds.max.x);
float zPos = Random.Range(Collider.bounds.min.z, Collider.bounds.max.z);
switch(type) {
case Obstacle.Type.Rectangular:
Obstacle r = Instantiate(rectangular, new Vector3(xPos, 0, zPos), Quaternion.identity);
r.transform.SetParent(transform);
if(EntityMgr.inst != null) EntityMgr.inst.AddObstacle(r);
break;
case Obstacle.Type.Circular:
Obstacle c = Instantiate(circular, new Vector3(xPos, 0, zPos), Quaternion.identity);
c.transform.SetParent(transform);
if (EntityMgr.inst != null) EntityMgr.inst.AddObstacle(c);
break;
}
}
}
}