-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfloating_text.lua
More file actions
66 lines (51 loc) · 1.39 KB
/
floating_text.lua
File metadata and controls
66 lines (51 loc) · 1.39 KB
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
--
-- block.lua
-- redditgamejam-05
--
-- Created by Jay Roberts on 2010-12-10.
-- Copyright 2010 GloryFish.org. All rights reserved.
--
require 'class'
require 'vector'
FloatingText = class(function(floatingtext)
floatingtext.texts = {}
floatingtext.speed = 30 -- Pixels per second
end)
function FloatingText:addText(msg, pos, col, lftm)
local text = {
message = msg,
position = pos,
color = col,
duration = 0,
lifetime = lftm,
state = 'floating'
}
table.insert(self.texts, text)
end
function FloatingText:update(dt)
local toRemove = {}
-- Move texts up
for index, text in pairs(self.texts) do
text.position.y = text.position.y - (self.speed * dt)
text.duration = text.duration + dt
if text.duration > text.lifetime then
table.insert(toRemove, index)
end
end
-- Remove texts that are past their lifetime
for i, index in pairs(toRemove) do
table.remove(self.texts, index)
end
end
function FloatingText:draw()
for index, text in pairs(self.texts) do
local alpha = 255 - (255 * (text.duration / text.lifetime))
love.graphics.setColor(text.color.r,
text.color.g,
text.color.b,
alpha);
love.graphics.print(text.message,
text.position.x,
text.position.y);
end
end