-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWallCreatorWithObstacles.cs
159 lines (139 loc) · 6.27 KB
/
WallCreatorWithObstacles.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using UnityEngine;
using System.Collections;
using System.IO;
using System.Xml.Serialization;
public class WallCreatorWithObstacles : MonoBehaviour
{
// This class has some nested classes which are used to deserialize the xml file containing geometry information.
// Nested class where walls are stored
public class Wall
{
public Vector2 Start {get; set; }
public Vector2 End {get; set; }
}
// nested class where obstacles are stored
public class Obstacle
{
public Vector2 Centre {get; set; }
public float Diameter {get; set; }
}
// nested class where geometry is stored. The geometry consists of an array with exterior walls
// an array with interior walls, and an array with obstacles.
public class Geometry
{
public Wall[] ExteriorWalls {get; set; }
public Wall[] InteriorWalls {get; set; }
public Obstacle[] Obstacles {get; set; }
}
// These game objects are prefabs used to instantiate the geometry
public GameObject Floor;
public GameObject ExtWall;
public GameObject IntWall;
public GameObject Obst;
public string XMLlocation; // the location of the xml file containing geometry information
private GameObject FloorClone; // the actual floor
private GameObject[] ExtWallArray; // the actual exterior walls
private GameObject[] IntWallArray; // the actual interior walls
private GameObject[] ObstArray; // the actual obstacles
private Transform tf; // used to transform the walls and obstacles.
// constructor. Sets references to the floor prefab, wall prefabs, obstacle prefabs, and xml location.
public WallCreatorWithObstacles(GameObject Floor, GameObject ExtWall, GameObject IntWall, GameObject Obst, string XMLlocation)
{
this.Floor = Floor;
this.ExtWall = ExtWall;
this.IntWall = IntWall;
this.Obst = Obst;
this.XMLlocation = XMLlocation;
}
// this function actually creates the geometry.
public void makeWalls()
{
//Read the xml file with geometry information
Geometry geometry;
XmlSerializer serializer = new XmlSerializer(typeof(Geometry));
StreamReader reader = new StreamReader(@XMLlocation);
geometry = (Geometry)serializer.Deserialize(reader); // store the information in geometry
reader.Close ();
// instantiate the floor
// first determine the coordinates of the floor. This is done by looking at the cooridnates of all exterior walls.
float minX = geometry.ExteriorWalls[0].Start.x;
float minY = geometry.ExteriorWalls[0].Start.y;
float maxX = geometry.ExteriorWalls[0].Start.x;
float maxY = geometry.ExteriorWalls[0].Start.y;
// finding the minimal and maximal coordinates
for(int i = 0; i < geometry.ExteriorWalls.Length; i++)
{
minX = Mathf.Min(minX, geometry.ExteriorWalls[i].Start.x);
minX = Mathf.Min(minX, geometry.ExteriorWalls[i].End.x);
minY = Mathf.Min(minY, geometry.ExteriorWalls[i].Start.y);
minY = Mathf.Min(minY, geometry.ExteriorWalls[i].End.y);
maxX = Mathf.Max(maxX, geometry.ExteriorWalls[i].Start.x);
maxX = Mathf.Max(maxX, geometry.ExteriorWalls[i].End.x);
maxY = Mathf.Max(maxY, geometry.ExteriorWalls[i].Start.y);
maxY = Mathf.Max(maxY, geometry.ExteriorWalls[i].End.y);
}
Instantiate (Floor); // instantiate the floor
FloorClone = GameObject.FindGameObjectWithTag("Floor");
tf = FloorClone.GetComponent<Transform>(); // transform the floor into the right position, rotation, and scale
tf.position = new Vector3((minX + maxX) / 2, 0, (minY + maxY) / 2);
tf.localScale = new Vector3((maxX - minX) / 10, tf.localScale.y, (maxY - minY) / 10);
// for each element in ExteriorWalls, instantiate a clone of prefab "ExteriorWallPrefab"
for(int i=0; i<geometry.ExteriorWalls.Length; i++)
{
Instantiate(ExtWall);
}
// for each element in InteriorWalls, instantiate a clone of prefab "InteriorWallPrefab"
for(int i=0; i<geometry.InteriorWalls.Length; i++)
{
Instantiate(IntWall);
}
// for each element in Obstacles, instantiate a clone of prefab "ObstaclePrefab"
for(int i=0; i<geometry.Obstacles.Length; i++)
{
Instantiate(Obst);
}
// Store all exterior walls in array
ExtWallArray = GameObject.FindGameObjectsWithTag("ExteriorWall");
// adapt the position, rotation and scale of the exterior walls
for(int i=0; i<ExtWallArray.Length; i++)
{
tf = ExtWallArray[i].GetComponent<Transform>();
//adapt position
tf.position = new Vector3 ((geometry.ExteriorWalls[i].Start.x + geometry.ExteriorWalls[i].End.x)/2,
tf.position.y, (geometry.ExteriorWalls[i].Start.y + geometry.ExteriorWalls[i].End.y)/2);
//adapt rotation
tf.Rotate (new Vector3(0, 0, Mathf.Sign(geometry.ExteriorWalls[i].Start.x - geometry.ExteriorWalls[i].End.x)
* Vector2.Angle (new Vector2 (0,1), geometry.ExteriorWalls[i].End - geometry.ExteriorWalls[i].Start)));
//adapt scale
tf.localScale = new Vector3 ((geometry.ExteriorWalls[i].End - geometry.ExteriorWalls[i].Start).magnitude/10,
tf.localScale.y, tf.localScale.z);
}
// store all interior walls in array
IntWallArray = GameObject.FindGameObjectsWithTag("InteriorWall");
// adapt the position, rotation and scale of interior walls
for(int i=0; i<IntWallArray.Length; i++)
{
tf = IntWallArray[i].GetComponent<Transform>();
//adapt position
tf.position = new Vector3 ((geometry.InteriorWalls[i].Start.x + geometry.InteriorWalls[i].End.x)/2,
tf.position.y, (geometry.InteriorWalls[i].Start.y + geometry.InteriorWalls[i].End.y)/2);
//adapt rotation
tf.Rotate (new Vector3(0, 0, Mathf.Sign(geometry.InteriorWalls[i].Start.x - geometry.InteriorWalls[i].End.x)
* Vector2.Angle (new Vector2 (0,1), geometry.InteriorWalls[i].End - geometry.InteriorWalls[i].Start)));
//adapt scale
tf.localScale = new Vector3 ((geometry.InteriorWalls[i].End - geometry.InteriorWalls[i].Start).magnitude/10,
tf.localScale.y, tf.localScale.z);
}
// store all obstacles in array
ObstArray = GameObject.FindGameObjectsWithTag("Obstacle");
// adapt the position and the scale of the obstacles
for(int i=0; i<ObstArray.Length; i++)
{
tf = ObstArray[i].GetComponent<Transform>();
//adapt position
tf.position = new Vector3 (geometry.Obstacles[i].Centre.x, tf.position.y, geometry.Obstacles[i].Centre.y);
//adapt scale
tf.localScale = new Vector3 (geometry.Obstacles[i].Diameter, tf.localScale.y, geometry.Obstacles[i].Diameter);
}
}
}