-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparticles.lua
74 lines (61 loc) · 1.75 KB
/
particles.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
if particles then return end -- avoid loading twice the same module
particles = {} -- create a table to represent the module
import "CoreLibs/graphics"
import "CoreLibs/sprites"
local gfx <const> = playdate.graphics
local SCREEN_WIDTH <const> = playdate.display.getWidth()
local SCREEN_HEIGHT <const> = playdate.display.getHeight()
local entities = {}
local physics = {}
function particles.add(imgPath, x, y, velx, vely, velrot)
local p = {
img = gfx.sprite.new(gfx.image.new(imgPath)),
vx = velx,
vy = vely,
rot = velrot
}
p.hw, p.hh = p.img:getSize()
p.hw, p.hh = p.hw / 2, p.hh / 2
p.img:add()
p.img:moveTo(x, y)
table.insert(entities, p)
end
function particles.update()
for k, p in pairs(entities) do
p.img:moveBy(p.vx, p.vy)
if p.img.x < -p.hw
or p.img.x > SCREEN_WIDTH + p.hw
or p.img.y < -p.hh
or p.img.y > SCREEN_HEIGHT + p.hh
then
p.img:remove()
entities[k] = nil
end
p.vx = p.vx + physics.gravityX
p.vy = p.vy + physics.gravityY
p.vx = p.vx * physics.frictionX
p.vy = p.vy * physics.frictionY
if p.rot ~= 0 then
p.img:setRotation(p.img:getRotation() + p.rot)
end
end
end
function particles.clear()
for k, p in pairs(entities) do
p.img:remove()
entities[k] = nil
end
end
function particles.setDefaultPhysics()
physics.gravityX = 0
physics.gravityY = 1
physics.frictionX = 0.92
physics.frictionY = 1
end
function particles.setPhysics(addX, addY, multX, multY)
physics.gravityX = addX
physics.gravityY = addY
physics.frictionX = multX
physics.frictionY = multY
end
particles.setDefaultPhysics()