your-own-drum/marble.lua

113 lines
2.6 KiB
Lua
Raw Normal View History

2023-01-13 20:05:19 +00:00
--Character controller. Renders point particle.
local love = love
--local wave = assert( require "wave" )
2023-01-13 21:34:01 +00:00
local oldBuffer = love.graphics.newCanvas()
local newBuffer = love.graphics.newCanvas()
2023-01-13 20:05:19 +00:00
local state
local FRICTION = 0.01
local MAXSPEED = 5.0
2023-01-13 20:05:19 +00:00
local t, x, y, dx, dy, ddx, ddy
local function OnImpact( impact )
end
2023-01-13 20:05:19 +00:00
local function Update()
end
local function Integrate( step )
2023-01-13 20:05:19 +00:00
t = love.timer.getTime()
dx = (1.0 - FRICTION) * dx + FRICTION * ddx
dy = (1.0 - FRICTION) * dy + FRICTION * ddy
2023-01-13 21:34:01 +00:00
local xf = x + dx * step * MAXSPEED
local yf = y + dy * step * MAXSPEED
x = xf
y = yf
2023-01-13 20:05:19 +00:00
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)
2023-01-13 20:05:19 +00:00
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
2023-01-13 21:34:01 +00:00
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 )
2023-01-13 21:34:01 +00:00
2023-01-13 21:34:01 +00:00
love.graphics.setCanvas( newBuffer )
love.graphics.setColor( 1.0, 1.0, 1.0 , 0.99 ) -- White.
2023-01-13 21:34:01 +00:00
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.
2023-01-13 21:34:01 +00:00
love.graphics.circle( "fill", xf, yf, 4.0 ) --TODO: replace with segments.
--love.graphics.line( xi, yi, xf, yf )
2023-01-13 21:34:01 +00:00
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 )
2023-01-13 21:34:01 +00:00
oldBuffer, newBuffer = newBuffer, oldBuffer
2023-01-13 20:05:19 +00:00
end
local function Impact( impact )
end
2023-01-13 21:34:01 +00:00
--Window resize.
local function Resize()
newBuffer = love.graphics.newCanvas()
--TODO: render oldBuffer to new newBuffer, but scaled down.
oldBuffer = love.graphics.newCanvas()
2023-01-13 21:34:01 +00:00
2023-01-13 21:34:01 +00:00
end
2023-01-13 20:05:19 +00:00
local function Reset()
t = love.timer.getTime()
x = 0
y = 0
dx = 0
dy = 0
ddx = 0
ddy = 0
end
2023-01-13 21:34:01 +00:00
2023-01-13 20:05:19 +00:00
Reset()
return {
Integrate = Integrate,
OnImpact = OnImpact,
2023-01-13 20:05:19 +00:00
Update = Update,
OnKey = OnKey,
Draw = Draw,
Impact = Impact,
Reset = Reset,
Resize = Resize,
2023-01-13 20:05:19 +00:00
}