Extract some round control to rounds.lua

Collected items are now unique entities in the world, instead of being held by the cart.
Commented-out sparking logic.
Very very very basic effects.lua, with things to apply to the cart.
This commit is contained in:
Sage Vaillancourt 2025-03-04 19:04:27 -05:00
parent 0b79dc68fb
commit 5d16c75cf6
10 changed files with 123 additions and 49 deletions

View File

@ -17,6 +17,10 @@ LettuceSprite = playdate.graphics.image.new("assets/images/LettuceSprite.png")
---@type pd_image
MushroomSprite = playdate.graphics.image.new("assets/images/MushroomSprite.png")
-- luacheck: ignore
---@type pd_image
Spark = playdate.graphics.image.new("assets/images/Spark.png")
-- luacheck: ignore
---@type pd_image
TomatoSprite = playdate.graphics.image.new("assets/images/TomatoSprite.png")

BIN
src/assets/images/Spark.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 557 B

View File

@ -1,5 +1,3 @@
import("plate.lua")
Cart = {}
local sizeX, sizeY = CartSprite:getSize()
@ -11,7 +9,7 @@ function Cart.reset(o)
y = 50,
}
o.velocity = {
x = 50 + (150 * math.random()),
x = 10 + (150 * math.random()),
y = 150 * (math.random() - 1),
}
o.size = size
@ -19,52 +17,27 @@ function Cart.reset(o)
flat = { x = 0, y = 0 },
mult = { x = 1, y = 1 },
}
o.canCollideWith = 1
o.canCollideWith = 1 | 4
o.mass = 1
o.drawAsSprite = CartSprite
o.focusPriority = 0
---@type pd_image[]
o.collectables = {}
---@param world World
function o:spawnEntitiesWhenStopped(world)
local y = self.position.y - 240
local rectWidth = 150
local plateSize = { x = rectWidth, y = 10 }
self.velocity = { x = 300, y = 0 }
world:addEntity(self)
self.canCollideWith = 4
world:addEntity({
roundAction = "end",
position = {
x = self.position.x,
y = self.position.y,
},
})
world:addEntity({
position = { x = self.position.x, y = self.position.y },
focusPriority = 1,
})
world:addEntity({
drawAsRectangle = { size = plateSize },
size = plateSize,
mass = 0.5,
velocity = { x = 0, y = 0 },
position = { x = self.position.x - (rectWidth / 2), y = y },
canCollideWith = 2,
canBeCollidedBy = 2,
isSolid = true,
stopMovingOnCollision = true,
})
for _, collectable in ipairs(self.collectables) do
local collX, collY = collectable:getSize()
y = y - collY - 5
world:addEntity({
drawAsSprite = collectable,
size = { x = collX, y = collY / 2 },
mass = 0.5,
velocity = { x = 0, y = 0 },
position = { x = self.position.x - (collX / 2), y = y },
canCollideWith = 2,
canBeCollidedBy = 2,
isSolid = true,
stopMovingOnCollision = true,
})
end
end
return o

10
src/effects.lua Normal file
View File

@ -0,0 +1,10 @@
Effects = {}
---@param entity { canBeBounced: CanBeBounced }
function Effects.makeBouncier(entity)
entity.canBeBounced.mult = entity.canBeBounced.mult * 1.5
end
---@param entity { mass: number }
function Effects.makeFloatier(entity)
entity.mass = entity.mass * 0.75
end

View File

@ -5,8 +5,8 @@ local size = { x = sizeX, y = sizeY }
local expireWhenOffScreenBy = { x = 2000, y = 480 }
local canBounce = {
flat = { x = 0, y = 190 },
mult = { x = 1, y = -2 },
flat = { x = 0, y = 290 },
mult = { x = 1, y = -1 },
}
function Mushroom.initialize(o, x, y)

View File

@ -16,6 +16,7 @@ import("systems/collision-detection.lua")
import("systems/collision-resolution.lua")
import("systems/draw.lua")
import("systems/gravity.lua")
import("systems/rounds.lua")
import("systems/velocity.lua")
import("ingredients/ingredients.lua")
import("cart.lua")
@ -30,13 +31,14 @@ local floorSize = { x = 10000, y = 10 }
floor = {
position = { x = 0, y = 235 },
size = floorSize,
canBeCollidedBy = 1 | 2,
canBeCollidedBy = 1 | 2 | 4,
canBounce = {
flat = { x = 0, y = 0 },
mult = { x = 0.9, y = -0.5 },
},
drawAsRectangle = { size = floorSize },
isSolid = true,
sparkOnCollision = true,
}
Camera = {
@ -64,6 +66,8 @@ end
world = tiny.world(
fallSystem,
velocitySystem,
roundSystem,
collectedEntities,
collidingEntities,
collisionResolution,
collisionDetection,
@ -74,7 +78,7 @@ world = tiny.world(
cart
)
local ingredientsEveryX = 90
local ingredientsEveryX = 60
local function init()
for i = 1, Ingredients.cacheSize() do
@ -93,9 +97,6 @@ function playdate.update()
gfx.clear(gfx.kColorWhite)
playdate.drawFPS(5, 5)
Camera.pan.x = math.max(0, cart.position.x - 200)
Camera.pan.y = math.min(0, cart.position.y - 120)
local newestX = Ingredients.newestIngredient().position.x
local panX = Camera.pan.x
local rightEdge = panX + 400

View File

@ -1,4 +1,4 @@
collisionResolution = filteredSystem({ collisionBetween = T.Collision }, function(e, _, system)
collisionResolution = filteredSystem({ collisionBetween = T.Collision }, function(e, dt, system)
local collidedInto, collider = e.collisionBetween[1], e.collisionBetween[2]
local colliderTop = collidedInto.position.y
@ -20,13 +20,29 @@ collisionResolution = filteredSystem({ collisionBetween = T.Collision }, functio
collider.velocity.y = collider.velocity.y * collidedInto.canBounce.mult.y * collider.canBeBounced.mult.y
end
if collider.focusOnCollide then
system.world:addEntity({
focusPriority = collider.focusOnCollide,
position = {
x = collider.position.x,
y = collider.position.y,
}
})
end
if collider.stopMovingOnCollision then
print("stopMovingOnCollision!")
collider.velocity = nil
collider.canCollideWith = nil
system.world:addEntity(collider)
end
if collider.onCollidingRemove then
for _, key in ipairs(collider.onCollidingRemove) do
collider[key] = nil
end
collider.onCollidingRemove = nil
end
if collidedInto.score then
Score:add(collidedInto.score)
end
@ -35,9 +51,29 @@ collisionResolution = filteredSystem({ collisionBetween = T.Collision }, functio
system.world:removeEntity(collidedInto)
end
if collidedInto.collectable and collider.collectables then
collider.collectables[#collider.collectables + 1] = collidedInto.collectable
if collidedInto.collectable then
system.world:addEntity({ collected = collidedInto.collectable })
end
-- if collidedInto.sparkOnCollision and collider.sparkOnCollision and (math.abs(collider.velocity.x) + math.abs(collider.velocity.y)) > 2 then
-- local size = { x = 1, y = 1 }
-- local spark = {
-- position = {
-- x = collider.position.x,
-- y = collider.position.y + collider.size.y - 2,
-- },
-- velocity = {
-- x = -collider.velocity.x,
-- y = 150,
-- },
-- size = size,
-- canCollideWith = 1,
-- mass = 1,
-- drawAsSprite = Spark,
-- expireAfterCollision = true,
-- }
-- system.world:addEntity(spark)
-- end
system.world:removeEntity(e)
end)

View File

@ -12,6 +12,10 @@ local XyPair = { x = 1, y = 1 }
---@alias BitMask number
---@alias CanBeBounced { flat: XyPair, mult: XyPair }
---@alias RoundStateAction "end" | "start"
T = {
XyPair = XyPair,
bool = true,
@ -30,6 +34,7 @@ T = {
mult = XyPair,
},
--- Receiver
---@type CanBeBounced
CanBeBounced = {
flat = XyPair,
mult = XyPair,
@ -38,6 +43,8 @@ T = {
Collision = { Entity, Entity },
---@type pd_image
PdImage = {},
---@type RoundStateAction
RoundStateAction = "start"
}
---@generic T

43
src/systems/rounds.lua Normal file
View File

@ -0,0 +1,43 @@
collectedEntities = filteredSystem({ collected = T.PdImage })
roundSystem = filteredSystem({ roundAction = T.RoundStateAction, position = Maybe(T.XyPair) }, function(e, _, system)
if e.roundAction == "end" then
playdate.setAutoLockDisabled(false)
local y = e.position.y - 240
local rectWidth = 150
local plateSize = { x = rectWidth, y = 10 }
system.world:addEntity({
drawAsRectangle = { size = plateSize },
size = plateSize,
mass = 0.5,
velocity = { x = 0, y = 0 },
position = { x = e.position.x - (rectWidth / 2), y = y },
canCollideWith = 2,
canBeCollidedBy = 2,
isSolid = true,
stopMovingOnCollision = true,
})
for i, collectable in ipairs(collectedEntities.entities) do
local collX, collY = collectable.collected:getSize()
local onCollidingRemove = {"mass", "velocity", "canCollideWith"}
y = y - collY - 15
system.world:addEntity({
drawAsSprite = collectable.collected,
size = { x = collX, y = collY / 2 },
mass = 0.5,
velocity = { x = 0, y = 0 },
position = { x = e.position.x - (collX / 2), y = y },
canCollideWith = 2,
canBeCollidedBy = 2,
isSolid = true,
stopMovingOnCollision = true,
onCollidingRemove = onCollidingRemove,
focusOnCollide = i,
})
system.world:removeEntity(collectable)
end
system.world:removeEntity(e)
end
end)

View File

@ -4,7 +4,7 @@ velocitySystem = filteredSystem({ position = T.XyPair, velocity = T.XyPair }, fu
if not e.velocity then
return
end
if sqrt((e.velocity.x * e.velocity.x) + (e.velocity.y * e.velocity.y)) < 1 then
if sqrt((e.velocity.x * e.velocity.x) + (e.velocity.y * e.velocity.y)) < 1.5 then
e.velocity = nil
if e.spawnEntitiesWhenStopped then
e:spawnEntitiesWhenStopped(system.world)