-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.lua
More file actions
410 lines (353 loc) · 12 KB
/
Copy pathplayer.lua
File metadata and controls
410 lines (353 loc) · 12 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
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
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "CoreLibs/animation"
import "particle"
import "platform"
import "constants"
class("Player").extends(playdate.graphics.sprite)
local gfx = playdate.graphics
-- Animation sequences (frame indices for your sprite sheet)
-- ✅ module-level constant: no per-frame allocation
local ANIM_SEQS = {
idle = {2, 1}, -- blink → standing
left = {3, 1}, -- left → standing
right = {4, 1}, -- right → standing
up = {5, 1}, -- climb up → standing
down = {6, 1}, -- down / ground pound → standing
}
-- Constructor
function Player:init(x, y)
-- Load player sprite sheet (6 images)
self.imageTable = gfx.imagetable.new("images/player")
assert(self.imageTable, "Missing player image table!")
-- Animation state (ANIM_SEQS-driven)
self.animName = "idle"
self.animFrameIdx = 1
self.animTimerMs = 0
self.animSpeedMs = 400
-- Set initial image from the idle sequence
self:setImage(self.imageTable:getImage(ANIM_SEQS.idle[1]))
self:moveTo(x, y)
self:setCollideRect(0, 0, self:getSize())
self:setGroups({ kCollisionGroupPlayer })
self:setCollidesWithGroups({ kCollisionGroupEnemy, kCollisionGroupPlatform })
self.collisionType = kCollisionGroupPlayer
self.collisionResponse = gfx.sprite.kCollisionTypeOverlap
self:add()
self.invincible = false
-- Gravity physics
self.gravity = 0.3
self.bounceDamping = 0.6
self.minBounceVelocity = 0.4
-- Placement
self.vx = 0
self.vy = 0
-- Speed dynamics
self.speed = 2
self.jumpStrength = -6
self.shortHopStrength = -6
self.groundPoundSpeed = 5
-- Status
self.onGround = false
self.isOnVine = false
self.preventVineGrab = false
self.airborneFromJump = nil
self.bounceActive = true
self.pendingVineAttach = false
end
-- =========================
-- Animation helpers
-- =========================
function Player:setAnim(name, speedMs)
if name == self.animName then
if speedMs then self.animSpeedMs = speedMs end
return
end
self.animName = name
self.animFrameIdx = 1
self.animTimerMs = 0
if speedMs then self.animSpeedMs = speedMs end
local seq = ANIM_SEQS[self.animName] or ANIM_SEQS.idle
self:setImage(self.imageTable:getImage(seq[self.animFrameIdx]))
end
function Player:stepAnim(dtMs)
local seq = ANIM_SEQS[self.animName] or ANIM_SEQS.idle
if #seq <= 1 then return end
self.animTimerMs += dtMs
if self.animTimerMs < self.animSpeedMs then return end
self.animTimerMs = 0
self.animFrameIdx += 1
if self.animFrameIdx > #seq then
self.animFrameIdx = 1
end
self:setImage(self.imageTable:getImage(seq[self.animFrameIdx]))
end
function Player:updateAnimation()
local newAnim = "idle"
local speedMs = 400
-- Determine current animation state
if self.vy > 2 then
-- Ground pound or falling fast
newAnim, speedMs = "down", 100
elseif self.isOnVine and self.vy < 0 then
-- Climbing up vine
newAnim, speedMs = "up", 200
elseif self.isOnVine and self.vy > 0 then
-- Climbing down vine
newAnim, speedMs = "down", 200
elseif self.vx < 0 then
-- Moving left
newAnim, speedMs = "left", 100
elseif self.vx > 0 then
-- Moving right
newAnim, speedMs = "right", 100
end
self:setAnim(newAnim, speedMs)
self:stepAnim(1000 / 30) -- assuming 30fps
end
-- =========================
-- Update
-- =========================
function Player:update()
self.onGround = false
-- Gravity (only when not on vine)
if not self.onGround and not self.isOnVine then
self.vy = self.vy + self.gravity
end
-- Move vertically first (collision-resolve Y)
local tentativeY = self.y + self.vy
local _, actualY, collisions, length = self:moveWithCollisions(self.x, tentativeY)
local resolvedY = actualY
-- Collision checking
for i = 1, length do
local col = collisions[i]
local other = col.other
-- If we hit a Flying Enemy
if other:isa(FlyingEnemy) and not self.invincible then
if Sound and Sound.playFlyingEnemyHitSound then Sound.playFlyingEnemyHitSound() end
self:handleHit()
resolvedY = actualY
break
end
-- If we hit a Vine Enemy
if other:isa(VineEnemy) and not self.invincible then
if Sound and Sound.playVineEnemyHitSound then Sound.playVineEnemyHitSound() end
self:handleHit()
resolvedY = actualY
break
end
-- Are we on the platform?
if other.collisionType == kCollisionGroupPlatform and self.vy > 0 then
-- Trigger Game over if we die while falling
if GameState.pendingGameOver then
playdate.wait(500)
playdate.display.flush()
GameState.state = "gameover"
break
end
-- Bounce handling
if self.bounceActive and math.abs(self.vy) > self.minBounceVelocity then
self.vy = -self.vy * self.bounceDamping
self:spawnBounceParticles()
else
self.vy = 0
self.bounceActive = false
self.preventVineGrab = false
end
-- We are on the ground
self.onGround = true
resolvedY = other.y - other.height / 2 - self.height / 2
break
end
end
-- Handle input (sets vx, and may modify vy/isOnVine)
self:handleInput()
-- Move horizontally (clamp X) and apply resolved Y
local nextX = math.max(self.width / 2, math.min(400 - self.width / 2, self.x + self.vx))
self:moveTo(nextX, resolvedY)
-- If we're not on the ground, not on a vine, and airborneFromJump is nil,
-- we're likely bouncing/falling so ensure it's set
if not self.onGround and not self.isOnVine and self.airborneFromJump == nil then
self.airborneFromJump = false
end
-- Allow a short jump onto a vine (attach on descent)
if self.pendingVineAttach and self.vy > 0 then
for _, vine in ipairs(gfx.sprite.getAllSprites()) do
if vine:isa(Vine) and self:boundsOverlap(vine) then
self.isOnVine = true
self.vy = 0
self.onGround = false
self.bounceActive = false
self.pendingVineAttach = false
break
end
end
end
-- Check for collectables and vines
self:checkCollectables()
self:checkVine() -- finalizes isOnVine for this frame
-- Update animation based on current state
self:updateAnimation()
-- ✅ Shuffle noise: controlled + restart-safe
-- Use INPUT intent rather than vx/vy to avoid edge-case frames (vine attach/detach, etc.)
local movingNow = false
if self.isOnVine then
movingNow =
playdate.buttonIsPressed(playdate.kButtonUp) or
playdate.buttonIsPressed(playdate.kButtonDown)
elseif self.onGround then
movingNow =
playdate.buttonIsPressed(playdate.kButtonLeft) or
playdate.buttonIsPressed(playdate.kButtonRight)
else
movingNow = false
end
if Sound and Sound.setShuffleMoving then
Sound.setShuffleMoving(movingNow, 1.0)
end
-- Reset horizontal velocity each frame (re-applied in handleInput)
self.vx = 0
end
-- If we're hit, drop life, display particles, briefly invincible
function Player:handleHit()
GameState.lives = math.max(GameState.lives - 1, 0)
self:spawnHitParticles()
self.invincible = true
playdate.timer.performAfterDelay(1000, function()
self.invincible = false
end)
end
-- What happens with the controller
function Player:handleInput()
-- Move left or right
if playdate.buttonIsPressed(playdate.kButtonLeft) then
self.vx = -self.speed
elseif playdate.buttonIsPressed(playdate.kButtonRight) then
self.vx = self.speed
end
-- If we're on a vine
if self.isOnVine then
-- Allow up and down on the vine, otherwise stop
if playdate.buttonIsPressed(playdate.kButtonUp) then
self.vy = -self.speed
elseif playdate.buttonIsPressed(playdate.kButtonDown) then
self.vy = self.speed
else
self.vy = 0
end
-- If B, then jump off the vine via ground pound
if playdate.buttonJustPressed(playdate.kButtonB) then
self.isOnVine = false
self:performGroundPound()
end
end
-- If A, then allow jump
if playdate.buttonJustPressed(playdate.kButtonA) then
local overlappingVine = false
-- Check if we're jumping on a vine
for _, vine in ipairs(gfx.sprite.getAllSprites()) do
if vine:isa(Vine) and self:boundsOverlap(vine) then
overlappingVine = true
break
end
end
-- Short hop
if overlappingVine then
self.vy = self.shortHopStrength
self.isOnVine = false
self.onGround = false
self.bounceActive = false
self.pendingVineAttach = true
-- Big jump off vine
elseif self.isOnVine then
self.vy = self.jumpStrength
self.isOnVine = false
self.airborneFromJump = true
-- Regular jump from ground
elseif self.onGround then
self.vy = self.jumpStrength
self.isOnVine = false
self.airborneFromJump = true
end
end
-- If B, Ground Pound
if playdate.buttonJustPressed(playdate.kButtonB) and not self.onGround then
if not self.isOnVine then
self:performGroundPound()
end
end
end
-- Ground Pound
function Player:performGroundPound()
self.vy = self.groundPoundSpeed
self.bounceActive = true
self.isOnVine = false
self.airborneFromJump = false
self.preventVineGrab = true
end
-- Check if we're close to a vine
function Player:checkVine()
local foundVine = false
if self.pendingVineAttach then return end
if not self.preventVineGrab then
for _, vine in ipairs(gfx.sprite.getAllSprites()) do
if vine:isa(Vine) and self:boundsOverlap(vine) then
if not self.isOnVine then
self.isOnVine = true
self.vy = 0
self.onGround = false
self.bounceActive = false
end
foundVine = true
break
end
end
end
-- Reset if not on a vine
if self.isOnVine and not foundVine then
self.isOnVine = false
self.bounceActive = true
end
end
-- Check if we're touching a collectable
function Player:checkCollectables()
for _, sprite in ipairs(gfx.sprite.getAllSprites()) do
if sprite:isa(Collectable) and self:boundsOverlap(sprite) then
sprite:remove()
GameState.score = GameState.score + 100
if Sound and Sound.playDiamondPickupSound then Sound.playDiamondPickupSound() end
end
end
end
-- Helper function for overlapping checks
function Player:boundsOverlap(other)
local ax, ay, aw, ah = self:getBounds()
local bx, by, bw, bh = other:getBounds()
return ax < bx + bw and ax + aw > bx and ay < by + bh and ay + ah > by
end
-- When we detach from a vine, set variables
function Player:detachFromVine()
self.isOnVine = false
self.bounceActive = true
self.preventVineGrab = true
self.airborneFromJump = false
self.vy = self.gravity * 2
end
-- When we bounce, particle effects!
function Player:spawnBounceParticles()
local px = self.x
local py = self.y + self.height / 2
for i = 1, 10 do
Particle(px + math.random(-4, 4), py, 2)
end
end
-- When we get hit, particle effects!
function Player:spawnHitParticles()
local px = self.x
local py = self.y
for i = 1, 8 do
Particle(px + math.random(-4, 4), py + math.random(-4, 4), 2)
end
end