your-own-drum/main.lua

151 lines
3.0 KiB
Lua

local love = love
local sounds = {
}
local state = {
isGameStarted = false,
beatScoreThreshold = 1.0,
currentBeat = 1,
startTime = 0.0,
particle = {
x = 0.0,
y = 0.0,
dx = 0.0,
dy = 0.0,
ddx = 0.0,
ddy = 0.0,
Update = function() end,
Draw = function() end
},
wave = {
x = { 1.0, 0.0, -0.5, 0.2, 0.4, 0.8, 0.3, 0.9, -0.4, 0.8, 0.5, 0.1, -0.9 },
dx = { 1.0, 0.0, -0.5, 0.2, 0.4, 0.8, 0.3, 0.9, -0.4, 0.8, 0.5, 0.1, -0.9 },
ddx = { 1.0, 0.0, -0.5, 0.2, 0.4, 0.8, 0.3, 0.9, -0.4, 0.8, 0.5, 0.1, -0.9 },
X = function(th) end,
DX = function(th) end,
DDX = function(th) end,
Draw = function() end,
AddImpulse = function( th, size ) end,
ImpactPoint = function( xi, yi, xf, yf )
local impact = { r = 0, th = 0, t = 0, x = 0, y = 0, dx = 0, dy = 0 }
return impact
end,
Update = function( dt ) end,
},
beat = {
t = nil,
mu = nil,
},
}
--Reset game state.
local function NewGame()
state.beat = {}
state.startTime = love.timer.getTime()
state.currentBeat = 1
end
local function BeatScore( t )
local beat = state.beat
--Base case 1: first tap.
if not beat.t then
beat.t = t
return 2.0
end
local dt = t - beat.t
beat.t = t
--Base case 2: second tap.
if not beat.mu then
beat.mu = dt
return 2.0
end
--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
--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()
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( 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