2023-01-13 20:05:19 +00:00
|
|
|
--Character controller. Renders point particle.
|
|
|
|
local love = love
|
2023-01-14 03:46:39 +00:00
|
|
|
--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 step = assert( step )
|
|
|
|
local state
|
2023-01-14 03:46:39 +00:00
|
|
|
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 Update()
|
|
|
|
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
|
2023-01-14 03:46:39 +00:00
|
|
|
|
|
|
|
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)
|
2023-01-14 03:46:39 +00:00
|
|
|
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()
|
2023-01-14 03:46:39 +00:00
|
|
|
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-14 03:46:39 +00:00
|
|
|
|
2023-01-13 21:34:01 +00:00
|
|
|
love.graphics.setCanvas( newBuffer )
|
2023-01-14 03:46:39 +00:00
|
|
|
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.
|
2023-01-14 03:46:39 +00:00
|
|
|
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.
|
2023-01-14 03:46:39 +00:00
|
|
|
--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 )
|
2023-01-14 03:46:39 +00:00
|
|
|
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()
|
2023-01-14 03:46:39 +00:00
|
|
|
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-14 03:46:39 +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 {
|
|
|
|
Update = Update,
|
|
|
|
OnKey = OnKey,
|
|
|
|
Draw = Draw,
|
|
|
|
Impact = Impact,
|
|
|
|
Reset = Reset,
|
2023-01-14 03:46:39 +00:00
|
|
|
Resize = Resize,
|
2023-01-13 20:05:19 +00:00
|
|
|
}
|