-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.cpp
531 lines (434 loc) · 16.9 KB
/
World.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include "World.hpp"
#include "Utility.hpp"
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Keyboard.hpp>
using namespace utility;
World::World(sf::RenderWindow& window)
: mWindow(window)
, mTextures()
, mSegments()
, mBackground()
, mCars()
, mPlayer(nullptr)
, mSegmentLength(200.f)
, mCameraDepth(1 / std::tan((100.f / 2.f) * static_cast<float>(M_PI / 180.f)))
, mCameraHeight(1000.f)
, mPosition()
, mRumbleLength(3u)
, mDrawDistance(500u)
, mTrackLength()
, mMaxSpeed(mSegmentLength / (1 / 60.f))
, mSteer()
{
loadTextures();
buildScene();
}
void World::update(float dt)
{
auto accel = mMaxSpeed / 5.f;
auto breaking = -mMaxSpeed;
auto decel = -accel;
auto offRoadDecel = -mMaxSpeed / 2.f;
auto offRoadLimit = mMaxSpeed / 4.f;
auto centrifugal = 0.3f;
bool keyLeft = false;
bool keyRight = false;
const auto& baseSegment = *mSegments[static_cast<std::size_t>(std::floor(mPosition / mSegmentLength)) % mSegments.size()];
const auto& playerSegment = *mSegments[static_cast<std::size_t>(std::floor((mPosition + mPlayer->getZValue()) / mSegmentLength)) % mSegments.size()];
auto speedPercent = mPlayer->getSpeed() / mMaxSpeed;
auto dx = dt * speedPercent;
// update cars
for (const auto& car : mCars)
{
auto& oldSegment = *mSegments[static_cast<std::size_t>(std::floor(car->getZValue() / mSegmentLength)) % mSegments.size()];
car->setOffset(car->getOffset() + updateCarOffset(car, oldSegment, playerSegment));
car->setZValue(increase(car->getZValue(), dt * car->getSpeed(), mTrackLength));
car->setPercent(percentRemaining(car->getZValue(), mSegmentLength));
auto& newSegment = *mSegments[static_cast<std::size_t>(std::floor(car->getZValue() / mSegmentLength)) % mSegments.size()];
if (oldSegment.getIndex() == newSegment.getIndex())
continue;
newSegment.addCar(car);
oldSegment.removeCar();
}
// update player
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
keyLeft = true;
mPlayer->moveLeft(dx);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
keyRight = true;
mPlayer->moveRight(dx);
}
mPlayer->adaptMoving((dx * speedPercent * playerSegment.getCurve() * centrifugal));
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
mPlayer->accelerate(accel * dt);
else
mPlayer->accelerate(decel * dt);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
mPlayer->accelerate(breaking * dt);
if ((mPlayer->getOffset() < -1.f) || (mPlayer->getOffset() > 1.f))
{
if (mPlayer->getSpeed() > offRoadLimit)
mPlayer->accelerate(offRoadDecel * dt);
// check for collision with side road objects
const auto& spriteVector = playerSegment.getSprites();
for (const auto& sprite : spriteVector)
{
if (!sprite->getBoundingRect().intersects(mPlayer->getBoundingRect()))
continue;
mPlayer->setSpeed(accel);
mPosition = increase(playerSegment.point1().world.z, -mPlayer->getZValue(), mTrackLength);
break;
}
}
// check for collision with cars
const auto& carsVector = playerSegment.getCars();
for (const auto& car : carsVector)
{
if (mPlayer->getSpeed() <= car->getSpeed())
continue;
if (!car->getBoundingRect().intersects(mPlayer->getBoundingRect()))
continue;
mPlayer->setSpeed(car->getSpeed() * (car->getSpeed() / mPlayer->getSpeed()));
mPosition = increase(car->getZValue(), -mPlayer->getZValue(), mTrackLength);
break;
}
mPlayer->setOffset(limit(mPlayer->getOffset(), -2.f, 2.f));
mPlayer->setSpeed(limit(mPlayer->getSpeed(), 0, mMaxSpeed));
mPosition = increase(mPosition, dt * mPlayer->getSpeed(), mTrackLength);
mSteer = (keyLeft ? -1 : keyRight ? 1 : 0);
// update backgrounds
for (const auto& object : mBackground)
object->update(dt, playerSegment.getCurve(), mPlayer->getSpeed());
}
void World::draw()
{
auto width = 640.f;
auto height = 480.f;
auto roadWidth = 2000.f;
auto fogDensity = 5.f;
const auto& baseSegment = *mSegments[static_cast<std::size_t>(std::floor(mPosition / mSegmentLength)) % mSegments.size()];
auto basePercent = percentRemaining(mPosition, mSegmentLength);
const auto& playerSegment = *mSegments[static_cast<std::size_t>(std::floor((mPosition + mPlayer->getZValue()) / mSegmentLength)) % mSegments.size()];
auto playerPercent = percentRemaining(mPosition + mPlayer->getZValue(), mSegmentLength);
auto playerY = interpolate(playerSegment.point1().world.y, playerSegment.point2().world.y, playerPercent);
auto x = 0.f;
auto dx = -(baseSegment.getCurve() * basePercent);
auto maxy = height;
for (const auto& object : mBackground)
mWindow.draw(*object);
// render segments
for (auto n = 0u; n < mDrawDistance; ++n)
{
auto& segment = *mSegments[(baseSegment.getIndex() + n) % mSegments.size()];
segment.setClip(maxy);
bool looped = segment.getIndex() < baseSegment.getIndex();
auto fog = exponentialFog(n / static_cast<float>(mDrawDistance), fogDensity);
auto camX = mPlayer->getOffset() * roadWidth;
auto camY = playerY + mCameraHeight;
auto camZ = mPosition - (looped ? mTrackLength : 0.f);
auto& point1 = segment.point1();
auto& point2 = segment.point2();
point1.project(camX - x, camY, camZ, mCameraDepth, width, height, roadWidth);
point2.project(camX - x - dx, camY, camZ, mCameraDepth, width, height, roadWidth);
x += dx;
dx += segment.getCurve();
if ((point1.camera.z <= mCameraDepth) || (point2.screen.y >= maxy || point2.screen.y >= point1.screen.y))
continue;
segment.setGrounds(roadWidth, fog);
mWindow.draw(segment);
maxy = point2.screen.y;
}
for (auto n = (mDrawDistance - 1); n > 0; --n)
{
const auto& segment = *mSegments[(baseSegment.getIndex() + n) % mSegments.size()];
// render side road sprites
const auto& spriteVector = segment.getSprites();
for (const auto& sprite : spriteVector)
{
auto spriteScale = segment.point1().screen.scale;
auto spriteX = segment.point1().screen.x + spriteScale * sprite->getOffset() * roadWidth * width / 2;
auto spriteY = segment.point1().screen.y;
auto offsetX = sprite->getOffset() < 0 ? -1.f : 0.f;
auto offsetY = -1.f;
sprite->update(width, roadWidth, spriteScale, spriteX, spriteY, offsetX, offsetY, segment.getClip());
mWindow.draw(*sprite);
}
// cars
const auto& carsVector = segment.getCars();
for (const auto& car : carsVector)
{
auto spriteScale = interpolate(segment.point1().screen.scale, segment.point2().screen.scale, car->getPercent());
auto interpolateValue = interpolate(segment.point1().screen.x, segment.point2().screen.x, car->getPercent());
auto spriteX = interpolateValue + spriteScale * car->getOffset() * roadWidth * width / 2;
auto spriteY = interpolate(segment.point1().screen.y, segment.point2().screen.y, car->getPercent());
car->update(width, roadWidth, spriteScale, spriteX, spriteY, -0.5f, -1.f, segment.getClip());
mWindow.draw(*car);
}
// player
if (segment.getIndex() != playerSegment.getIndex())
continue;
auto spriteScale = mCameraDepth / mPlayer->getZValue();
auto interpolateValue = interpolate(playerSegment.point1().camera.y, playerSegment.point2().camera.y, playerPercent);
auto correctionValue = mCameraDepth / mPlayer->getZValue() * interpolateValue * height / 2;
auto clip = playerSegment.point2().world.y - playerSegment.point1().world.y;
auto spriteX = width / 2;
auto spriteY = height / 2 - correctionValue;
mPlayer->update(width, roadWidth, spriteScale, spriteX, spriteY, mSteer, clip);
mWindow.draw(*mPlayer);
}
}
void World::loadTextures()
{
mTextures.load(Textures::Sprites, "Media/sprites.png");
mTextures.load(Textures::Sky, "Media/sky.png");
mTextures.load(Textures::Hills, "Media/hills.png");
mTextures.load(Textures::Trees, "Media/trees.png");
}
void World::buildScene()
{
// setup sprites
auto& skyTexture = mTextures.get(Textures::Sky);
auto sky = std::make_unique<Backgrounds>(Backgrounds::Sky, skyTexture);
mBackground.push_back(std::move(sky));
auto& hillsTexture = mTextures.get(Textures::Hills);
hillsTexture.setRepeated(true);
sf::IntRect hillRect(0, 0, 4000, mWindow.getSize().y);
auto hills = std::make_unique<Backgrounds>(Backgrounds::Hills, hillsTexture, hillRect);
hills->setPosition(-hillRect.width / 2.f, 0.f);
mBackground.push_back(std::move(hills));
auto& treesTexture = mTextures.get(Textures::Trees);
treesTexture.setRepeated(true);
sf::IntRect treeRect(0, 0, 6000, mWindow.getSize().y);
auto tree = std::make_unique<Backgrounds>(Backgrounds::Trees, treesTexture, treeRect);
tree->setPosition(-treeRect.width / 2.f, 0.f);
mBackground.push_back(std::move(tree));
// add player
SpritesData spritesData;
mPlayer = std::make_unique<Player>(mTextures, spritesData.PlayerStraight, mCameraHeight * mCameraDepth);
// build road
addStraight(length.shorty);
addLowRollingHills();
addSCurves();
addCurve(length.medium, curve.medium, hill.low);
addBumps();
addLowRollingHills();
addCurve(length.longy * 2, curve.medium, hill.medium);
addStraight();
addHill(length.medium, hill.high);
addSCurves();
addCurve(length.longy, -curve.medium, hill.none);
addHill(length.longy, hill.high);
addCurve(length.longy, curve.medium, -hill.low);
addBumps();
addHill(length.longy, -hill.medium);
addStraight();
addSCurves();
addDownhillToEnd();
// add side sprites
addSprite(20u, spritesData.BillBoard07, -1.f);
addSprite(40u, spritesData.BillBoard06, -1.f);
addSprite(60u, spritesData.BillBoard08, -1.f);
addSprite(80u, spritesData.BillBoard09, -1.f);
addSprite(100u, spritesData.BillBoard01, -1.f);
addSprite(120u, spritesData.BillBoard02, -1.f);
addSprite(140u, spritesData.BillBoard03, -1.f);
addSprite(160u, spritesData.BillBoard04, -1.f);
addSprite(180u, spritesData.BillBoard05, -1.f);
addSprite(240u, spritesData.BillBoard07, -1.2f);
addSprite(240u, spritesData.BillBoard06, 1.2f);
addSprite(mSegments.size() - 25, spritesData.BillBoard07, -1.2f);
addSprite(mSegments.size() - 25, spritesData.BillBoard06, 1.2f);
static auto& dist1 = std::uniform_real_distribution<float>(0.6f, 0.9f);
static auto& dist2 = std::uniform_real_distribution<float>(1.5f, 5.5f);
for (auto n = 10u; n < 240; n += 4 + n / 100u)
{
addSprite(n, spritesData.PalmTree, random(dist1));
addSprite(n, spritesData.PalmTree, random(dist2));
}
static auto& dist3 = std::uniform_int_distribution<unsigned>(0, 5);
static auto& dist4 = std::uniform_real_distribution<float>(1.1f, 5.5f);
for (auto n = 250u; n < 1000; n += 5)
{
addSprite(n, spritesData.Column, 1.1f);
addSprite(n + random(dist3), spritesData.Tree1, -random(dist4));
addSprite(n + random(dist3), spritesData.Tree2, -random(dist4));
}
std::vector<float> vec{ 1.f, -1.f };
for (auto n = 200u; n < mSegments.size(); n += 3)
{
addSprite(n, randomChoice(spritesData.Plants), randomChoice(vec) * random(dist4));
}
static auto& dist5 = std::uniform_int_distribution<unsigned>(0, 50);
for (auto n = 1000u; n < (mSegments.size() - 50); n += 100)
{
auto side = randomChoice(vec);
addSprite(n + random(dist5), randomChoice(spritesData.BillBoads), -side);
for (auto i = 0u; i < 20; i++)
{
addSprite(n + random(dist5), randomChoice(spritesData.Plants), side * random(dist4));
}
}
// add Cars
std::vector<float> vecCars{ 0.9f, 0.f, -0.6f };
auto totalCars = 100u;
static auto& dist6 = std::uniform_int_distribution<unsigned>(0u, mSegments.size());
static auto& dist7 = std::uniform_real_distribution<float>(0.f, 0.7f);
for (auto n = 0u; n < totalCars; n++)
{
auto offset = randomChoice(vecCars);
auto z = random(dist6) * mSegmentLength;
auto sprite = spritesData.Car01; //randomChoice(spritesData.Cars);
auto speed = mMaxSpeed / 4.f + random(dist7) * mMaxSpeed / (sprite == spritesData.Semi ? 4.f : 2.f);
auto car = std::make_shared<Cars>(mTextures, sprite, offset, z, speed);
auto& segment = *mSegments[static_cast<std::size_t>(std::floor(z / mSegmentLength)) % mSegments.size()];
mCars.push_back(car);
segment.addCar(car);
}
// setup start and finish of road marks
mSegments[mSegments[static_cast<std::size_t>(std::floor(mPlayer->getZValue() / mSegmentLength)) % mSegments.size()]->getIndex() + 2]->setSegmentColors(Colors::Start);
mSegments[mSegments[static_cast<std::size_t>(std::floor(mPlayer->getZValue() / mSegmentLength)) % mSegments.size()]->getIndex() + 3]->setSegmentColors(Colors::Start);
for (auto n = 0u; n < mRumbleLength; n++)
{
mSegments[mSegments.size() - 1 - n]->setSegmentColors(Colors::Finish);
}
mTrackLength = mSegments.size() * mSegmentLength;
}
void World::addSegment(float curve, float y)
{
auto n = mSegments.size();
auto segment = std::make_unique<Segment>();
segment->setIndex(n);
segment->setCurve(curve);
segment->point1().world.y = lastY();
segment->point2().world.y = y;
segment->point1().world.z = n * mSegmentLength;
segment->point2().world.z = (n + 1) * mSegmentLength;
if (static_cast<std::size_t>(std::floor(n / mRumbleLength)) % 2)
segment->setSegmentColors(Colors::Light);
else
segment->setSegmentColors(Colors::Dark);
mSegments.push_back(std::move(segment));
}
void World::addSprite(std::size_t n, const sf::IntRect& spriteRect, float offset)
{
auto sprite = std::make_unique<Sprites>(mTextures, spriteRect, offset);
mSegments[n]->addSprite(std::move(sprite));
}
void World::addRoad(float enter, float hold, float leave, float curve, float y)
{
auto startY = lastY();
auto endY = startY + (y * mSegmentLength);
auto total = enter + hold + leave;
for (auto n = 0.f; n < enter; n++)
addSegment(easeIn(0, curve, n / enter), easeInOut(startY, endY, n / total));
for (auto n = 0.f; n < hold; n++)
addSegment(curve, easeInOut(startY, endY, (enter + n) / total));
for (auto n = 0.f; n < leave; n++)
addSegment(easeInOut(curve, 0, n / leave), easeInOut(startY, endY, (enter + hold + n) / total));
}
void World::addStraight(float n)
{
auto num = (n == 0) ? length.medium : n;
addRoad(num, num, num, 0, 0);
}
void World::addHill(float n, float h)
{
auto num = (n == 0) ? length.medium : n;
auto height = (h == 0) ? hill.medium : h;
addRoad(num, num, num, 0, height);
}
void World::addCurve(float n, float c, float h)
{
auto num = (n == 0) ? length.medium : n;
auto curves = (c == 0) ? curve.medium : c;
auto height = (h == 0) ? hill.none : h;
addRoad(num, num, num, curves, height);
}
void World::addDownhillToEnd(float n)
{
auto num = (n == 0) ? 200.f : n;
addRoad(num, num, num, -curve.easy, -lastY() / mSegmentLength);
}
void World::addSCurves()
{
addRoad(length.medium, length.medium, length.medium, -curve.easy, hill.none);
addRoad(length.medium, length.medium, length.medium, curve.medium, hill.medium);
addRoad(length.medium, length.medium, length.medium, curve.easy, -hill.low);
addRoad(length.medium, length.medium, length.medium, -curve.easy, hill.medium);
addRoad(length.medium, length.medium, length.medium, -curve.medium, -hill.medium);
}
void World::addLowRollingHills(float n, float h)
{
auto num = (n == 0) ? length.shorty : n;
auto height = (h == 0) ? hill.low : h;
addRoad(num, num, num, 0, height / 2);
addRoad(num, num, num, 0, -height);
addRoad(num, num, num, 0, height);
addRoad(num, num, num, 0, 0);
addRoad(num, num, num, 0, height / 2);
addRoad(num, num, num, 0, 0);
}
void World::addBumps()
{
addRoad(10, 10, 10, 0, 5);
addRoad(10, 10, 10, 0, -2);
addRoad(10, 10, 10, 0, -5);
addRoad(10, 10, 10, 0, 8);
addRoad(10, 10, 10, 0, 5);
addRoad(10, 10, 10, 0, -7);
addRoad(10, 10, 10, 0, 5);
addRoad(10, 10, 10, 0, -2);
}
float World::lastY()
{
return (mSegments.size() == 0) ? 0 : mSegments[mSegments.size() - 1]->point2().world.y;
}
float World::updateCarOffset(Cars::Ptr car, const Segment& carSegment, const Segment& playerSegment)
{
// optimization, dont bother steering around other cars when 'out of sight' of the player
if (std::abs(static_cast<float>(carSegment.getIndex()) - static_cast<float>(playerSegment.getIndex())) > mDrawDistance)
return 0.f;
auto dir = 0;
for (auto i = 1u; i < 20; ++i)
{
const auto& segment = *mSegments[(carSegment.getIndex() + i) % mSegments.size()];
if (segment.getIndex() < playerSegment.getIndex()
&& car->getSpeed() > mPlayer->getSpeed()
&& car->getBoundingRect().intersects(mPlayer->getBoundingRect()))
{
if (mPlayer->getOffset() > 0.5f)
dir = -1;
else if (mPlayer->getOffset() < -0.5f)
dir = 1;
else
dir = car->getOffset() > mPlayer->getOffset() ? 1 : -1;
// the closer the cars (smaller i) and the greated the speed ratio, the larger the offset
return dir * 1 / static_cast<float>(i) * (car->getSpeed() - mPlayer->getSpeed()) / mMaxSpeed;
}
const auto& carVector = segment.getCars();
for (const auto& otherCar : carVector)
{
if (car->getSpeed() <= otherCar->getSpeed())
continue;
if (!car->getBoundingRect().intersects(otherCar->getBoundingRect()))
continue;
if (otherCar->getOffset() > 0.5f)
dir = -1;
else if (otherCar->getOffset() < -0.5f)
dir = 1;
else
dir = car->getOffset() > otherCar->getOffset() ? 1 : -1;
return dir * 1 / static_cast<float>(i) * (car->getSpeed() - otherCar->getSpeed()) / mMaxSpeed;
}
}
// if no cars ahead, but I have somehow ended up off road, then steer back on
if (car->getOffset() < -0.9f)
return 0.1f;
else if (car->getOffset() > 0.9f)
return -0.1f;
else
return 0.f;
}