summaryrefslogtreecommitdiff
path: root/room.lua
diff options
context:
space:
mode:
authorAki <nthirtyone@gmail.com>2017-02-26 05:04:34 +0100
committerAki <nthirtyone@gmail.com>2017-02-26 05:04:34 +0100
commit72533c0efb9d5659cd2c1fb0e9f067696d759812 (patch)
tree9fd29c0700c182de5e787af45c4277ee62140787 /room.lua
downloadvisitor-72533c0efb9d5659cd2c1fb0e9f067696d759812.zip
visitor-72533c0efb9d5659cd2c1fb0e9f067696d759812.tar.gz
visitor-72533c0efb9d5659cd2c1fb0e9f067696d759812.tar.bz2
Initial commit, after jam
Diffstat (limited to 'room.lua')
-rw-r--r--room.lua98
1 files changed, 98 insertions, 0 deletions
diff --git a/room.lua b/room.lua
new file mode 100644
index 0000000..b7f5206
--- /dev/null
+++ b/room.lua
@@ -0,0 +1,98 @@
+-- Metatable.
+local Room = {
+ x = 0,
+ y = 0,
+ width = 64,
+ height = 32,
+ discovered = false,
+ style = "planet",
+ seed = 0,
+ opacity = 255,
+ outside = 1,
+}
+Room.__index = Room
+
+-- Create new empty Room with given dimensions.
+local function newRoom()
+ return setmetatable({seed = math.random(1,65535)}, Room)
+end
+
+-- Sets style of a Room.
+function Room:setStyle(style)
+ self.style = style
+ return self
+end
+function Room:setOutside(outside)
+ self.outside = outside
+ return self
+end
+
+-- Dimensions related functions.
+function Room:getDimensions()
+ return self.width, self.height
+end
+function Room:setDimensions(width, height)
+ self.width, self.height = width, height
+ return self
+end
+
+-- Position related functions.
+function Room:getPosition()
+ return self.x, self.y
+end
+function Room:setPosition(x, y)
+ self.x, self.y = x, y
+ return self
+end
+function Room:randomX(w)
+ local x = self:getPosition()
+ local width = self:getDimensions()
+ return x+math.random(3, width-3-w)
+end
+
+-- Discovers room.
+function Room:discover(animate)
+ self.discovered = true
+ if animate then
+ self.opacity = 0
+ end
+ return self
+end
+
+-- Drawing functions
+function Room:drawOutside(scale, offset_x, offset_y)
+ local x, y = self:getPosition()
+ local width, height = self:getDimensions()
+ love.graphics.push()
+ love.graphics.origin()
+ love.graphics.setScissor((x+1)*scale + offset_x, (y+1)*scale + offset_y, (width-2)*scale, (height-2)*scale)
+ love.graphics.setColor(self.opacity,self.opacity,self.opacity)
+ love.graphics.draw(Game.sprites.outside, Game.quads.outside[self.outside][1], 0, Game.getHeight()/2, 0, scale, scale, 0, 75/2)
+ love.graphics.setScissor()
+ love.graphics.pop()
+end
+function Room:drawInside(scale)
+ local x, y = self:getPosition()
+ local width, height = self:getDimensions()
+ local sprite = Game.sprites.room
+ local quad = Game.quads.room[self.style]
+ love.graphics.setColor(self.opacity,self.opacity,self.opacity)
+ love.graphics.draw(sprite, quad[1], (x)*scale, (y)*scale, 0, scale, scale)
+end
+
+-- LÖVE2D callbacks.
+function Room:update()
+ if self.opacity < 255 then
+ self.opacity = math.min(255, self.opacity + 64)
+ end
+end
+function Room:draw(scale, offset_x, offset_y)
+ if self.discovered then
+ if self.style == "planet" then
+ self:drawOutside(scale, offset_x, offset_y)
+ end
+ self:drawInside(scale)
+ end
+end
+
+return newRoom \ No newline at end of file