Add autorhythm
This commit is contained in:
parent
d9a0f5e23b
commit
fd520944e4
|
@ -0,0 +1 @@
|
||||||
|
build/
|
Binary file not shown.
121
main.lua
121
main.lua
|
@ -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
|
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
|
||||||
end
|
return 1.15 * score
|
||||||
local dt = impact.t - beat.t
|
else
|
||||||
beat.t = impact.t
|
return 1.15 / score
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--Handle sound.
|
|
||||||
love.audio.play(sound)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function OnVictory()
|
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
|
end
|
||||||
|
|
||||||
function love.draw()
|
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.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()
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update( dt )
|
function love.update( dt )
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
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
|
Loading…
Reference in New Issue