-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworldrenderengine.ts
More file actions
277 lines (236 loc) · 12.2 KB
/
Copy pathworldrenderengine.ts
File metadata and controls
277 lines (236 loc) · 12.2 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
enum ObstacleDirection {
Top,
Left,
Right
}
class RenderObstacle {
public image: Image;
public x: number;
public y: number;
public mirror: boolean;
public scale: number;
}
class WorldRenderEngine {
private sinTable: number[];
private drawZ: number;
private drawY: number;
private roadAngleX: number;
private roadAngleY: number;
private roadCenter: number;
private roadY: number;
private stripeToggle: boolean;
private backdropOffset: number;
private perspectiveHorizontalCenter: number;
private obstaclesToRenders: RenderObstacle[];
constructor() {
this.sinTable = this.createSinTableFP();
this.backdropOffset = 0;
}
public draw(targetImg: Image, travelDistance: number, perspectiveHorizontalCenter: number): boolean {
const firstStripe = Math.idiv(travelDistance, STRIPE_HEIGHT);
const firstStripeOffeset = travelDistance % STRIPE_HEIGHT;
const firstStripeIndex = firstStripe * CIRCUIT_STRIPE_RECORD_LEN;
if (firstStripeIndex + STRIPTES_VIEW_PORT * CIRCUIT_STRIPE_RECORD_LEN >= CIRCUIT.length)
return false;
this.perspectiveHorizontalCenter = perspectiveHorizontalCenter;
// Draw the steet
this.drawZ = 0;
this.drawY = SCREEN_HEIGHT;
this.roadAngleX = 0;
this.roadAngleY = 0;
this.roadCenter = 0;
this.roadY = Math.round((SCREEN_HEIGHT - PERSPECTIVE_VERTICAL_CENTER) * POS_FIXED_MATH_ONE);
this.stripeToggle = firstStripe % 2 == 0;
this.obstaclesToRenders = [];
for (let i = 0; i < STRIPTES_VIEW_PORT; i++) {
const circuitIndex = firstStripeIndex + i * CIRCUIT_STRIPE_RECORD_LEN;
const offset = i == 0 ? firstStripeOffeset : 0;
this.drawStripe(circuitIndex, i, targetImg , offset);
this.stripeToggle = !this.stripeToggle;
}
// Draw the sky
this.drawY--;
targetImg.fillRect(0, 0, SCREEN_WIDTH, this.drawY , 9);
// Draw backdrop image
let backdropOffset = this.backdropOffset - (((CIRCUIT[firstStripeIndex] as number) * 2) >> ANGLES_BITS);
if (backdropOffset < 0)
backdropOffset = SCREEN_WIDTH + backdropOffset;
else if (backdropOffset > SCREEN_WIDTH)
backdropOffset -= SCREEN_WIDTH;
targetImg.drawTransparentImage(BACKDROP_IMG, backdropOffset, this.drawY - BACKDROP_IMG.height);
if (backdropOffset > 0)
targetImg.drawTransparentImage(BACKDROP_IMG, backdropOffset - BACKDROP_IMG.width, this.drawY - BACKDROP_IMG.height);
this.backdropOffset = backdropOffset;
// Display ostacles sprite
const lastObstacleToRender = this.obstaclesToRenders.length - 1;
for (let i = lastObstacleToRender; i >= 0; i--) {
const obstacle = this.obstaclesToRenders[i];
if (obstacle.image.width >= obstacle.image.height)
this.drawScaledH(obstacle.image, targetImg, obstacle.scale, obstacle.mirror, obstacle.x, obstacle.y);
else
this.drawScaledV(obstacle.image, targetImg, obstacle.scale, obstacle.mirror, obstacle.x, obstacle.y);
}
return true;
}
private drawStripe(circuitIndex: number, stripeNum: number, img: Image, startSTRIPE_HEIGHT: number): void {
const directionAngleDelta = CIRCUIT[circuitIndex] as number;
const slopeAngleDelta = CIRCUIT[circuitIndex + 1] as number;
const leftObstacle = CIRCUIT[circuitIndex + 2] as Obstacle;
const rightObstacle = CIRCUIT[circuitIndex + 3] as Obstacle;
const topObstacle = CIRCUIT[circuitIndex + 4] as Obstacle;
const leftDirtColor = CIRCUIT[circuitIndex + 5] as number;
const rightDirtColor = CIRCUIT[circuitIndex + 6] as number;
const roadColor = this.stripeToggle ? 12 : 11;
const borderColor = this.stripeToggle ? 1 : 12;
const laneColor = 1;
for (let i = startSTRIPE_HEIGHT; i < STRIPE_HEIGHT; i++) {
this.roadAngleX += directionAngleDelta;
this.roadAngleY += slopeAngleDelta;
if (this.roadAngleX >= 0) {
if (this.roadAngleX > MAX_ANGLE)
this.roadAngleX = MAX_ANGLE;
this.roadCenter += this.sinTable[this.roadAngleX >> ANGLES_BITS];
} else {
if (this.roadAngleX < MIN_ANGLE)
this.roadAngleX = MIN_ANGLE;
this.roadCenter -= this.sinTable[-(this.roadAngleX >> ANGLES_BITS)];
}
if (this.roadAngleY >= 0) {
if (this.roadAngleY > MAX_ANGLE)
this.roadAngleY = MAX_ANGLE;
this.roadY += this.sinTable[this.roadAngleY >> ANGLES_BITS];
} else {
if (this.roadAngleY < MIN_ANGLE)
this.roadAngleY = MIN_ANGLE;
this.roadY -= this.sinTable[-(this.roadAngleY >> ANGLES_BITS)];
}
// 3D coordinates
const x1_3d = Math.round(this.roadCenter - STRIPE_HALF_WIDTH_FP);
const x2_3d = Math.round(this.roadCenter + STRIPE_HALF_WIDTH_FP);
const y_3d = this.roadY;
// Swith to 2D
const roadLine2D = this.horizontalLine3Dto2D(x1_3d, x2_3d, y_3d, this.drawZ);
// Calculate line boundaries
const x1_borderL = roadLine2D[0];
const x2_borderR = roadLine2D[1];
const width = x2_borderR - x1_borderL;
const borderWidht = Math.idiv(width, 18);
const x1_road = x1_borderL + borderWidht;
const x2_road = x2_borderR - borderWidht;
const laneLineWidth = Math.idiv(borderWidht, 3);
const laneWidth = Math.idiv(width, 3);
const x1_lane1 = x1_borderL + laneWidth;
const x2_lane1 = x1_lane1 + laneLineWidth;
const x2_lane2 = x2_borderR - laneWidth;
const x1_lane2 = x2_lane2 - laneLineWidth;
// Draw side obstacles
if (i == STRIPE_HALF_HEIGHT && roadLine2D[2] <= this.drawY) {
if (topObstacle != null) {
this.drawSideObstacles(this.roadCenter, y_3d, this.drawZ, topObstacle, ObstacleDirection.Top);
}
if (leftObstacle != null) {
this.drawSideObstacles(x1_3d, y_3d, this.drawZ, leftObstacle, ObstacleDirection.Left);
}
if (rightObstacle != null) {
this.drawSideObstacles(x2_3d, y_3d, this.drawZ, rightObstacle, ObstacleDirection.Right);
}
}
// Draw steet
while (roadLine2D[2] < this.drawY) {
this.drawY--;
img.drawLine(0, this.drawY, x1_borderL, this.drawY, leftDirtColor);
img.drawLine(x2_borderR, this.drawY, SCREEN_WIDTH, this.drawY, rightDirtColor);
img.drawLine(x1_borderL, this.drawY, x1_road, this.drawY, borderColor);
img.drawLine(x1_road, this.drawY, x2_road, this.drawY, roadColor);
img.drawLine(x2_road, this.drawY, x2_borderR, this.drawY, borderColor);
// Central lane stripes
if (this.stripeToggle) {
if (i > STRIPE_HALF_HEIGHT) {
img.drawLine(x1_lane1, this.drawY, x2_lane1, this.drawY, laneColor);
img.drawLine(x1_lane2, this.drawY, x2_lane2, this.drawY, laneColor);
}
} else {
if (i < STRIPE_HALF_HEIGHT) {
img.drawLine(x1_lane1, this.drawY, x2_lane1, this.drawY, laneColor);
img.drawLine(x1_lane2, this.drawY, x2_lane2, this.drawY, laneColor);
}
}
}
this.drawZ += POS_FIXED_MATH_ONE;
}
}
private horizontalLine3Dto2D(x1: number, x2: number, y: number, z: number): number[] {
const denom = POS_FIXED_MATH_ONE + Math.idiv(z, Z_PERSPECTIVE_FACTOR);
return [
Math.idiv(x1, denom) + this.perspectiveHorizontalCenter,
Math.idiv(x2, denom) + this.perspectiveHorizontalCenter,
Math.idiv(y, denom) + PERSPECTIVE_VERTICAL_CENTER
];
}
private drawSideObstacles(x: number, y: number, z: number, obstacle: Obstacle, direction: ObstacleDirection): void {
const obstacleToRender = new RenderObstacle();
const denom = POS_FIXED_MATH_ONE + Math.idiv(z, Z_PERSPECTIVE_FACTOR);
obstacleToRender.image = obstacle.image;
obstacleToRender.scale = denom;
obstacleToRender.mirror = direction == ObstacleDirection.Right && obstacle.reqMirror;
const imageWidthFixed = obstacleToRender.image.width * POS_FIXED_MATH_ONE;
const imageHeightFixed = obstacleToRender.image.height * POS_FIXED_MATH_ONE;
if (direction == ObstacleDirection.Left) {
x += obstacle.offset;
const x_2D = Math.idiv(x - imageWidthFixed, denom) + this.perspectiveHorizontalCenter;
const y_2D = Math.idiv(y - imageHeightFixed, denom) + PERSPECTIVE_VERTICAL_CENTER;
obstacleToRender.x = x_2D;
obstacleToRender.y = y_2D;
} else if (direction == ObstacleDirection.Right) {
x -= obstacle.offset;
const x_2D = Math.idiv(x, denom) + this.perspectiveHorizontalCenter;
const y_2D = Math.idiv(y - imageHeightFixed, denom) + PERSPECTIVE_VERTICAL_CENTER;
obstacleToRender.x = x_2D;
obstacleToRender.y = y_2D;
} else if (direction == ObstacleDirection.Top) {
y -= obstacle.offset;
const x_2D = Math.idiv(x - Math.idiv(imageWidthFixed, 2), denom) + this.perspectiveHorizontalCenter;
const y_2D = Math.idiv(y - imageHeightFixed, denom) + PERSPECTIVE_VERTICAL_CENTER;
obstacleToRender.x = x_2D;
obstacleToRender.y = y_2D;
}
this.obstaclesToRenders.push(obstacleToRender);
}
private drawScaledH(sourceImg: Image, targetImg: Image, scale: number, mirror: boolean, dstX: number, dstY: number) {
const outputBuffer = image.create(sourceImg.width, Math.idiv(Math.imul(sourceImg.height, POS_FIXED_MATH_ONE), scale))
const blitRowScale = Math.idiv(Math.imul(sourceImg.width, POS_FIXED_MATH_ONE), scale);
const xEnd = Math.imul(sourceImg.width - 1, POS_FIXED_MATH_ONE);
if (mirror) {
for (let x = 0; x <= xEnd; x += scale)
outputBuffer.blitRow(Math.idiv(xEnd - x, scale), 0, sourceImg, Math.idiv(x, POS_FIXED_MATH_ONE), blitRowScale);
} else {
for (let x = 0; x <= xEnd; x += scale)
outputBuffer.blitRow(Math.idiv(x, scale), 0, sourceImg, Math.idiv(x, POS_FIXED_MATH_ONE), blitRowScale);
}
targetImg.drawTransparentImage(outputBuffer, dstX, dstY)
}
private drawScaledV(sourceImg: Image, targetImg: Image, scale: number, mirror: boolean, dstX: number, dstY: number) {
const maxDimension = sourceImg.height;
const inputBuffer = image.create(maxDimension, maxDimension);
inputBuffer.drawImage(sourceImg, 0, 0);
const outputBuffer = image.create(
Math.idiv(Math.imul(inputBuffer.width, POS_FIXED_MATH_ONE), scale),
Math.idiv(Math.imul(inputBuffer.height, POS_FIXED_MATH_ONE), scale));
const blitRowScale = Math.idiv(Math.imul(maxDimension, POS_FIXED_MATH_ONE), scale);
const xEnd = Math.imul(sourceImg.width - 1, POS_FIXED_MATH_ONE);
if (mirror) {
for (let x = 0; x <= xEnd; x += scale)
outputBuffer.blitRow(Math.idiv(xEnd - x, scale), 0, inputBuffer, Math.idiv(x, POS_FIXED_MATH_ONE), blitRowScale);
} else {
for (let x = 0; x <= xEnd; x += scale)
outputBuffer.blitRow(Math.idiv(x, scale), 0, inputBuffer, Math.idiv(x, POS_FIXED_MATH_ONE), blitRowScale);
}
targetImg.drawTransparentImage(outputBuffer, dstX, dstY)
}
private createSinTableFP(): number[] {
const table = [];
for (let i = 0; i < 90; i++)
table.push(Math.round(Math.sin(Math.PI * i / 180) * 4 * POS_FIXED_MATH_ONE));
return table;
}
}