your-own-drum/main.lua

179 lines
3.7 KiB
Lua

local love = love
local sitelenpona
local text
local sounds = {
}
local state
state = {
Reset = function()
state.beat = {}
state.startTime = love.timer.getTime()
state.currentBeat = 1
end,
isGameStarted = false,
beatScoreThreshold = 1.0,
currentBeat = 1,
startTime = 0.0,
stickX = 0.0,
stickY = 0.0,
stickT = 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,
},
}
function love.load()
sounds.goodPing = love.audio.newSource("soundTest.ogg", "static")
sounds.badPing = love.audio.newSource("chime8.ogg", "static")
sitelenpona = assert( require "sitelenpona" )
text = assert( require "text" )
return state.Reset()
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.00001
if dt < mu then
return TOLERANCE * score
else
return TOLERANCE / 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 - 0.05
love.audio.play(sounds.badPing)
end
end
function love.draw()
love.graphics.setColor(1.0, 1.0, 1.0)
love.graphics.print( state.beat.mu or 0, 0)
love.graphics.print( state.beat.t or 0, 0, 10)
love.graphics.print( state.beatScoreThreshold, 0, 20)
love.graphics.setColor(0, 0.4, 0.4)
sitelenpona.Draw( text.tok[state.currentBeat] )
love.graphics.setColor(0.5, 0.2, 0.8, 0.5)
love.graphics.circle("fill", 0, 0, 10)
end
function love.update( dt )
end
local function UpdateStick()
state.stickT = love.timer.getTime()
local x = (love.keyboard.isDown( "w" ) and 1.0 or 0.0) - (love.keyboard.isDown( "s" ) and 1.0 or 0.0)
local y = (love.keyboard.isDown( "d" ) and 1.0 or 0.0) - (love.keyboard.isDown( "a" ) and 1.0 or 0.0)
local n = math.sqrt( x * x + y * y )
if n < 0.001 then
state.stickX = x
state.stickY = y
else
state.stickX = x / n
state.stickY = y / n
end
end
function love.keypressed( key, code, isRepeat )
if key == "escape" then return love.event.quit() end
if key == "enter" then return OnImpact{ t = love.timer.getTime() } end
return UpdateStick()
end
function love.keyreleased( key, code )
return UpdateStick()
end