Marble controller.

This commit is contained in:
yaw-man 2023-01-13 16:05:19 -04:00
parent 11865da481
commit 31f7c5c0c7
2 changed files with 78 additions and 34 deletions

View File

@ -1,6 +1,9 @@
local love = love local love = love
step = 1.0 / 120.0
local step = step
local sitelenpona local sitelenpona
local text local text
local marble
local sounds = { local sounds = {
@ -15,28 +18,16 @@ state = {
state.beat = {} state.beat = {}
state.startTime = love.timer.getTime() state.startTime = love.timer.getTime()
state.currentBeat = 1 state.currentBeat = 1
state.timeToSimulate = 0.0
end, end,
timeToSimulate = 0.0,
isGameStarted = false, isGameStarted = false,
beatScoreThreshold = 1.0, beatScoreThreshold = 1.0,
lastBeatScore = 0.0,
currentBeat = 1, currentBeat = 1,
startTime = 0.0, 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 = { 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 }, 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 }, 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 },
@ -62,10 +53,13 @@ state = {
} }
function love.load() function love.load()
love.graphics.setBackgroundColor( 245 / 255, 169 / 255, 184 / 255 ) --Trans pink.
--love.graphics.setBackgroundColor( 91 / 255, 206 / 255, 250 / 255 ) --Trans blue.
sounds.goodPing = love.audio.newSource("soundTest.ogg", "static") sounds.goodPing = love.audio.newSource("soundTest.ogg", "static")
sounds.badPing = love.audio.newSource("chime8.ogg", "static") sounds.badPing = love.audio.newSource("chime8.ogg", "static")
sitelenpona = assert( require "sitelenpona" ) sitelenpona = assert( require "sitelenpona" )
text = assert( require "text" ) text = assert( require "text" )
marble = assert( require "marble" )
return state.Reset() return state.Reset()
end end
@ -101,7 +95,7 @@ local function BeatScore( t )
--Calculate beat score. --Calculate beat score.
local score = dt * dt / ( mu * mu ) local score = dt * dt / ( mu * mu )
local TOLERANCE = 1.00001 local TOLERANCE = 1.04
if dt < mu then if dt < mu then
return TOLERANCE * score return TOLERANCE * score
else else
@ -117,6 +111,8 @@ end
local function OnImpact( impact ) local function OnImpact( impact )
local score = BeatScore( impact.t ) local score = BeatScore( impact.t )
--DEBUG
state.lastBeatScore = score
if score > state.beatScoreThreshold then if score > state.beatScoreThreshold then
state.beatScoreThreshold = 1.0 state.beatScoreThreshold = 1.0
@ -142,8 +138,9 @@ function love.draw()
love.graphics.print( state.beat.mu or 0, 0) love.graphics.print( state.beat.mu or 0, 0)
love.graphics.print( state.beat.t or 0, 0, 10) love.graphics.print( state.beat.t or 0, 0, 10)
love.graphics.print( state.beatScoreThreshold, 0, 20) love.graphics.print( state.beatScoreThreshold, 0, 20)
love.graphics.setColor(0, 0.4, 0.4) love.graphics.print( state.lastBeatScore, 0, 30 )
sitelenpona.Draw( text.tok[state.currentBeat] ) sitelenpona.Draw( text.tok[state.currentBeat] )
marble.Draw()
love.graphics.setColor(0.5, 0.2, 0.8, 0.5) love.graphics.setColor(0.5, 0.2, 0.8, 0.5)
love.graphics.circle("fill", 0, 0, 10) love.graphics.circle("fill", 0, 0, 10)
end end
@ -151,29 +148,19 @@ end
function love.update( dt ) function love.update( dt )
dt = dt + state.timeToSimulate
end while dt > step do
marble.Update()
local function UpdateStick() dt = dt - step
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
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 == "enter" then return OnImpact{ t = love.timer.getTime() } end if key == "return" then return OnImpact{ t = love.timer.getTime() } end
return UpdateStick() return marble.OnKey()
end end
function love.keyreleased( key, code ) function love.keyreleased( key, code )
return UpdateStick() return marble.OnKey()
end end

57
marble.lua Normal file
View File

@ -0,0 +1,57 @@
--Character controller. Renders point particle.
local love = love
local step = assert( step )
local state
local FRICTION = 0.005
local MAXSPEED = 1.0
local t, x, y, dx, dy, ddx, ddy
local function Update()
t = love.timer.getTime()
dx = (1.0 - FRICTION) * dx + FRICTION * ddx
dy = (1.0 - FRICTION) * dy + FRICTION * ddy
x = x + dx * step * MAXSPEED
y = y + dy * step * MAXSPEED
end
local function OnKey()
ddx = (love.keyboard.isScancodeDown( "d" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "a" ) and 1.0 or 0.0)
ddy = (love.keyboard.isScancodeDown( "s" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "w" ) and 1.0 or 0.0)
local n = math.sqrt( ddx * ddx + ddy * ddy )
if n < 0.001 then return end
ddx, ddy = ddx / n, ddy / n
end
local function Draw()
local dt = love.timer.getTime() - t
local rx, ry = x + dt * dx, y + dt * dy
love.graphics.setColor( 0.0, 0.0, 0.0, 0.8 )
love.graphics.circle( "fill", rx, ry, 10 )
end
local function Impact( impact )
end
local function Reset()
t = love.timer.getTime()
x = 0
y = 0
dx = 0
dy = 0
ddx = 0
ddy = 0
end
Reset()
return {
Update = Update,
OnKey = OnKey,
Draw = Draw,
Impact = Impact,
Reset = Reset,
}