summaryrefslogtreecommitdiff
path: root/message.lua
blob: 24662fcf0a4d4910ff022811b4f5b72c289004e2 (plain)
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
99
100
101
-- Metatable.
local Message = {
	text, -- actually added in constructor
	opacity = 0, -- [0..3]
	visible = true,
}
Message.__index = Message

-- Constructor.
local function newMessage()
	return setmetatable({text = love.graphics.newText(Game.font)}, Message)
end

-- Dimensions related functions.
function Message:getDimensions()
	local width = Game.getWidth() / self:getScale()
	local height = 0
	if self.text then
		height = self.text:getHeight()
	end
	return width, height
end

-- Position related functions.
function Message:getPosition(scale)
	local w, h = self:getDimensions()
	local x = 2 * scale
	local y = Game.getHeight() - (h+2) * scale
	return x, y
end

-- Internal scale for Message's text.
function Message:getScale()
	return math.min(7, math.ceil(Game.getWidth() / 190))
end

-- Text, set it, motherfucker.
function Message:setText(speaker, content)
	local width = self:getDimensions()
	if self.text and speaker and content then
		self.text:setf({{240,100,100}, speaker .. "\n" , {240,240,240}, content},width, "left")
	end
	return self
end

-- Also set use, not seduce.
function Message:setUse(func)
	if type(func) == "function" then
		self.use = func
	end
	return self
end

-- Manages Message's visibility.
function Message:isVisible()
	if self.opacity > 0 then
		return true
	end
	return false
end
function Message:hide()
	self.visible = false
	return self
end
function Message:show()
	self.visible = true
	return self
end

-- Callback when mouse is pressed.
function Message:click()
	self:hide()
end
function Message:use()
end

-- LÖVE2D callbacks.
function Message:update(dt)
	if self.visible and self.opacity < 3 then
		self.opacity = self.opacity + 1
	end
	if not self.visible and self.opacity > 0 then
		self.opacity = self.opacity - 1
	end
	if not self.visible and self.opacity == 0 then
		self:use()
	end
end
function Message:draw(scale)
	if not self:isVisible() then return end
	local scale = self:getScale()
	local x,y = self:getPosition(scale)
	local w, h = self:getDimensions()
	love.graphics.push()
	love.graphics.origin()
	love.graphics.setColor(255,255,255,255/3*self.opacity)
	love.graphics.draw(self.text, x,y, 0, scale, scale)
	love.graphics.pop()
end

return newMessage