Compare commits

..

4 Commits

Author SHA1 Message Date
Sage Vaillancourt 23f5ba9f03 Get some cart upgrades in there.
Replace "key" => value with a simple object of component replacements.
2025-03-08 09:56:53 -05:00
Sage Vaillancourt d6c934a265 Slightly more ECS spawner upgrades?
Not positive this is in the spirit, but we're figuring it out, kay?
2025-03-07 17:10:04 -05:00
Sage Vaillancourt 60461aa14f Some renames to be a little less generic
I.e. `entity` is unclear, and hard to search for.
2025-03-07 13:20:38 -05:00
Sage Vaillancourt e90e06d15c Rework fallSystem for modifiable gravity.
Add receivedInputThisFrame bool to InputState
2025-03-06 23:50:14 -05:00
13 changed files with 260 additions and 132 deletions

View File

@ -1,3 +1,6 @@
if not playdate.isSimulator then
return
end
getCurrentTimeMilliseconds = playdate.getCurrentTimeMilliseconds
tinyTrackEntityAges = true
@ -10,4 +13,33 @@ if tinyTrackEntityAges then
end
tinyLogSystemUpdateTime = false
tinyLogSystemChanges = false
tinyLogSystemChanges = false
tinyWarnWhenNonDataOnEntities = false
if tinyWarnWhenNonDataOnEntities then
function checkForNonData(e, nested, tableCache)
nested = nested or false
tableCache = tableCache or {}
local valType = type(e)
if valType == "table" then
if tableCache[e] then
return
end
tableCache[e] = true
for k, v in pairs(e) do
local keyWarning = checkForNonData(k, true, tableCache)
if keyWarning then
return keyWarning
end
local valueWarning = checkForNonData(v, true, tableCache)
if valueWarning then
return valueWarning
end
end
elseif valType == "function" or valType == "thread" or valType == "userdata" then
return valType
end
end
end

View File

@ -489,6 +489,20 @@ if tinyTrackEntityAges then
function tiny.addEntity(world, entity)
local added = wrapped(world, entity)
added[ENTITY_INIT_MS] = getCurrentTimeMilliseconds()
return added
end
tiny_addEntity = tiny.addEntity
end
if tinyWarnWhenNonDataOnEntities then
local wrapped = tiny.addEntity
function tiny.addEntity(world, entity)
local added = wrapped(world, entity)
local nonDataType = checkForNonData(added)
if nonDataType then
print("Detected non-data type '" .. nonDataType .. "' on entity")
end
return added
end
tiny_addEntity = tiny.addEntity
end

View File

@ -1,42 +0,0 @@
function bouncer(v)
return function()
local h0 = 0.1 -- m/s
-- local v = 10 -- m/s, current velocity
local g = 10 -- m/s/s
local t = 0 -- starting time
local rho = 0.60 -- coefficient of restitution
local tau = 0.10 -- contact time for bounce
local hmax = h0 -- keep track of the maximum height
local h = h0
local hstop = 0.01 -- stop when bounce is less than 1 cm
local freefall = true -- state: freefall or in contact
local t_last = -math.sqrt(2 * h0 / g) -- time we would have launched to get to h0 at t=0
local vmax = math.sqrt(v * g)
while hmax > hstop do
local dt = coroutine.yield(h, not freefall)
if freefall then
local hnew = h + v * dt - 0.5 * g * dt * dt
if hnew < 0 then
-- Bounced!
t = t_last + 2 * math.sqrt(2 * hmax / g)
freefall = false
t_last = t + tau
h = 0
else
t = t + dt
v = v - g * dt
h = hnew
end
else
t = t + tau
vmax = vmax * rho
v = vmax
freefall = true
h = 0
end
hmax = 0.5 * vmax * vmax / g
end
return 0
end
end

View File

@ -12,8 +12,8 @@ function Cart.reset(o)
y = 50,
}
o.velocity = {
x = 300 + (100 * math.random()),
y = 175 * (math.random() - 1),
x = o.baseVelocity.x + (100 * math.random()),
y = o.baseVelocity.y * math.random(),
}
o.size = size
o.canBeBounced = {
@ -27,6 +27,7 @@ function Cart.reset(o)
---@param world World
function o:spawnEntitiesWhenStopped(world)
-- it'd be funny if the cart fully just exploded instead
self.velocity = { x = 300, y = 0 }
self.canCollideWith = 4
world:addEntity({
@ -48,5 +49,10 @@ function Cart.reset(o)
end
function Cart.new()
return setmetatable(Cart.reset({}), { __index = Cart })
return setmetatable(Cart.reset({
baseVelocity = {
x = 300,
y = -170,
}
}), { __index = Cart })
end

View File

@ -24,6 +24,7 @@ import("systems/rounds.lua")
import("systems/camera-pan.lua")
import("systems/collision-resolution.lua")
import("systems/collision-detection.lua")
import("systems/upgrade-utils.lua")
import("systems/draw.lua")
import("systems/input.lua")
@ -37,7 +38,6 @@ local gfx <const> = playdate.graphics
playdate.display.setRefreshRate(50)
gfx.setBackgroundColor(gfx.kColorWhite)
cart = Cart.new()
local floorSize = { x = 10000, y = 10 }
floor = {
position = { x = 0, y = 235 },
@ -67,7 +67,7 @@ function Score:draw()
end
world:addEntity(floor)
world:addEntity(cart)
world:addEntity(Cart.new())
addAllSpawners(world)
world:addEntity(Ingredients.getFirst())

View File

@ -3,14 +3,19 @@
-- This file is composed of, essentially, "base types"
local SOME_TABLE <const> = {}
---@alias AnyComponent any
---@alias BitMask number
---@alias CanBeBounced { flat: XyPair, mult = XyPair }
---@alias CanSpawn { entity: Entity }
---@alias CanSpawn { entityToSpawn: Entity }
---@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 ReplaceRelation { entityToModify: Entity, replacement: table }
---@alias RoundStateAction "end" | "start"
---@alias Selectable { additions: Entity[] | nil, replacements: ReplaceRelation[] | nil, highlighted: boolean, navigateDown: Selectable | nil, navigateUp: Selectable | nil }
---@alias XyPair { x: number, y: number }
@ -19,7 +24,7 @@ T = {
number = 0,
numberArray = { 1, 2, 3 },
str = "",
marker = {},
marker = SOME_TABLE,
---@type fun(self)
SelfFunction = function() end,
--- Actor
@ -29,28 +34,46 @@ T = {
mult = XyPair,
},
---@type pd_image
pd_image = {},
pd_image = SOME_TABLE,
---@type pd_font
pd_font = {},
pd_font = SOME_TABLE,
---@type AnyComponent
AnyComponent = SOME_TABLE,
---@type BitMask
BitMask = 0,
---@type CanBeBounced
CanBeBounced = {},
CanBeBounced = SOME_TABLE,
---@type CanSpawn
CanSpawn = {},
CanSpawn = SOME_TABLE,
---@type Collision
Collision = {},
Collision = SOME_TABLE,
---@type Entity
Entity = {},
Entity = SOME_TABLE,
---@type InRelations
InRelations = {},
InRelations = SOME_TABLE,
---@type InputState
InputState = {},
InputState = SOME_TABLE,
---@type ReplaceRelation
ReplaceRelation = SOME_TABLE,
---@type RoundStateAction
RoundStateAction = "start",
---@type Selectable
Selectable = SOME_TABLE,
---@type XyPair
XyPair = {},
XyPair = SOME_TABLE,
}
---@generic T
@ -60,6 +83,13 @@ function Maybe(t)
return { maybe = t }
end
---@generic T
---@param t T
---@return T[]
function Arr(t)
return { arrOf = t }
end
TextStyle = {
Inverted = "INVERTED",
Bordered = "BORDERED",

View File

@ -12,7 +12,7 @@ function t(name, type, value)
elseif type == "string" then
value = ""
else
value = "{}"
value = "SOME_TABLE"
end
end
types[#types + 1] = { name = name, type = type, value = value }
@ -42,7 +42,7 @@ end
function dumpTypeObjects()
local ret = ""
for _, v in ipairs(types) do
local line = "\n ---@type " .. v.name .. "\n " .. v.name .. " = " .. v.value .. ","
local line = "\n\n ---@type " .. v.name .. "\n " .. v.name .. " = " .. v.value .. ","
ret = ret .. line
end
return ret
@ -51,6 +51,8 @@ end
-- This file is composed of, essentially, "base types"
local SOME_TABLE <const> = {}
!!(tMany({
Entity = "table",
XyPair = "{ x: number, y: number }",
@ -58,10 +60,14 @@ end
BitMask = "number",
CanBeBounced = "{ flat: XyPair, mult: XyPair }",
RoundStateAction = { '"end" | "start"', '"start"' },
CanSpawn = "{ entity: Entity }",
AnyComponent = "any",
CanSpawn = "{ entityToSpawn: 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 }",
ReplaceRelation = "{ entityToModify: Entity, replacement: table }",
Selectable = "{ additions: Entity[] | nil, replacements: ReplaceRelation[] | nil, highlighted: boolean, navigateDown: Selectable | nil, navigateUp: Selectable | nil }",
}))
T = {
@ -69,7 +75,7 @@ T = {
number = 0,
numberArray = { 1, 2, 3 },
str = "",
marker = {},
marker = SOME_TABLE,
---@type fun(self)
SelfFunction = function() end,
--- Actor
@ -79,9 +85,9 @@ T = {
mult = XyPair,
},
---@type pd_image
pd_image = {},
pd_image = SOME_TABLE,
---@type pd_font
pd_font = {},
pd_font = SOME_TABLE,
!!(dumpTypeObjects())
}
@ -92,6 +98,13 @@ function Maybe(t)
return { maybe = t }
end
---@generic T
---@param t T
---@return T[]
function Arr(t)
return { arrOf = t }
end
TextStyle = {
Inverted = "INVERTED",
Bordered = "BORDERED",

View File

@ -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)

View File

@ -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

View File

@ -1,23 +1,43 @@
---@alias MenuItem { onSelect: fun(), highlighted: boolean, navigateDown: MenuItem | nil, navigateUp: MenuItem | nil }
---@param relation ReplaceRelation
---@param world World
local function applyReplacementRelation(relation, world)
for k, v in pairs(relation.replacement) do
relation.entityToModify[k] = v
end
world:addEntity(relation.entityToModify)
end
---@type MenuItem[]
local MenuItems = {}
menuController = filteredSystem("menuController", { menuItems = MenuItems, inputState = T.InputState }, function(e, _, system)
menuController = filteredSystem("menuController", { menuItems = Arr(T.Selectable), inputState = T.InputState }, function(e, _, system)
for _, menuItem in pairs(e.menuItems) do
if menuItem.highlighted then
if e.inputState.aJustPressed then
menuItem.onSelect(system.world)
-- Prepare to remove the menu and all menu items
system.world:removeEntity(e)
for _, item in pairs(e.menuItems) do
system.world:removeEntity(item)
end
system.world:removeEntity(e)
-- TODO: Larger menu that stays open for more purchases before the next round
world:addEntity({ roundAction = "start" })
if menuItem.replacements then
for _, v in ipairs(menuItem.replacements) do
applyReplacementRelation(v, system.world)
end
end
if menuItem.additions then
for _, v in ipairs(menuItem.additions) do
system.world:addEntity(v)
end
end
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 +45,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

View File

@ -32,12 +32,12 @@ end)
removeAtRoundStart = filteredSystem("removeAtRoundStart", { removeAtRoundStart = T.bool })
filteredSystem("afterDelayAdd", { afterDelayAdd = { entity = T.Entity, delay = T.number } }, function(e, dt, system)
filteredSystem("afterDelayAdd", { afterDelayAdd = { entityToAdd = T.Entity, delay = T.number } }, function(e, dt, system)
e.afterDelayAdd.delay = e.afterDelayAdd.delay - dt
if e.afterDelayAdd.delay > 0 then
return
end
system.world:addEntity(e.afterDelayAdd.entity)
system.world:addEntity(e.afterDelayAdd.entityToAdd)
system.world:removeEntity(e)
end)
@ -102,7 +102,8 @@ roundSystem = filteredSystem("round", { roundAction = T.RoundStateAction, positi
system.world:removeEntity(collectable)
end
local availableUpgrades = Utils.getNDifferentValues(getAvailableSpawnerUpgrades(), 3)
---@type NamedUpgrade[]
local availableUpgrades = Utils.getNDifferentValues(getAvailableUpgrades(), 3)
-- Sorting from shortest to longest sort of makes them look like a bun?
table.sort(availableUpgrades, function(a, b)
return #a.name > #b.name
@ -119,8 +120,9 @@ roundSystem = filteredSystem("round", { roundAction = T.RoundStateAction, positi
i = i + 1
local collX, collY = 75, 21
y = y - collY - 15 - 15
---@type Selectable
local upgradeEntity = {
onSelect = upgrade.apply,
replacements = { upgrade.replace },
drawAsText = {
text = upgrade.name,
style = TextStyle.Inverted,
@ -150,7 +152,7 @@ roundSystem = filteredSystem("round", { roundAction = T.RoundStateAction, positi
system.world:addEntity({
afterDelayAdd = {
delay = delay,
entity = upgradeEntity
entityToAdd = upgradeEntity
},
})
system.world:addEntity(menuEntity)

View File

@ -35,7 +35,7 @@ function spawnerSystem:preProcess()
return tiny.SKIP_PROCESS
end
local spawnEveryX = 30
local spawnEveryX = 35
-- Currently spawns AT MOST one new ingredient per frame, which is probably not enough at high speeds!
function spawnerSystem:postProcess()
@ -43,7 +43,7 @@ function spawnerSystem:postProcess()
local newlySpawned = Ingredients.nextInCache()
-- TODO: If performance becomes an issue, maybe just swap out __index
for k, v in pairs(selectedSpawner.canSpawn.entity) do
for k, v in pairs(selectedSpawner.canSpawn.entityToSpawn) do
newlySpawned[k] = v
end
@ -62,13 +62,13 @@ function addAllSpawners(world)
local size = { x = sizeX, y = sizeY / 2 }
world:addEntity({
-- NOTE: This name should NOT be used to identify the spawner.
-- NOTE: This name should NOT be used to *identify* the spawner.
-- It should only be used to supply visual information when searching for available upgrades.
name = name,
odds = spawnerOdds,
canSpawn = {
yRange = yRange,
entity = {
entityToSpawn = {
score = score,
canBeCollidedBy = 1,
expireAfterCollision = true,
@ -101,47 +101,3 @@ function addAllSpawners(world)
})
end
---@alias Upgrade { name: string, apply = fun() }
---@return Upgrade[]
function getAvailableSpawnerUpgrades()
local upgrades = {}
for _, spawner in pairs(spawnerSystem.entities) do
if spawner.hasUpgradeSpeed then
-- upgrades[#upgrades + 1] = { hasUpgradeSpeed = spawner.hasUpgradeSpeed }
end
if spawner.canSpawn.entity.score then
local name = "Double " .. spawner.name .. " value"
upgrades[#upgrades + 1] = {
name = name,
apply = function(world)
print("Applying " .. name)
spawner.canSpawn.entity.score = spawner.canSpawn.entity.score * 2
world:addEntity({ roundAction = "start" })
end,
}
end
assert(spawner.odds, "Expected all spawners to have an `odds` field!")
local name = "Double " .. spawner.name .. " frequency"
upgrades[#upgrades + 1] = {
name = name,
apply = function(world)
print("Applying " .. name)
spawner.odds = spawner.odds * 2
world:addEntity({ roundAction = "start" })
end,
}
-- if not spawner.canSpawn.entity.velocity then
-- upgrades[#upgrades + 1] = {
-- name = spawner.name .. " Movement",
-- upgrade = function()
-- spawner.canSpawn.entity.velocity = { x = -10, y = 0 }
-- end,
-- }
-- end
end
return upgrades
end

View File

@ -0,0 +1,77 @@
---@alias NamedUpgrade { name: string, replace: ReplaceRelation }
--- Appends available upgrades to the given upgrades array.
---@param upgrades NamedUpgrade[]
function getAvailableSpawnerUpgrades(upgrades)
for _, spawner in pairs(spawnerSystem.entities) do
-- if spawner.hasUpgradeSpeed then
-- upgrades[#upgrades + 1] = { hasUpgradeSpeed = spawner.hasUpgradeSpeed }
-- end
local entityToSpawn = spawner.canSpawn.entityToSpawn
if entityToSpawn.score then
upgrades[#upgrades + 1] = {
name = "Double " .. spawner.name .. " value",
replace = {
entityToModify = entityToSpawn,
replacement = {
score = entityToSpawn.score * 2,
},
},
}
end
assert(spawner.odds, "Expected all spawners to have an `odds` field!")
upgrades[#upgrades + 1] = {
name = "Double " .. spawner.name .. " frequency",
replace = {
entityToModify = spawner,
replacement = {
odds = spawner.odds,
}
},
}
end
end
--- Appends available upgrades to the given upgrades array.
---@param upgrades NamedUpgrade[]
function getAvailableCartUpgrades(upgrades)
for _, cart in pairs(cartSystem.entities) do
upgrades[#upgrades + 1] = {
name = "10% Lighter Cart",
replace = {
entityToModify = cart,
replacement = {
mass = cart.mass * 0.9
}
},
}
upgrades[#upgrades + 1] = {
name = "+20% Cart Launch Right",
replace = {
entityToModify = cart.baseVelocity,
replacement = {
x = cart.baseVelocity.x * 1.2
}
},
}
upgrades[#upgrades + 1] = {
name = "+20% Cart Launch Up",
replace = {
entityToModify = cart.baseVelocity,
replacement = {
y = cart.baseVelocity.y * 1.2
}
},
}
end
end
---@return NamedUpgrade[]
function getAvailableUpgrades()
local upgrades = {}
getAvailableSpawnerUpgrades(upgrades)
getAvailableCartUpgrades(upgrades)
return upgrades
end