Skip to content

Commit

Permalink
Added connected entities helper
Browse files Browse the repository at this point in the history
Has helper functions for entities that are "connected" visually
Now used by floaty space block to add the placement preview
This solution isn't perfect, will be improved in the future
  • Loading branch information
Cruor committed Sep 8, 2021
1 parent 8f31c50 commit d9bbfca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/entities/floaty_space_block.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local fakeTilesHelper = require("fake_tiles_helper")
local connectedEntities = require("helpers.connected_entities")
local utils = require("utils")
local matrixLib = require("matrix")

Expand Down Expand Up @@ -26,6 +27,9 @@ end
function floatySpaceBlock.sprite(room, entity)
local searchPredicate = getSearchPredicate(entity)
local relevantBlocks = utils.filter(getSearchPredicate(entity), room.entities)

connectedEntities.appendIfMissing(relevantBlocks, entity)

local firstEntity = relevantBlocks[1] == entity

if firstEntity then
Expand Down
52 changes: 52 additions & 0 deletions src/helpers/connected_entities.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
local utils = require("utils")

local connectedEntities = {}

-- Useful for adding a placement preview entity into the entities list
function connectedEntities.appendIfMissing(entities, target)
local seen = false

for _, entity in ipairs(entities) do
if entity == target then
seen = true

break
end
end

if not seen then
table.insert(entities, target)
end

return seen
end

function connectedEntities.getEntityRectangles(entities)
local rectangles = {}
local seenExtra = false

for _, entity in ipairs(entities) do
table.insert(rectangles, utils.rectangle(entity.x, entity.y, entity.width, entity.height))

if entity == extra then
seenExtra = true
end
end

return rectangles
end

function connectedEntities.hasAdjacent(entity, offsetX, offsetY, rectangles, checkWidth, checkHeight)
local x, y = entity.x or 0, entity.y or 0
local checkX, checkY = x + offsetX, y + offsetY

for _, rect in ipairs(rectangles) do
if utils.aabbCheckInline(rect.x, rect.y, rect.width, rect.height, checkX, checkY, checkWidth or 8, checkHeight or 8) then
return true
end
end

return false
end

return connectedEntities

0 comments on commit d9bbfca

Please sign in to comment.