-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.ts
197 lines (166 loc) · 5.9 KB
/
classes.ts
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
195
196
197
import { Vector3 } from "three"
const { getRoadParts } = require('./utils')
type RoadPartType = {
url: string
name: string
directions?: Vector3[]
rotation?: number
}
class RoadPart {
url: string
name: string
directions: Vector3[]
rotation?: number
constructor(parameters: RoadPartType) {
if (!parameters) return
this.directions = parameters.directions instanceof Array ? parameters.directions : []
this.url = parameters.url
this.name = parameters.name
if (parameters.rotation || parameters.rotation === 0) {
this.rotation = parameters.rotation
}
}
rotate(angle: number) {
this.directions = this.directions.map((direction: Vector3) => direction.applyAxisAngle(new Vector3(0,1,0),angle))
if (this.rotation) {
this.rotation += angle
} else {
this.rotation = angle
}
}
clone(): RoadPart {
const {url, name, directions, rotation} = this
const roadClone = new RoadPart({
url,
name,
directions: directions.map(direction => direction.clone())
})
if (rotation || rotation === 0) {
roadClone.rotation = rotation
}
return roadClone
}
}
class LinkedRoadPart extends RoadPart {
position: Vector3
usedDirections: Vector3[]
constructor(roadPart: RoadPart, position?: Vector3, usedDirections?: Vector3[]) {
super(roadPart instanceof RoadPart ? roadPart.clone() : roadPart)
this.position = position instanceof Vector3 ? position : new Vector3()
this.usedDirections = usedDirections instanceof Array ? usedDirections : []
}
translate(newPosition: Vector3) {
this.position.add(newPosition)
}
private selectDirection(direction?: Vector3): Vector3 {
if (this.getAvailableDirections().length < 1) {
throw new Error('No direction available')
}
let nextDirection: Vector3 = this.getAvailableDirections()[0]
if (direction instanceof Vector3) {
const connectDirection = this.getAvailableDirections().find(dir => dir.clone().add(direction).length() < 0.0000001)
if (connectDirection instanceof Vector3) {
nextDirection = connectDirection.clone()
} else {
let connectDirectionRotated: Vector3 | null = null
while (!(connectDirectionRotated instanceof Vector3) || this.rotation > 3*Math.PI/2) {
this.rotate(Math.PI/2)
connectDirectionRotated = this.getAvailableDirections().find(dir => dir.clone().add(direction).length() < 0.0000001)
}
if (!(connectDirectionRotated instanceof Vector3)) {
throw new Error('Cound not connect to direction !')
} else {
nextDirection = connectDirectionRotated.clone()
}
}
}
this.usedDirections.push(nextDirection)
return nextDirection
}
connect(next: RoadPart): LinkedRoadPart {
if (this.getAvailableDirections().length < 1) {
throw new Error('No direction available')
}
const nextRoadPart = new LinkedRoadPart(next)
nextRoadPart.translate(this.position)
const direction = this.selectDirection()
nextRoadPart.translate(direction)
const nextDir = nextRoadPart.selectDirection(direction)
// console.log(this.name+' -> '+next.name, nextDir)
return nextRoadPart
}
getAvailableDirections(): Vector3[] {
return this.directions.filter(direction => !this.usedDirections.includes(direction))
}
}
class Road {
parts: RoadPart[]
path: LinkedRoadPart[]
static build(length: number): Promise<Road> {
return new Promise((resolve,reject) => {
getRoadParts().then((parts: RoadPart[]) => {
resolve(new Road(parts,length))
}).catch((err: string) => reject(err))
})
}
constructor(parts: RoadPart[], length: number) {
this.parts = parts.map((part: RoadPart) => new RoadPart(part))
this.path = []
this.createPath(length, 5)
}
createPath(length: number, turns: number) {
const end = this.getRoadPartByName('road_end')
const start = end.clone()
start.rotate(Math.PI)
this.connect(start)
this.connectAll(this.createStraightLine(length))
for (let index = 0; index < turns; index++) {
const turn = this.getRoadPartByName('road_bend')
this.connect(turn)
this.connectAll(this.createStraightLine(length))
}
this.connect(end)
}
connectAll(parts: RoadPart[]) {
parts.forEach(part => this.connect(part))
}
connect(part: RoadPart) {
let linkedRoadPart = new LinkedRoadPart(part)
if (this.path.length > 0) {
const previous = [...this.path].pop()
linkedRoadPart = previous.connect(part)
}
this.path.push(linkedRoadPart)
}
createStraightLine(length: number): RoadPart[] {
const path = []
if (length < 2) return path
const straight = this.getRoadPartByName('road_straight')
const crossing = this.getRoadPartByName('road_crossing')
const crossingPosition = Math.floor(length / 2)
for (let i=0;i<length;++i) {
if (i === crossingPosition) {
path.push(crossing)
} else {
path.push(straight.clone())
}
}
return path
}
getRoadPartByName(name: string): RoadPart {
const part = this.parts.find(part => part.name === name)
if (part instanceof RoadPart) {
return part.clone()
} else {
return null
}
}
getPath() {
return this.path
}
}
module.exports = {
RoadPart,
Road,
LinkedRoadPart
}