Rocket-Bytes/Source/Saws.lua

118 lines
3.5 KiB
Lua
Raw Normal View History

2022-04-24 18:15:38 +00:00
import "CoreLibs/animator"
2022-05-14 16:34:53 +00:00
import "CoreLibs/animation"
2022-04-24 05:03:32 +00:00
local gfx <const> = playdate.graphics
2022-05-14 16:34:53 +00:00
local imgSaw = gfx.imagetable.new("gfx/sawblades")
2022-04-24 05:03:32 +00:00
local imgTarget = gfx.image.new("gfx/target")
assert(imgSaw)
assert(imgTarget)
2022-05-14 16:34:53 +00:00
local loopSaws = gfx.animation.loop.new(200, imgSaw)
2022-04-24 05:03:32 +00:00
local blades = {}
local spinblades = {}
2022-04-24 05:03:32 +00:00
function loadBlades(_blades)
for i = 1, #_blades,1 do
local b = _blades[i]
b.start = playdate.geometry.point.new(b.start.x,b.start.y)
b.ends = playdate.geometry.point.new(b.ends.x,b.ends.y)
2022-05-14 16:34:53 +00:00
b.saw = gfx.sprite.new(loopSaws:image())
2022-04-24 05:03:32 +00:00
b.saw:moveTo(b.start)
b.saw:setCollideRect(0,0,16,16)
b.saw:add()
2022-04-24 18:15:38 +00:00
local a = gfx.animator.new(b.speed*1000, b.start, b.ends)
b.saw:setAnimator(a)
2022-04-24 05:03:32 +00:00
b.t1 = gfx.sprite.new(imgTarget)
b.t1:moveTo(b.start)
b.t2 = gfx.sprite.new(imgTarget)
b.t2:moveTo(b.ends)
b.t1:add()
b.t2:add()
blades[i] = b
end
end
2022-04-27 02:40:39 +00:00
function loadSpins(_spins)
for i = 1, #_spins, 1 do
local s = _spins[i]
if s.middle then
2022-05-14 16:34:53 +00:00
s.middle = gfx.sprite.new(loopSaws:image())
2022-04-27 02:40:39 +00:00
s.middle:setCollideRect(0,0,16,16)
else
s.middle = gfx.sprite.new(imgTarget)
end
s.middle:moveTo(s.x,s.y)
s.middle:add()
2022-05-01 18:04:14 +00:00
2022-05-08 17:52:50 +00:00
local sb = {speed=s.speed,arms={},mid=s.middle,time=0}
2022-05-01 18:04:14 +00:00
for i = 1, s.arms, 1 do
2022-05-08 17:52:50 +00:00
sb.arms[i] = {}
2022-05-01 18:04:14 +00:00
for p = 1, s.armlen, 1 do
2022-05-01 18:15:05 +00:00
local degrees = (360 / s.arms) * i
local position = {x=math.sin(math.rad(degrees)) * 20 * p,y=math.cos(math.rad(degrees)) * 20 * p}
2022-05-01 18:04:14 +00:00
2022-05-14 16:34:53 +00:00
sb.arms[i][p] = gfx.sprite.new(loopSaws:image())
2022-05-08 17:52:50 +00:00
sb.arms[i][p]:moveTo(s.x + position.x, s.y + position.y)
sb.arms[i][p]:setCollideRect(0,0,16,16)
sb.arms[i][p]:add()
2022-05-01 18:04:14 +00:00
end
end
spinblades[#spinblades+1] = sb
2022-04-27 02:40:39 +00:00
end
end
2022-04-24 18:15:38 +00:00
function updateSaws()
for b=1, #blades, 1 do
b = blades[b]
2022-05-14 16:34:53 +00:00
b.saw:setImage(loopSaws:image())
2022-04-24 18:15:38 +00:00
local pos = playdate.geometry.point.new(b.saw:getPosition())
if pos == b.start then
local a = gfx.animator.new(b.speed*1000, b.start, b.ends)
b.saw:setAnimator(a)
elseif pos == b.ends then
local a = gfx.animator.new(b.speed*1000, b.ends, b.start)
b.saw:setAnimator(a)
end
end
2022-05-08 17:31:53 +00:00
2022-05-08 20:44:59 +00:00
for spinner = 1, #spinblades, 1 do
for arm = 1, #spinblades[spinner].arms do
for blade = 1, #spinblades[spinner].arms[arm] do
2022-05-09 04:31:57 +00:00
2022-05-08 20:44:59 +00:00
spinblades[spinner].time += spinblades[spinner].speed
2022-05-09 04:31:57 +00:00
local degrees = (360 / #spinblades[spinner].arms * arm + spinblades[spinner].time)
2022-05-08 20:44:59 +00:00
local position = {x=math.sin(math.rad(degrees)) * 20 * blade,y=math.cos(math.rad(degrees)) * 20 * blade}
2022-05-14 16:34:53 +00:00
spinblades[spinner].arms[arm][blade]:setImage(loopSaws:image())
2022-05-08 20:44:59 +00:00
spinblades[spinner].arms[arm][blade]:moveTo(spinblades[spinner].mid.x + position.x, spinblades[spinner].mid.y + position.y)
end
end
end
2022-04-24 18:15:38 +00:00
end
2022-04-24 05:03:32 +00:00
function killBlades()
for i = 1, #blades, 1 do
blades[i].t1:remove()
blades[i].t2:remove()
blades[i].saw:remove()
end
blades = {}
2022-05-08 05:40:21 +00:00
for i = 1, #spinblades, 1 do
spinblades[i].mid:remove()
2022-05-08 17:52:50 +00:00
for arm = 1, #spinblades[i].arms do
for blade = 1, #spinblades[i].arms[arm] do
spinblades[i].arms[arm][blade]:remove()
end
2022-05-08 05:40:21 +00:00
end
end
spinblades = {}
2022-04-24 05:03:32 +00:00
end