tiny-ecs-love-template/systems/input.lua

61 lines
1.3 KiB
Lua

local world = require("world")
---@type KeyState
local keyState = {}
local keyInputSystem = world:filteredSystem("keyInput", { canReceiveKeys = T.marker }, function(e, _, system)
e.keyState = keyState
system.world:addEntity(e)
end)
function keyInputSystem:preProcess()
if #self.entities == 0 then
return
end
end
function love.keypressed(key, _, _)
keyState[key] = true
end
function ClearKeyState()
for key in pairs(keyState) do
keyState[key] = nil
end
end
local mouse = world:filteredSystem("mouse", { mouseKeyPress = { position = T.XyPair, key = T.number } })
function MouseJustPressed(key, clear)
clear = clear ~= nil and clear or true
for _, event in pairs(mouse.entities) do
if event.mouseKeyPress and event.mouseKeyPress.key == key then
if clear then
event.mouseKeyPress = nil
world:removeEntity(event)
end
return true
end
end
return false
end
local mouseX, mouseY = -9999, -9999
function love.mousemoved(x, y)
mouseX, mouseY = x, y
end
function love.mousepressed(x, y, key)
world:addEntity({
mouseKeyPress = {
position = {
x = x,
y = y,
},
key = key,
},
liveForNFrames = 1,
})
end