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.

141
main.lua
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,74 +50,102 @@ 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
return false
end end
local function OnImpact( impact ) --General case: update average beat length.
--Update beat timer. local WEIGHT = 0.25 --High number makes the last beat more significant.
if not beat then local mu = ( 1.0 - WEIGHT ) * beat.mu + WEIGHT * dt
beat = { mu = beat.mu = mu
end
local dt = impact.t - beat.t
beat.t = impact.t
--Safety: avoid a dbz.
if mu < 0.001 or dt < 0.001 then
error( "DBZ! Beat length too small." )
end
--Calculate beat score.
local score = dt * dt / ( mu * mu )
local TOLERANCE = 1.15
if dt < mu then
return 1.15 * score
else
return 1.15 / score
end
end
local function OnVictory()
--Handle sound. end
love.audio.play(sound)
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 end
local function OnVictory() love.audio.play(sounds.goodPing)
end else
function love.draw() state.beatScoreThreshold = state.beatScoreThreshold - score
love.graphics.setColor(1.0, 1.0, 1.0) love.audio.play(sounds.badPing)
love.graphics.print("Hello World!", 400, 300) end
love.graphics.print( beat.t, 0, 0 )
love.graphics.print( beat.dt, 0, 100 )
love.graphics.setColor(0, 0.4, 0.4)
--love.graphics.circle("fill", 0, 0, 10)
end
function love.load()
sound = love.audio.newSource("soundTest.ogg", "static")
return NewGame()
end
function love.update( dt )
end
function love.keypressed( key, code, isRepeat ) end
if key == "escape" then return love.event.quit() end
if key == "w" then return OnImpact( ) end
if key == "enter" then return NewGame() end
end function love.draw()
love.graphics.setColor(1.0, 1.0, 1.0)
love.graphics.print("Hello World!", 400, 300)
love.graphics.print( state.beat.mu or 0, 0, 0, 0, 10, 10 )
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.circle("fill", 0, 0, 10)
end
function love.load()
sounds.goodPing = love.audio.newSource("soundTest.ogg", "static")
sounds.badPing = love.audio.newSource("chime8.ogg", "static")
return NewGame()
end
function love.update( dt )
end
function love.keypressed( key, code, isRepeat )
if key == "escape" then return love.event.quit() end
if key == "w" then return OnImpact{ t = love.timer.getTime() } end
if key == "enter" then return NewGame() end
end