your-own-drum/marble.lua

104 lines
2.5 KiB
Lua

--Character controller. Renders point particle.
local love = love
--local wave = assert( require "wave" )
local oldBuffer = love.graphics.newCanvas()
local newBuffer = love.graphics.newCanvas()
local step = assert( step )
local state
local FRICTION = 0.01
local MAXSPEED = 5.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
local xf = x + dx * step * MAXSPEED
local yf = y + dy * step * MAXSPEED
x = xf
y = yf
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( "w" ) and 1.0 or 0.0) - (love.keyboard.isScancodeDown( "s" ) 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 xf, yf = x + dt * dx, y + dt * dy
local w, h = love.graphics.getDimensions()
local xi, yi = transform:transformPoint( x, y )
xf, yf = transform:transformPoint( xf, yf )
love.graphics.setColor( 0, 0, 0, 1 )
love.graphics.print( x, 0, 60 )
love.graphics.print( y, 0, 80 )
love.graphics.setCanvas( newBuffer )
love.graphics.setColor( 1.0, 1.0, 1.0 , 0.99 ) -- White.
love.graphics.draw( oldBuffer ) --Time-dependent fade: overlay canvas over itself.
--Render latest segment in trail.
love.graphics.setColor( 245 / 255, 169 / 255, 184 / 255, 1.0 ) --Trans pink.
love.graphics.circle( "fill", xf, yf, 4.0 ) --TODO: replace with segments.
--love.graphics.line( xi, yi, xf, yf )
love.graphics.setCanvas( oldBuffer )
love.graphics.clear( 1.0, 1.0, 1.0, 0.0 )
--Render circle directly to screen.
love.graphics.setCanvas()
love.graphics.draw( newBuffer )
love.graphics.circle( "fill", xf, yf, 5.0 )
love.graphics.setColor( 1, 1, 1, 1.0 ) --White.
love.graphics.circle( "line", xf, yf, 5.0 )
oldBuffer, newBuffer = newBuffer, oldBuffer
end
local function Impact( impact )
end
--Window resize.
local function Resize()
newBuffer = love.graphics.newCanvas()
--TODO: render oldBuffer to new newBuffer, but scaled down.
oldBuffer = love.graphics.newCanvas()
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,
Resize = Resize,
}