-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.cs
194 lines (158 loc) · 7.23 KB
/
generate.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
public void Generate()
{
this.redirection = this.locomotionType == LocomotionType.Redirection;
Vector3[] corners = initalCorners;
int direction = startingDirection;
bool forceLeftDoor = false;
bool forceRightDoor = false;
bool outCorridorSet = false;
Vector3 frontInCorridorCorner = default;
Vector3 backInCorridorCorner = default;
int entranceCornerIndex = default;
roomAndProgressManager.rooms = new List<GameObject>();
roomAndProgressManager.showAllWalls = !this.redirection;
Quaternion rotation = Quaternion.identity;
bool rotationWasSet = false;
bool wasOnShortSide = false;
Vector3 debugLastRotationPoint = Vector3.zero;
for (int i = 0; i < this.levelLength; i++)
{
GameObject room = new GameObject();
room.AddComponent<RoomGenerator>();
float doorPosition = GenerateRandomDoorPosition(forceLeftDoor, forceRightDoor);
var script = room.GetComponent<RoomGenerator>();
script.Generate(
i,
corners,
direction,
corridorDepth,
corridorHeight,
eyeHeight,
wallThickness,
doorPosition,
doorWidth,
doorHeight,
wallMaterial,
doorMaterial,
dissolveMaterial,
doorTransitionLength,
wallPrefab
);
if (!redirection)
{
room.AddComponent<MeshCollider>();
}
Vector3 center = script.CalculateCenterOfRoom();
if (!rotationWasSet)
{
rotation = Quaternion.LookRotation(script.corners[0] - script.corners[1]);
rotationWasSet = true;
}
GameObject rotationRedirector = Instantiate(rotationRedirectorPrefab, center, rotation);
rotationRedirector.transform.SetParent(room.transform);
rotationRedirector.AddComponent<RotationRedirectorCollision>();
var rotationRedirectorScript = rotationRedirector.GetComponent<RotationRedirector>();
rotationRedirectorScript.PlayAreaDimensions = new Vector2(roomDepth, roomWidth);
rotationRedirectorScript.redirectionObject = redirectionObject.transform;
var rotationCollider = rotationRedirector.AddComponent<BoxCollider>();
float colliderWidth = corridorDepth;
rotationCollider.isTrigger = true;
rotationCollider.size = new Vector3(colliderWidth, corridorHeight, colliderWidth);
Vector3 localRotationPoint = Quaternion.Inverse(rotation) * (script.rotationPoint - center);
rotationRedirectorScript.RotationPoint = localRotationPoint;
rotationCollider.center = localRotationPoint + Vector3.up * corridorHeight / 2;
rotationRedirector.layer = 8;
roomAndProgressManager.rooms.Add(room);
roomAndProgressManager.roomScripts.Add(script);
//calculate numpad position and rotation
(Vector3 numPadPos, Quaternion numPadRot) = script.CalculateNumPadPosRot();
GameObject numPad = Instantiate(numPadPrefab, numPadPos, numPadRot, room.transform);
(Vector3 boardPos, Quaternion boardRot) = script.CalculateBoardPosRot();
GameObject board = Instantiate(boardPrefab, boardPos, boardRot, room.transform);
var localBoardScale = board.transform.localScale;
var newBoardX = corridorDepth - wallThickness * 2;
var newBoardY = localBoardScale.y / localBoardScale.x * newBoardX;
board.transform.localScale = new Vector3(newBoardX, newBoardY, localBoardScale.z);
room.AddComponent<RotationGainMechanism>().Init(numPad, board, rotationRedirector.GetComponent<RotationRedirector>(), this.redirection);
if (!outCorridorSet)
{
if (redirection)
{
script.GenerateWalls();
}
else
{
script.GenerateWallsLegacy();
}
} else
{
bool longerSecondWall = !script.IsCorridorOnShortSide() && !wasOnShortSide;
bool longerThirdWall = script.IsCorridorOnShortSide() && wasOnShortSide;
if (redirection || i == levelLength - 1)
{
script.GenerateWalls(frontInCorridorCorner, backInCorridorCorner, entranceCornerIndex, longerSecondWall, longerThirdWall);
}
else
{
script.GenerateWallsLegacy(frontInCorridorCorner, backInCorridorCorner, entranceCornerIndex, longerSecondWall, longerThirdWall);
}
}
rotationRedirector.GetComponent<RotationRedirector>().enabled = this.redirection;
// calculations for the next room
if (script.sideDoorIsRight)
{
rotationRedirectorScript.RotationDegrees = 270;
rotation = rotation * Quaternion.Euler(0, 270, 0);
}
else
{
rotationRedirectorScript.RotationDegrees = 90;
rotation = rotation * Quaternion.Euler(0, 90, 0);
}
Vector3 outOfDoor = script.sideDoorIsRight ? script.leftToRight : -script.leftToRight;
frontInCorridorCorner = script.corridorCornerInFrontOfRotationPoint;
backInCorridorCorner = script.corridorCornerBehindRotationPoint;
entranceCornerIndex = script.sideDoorIsRight ? 0 : 3;
outCorridorSet = true;
int secondDirectionBlock;
if (script.sideDoorIsRight)
{
secondDirectionBlock = 0;
}
else
{
secondDirectionBlock = 2;
}
direction = RandomDirectionBlocked(new List<int>() {3, secondDirectionBlock}); // 3: side would be where the current door is,
if (script.sideDoorIsRight)
{
if (direction == 1)
{
forceRightDoor = true;
forceLeftDoor = false;
}
else if (direction == 2)
{
forceLeftDoor = true;
forceRightDoor = false;
}
}
else
{
if (direction == 0)
{
forceRightDoor = true;
forceLeftDoor = false;
} else if (direction == 1)
{
forceRightDoor = false;
forceLeftDoor = true;
}
}
corners = CalculateNewCorners(outOfDoor, script.toFrontWall, script.rotationPoint, script.sideDoorIsRight, this.CorridorIsOnWidthSide(script.leftToRight.magnitude));
wasOnShortSide = script.IsCorridorOnShortSide();
Debug.DrawLine(script.rotationPoint, debugLastRotationPoint, Color.yellow, 10000f, false);
debugLastRotationPoint = script.rotationPoint;
}
roomAndProgressManager.Init();
}