-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.lua
246 lines (228 loc) · 5.21 KB
/
main.lua
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
local VW, VW2, VH, VH2
local carZ, carX, speed
local curves, hills, curvesLength, hillsLength, levelLength
local dx
local roadSize = 500
local roadSegments = 40
local laneMarkerSize = 25
local font
local t0, t1
local debug = false
function love.load(p)
VW = love.graphics.getWidth()
VH = love.graphics.getHeight()
VW2 = 0.5 * VW
VH2 = 0.5 * VH
carX = 0
carZ = 1
speed = 0
dx = 0
loadLevel('level1')
font = love.graphics.newFont(48)
love.graphics.setFont(font)
t0 = love.timer.getTime()
end
function love.keypressed(key, scancode, isRepeat)
if debug and isRepeat == false then
if key == 'kp0' then
carZ = 1
speed = 0
elseif key == 'kp+' then
carZ = carZ + 1
elseif key == 'kp-' then
carZ = carZ - 1
end
end
end
function love.update(dt)
if carZ >= levelLength then
return
end
local acc = -0.666
local steer = 0
local speedMax = 16
local steerMax = 1200
if love.keyboard.isDown('up') or love.keyboard.isDown('space') then
acc = accFunc(speed)
elseif love.keyboard.isDown('down') then
acc = -2
end
if love.keyboard.isDown('left') then
steer = -steerMax
end
if love.keyboard.isDown('right') then
steer = steerMax
end
if math.abs(carX) >= roadSize then
local f = math.max(0, speed / speedMax + 0.1)
acc = acc - pow3out(f) * 2
end
if math.abs(carX) > roadSize * 1.5 then
carX = roadSize * 1.5 * sign(carX)
end
if math.abs(steer) > 1 then
acc = acc * 0.97
end
local c = 0.86
local x = ((c - 1) * clamp(dt, 0, 1) + 1)
speed = speed * x
speed = clamp(speed + acc * dt, 0, speedMax)
local speedPercent = speed / speedMax
local offset = dt * speed * dx * math.abs(dx)
carX = carX + steer * speedPercent * dt - offset
carZ = carZ + speed * dt
t1 = love.timer.getTime()
end
function love.draw()
love.graphics.scale(1, -1)
love.graphics.translate(VW2, -VH)
love.graphics.setColor(102, 153, 255)
love.graphics.rectangle('fill', -VW2, 0, VW, VH)
local di = math.floor(carZ)
local ibegin = di
local iend = roadSegments + di
local sx0 = getSegment(di)
local sx1 = getSegment(di + 1)
local _, f = math.modf(carZ)
dx = mix(sx0, sx1, smooth(f))
for n = iend, ibegin, -1 do
drawSegment(n)
end
if carZ >= levelLength then
love.graphics.setColor(255, 255, 255)
local s = string.format('CONGRATULATIONS!\nTime: %.1f s', t1 - t0)
love.graphics.printf(s, -VW2, VH * 0.9, VW, 'center', 0, 1, -1)
end
end
function sign(x)
if x < 0 then
return -1
elseif x > 0 then
return 1
else
return 0
end
end
function accFunc(speed)
return -1.352002 + (2.838528 + 1.352002) / (1 + math.pow(speed / 11.35162, 1.495932))
end
function smooth(a)
return a * a * (3 - 2 * a)
end
function pow3out(a)
return math.pow(a - 1, 3) + 1
end
function mix(a, b, f)
return a * (1 - f) + b * f
end
function clamp(x, a, b)
return math.min(math.max(x, a), b)
end
function loadLevel(name)
curves, hills = require(name)()
curvesLength = table.getn(curves)
hillsLength = table.getn(hills)
levelLength = math.max(curvesLength, hillsLength)
end
function mapCoord(x, y, z)
return x / (z + 1), (VH2 + y) * z / (z + 1)
end
function roadColor(n)
if n % 2 == 0 then
return 96, 96, 96
else
return 105, 105, 105
end
end
function grassColor(n)
if n % 2 == 0 then
return 0, 102, 34
else
return 0, 153, 51
end
end
function laneMarkerColor(n)
return 210, 210, 210
end
function fogColor(n)
local r, g, b = 38, 77, 0
local a = (n - carZ) / roadSegments * 255
return r, g, b, a
end
function getSegment(n)
local x, y, z
if n < curvesLength then
x = curves[n]
else
x = curves[curvesLength]
end
if n < hillsLength then
y = hills[n]
else
y = hills[hillsLength]
end
z = n - carZ
return x, y, z
end
function drawSegment(n)
local sx, sy, sz = getSegment(n)
local zx, zy, zz = getSegment(n + 1)
local dsx = (n - carZ) * dx
local dzx = (n + 1 - carZ) * dx
local x1, y1 = mapCoord(sx - roadSize - carX, sy, sz)
local x2, y2 = mapCoord(zx - roadSize - carX, zy, zz)
local x3, y3 = mapCoord(zx + roadSize - carX, zy, zz)
local x4, y4 = mapCoord(sx + roadSize - carX, sy, sz)
local lx1, ly1 = mapCoord(sx - roadSize - carX + laneMarkerSize, sy, sz)
local lx2, ly2 = mapCoord(zx - roadSize - carX + laneMarkerSize, zy, zz)
local lx3, ly3 = mapCoord(zx + roadSize - carX - laneMarkerSize, zy, zz)
local lx4, ly4 = mapCoord(sx + roadSize - carX - laneMarkerSize, sy, sz)
love.graphics.setColor(roadColor(n))
love.graphics.polygon('fill',
x1 + dsx, y1,
x2 + dzx, y2,
x3 + dzx, y3,
x4 + dsx, y4
)
if n % 2 == 1 then
love.graphics.setColor(laneMarkerColor(n))
love.graphics.polygon('fill',
x1 + dsx, y1,
x2 + dzx, y2,
lx2 + dzx, ly2,
lx1 + dsx, ly1
)
love.graphics.polygon('fill',
x4 + dsx, y4,
x3 + dzx, y3,
lx3 + dzx, ly3,
lx4 + dsx, ly4
)
love.graphics.polygon('fill',
(x1 + lx4) / 2.0 + dsx, (y1 + ly4) / 2.0,
(x2 + lx3) / 2.0 + dzx, (y2 + ly3) / 2.0,
(x3 + lx2) / 2.0 + dzx, (y3 + ly2) / 2.0,
(x4 + lx1) / 2.0 + dsx, (y4 + ly1) / 2.0
)
end
love.graphics.setColor(grassColor(n))
love.graphics.polygon('fill',
x1 + dsx, y1,
x2 + dzx, y2,
-VW2, y2,
-VW2, y1
)
love.graphics.polygon('fill',
x4 + dsx, y4,
x3 + dzx, y3,
VW2, y3,
VW2, y4
)
love.graphics.setColor(fogColor(n))
love.graphics.polygon('fill',
-VW2, y1,
-VW2, y2,
VW2, y3,
VW2, y4
)
end