Skip to content

Commit

Permalink
Rewrite beatHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
GuglioIsStupid committed May 21, 2024
1 parent cbe1519 commit 2df5f49
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 50 deletions.
78 changes: 28 additions & 50 deletions src/love/modules/beatHandler.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
--[[----------------------------------------------------------------------------
This file is apart of Rit; a free and open sourced rhythm game made with LÖVE.
Copyright (C) 2023 GuglioIsStupid
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
------------------------------------------------------------------------------]]
-- blah blah i made this code i do what i want with it
-- its simple and easy to use
local beatHandler = {}

beatHandler = {}
beatHandler.beat = 0
beatHandler.beatTime = 0

beatHandler.bpm = 100
beatHandler.curStep = 0 -- 1/4 of a beat
beatHandler.stepTime = 0

beatHandler.crochet = (60/beatHandler.bpm) * 1000
beatHandler.stepCrochet = beatHandler.crochet / 4

beatHandler.lastBeat = 0
beatHandler.curBeat = 0
beatHandler.isBeatHit = false
beatHandler.isStepHit = false

function beatHandler.setBPM(bpm)
bpm = bpm or 100
Expand All @@ -58,30 +35,36 @@ function beatHandler.getStepCrochet()
end

function beatHandler.update(dt)
beatHandler.isBeatHit = false
beatHandler.curBeat = math.abs(math.floor((musicTime / 1000) * (beatHandler.bpm / 60)))
-- check for step
beatHandler.stepTime = beatHandler.stepTime + dt * 1000
if beatHandler.stepTime >= beatHandler.stepCrochet then
beatHandler.stepTime = 0
beatHandler.curStep = beatHandler.curStep + 1
beatHandler.isStepHit = true
else
beatHandler.isStepHit = false
end

if math.floor((musicTime / 1000) * (beatHandler.bpm / 60)) > 0 then
if beatHandler.curBeat > beatHandler.lastBeat then
beatHandler.isBeatHit = true
beatHandler.beat = beatHandler.beat + 1
beatHandler.lastBeat = beatHandler.curBeat
end
-- check for beat
beatHandler.beatTime = beatHandler.beatTime + dt * 1000
if beatHandler.beatTime >= beatHandler.crochet then
beatHandler.beatTime = 0
beatHandler.beat = beatHandler.beat + 1
beatHandler.isBeatHit = true
else
if beatHandler.curBeat < beatHandler.lastBeat then
beatHandler.isBeatHit = true
beatHandler.beat = beatHandler.beat + 1
beatHandler.lastBeat = beatHandler.curBeat
end
beatHandler.isBeatHit = false
end
end

function beatHandler.reset()
beatHandler.beat = 0
beatHandler.beatTime = 0
beatHandler.lastBeat = 0
beatHandler.curBeat = 0
beatHandler.isBeatHit = false
beatHandler.curStep = 0
beatHandler.stepTime = 0
beatHandler.curStep = 0
beatHandler.isStepHit = false
end

function beatHandler.onBeat()
Expand All @@ -90,18 +73,13 @@ end

function beatHandler.setBeat(beat)
beatHandler.beat = beat
beatHandler.beatTime = 0
beatHandler.curStep = beat * 4
beatHandler.stepTime = 0
end

function beatHandler.onStep()
return beatHandler.beatTime % beatHandler.stepCrochet == 0
end

function beatHandler.onBeatNumber(beatNumber)
return beatHandler.beat % beatNumber == 0 and beatHandler.onBeat()
end

function beatHandler.onStepNumber(stepNumber)
return beatHandler.beatTime % (beatHandler.stepCrochet * stepNumber) == 0 and beatHandler.onStep()
return beatHandler.isStepHit
end

return beatHandler
1 change: 1 addition & 0 deletions src/love/states/weeks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ return {
if inst then inst:play() end
if voicesBF then voicesBF:play() end
if voicesEnemy then voicesEnemy:play() end
beatHandler.setBeat(0)
end
end
)
Expand Down

0 comments on commit 2df5f49

Please sign in to comment.