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