require "entity" Door = class("Door", Entity) function Door:initialize(x, y) Entity.initialize(self, { x = x, y = y, color = "white", collision = "unmoveable", sprites = { red = loadSprite("art/game/door_r.png"), green = loadSprite("art/game/door_g.png"), blue = loadSprite("art/game/door_b.png"), }, }) self.locked = true self.colors = {} for _, color in pairs({"red", "green", "blue"}) do self.colors[color] = true --these are currently loc_ed end self:refreshSprites() end -- keep the per-channel sprite map in sync with lock state so the shared -- Entity:draw(channel) renders the door with no door-specific draw code function Door:refreshSprites() self.sprites = {} for _, c in ipairs({"red", "green", "blue"}) do if self.locked then if self.colors[c] then self.sprites[c] = loadSprite("art/game/door_" .. c:sub(1, 1) .. ".png") end else self.sprites[c] = loadSprite("art/game/door_unlocked.png") end end end function Door:setColors(colors) local offColors = {} for color, off in pairs(colors) do if off then if self.colors[color] then self.colors[color] = false end table.insert(offColors, color) else if not self.colors[color] then self.colors[color] = true end end end if #offColors == 3 then if self.locked then self:unlock() end else if not self.locked then self:lock() end end self:refreshSprites() end function Door:unlock() self.locked = false self.collision = "none" end function Door:lock() self.locked = true self.collision = "unmoveable" end