-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.lua
364 lines (329 loc) · 8.56 KB
/
player.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
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
local player = {}
local width = 25
local height = 50
local killY = -3000
local deadTimerMax = 1.5
function player.new(world, input, draw, camera, map)
local body = love.physics.newBody(world, 0, 10, "dynamic")
local shape = love.physics.newRectangleShape(width, height)
local fixture = love.physics.newFixture(body, shape, 1)
body:setFixedRotation(true)
-- print(body:getMass())
body:setMass(2.77)
local bgm = love.audio.newSource("bgm.wav", "stream")
bgm:setLooping(true)
local frictionSound = love.audio.newSource("friction.wav", "static")
frictionSound:setLooping(true)
local pl =
setmetatable(
{
world = world,
input = input,
draw = draw,
camera = camera,
map = map,
body = body,
shape = shape,
fixture = fixture,
jumpMax = 0,
dashMax = 0,
jumpNum = 0,
dashNum = 0,
jumpRepair = 0,
dashRepair = 0,
dashTime = 0,
gameTime = nil,
goalTime = nil,
dead = false,
deadTimer = deadTimerMax,
shadows = {},
respawnPoint = nil,
groundConsequent = 0,
state = "ground",
dashSound = love.audio.newSource("dash.wav", "static"),
jumpSound = love.audio.newSource("jump.wav", "static"),
groundSound = love.audio.newSource("ground.wav", "static"),
deathSound = love.audio.newSource("death.wav", "static"),
itemSound = love.audio.newSource("item.wav", "static"),
checkpointSound = love.audio.newSource("checkpoint.wav", "static"),
goalSound = love.audio.newSource("goal.wav", "static"),
walkSound = love.audio.newSource("walk.wav", "static"),
frictionSound = frictionSound,
bgm = bgm
},
{__index = player}
)
fixture:setUserData(pl)
return pl
end
function player:getType()
return "P"
end
function player:setRespawnPoint(x, y)
self.respawnPoint = {x = x, y = y}
end
function player:kill()
self.dead = true
love.audio.play(self.deathSound)
end
function player:respawn()
local point = self.respawnPoint
self:warpTo(point.x, point.y)
self.camera:set(point.x, point.y)
self.dead = false
self.deadTimer = deadTimerMax
self.body:setActive(true)
self.body:setLinearVelocity(0, 0)
end
function player:warpTo(x, y)
self.body:setPosition(x, y)
end
function player:getPosition()
return self.body:getPosition()
end
function player:dashing()
return self.dashTime > 0
end
function player:getVelocity()
return self.body:getLinearVelocity()
end
function player:jumpable()
return self.jumpNum > 0 and not self.dead
end
function player:dashable()
return self.dashNum > 0 and not (self.dashTime > 0) and not self.dead
end
function player:addShadow()
x, y = self:getPosition()
table.insert(self.shadows, {x = x, y = y})
end
function player:consumeShadow()
table.remove(self.shadows, 1)
end
function player:update(dt)
if self.gameTime ~= nil then
self.gameTime = self.gameTime + dt
end
local ix, iy = self.input.getAxis()
local contacts = self.body:getContacts()
local touchNum = 0
for i, contact in ipairs(contacts) do
if contact:isTouching() then
-- wall's getUserData is nil
local a, b = contact:getFixtures()
if a:getUserData() == self then
if b:getUserData() == nil then
touchNum = touchNum + 1
end
elseif b:getUserData() == self then
if a:getUserData() == nil then
touchNum = touchNum + 1
end
end
end
end
if touchNum > 0 then
self.state = "ground"
local vx, vy = self:getVelocity()
if (vx * vx + vy * vy) > 15000 then
if math.abs(vy) < 10.0 then
self.walkSound:play()
elseif vy < -100 then
self.frictionSound:play()
end
else
self.walkSound:stop()
self.frictionSound:stop()
end
else
self.state = "air"
self.frictionSound:stop()
self.walkSound:stop()
end
if self.state == "ground" then
if self.dashNum < self.dashMax then
self.dashRepair = self.dashRepair - 1
if self.dashRepair <= 0 then
self.dashRepair = 2
self.dashNum = self.dashMax
end
end
if self.jumpNum < self.jumpMax then
self.jumpRepair = self.jumpRepair - 1
if self.jumpRepair <= 0 then
self.jumpRepair = 2
self.jumpNum = self.jumpMax
end
end
end
self:addShadow()
if self:dashing() then
self.dashTime = self.dashTime - dt
else
if #self.shadows > 1 then
self:consumeShadow()
self:consumeShadow()
end
end
-- move x
local force = 10
local velocity = 250
if self:dashing() then
velocity = 700
end
local vx, vy = self:getVelocity()
self.body:applyForce(force * (ix * velocity - vx), 0)
-- dash
if self.input:getDash() and self:dashable() then
local force = 10
local velocity = 300
local vx, vy = self.body:getLinearVelocity()
local mass = self.body:getMass()
local fx = ix * force * math.max(velocity - math.abs(vx), 0)
local fy = math.max(-vy * mass, 0)
self.body:applyLinearImpulse(fx, fy)
self.dashTime = 0.5
self.dashNum = self.dashNum - 1
love.audio.play(self.dashSound)
end
-- jump
if self.input:getJump() and self:jumpable() then
local xx, yy = 0, 0
for i, contact in ipairs(contacts) do
local a, b = contact:getFixtures()
-- only accumulate wall collision
if contact:isTouching() and (a:getUserData() == nil or b:getUserData() == nil) then
local x, y = contact:getNormal()
xx, yy = xx + x, yy + y
end
end
local a = -math.atan2(xx, yy) + math.pi * 0.5 -- angle of normal
local vx, vy = self:getVelocity()
local jumpUpVelo = 500
local jumpNormalVelo = 200
local nvx, nvy = 0, jumpUpVelo
nvx = nvx + math.cos(a) * jumpNormalVelo
nvy = nvy + math.sin(a) * jumpNormalVelo
local dvx, dvy = nvx - vx, nvy - vy
local mass = self.body:getMass()
local fx, fy = dvx * mass, dvy * mass
self.body:applyLinearImpulse(fx, fy)
self.jumpNum = self.jumpNum - 1
love.audio.play(self.jumpSound)
end
local x, y = self:getPosition()
if y < killY then
self:kill()
end
if self.dead then
self.body:setActive(false)
self.deadTimer = self.deadTimer - dt
if self.deadTimer < 0 then
self:respawn()
end
end
-- camera
local vl = math.sqrt(vx * vx, vy * vy)
local ts = 0.5 - 0.25 * math.min(vl * 0.002, 1)
local x, y = self.body:getPosition()
self.camera:target(x, y, ts)
end
function timeString(sec)
return string.format("%4d:%02d.%02d", math.floor(sec / 60), math.floor(sec % 60), math.floor(sec * 100) % 100)
end
function player:renderui()
if self.gameTime then
love.graphics.print("time:" .. timeString(self.gameTime), 0, 0)
end
if self.goalTime then
love.graphics.print("goal:" .. timeString(self.goalTime), 0, 20)
end
if self.jumpMax > 0 then
love.graphics.print(string.format("jump: %d", self.jumpNum), 0, 60)
end
if self.dashMax > 0 then
love.graphics.print(string.format("dash: %d", self.dashNum), 0, 80)
end
--[[
love.graphics.print(string.format("state: %8s %8s", self.state, self.dashTime > 0 and "dash" or "nodash"), 0, 100)
local x, y = self.body:getPosition()
love.graphics.print(string.format("pos: %6.1f %6.1f", x, y), 0, 120)
local vx, vy = self.body:getLinearVelocity()
love.graphics.print(string.format("velo: %6.1f %6.1f", vx, vy), 0, 140)
]]
end
function pack(...)
return {n = select("#", ...), ...}
end
function player:renderShadow()
local points = pack(self.shape:getPoints())
for i, shadow in ipairs(self.shadows) do
local newPoints = {}
for pi = 1, #points, 2 do
local px, py = points[pi] + shadow.x, points[pi + 1] + shadow.y
table.insert(newPoints, px)
table.insert(newPoints, py)
end
self.draw:polygon("line", newPoints)
end
end
function player:render()
self.draw:polygon("line", self.body:getWorldPoints(self.shape:getPoints()))
self:renderShadow()
if self:jumpable() and (self.jumpNum < self.jumpMax) then
local x, y = self.body:getPosition()
love.graphics.ellipse("line", x, y - height / 2, 30, 10)
end
end
function player:onContact(o)
local t = o and o:getType()
if not t then
love.audio.play(self.groundSound)
return
elseif t == "K" then
self:kill()
return
end
if t == "C" then
local x, y = o:getPosition()
self:setRespawnPoint(x, y)
love.audio.play(self.checkpointSound)
return
end
if t == "G" then
self.goalTime = self.gameTime
self.gameTime = nil
self.jumpNum = 0
self.jumpMax = 0
self.dashNum = 0
self.dashMax = 0
self.map:resetItems()
love.audio.stop(self.bgm)
love.audio.play(self.goalSound)
self:setRespawnPoint(self.map:getStartPoint())
return
end
if t == "S" then
return
end
if o:consume() then
if t == "J" then
self.jumpMax = self.jumpMax + 1
elseif t == "D" then
self.dashMax = self.dashMax + 1
end
love.audio.play(self.itemSound)
end
end
function player:onEndContact(o)
local t = o:getType()
if t == "S" then
if self.gameTime == nil and not self.dead then
self.gameTime = 0
self.bgm:seek(0)
self.bgm:play()
end
return
end
end
return player