1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
|