Add autorhythm
This commit is contained in:
parent
d9a0f5e23b
commit
fd520944e4
|
@ -0,0 +1 @@
|
|||
build/
|
Binary file not shown.
99
main.lua
99
main.lua
|
@ -1,8 +1,15 @@
|
|||
local love = love
|
||||
local sound
|
||||
local sounds = {
|
||||
|
||||
|
||||
|
||||
}
|
||||
local state = {
|
||||
|
||||
isGameStarted = false,
|
||||
beatScoreThreshold = 1.0,
|
||||
currentBeat = 1,
|
||||
startTime = 0.0,
|
||||
|
||||
particle = {
|
||||
x = 0.0,
|
||||
|
@ -35,9 +42,7 @@ local state = {
|
|||
|
||||
beat = {
|
||||
t = nil,
|
||||
dt = nil,
|
||||
mu = nil,
|
||||
score = 0,
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -45,62 +50,90 @@ local state = {
|
|||
--Reset game state.
|
||||
local function NewGame()
|
||||
state.beat = {}
|
||||
state.wave.Reset()
|
||||
state.startTime = love.timer.getTime()
|
||||
state.currentBeat = 1
|
||||
end
|
||||
|
||||
local beat = { }
|
||||
local function IsOnBeat( t )
|
||||
local function BeatScore( t )
|
||||
local beat = state.beat
|
||||
|
||||
--Base case 1: first tap.
|
||||
if not beat.t then
|
||||
beat.t = t
|
||||
return true
|
||||
return 2.0
|
||||
end
|
||||
|
||||
if not beat.dt then
|
||||
local dt = t - beat.t
|
||||
beat.t = t
|
||||
|
||||
|
||||
local WEIGHT = 0.25
|
||||
beat.mu = ( 1.0 - WEIGHT ) * beat.mu + WEIGHT * dt
|
||||
|
||||
|
||||
if beat.score >= 1.0 then
|
||||
beat.score = 0
|
||||
return true
|
||||
--Base case 2: second tap.
|
||||
if not beat.mu then
|
||||
beat.mu = dt
|
||||
return 2.0
|
||||
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
|
||||
|
||||
local function OnImpact( impact )
|
||||
--Update beat timer.
|
||||
if not beat then
|
||||
beat = { mu =
|
||||
--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
|
||||
local dt = impact.t - beat.t
|
||||
beat.t = impact.t
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
--Handle sound.
|
||||
love.audio.play(sound)
|
||||
end
|
||||
|
||||
local function OnVictory()
|
||||
|
||||
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()
|
||||
love.graphics.setColor(1.0, 1.0, 1.0)
|
||||
love.graphics.print("Hello World!", 400, 300)
|
||||
love.graphics.print( beat.t, 0, 0 )
|
||||
love.graphics.print( beat.dt, 0, 100 )
|
||||
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()
|
||||
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()
|
||||
|
||||
|
@ -113,6 +146,6 @@ local function IsOnBeat( t )
|
|||
|
||||
function love.keypressed( key, code, isRepeat )
|
||||
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
|
||||
end
|
Loading…
Reference in New Issue