Rocket-Bytes/Source/Particles.lua

130 lines
3.1 KiB
Lua
Raw Normal View History

local gfx <const> = playdate.graphics
math.randomseed(playdate.getSecondsSinceEpoch())
local exps = {}
local stars = {}
2022-07-23 20:42:51 +00:00
function createStars(targetVelocityX,targetVelocityY, max)
max = max or 4
stars = {}
for _i = 1, 30, 1 do
stars[#stars+1] = {x=math.random(0,400),y=math.random(0,240),dx=math.random(-1.0,1.0)*0.5,dy=math.random(-1.0,1.0)*0.5,size=math.random(2,4)}
end
2022-07-18 23:12:56 +00:00
for star = 1, #stars do
star = stars[star]
2022-07-18 23:12:56 +00:00
2022-07-23 20:42:51 +00:00
if star.size > max then star.size = max end
2022-07-18 23:12:56 +00:00
if targetVelocityX then
star.dx += targetVelocityX
else
targetVelocityX = 0
end
if targetVelocityY then
star.dy += targetVelocityY
else
targetVelocityY = 0
end
if star.dx == 0 then star.dx = 0.2 + targetVelocityX end
if star.dy == 0 then star.dy = 0.2 + targetVelocityY end
end
end
createStars()
function explode(_x,_y)
for i = 1, 10, 1 do
local part = {
x = _x,
y = _y,
dir = math.random(0,359),
size = math.random(10,15),
speed = math.random(1,3)
}
exps[#exps+1] = part
end
end
2022-07-02 05:03:59 +00:00
function miniExplode(_x,_y)
for i = 1, 6, 1 do
local part = {
x = _x,
y = _y,
dir = math.random(0,359),
size = math.random(7,10),
speed = math.random(1,3)
}
exps[#exps+1] = part
end
end
function processStars(_x,_y)
gfx.setColor(gfx.kColorWhite)
for star = 1, #stars do
star = stars[star]
star.x += star.dx
star.y += star.dy
gfx.fillRect(star.x,star.y,star.size,star.size)
if star.x < _x then
star.x += 420
elseif star.x > _x + 420 then
star.x -= 420
end
if star.y < _y then
star.y += 260
elseif star.y > _y + 260 then
star.y -= 260
end
end
end
function processExplosions()
gfx.setColor(gfx.kColorWhite)
for part = 1, #exps do
local particle = exps[part]
particle.x += math.sin(particle.dir) * particle.speed
particle.y -= math.cos(particle.dir) * particle.speed
gfx.fillCircleAtPoint(particle.x,particle.y,particle.size)
exps[part].size -= .3
if exps[part].size < 0 then exps[part].size = 0 end
end
for part = 1, #exps do
if exps[part].size <= 0.1 then
table.remove(exps, part)
break
end
end
end
function applyStarSpeed(_x,_y)
for star = 1, #stars do
star = stars[star]
star.dx+=_x
star.dy+=_y
end
2022-07-30 02:51:08 +00:00
end
function testParts()
gfx.clear()
processStars(-10,-10)
processExplosions()
gfx.drawText("PARTICLES TEST", 200-(gfx.getTextSize("PARTICLES TEST")/2), 6)
gfx.drawText("BIG", 100-(gfx.getTextSize("BIG")/2), 200)
gfx.drawText("SMALL", 300-(gfx.getTextSize("SMALL")/2), 200)
if playdate.buttonJustPressed(playdate.kButtonA) then
explode(100,120)
miniExplode(300,120)
end
end