120 lines
3.6 KiB
Lua
120 lines
3.6 KiB
Lua
---@type engine.module
|
|
local module = {}
|
|
local abot, sound, rate, channels, bg, eyes, char
|
|
local spectrum = {}
|
|
local visualizers = {}
|
|
|
|
require("modules.luafft")
|
|
local logging = require("modules.logging")
|
|
|
|
local function divide(list, factor)
|
|
for i, v in ipairs(list) do
|
|
list[i] = list[i] / factor
|
|
end
|
|
end
|
|
|
|
---@param character engine.character
|
|
function module.onAdded(character)
|
|
char = character
|
|
abot = Atlas("sprites/characters/abot/abotSystem")
|
|
abot:PlayAnimation("")
|
|
abot.layer = character.sprite.layer - .1
|
|
abot.position = character.sprite.position:Add(Vector2(0, 0))
|
|
|
|
for i = 1, 7 do
|
|
local viz = Rect("sprites/characters/abot/aBotViz.png", "sprites/characters/abot/aBotViz.xml")
|
|
if i > 1 then
|
|
viz.position = abot.position:Add(Vector2(i * 80 + 55, 60))
|
|
else
|
|
-- don't question my code choices, i really tried.
|
|
-- The bugs didn't even make sense atp so yeah.
|
|
-- - entar
|
|
viz.position = abot.position:Add(Vector2(100000000, 10000000))
|
|
end
|
|
viz.layer = abot.layer - 0.1
|
|
viz:Frame(string.format("viz%s", i), 5)
|
|
visualizers[i] = viz
|
|
end
|
|
|
|
bg = Image("sprites/characters/abot/stereoBG.png")
|
|
bg.layer = abot.layer - 0.2
|
|
bg.position = abot.position:Add(Vector2(153, 43))
|
|
|
|
eyes = Atlas("sprites/characters/abot/systemEyes")
|
|
eyes.layer = abot.layer - 0.1
|
|
eyes.position = abot.position:Add(Vector2(45, 250))
|
|
eyes:PlayAnimation("a bot eyes lookin")
|
|
end
|
|
|
|
function module.onCreate(song)
|
|
local audiopath = string.format("songs/%s/Inst.ogg", song)
|
|
print(audiopath)
|
|
sound = love.sound.newSoundData(audiopath)
|
|
rate = sound:getSampleRate()
|
|
channels = sound:getChannels()
|
|
end
|
|
|
|
function module.onBeat(beat)
|
|
-- if beat % 2 == 0 then
|
|
abot:PlayAnimation()
|
|
-- end
|
|
end
|
|
|
|
function module.onUpdate(dt, el)
|
|
abot.layer = char.sprite.layer - .1
|
|
bg.layer = abot.layer - 0.2
|
|
eyes.layer = bg.layer
|
|
|
|
abot.position = char.sprite.position:Add(Vector2(-80, 320)):Add(char.stagePosition)
|
|
|
|
bg.position = abot.position:Add(Vector2(153, 43))
|
|
eyes.position = abot.position:Add(Vector2(45, 250))
|
|
|
|
pcall(function ()
|
|
local curSample = inst:tell('samples')
|
|
|
|
local wave = {}
|
|
local size = next_possible_size(2048)
|
|
if channels == 2 then
|
|
for i = curSample, (curSample + (size - 1) / 2) do
|
|
local sample = (sound:getSample(i * 2) + sound:getSample(i * 2 + 1)) * 0.5
|
|
table.insert(wave, complex.new(sample, 0))
|
|
end
|
|
else
|
|
for i = curSample, curSample + (size - 1) do
|
|
local sample = (sound:getSample(i * 2) + sound:getSample(i * 2 + 1)) * 0.5
|
|
table.insert(wave, complex.new(sample, 0))
|
|
table.insert(wave, complex.new(sound:getSample(i), 0))
|
|
end
|
|
end
|
|
|
|
local spec = fft(wave, false)
|
|
|
|
divide(spec, size / 2)
|
|
|
|
spectrum = spec
|
|
local division = 142.857142857
|
|
|
|
for i=2, (#spectrum/division) do
|
|
local n = spectrum[i]:abs()
|
|
|
|
visualizers[i]:Frame(string.format("viz%s", i), math.floor(n / 0.06) > 5 and 0 or 5 - math.floor(n / 0.06))
|
|
visualizers[i].layer = abot.layer - 0.1
|
|
visualizers[i].position = abot.position:Add(Vector2(i * 80 + 55, 60))
|
|
end
|
|
end)
|
|
end
|
|
|
|
function module.onClose()
|
|
|
|
end
|
|
|
|
function module.onEvent(event)
|
|
if event.name == "FocusCamera" then
|
|
local str = event.vars.char == 1 and "a bot eyes lookin" or "a bot eyes lookin"
|
|
eyes:PlayAnimation(str)
|
|
end
|
|
end
|
|
|
|
return module
|