Add autorhythm

This commit is contained in:
yaw-man 2023-01-13 12:43:12 -04:00
parent d9a0f5e23b
commit fd520944e4
3 changed files with 88 additions and 54 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
build/

BIN
chime8.ogg Normal file

Binary file not shown.

View File

@ -1,8 +1,15 @@
local love = love local love = love
local sound local sounds = {
}
local state = { local state = {
isGameStarted = false, isGameStarted = false,
beatScoreThreshold = 1.0,
currentBeat = 1,
startTime = 0.0,
particle = { particle = {
x = 0.0, x = 0.0,
@ -35,9 +42,7 @@ local state = {
beat = { beat = {
t = nil, t = nil,
dt = nil,
mu = nil, mu = nil,
score = 0,
}, },
} }
@ -45,62 +50,90 @@ local state = {
--Reset game state. --Reset game state.
local function NewGame() local function NewGame()
state.beat = {} state.beat = {}
state.wave.Reset() state.startTime = love.timer.getTime()
state.currentBeat = 1
end end
local beat = { } local function BeatScore( t )
local function IsOnBeat( t ) local beat = state.beat
--Base case 1: first tap.
if not beat.t then if not beat.t then
beat.t = t beat.t = t
return true return 2.0
end end
if not beat.dt then local dt = t - beat.t
beat.t = t
--Base case 2: second tap.
local WEIGHT = 0.25 if not beat.mu then
beat.mu = ( 1.0 - WEIGHT ) * beat.mu + WEIGHT * dt beat.mu = dt
return 2.0
if beat.score >= 1.0 then
beat.score = 0
return true
end end
return false --General case: update average beat length.
local WEIGHT = 0.25 --High number makes the last beat more significant.
local mu = ( 1.0 - WEIGHT ) * beat.mu + WEIGHT * dt
beat.mu = mu
--Safety: avoid a dbz.
if mu < 0.001 or dt < 0.001 then
error( "DBZ! Beat length too small." )
end end
local function OnImpact( impact ) --Calculate beat score.
--Update beat timer. local score = dt * dt / ( mu * mu )
if not beat then local TOLERANCE = 1.15
beat = { mu = if dt < mu then
return 1.15 * score
else
return 1.15 / score
end end
local dt = impact.t - beat.t
beat.t = impact.t
--Handle sound.
love.audio.play(sound)
end end
local function OnVictory() local function OnVictory()
end end
local function OnImpact( impact )
local score = BeatScore( impact.t )
if score > state.beatScoreThreshold then
state.beatScoreThreshold = 1.0
state.currentBeat = state.currentBeat + 1
if state.currentBeat >= 120 then
return OnVictory()
end
love.audio.play(sounds.goodPing)
else
state.beatScoreThreshold = state.beatScoreThreshold - score
love.audio.play(sounds.badPing)
end
end
function love.draw() function love.draw()
love.graphics.setColor(1.0, 1.0, 1.0) love.graphics.setColor(1.0, 1.0, 1.0)
love.graphics.print("Hello World!", 400, 300) love.graphics.print("Hello World!", 400, 300)
love.graphics.print( beat.t, 0, 0 ) love.graphics.print( state.beat.mu or 0, 0, 0, 0, 10, 10 )
love.graphics.print( beat.dt, 0, 100 ) love.graphics.print( state.beat.t or 0, 0, 100, 0, 10, 10 )
love.graphics.print( state.beatScoreThreshold, 0, 200, 0, 10, 10 )
love.graphics.setColor(0, 0.4, 0.4) love.graphics.setColor(0, 0.4, 0.4)
--love.graphics.circle("fill", 0, 0, 10) --love.graphics.circle("fill", 0, 0, 10)
end end
function love.load() function love.load()
sound = love.audio.newSource("soundTest.ogg", "static") sounds.goodPing = love.audio.newSource("soundTest.ogg", "static")
sounds.badPing = love.audio.newSource("chime8.ogg", "static")
return NewGame() return NewGame()
@ -113,6 +146,6 @@ local function IsOnBeat( t )
function love.keypressed( key, code, isRepeat ) function love.keypressed( key, code, isRepeat )
if key == "escape" then return love.event.quit() end if key == "escape" then return love.event.quit() end
if key == "w" then return OnImpact( ) end if key == "w" then return OnImpact{ t = love.timer.getTime() } end
if key == "enter" then return NewGame() end if key == "enter" then return NewGame() end
end end