-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
281 lines (230 loc) · 8.73 KB
/
main.cpp
File metadata and controls
281 lines (230 loc) · 8.73 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
278
279
280
281
#include <iostream>
#include <string>
using namespace std;
#include "retroConsoleGameEngine.h"
class retroCarRacing : public retroConsoleGameEngine
{
public:
retroCarRacing()
{
m_sAppName = L"Retro Formula 1";
}
private:
// Distance car has travelled around track
float fDistance = 0.0f;
// Current track curvature, lerped between track sections
float fCurvature = 0.0f;
// Accumulation of track curvature
float fTrackCurvature = 0.0f;
// Total distance of track
float fTrackDistance = 0.0f;
// Current car position
float fCarPos = 0.0f;
// Accumulation of player curvature
float fPlayerCurvature = 0.0f;
// Current player speed
float fSpeed = 0.0f;
// Track sections, sharpness of bend, length of section
vector<pair<float, float>> vecTrack;
// List of previous lap times
list<float> listLapTimes;
// Current lap time
float fCurrentLapTime;
protected:
// Called by retroConsoleGameEngine
virtual bool OnUserCreate()
{
// Define track
// Short section for start/finish line
vecTrack.push_back(make_pair(0.0f, 10.0f));
vecTrack.push_back(make_pair(0.0f, 200.0f));
vecTrack.push_back(make_pair(1.0f, 200.0f));
vecTrack.push_back(make_pair(0.0f, 400.0f));
vecTrack.push_back(make_pair(-1.0f, 100.0f));
vecTrack.push_back(make_pair(0.0f, 200.0f));
vecTrack.push_back(make_pair(-1.0f, 200.0f));
vecTrack.push_back(make_pair(1.0f, 200.0f));
vecTrack.push_back(make_pair(0.0f, 200.0f));
vecTrack.push_back(make_pair(0.2f, 500.0f));
vecTrack.push_back(make_pair(0.0f, 200.0f));
// Calculate total track distance, so we can set lap times
for (auto t : vecTrack)
fTrackDistance += t.second;
listLapTimes = { 0,0,0,0,0 };
fCurrentLapTime = 0.0f;
return true;
}
// Called by retroConsoleGameEngine
virtual bool OnUserUpdate(float fElapsedTime)
{
// Handle control input
int nCarDirection = 0;
if (m_keys[VK_UP].bHeld)
fSpeed += 2.0f * fElapsedTime;
else
fSpeed -= 1.0f * fElapsedTime;
// Car Curvature is accumulated left/right input, but inversely proportional to speed
// i.e. it is harder to turn at high speed
if (m_keys[VK_LEFT].bHeld)
{
fPlayerCurvature -= 0.7f * fElapsedTime * (1.0f - fSpeed / 2.0f);
nCarDirection = -1;
}
if (m_keys[VK_RIGHT].bHeld)
{
fPlayerCurvature += 0.7f * fElapsedTime * (1.0f - fSpeed / 2.0f);
nCarDirection = +1;
}
// If car curvature is too different to track curvature, slow down
// as car has gone off track
if (fabs(fPlayerCurvature - fTrackCurvature) >= 0.8f)
fSpeed -= 5.0f * fElapsedTime;
// Clamp Speed
if (fSpeed < 0.0f) fSpeed = 0.0f;
if (fSpeed > 1.0f) fSpeed = 1.0f;
// Move car along track according to car speed
fDistance += (70.0f * fSpeed) * fElapsedTime;
// Get Point on track
float fOffset = 0;
int nTrackSection = 0;
// Lap Timing and counting
fCurrentLapTime += fElapsedTime;
if (fDistance >= fTrackDistance)
{
fDistance -= fTrackDistance;
listLapTimes.push_front(fCurrentLapTime);
listLapTimes.pop_back();
fCurrentLapTime = 0.0f;
}
// Find position on track (could optimise)
while (nTrackSection < vecTrack.size() && fOffset <= fDistance)
{
fOffset += vecTrack[nTrackSection].second;
nTrackSection++;
}
// Interpolate towards target track curvature
float fTargetCurvature = vecTrack[nTrackSection - 1].first;
float fTrackCurveDiff = (fTargetCurvature - fCurvature) * fElapsedTime * fSpeed;
// Accumulate player curvature
fCurvature += fTrackCurveDiff;
// Accumulate track curvature
fTrackCurvature += (fCurvature)*fElapsedTime * fSpeed;
// Draw Sky - light blue and dark blue
for (int y = 0; y < ScreenHeight() / 2; y++)
for (int x = 0; x < ScreenWidth(); x++)
Draw(x, y, y < ScreenHeight() / 4 ? PIXEL_HALF : PIXEL_SOLID, FG_DARK_BLUE);
// Draw Scenery - our hills are a rectified sine wave, where the phase is adjusted by the
// accumulated track curvature
for (int x = 0; x < ScreenWidth(); x++)
{
int nHillHeight = (int)(fabs(sinf(x * 0.01f + fTrackCurvature) * 16.0f));
for (int y = (ScreenHeight() / 2) - nHillHeight; y < ScreenHeight() / 2; y++)
Draw(x, y, PIXEL_SOLID, FG_DARK_YELLOW);
}
// Draw Track - Each row is split into grass, clip-board and track
for (int y = 0; y < ScreenHeight() / 2; y++)
for (int x = 0; x < ScreenWidth(); x++)
{
// Perspective is used to modify the width of the track row segments
float fPerspective = (float)y / (ScreenHeight() / 2.0f);
// Min 10% Max 90%
float fRoadWidth = 0.1f + fPerspective * 0.8f;
float fClipWidth = fRoadWidth * 0.15f;
// Halve it as track is symmetrical around center of track.
fRoadWidth *= 0.5f;
// Depending on where the middle point is, which is defined by the current
// track curvature.
float fMiddlePoint = 0.5f + fCurvature * powf((1.0f - fPerspective), 3);
// Work out segment boundaries
int nLeftGrass = (fMiddlePoint - fRoadWidth - fClipWidth) * ScreenWidth();
int nLeftClip = (fMiddlePoint - fRoadWidth) * ScreenWidth();
int nRightClip = (fMiddlePoint + fRoadWidth) * ScreenWidth();
int nRightGrass = (fMiddlePoint + fRoadWidth + fClipWidth) * ScreenWidth();
int nRow = ScreenHeight() / 2 + y;
// Using periodic oscillatory functions to give lines, where the phase is controlled
// by the distance around the track. These take some fine tuning to give the right "feel"
int nGrassColour = sinf(20.0f * powf(1.0f - fPerspective, 3) + fDistance * 0.1f) > 0.0f ? FG_GREEN : FG_DARK_GREEN;
int nClipColour = sinf(80.0f * powf(1.0f - fPerspective, 2) + fDistance) > 0.0f ? FG_RED : FG_WHITE;
// Start finish straight changes the road colour to inform the player lap is reset
int nRoadColour = (nTrackSection - 1) == 0 ? FG_WHITE : FG_GREY;
// Draw the row segments
if (x >= 0 && x < nLeftGrass)
Draw(x, nRow, PIXEL_SOLID, nGrassColour);
if (x >= nLeftGrass && x < nLeftClip)
Draw(x, nRow, PIXEL_SOLID, nClipColour);
if (x >= nLeftClip && x < nRightClip)
Draw(x, nRow, PIXEL_SOLID, nRoadColour);
if (x >= nRightClip && x < nRightGrass)
Draw(x, nRow, PIXEL_SOLID, nClipColour);
if (x >= nRightGrass && x < ScreenWidth())
Draw(x, nRow, PIXEL_SOLID, nGrassColour);
}
// Draw Car - car position on road is proportional to difference between
// current accumulated track curvature, and current accumulated player curvature
fCarPos = fPlayerCurvature - fTrackCurvature;
// Offset for sprite
int nCarPos = ScreenWidth() / 2 + ((int)(ScreenWidth() * fCarPos) / 2.0) - 7;
// Draw a car that represents what the player is doing.
switch (nCarDirection)
{
case 0:
DrawStringAlpha(nCarPos, 80, L" ||####|| ");
DrawStringAlpha(nCarPos, 81, L" ## ");
DrawStringAlpha(nCarPos, 82, L" #### ");
DrawStringAlpha(nCarPos, 83, L" #### ");
DrawStringAlpha(nCarPos, 84, L"||| #### |||");
DrawStringAlpha(nCarPos, 85, L"|||########|||");
DrawStringAlpha(nCarPos, 86, L"||| #### |||");
break;
case +1:
DrawStringAlpha(nCarPos, 80, L" //####//");
DrawStringAlpha(nCarPos, 81, L" ## ");
DrawStringAlpha(nCarPos, 82, L" #### ");
DrawStringAlpha(nCarPos, 83, L" #### ");
DrawStringAlpha(nCarPos, 84, L"/// ####//// ");
DrawStringAlpha(nCarPos, 85, L"//#######///O ");
DrawStringAlpha(nCarPos, 86, L"/// #### //// ");
break;
case -1:
DrawStringAlpha(nCarPos, 80, L"\\\\####\\\\ ");
DrawStringAlpha(nCarPos, 81, L" ## ");
DrawStringAlpha(nCarPos, 82, L" #### ");
DrawStringAlpha(nCarPos, 83, L" #### ");
DrawStringAlpha(nCarPos, 84, L" \\\\\\\\#### \\\\\\");
DrawStringAlpha(nCarPos, 85, L" O\\\\\\#######\\\\");
DrawStringAlpha(nCarPos, 86, L" \\\\\\\\ #### \\\\\\");
break;
}
// Draw Stats
DrawString(0, 0, L"Distance: " + to_wstring(fDistance));
DrawString(0, 1, L"Target Curvature: " + to_wstring(fCurvature));
DrawString(0, 2, L"Player Curvature: " + to_wstring(fPlayerCurvature));
DrawString(0, 3, L"Player Speed : " + to_wstring(fSpeed));
DrawString(0, 4, L"Track Curvature : " + to_wstring(fTrackCurvature));
auto disp_time = [](float t) // Little lambda to turn floating point seconds into minutes:seconds:millis string
{
int nMinutes = t / 60.0f;
int nSeconds = t - (nMinutes * 60.0f);
int nMilliSeconds = (t - (float)nSeconds) * 1000.0f;
return to_wstring(nMinutes) + L"." + to_wstring(nSeconds) + L":" + to_wstring(nMilliSeconds);
};
// Display current laptime
DrawString(10, 8, disp_time(fCurrentLapTime));
// Display last 5 lap times
int j = 10;
for (auto l : listLapTimes)
{
DrawString(10, j, disp_time(l));
j++;
}
return true;
}
};
int main()
{
// Use retroConsoleGameEngine derived app
retroCarRacing game;
game.ConstructConsole(160, 100, 8, 8);
game.Start();
return 0;
}