From 72533c0efb9d5659cd2c1fb0e9f067696d759812 Mon Sep 17 00:00:00 2001 From: Aki Date: Sun, 26 Feb 2017 05:04:34 +0100 Subject: Initial commit, after jam --- prop.lua | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 prop.lua (limited to 'prop.lua') diff --git a/prop.lua b/prop.lua new file mode 100644 index 0000000..67358e1 --- /dev/null +++ b/prop.lua @@ -0,0 +1,93 @@ +-- Metatable. +local Prop = { + x = 0, + y = 0, + width = 0, + height = 0, + frame = 1, + animation = "closed", + style = "door", +} +Prop.__index = Prop + +-- Constructor. +local function newProp() + return setmetatable({}, Prop) +end + +-- Dimensions related functions. +function Prop:getDimensions() + return self.width, self.height +end +function Prop:setDimensions(width, height) + self.width, self.height = width, height + return self +end + +-- Position related functions. +function Prop:getPosition() + return self.x, self.y +end +function Prop:setPosition(x, y) + self.x, self.y = x, y + return self +end + +-- Don't mistake with seduce. +function Prop:setUse(func) + if type(func) == "function" then + self.use = func + end + return self +end + +-- Animation. +function Prop:changeAnimation(name) + if self.animation ~= name and name then + self.animation = name + self.frame = 1 + end + return self +end + +-- Stylish. +function Prop:setStyle(style) + if self.style ~= style and style then + self.style = style + end + return self +end + +-- Returns true if given position is inside Prop's hitbox. +function Prop:testPosition(x, y) + local px, py = self:getPosition() + local w, h = self:getDimensions() + if x > px and x < px+w and y > py and y < py+h then + return self + end +end + +-- Being used by player callback. +function Prop:use() end + +-- LÖVE2D callbacks. +function Prop:update() + -- Animations + local frames = #Game.quads[self.style][self.animation] + self.frame = (self.frame%frames)+1 + -- Automatic animation changes + if self.animation == "opening" and self.frame == 7 then + self:changeAnimation("open") + end +end +function Prop:draw(scale) + local x,y = self:getPosition() + if Game.inRange(x, y) then + local sprite = Game.sprites[self.style] + local quad = Game.quads[self.style][self.animation][self.frame] + love.graphics.setColor(255,255,255) + love.graphics.draw(sprite, quad, (x)*scale, (y)*scale, 0, scale, scale) + end +end + +return newProp \ No newline at end of file -- cgit v1.1