chroma_solstice/entity.lua
Your Name 7d84f595e4 Replace box variants with a moveable adhesion property
Collapse the spiky/sticky/skewer box-variant assets into a single per-
instance `adhesion` property on any moveable block, chosen in the editor
via a dropdown next to the colour picker and stored in the .sav alongside
the class.

- spiky -> sticky: soft grab on every true edge (goopy shader)
- skewer -> gluey: rigid weld on every true edge (spiked outline)
- old trailing-face sticky is removed; its saves fold into new sticky

Move the spike geometry + drawGlueySprite into shaders.lua, fold drawing
and the adhesion flag into GenericMoveable, and delete the three
Generic{Sticky,Spiky,Skewer} classes and box_variants.lua. Room:createEntity
normalises legacy spiky_/skewer_/sticky_ class prefixes so old saves load,
and serialization emits `adhesion`. World/map previews and the movement
proxy follow the rename.

Backfill rooms/*.sav to the new format via tools/backfill_adhesion.lua
(41 objects across 8 files). Update tests to the two-mode model; the two
tests asserting the removed lateral-shear limitation are dropped and a
legacy-load migration test added. 27/27 pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-08-21 16:34:01 -04:00

198 lines
5 KiB
Lua

Entity = class("Entity")
function Entity:initialize(t)
self.x, self.y = t.x, t.y
-- sprites keyed by channel ("red"/"green"/"blue"). Single-color entities pass
-- t.sprite + t.color and get one entry; multi-channel ones (doors) pass
-- t.sprites directly. The renderer draws only the channels the player has, so
-- no entity decides its own visibility (see Room:draw).
self.sprites = t.sprites or { [t.color] = t.sprite }
local repSprite = t.sprite or (t.sprites and select(2, next(t.sprites)))
self.spriteSize = {w = repSprite:getWidth(), h = repSprite:getHeight()}
self.size = {w = self.spriteSize.w / 16, h = self.spriteSize.h / 16}
self.cellShape = t.cellShape or self:cellShapeFromBox(self.size)
self.name = t.name
self.color = Color:new(t.color)
self.collision = t.collision --"none", "moveable", "unmoveable"
-- Per-instance adhesion for moveable blocks: "sticky" (soft grab on every true
-- edge) or "gluey" (rigid weld on every true edge). nil = plain moveable.
-- See docs/adhesion-refactor-plan.md.
self.adhesion = t.adhesion
self.sprite = repSprite
self.shaAmount = t.sha or 1
-- onBump is a method (see Entity:onBump) so mixins like Dialogable can
-- override it; a per-instance t.onBump still wins when supplied.
if t.onBump then self.onBump = t.onBump end
self.tick = t.tick or function(self) return true end
self.drawOffset = {x = 0, y = 0}
if self.size.w < 1 or self.size.h < 1 then
-- Small sprites (notably 10px switches) are drawn inside a 16px cell.
-- Offset by the leftover space in native sprite-pixels (e.g. (16-10)/2 = 3);
-- drawScale is applied at draw time so this stays correct across resizes.
self.drawOffset.x = (16 - self.spriteSize.w) / 2
self.drawOffset.y = (16 - self.spriteSize.h) / 2
end
end
function Entity:getCollision()
return self.collision
end
function Entity:atGridPos(x, y)
-- we probably don't need this anymore
local size = self:getGridSize()
if x >= self.x and y >= self.y and x < self.x + size.x and y < self.y + size.y then
return true
end
return false
end
function Entity:getOccupiedCells(coords)
--we have this offset so we can project what a movement would do
local offset = coords or {x = 0, y = 0}
local cells = {}
local pos = self:getGridPos()
for _, cell in ipairs(self:getCellShape()) do
local adjustedCell = {}
adjustedCell.x = pos.x + cell.x - 1 + offset.x
adjustedCell.y = pos.y + cell.y - 1 + offset.y
table.insert(cells, adjustedCell)
end
-- print("cels!!!!", cells[1].x, cells[1].y)
return cells
end
function Entity:cellShapeFromBox(box)
local cells = {}
for i = 1, box.w do
for j = 1, box.h do
table.insert(cells, {x = i, y = j})
end
end
return cells
end
function Entity:getCellShape()
return self.cellShape
end
function Entity:getDrawBox()
local drawPos = self:getDrawPos()
return {x = drawPos.x, y = drawPos.y, w = self.spriteSize.w * drawScale, h = self.spriteSize.h * drawScale}
end
function Entity:getGridBox()
return {x = self.x, y = self.y, w = self.size.w, h = self.size.h}
end
function Entity:getGridPos()
return {x = self.x, y = self.y}
end
function Entity:getDrawPos()
local gridPos = self:getGridPos()
return {
x = (gridPos.x - 1) * width / gridWidth + self:sha(),
y = (gridPos.y - 1) * height / gridHeight + self:sha()
}
end
function Entity:getGridSize()
return {x = self.size.w, y = self.size.h}
end
function Entity:occupiesCell(inCell)
for _, cell in pairs(self:getOccupiedCells()) do
if cell.x == inCell.x and cell.y == inCell.y then
return true
end
end
return false
end
function Entity:getname()
return self.name
end
function Entity:tick()
return self.tick()
end
function Entity:draw(channel)
local sprite = self.sprites[channel]
if not sprite then return end
love.graphics.setColor(gColor[channel]:set())
local p = self:getDrawPos()
love.graphics.draw(sprite, p.x + self.drawOffset.x * drawScale, p.y + self.drawOffset.y * drawScale, 0, drawScale, drawScale)
end
function Entity:canMove()
if self:getCollision() == "moveable" then
return true
end
return false
end
function Entity:isSticky()
return self.adhesion == "sticky"
end
function Entity:isGluey()
return self.adhesion == "gluey"
end
function Entity:canPassOver()
if self:getCollision() == "none" then
return true
end
return false
end
function Entity:blocksPlayer()
return false
end
function Entity:getClass()
return self.class.name
end
function Entity:getSerializationTable()
local pos = self:getGridPos()
local table = {}
table.x, table.y, table.color = pos.x, pos.y, self.color:getname()
if self.name then table.name = self.name end
return table
end
function Entity:move(axis, direction)
self[axis] = self[axis] + direction
end
function Entity:setSprite(sprite)
self.sprite = sprite
end
function Entity:sha()
return math.random(-worldSha, worldSha) * .125 * drawScale
end
function Entity:onBump()
return true
end
function Entity:bump()
return self:onBump()
end
Entity.static.positionsAreEqual = function(cell1, cell2)
if cell1.x == cell2.x and cell1.y == cell2.y then
return true
end
return false
end