Rework fallSystem for modifiable gravity.
Add receivedInputThisFrame bool to InputState
This commit is contained in:
parent
110d02fe2c
commit
e90e06d15c
|
@ -9,7 +9,7 @@
|
|||
---@alias Collision { collisionBetween: Entity[] }
|
||||
---@alias Entity table
|
||||
---@alias InRelations Entity[]
|
||||
---@alias InputState { aJustPressed: boolean, bJustPressed: boolean, upJustPressed: boolean, downJustPressed: boolean, leftJustPressed: boolean, rightJustPressed: boolean }
|
||||
---@alias InputState { receivedInputThisFrame: boolean, aJustPressed: boolean, bJustPressed: boolean, upJustPressed: boolean, downJustPressed: boolean, leftJustPressed: boolean, rightJustPressed: boolean }
|
||||
---@alias RoundStateAction "end" | "start"
|
||||
---@alias XyPair { x: number, y: number }
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ end
|
|||
CanSpawn = "{ entity: Entity }",
|
||||
InRelations = "Entity[]",
|
||||
CanBeBounced = "{ flat: XyPair, mult = XyPair }",
|
||||
InputState = "{ aJustPressed: boolean, bJustPressed: boolean, upJustPressed: boolean, downJustPressed: boolean, leftJustPressed: boolean, rightJustPressed: boolean }",
|
||||
InputState = "{ receivedInputThisFrame: boolean, aJustPressed: boolean, bJustPressed: boolean, upJustPressed: boolean, downJustPressed: boolean, leftJustPressed: boolean, rightJustPressed: boolean }",
|
||||
}))
|
||||
|
||||
T = {
|
||||
|
|
|
@ -1,4 +1,15 @@
|
|||
local G = -300
|
||||
fallSystem = filteredSystem("fall", { velocity = T.XyPair, mass = T.number }, function(e, dt)
|
||||
e.velocity.y = e.velocity.y - (G * dt * e.mass) - (0.5 * dt * dt)
|
||||
world:addEntity({ gravity = -300 })
|
||||
|
||||
gravities = filteredSystem("gravities", { gravity = T.number })
|
||||
|
||||
filteredSystem("changeGravity", { changeGravityTo = T.number }, function(e, _, _)
|
||||
for _, ge in pairs(gravities.entities) do
|
||||
ge.gravity = e.changeGravityTo
|
||||
end
|
||||
end)
|
||||
|
||||
fallSystem = filteredSystem("fall", { velocity = T.XyPair, mass = T.number }, function(e, dt)
|
||||
for _, ge in pairs(gravities.entities) do
|
||||
e.velocity.y = e.velocity.y - (ge.gravity * dt * e.mass) - (0.5 * dt * dt)
|
||||
end
|
||||
end)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
---@alias InputState { upJustPressed: boolean, downJustPressed: boolean, rightJustPressed: boolean, leftJustPressed: boolean, aJustPressed: boolean, bJustPressed: boolean }
|
||||
|
||||
local buttonJustPressed = playdate.buttonJustPressed
|
||||
|
||||
---@type InputState
|
||||
local inputState = {}
|
||||
|
||||
inputSystem = filteredSystem("input", { canReceiveInput = T.marker }, function(e, _, system)
|
||||
|
@ -15,4 +15,12 @@ function inputSystem:preProcess()
|
|||
inputState.leftJustPressed = buttonJustPressed(playdate.kButtonLeft)
|
||||
inputState.aJustPressed = buttonJustPressed(playdate.kButtonA)
|
||||
inputState.bJustPressed = buttonJustPressed(playdate.kButtonB)
|
||||
|
||||
inputState.receivedInputThisFrame =
|
||||
inputState.upJustPressed
|
||||
or inputState.downJustPressed
|
||||
or inputState.rightJustPressed
|
||||
or inputState.leftJustPressed
|
||||
or inputState.aJustPressed
|
||||
or inputState.bJustPressed
|
||||
end
|
||||
|
|
|
@ -4,20 +4,26 @@
|
|||
local MenuItems = {}
|
||||
|
||||
menuController = filteredSystem("menuController", { menuItems = MenuItems, inputState = T.InputState }, function(e, _, system)
|
||||
if not e.inputState.receivedInputThisFrame then
|
||||
return
|
||||
end
|
||||
|
||||
for _, menuItem in pairs(e.menuItems) do
|
||||
if menuItem.highlighted then
|
||||
if e.inputState.aJustPressed then
|
||||
menuItem.onSelect(system.world)
|
||||
menuItem.onSelect(system.world) -- TODO: That's not very ECS of you!
|
||||
for _, item in pairs(e.menuItems) do
|
||||
system.world:removeEntity(item)
|
||||
end
|
||||
system.world:removeEntity(e)
|
||||
end
|
||||
|
||||
if e.inputState.downJustPressed and menuItem.navigateDown then
|
||||
menuItem.highlighted = false
|
||||
menuItem.navigateDown.highlighted = true
|
||||
return
|
||||
end
|
||||
|
||||
if e.inputState.upJustPressed and menuItem.navigateUp then
|
||||
menuItem.highlighted = false
|
||||
menuItem.navigateUp.highlighted = true
|
||||
|
@ -25,6 +31,7 @@ menuController = filteredSystem("menuController", { menuItems = MenuItems, input
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, menuItem in pairs(e.menuItems) do
|
||||
if menuItem.highlighted then
|
||||
menuItem.drawAsText.style = TextStyle.Inverted
|
||||
|
|
Loading…
Reference in New Issue