-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscoring.lua
More file actions
178 lines (156 loc) · 5.11 KB
/
scoring.lua
File metadata and controls
178 lines (156 loc) · 5.11 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
Score = {
initialized = false,
timer = {
active = true,
font_size = 26,
countType = 'up',
text_align = 'left',
font = nil,
position = { x = 0, y = 0 },
current_time = 0,
prefix = 'Time: ',
overflow_limit = 2000
},
score_count = {
current_score = 0,
overflow_limit = 2000,
text_align = 'left',
prefix = 'Score: ',
font_size = 26,
position = { x = 0, y = 0 },
font = nil,
score_points_queue = {},
multiplier = {
multiplier_suffix = 'x',
multiplier = 1,
position = { x = 0, y = 0 },
font_size = 26,
minimum_multiplier = 1
}
}
}
local function poll_score(score_table)
if #score_table.score_count.score_points_queue > 0 then
local stuff = score_table.score_count.score_points_queue[1]
table.remove(score_table.score_count.score_points_queue, 1)
return stuff
else
return nil
end
end
function Score:initialize()
if self.initialized then
self:resetMultiplier()
self:resetScoreCount()
self:resetTimer()
else
self:setupTimer()
self:setupScoreCount()
self:setupMultiplier()
self.initialized = true
end
end
function Score:setupTimer(start_time, font, font_size, position)
self.timer.font_size = font_size ~= nil and font_size or 24
self.timer.current_time = start_time ~= nil and start_time or 0
if font == nil then
self.timer.font = love.graphics.newFont("Assets/PressStart2P.ttf",
self.timer.font_size)
else
self.timer.font = font
end
if position ~= nil then
self.timer.position = position
else
local prefix_length = self.timer.font:getWidth(self.timer.prefix)
local text_length = self.timer.font:getWidth(tostring(self.timer.current_time))
self.timer.position = { x = love.graphics.getWidth() / 2 - text_length / 2 - prefix_length / 2,
y = 40 }
end
end
function Score:setupScoreCount(font, font_size, position)
self.score_count.font_size = font_size ~= nil and font_size or 24
if self.score_count.font == nil then
self.score_count.font = love.graphics.newFont("Assets/PressStart2P.ttf", self.score_count.font_size)
else
self.score_count.font = font
end
if position ~= nil then
self.timer.position = position
else
local prefix_length = self.score_count.font:getWidth(self.score_count.prefix)
local text_length = self.timer.font:getWidth(tostring(self.score_count.current_score))
self.score_count.position = {x = love.graphics.getWidth() / 2 - text_length / 2 - prefix_length / 2,
y = 45 + self.score_count.font_size}
end
end
function Score:setupMultiplier(minimum_multiplier)
if minimum_multiplier ~= nil then
self.score_count.multiplier.multiplier = minimum_multiplier
end
self.score_count.multiplier.position = { x = 30, y = 50 }
end
function Score:addToMultiplier(value)
self.score_count.multiplier.multiplier = math.max(self.score_count.multiplier.multiplier + value, self.score_count.multiplier.minimum_multiplier)
end
function Score:getCurrentGameTime()
return self.timer.current_time
end
function Score:drawTimer()
local old_font = love.graphics.getFont()
love.graphics.setFont(self.timer.font)
love.graphics.printf(self.timer.prefix .. string.format("%02.1f", self.timer.current_time), self.timer.position.x,
self.timer.position.y, self.timer.overflow_limit, self.timer.text_align)
love.graphics.setFont(old_font)
end
function Score:updateTimer(dt)
if not self.timer.active then return end
if self.timer.countType == "up" then
self.timer.current_time = (self.timer.current_time + dt)
elseif self.timer.countType == "down" and self.timer.current_time > 0 then
self.timer.current_time = (self.timer.current_time - dt)
end
end
function Score:pushScore(points)
table.insert(self.score_count.score_points_queue, points * self.score_count.multiplier.multiplier)
end
function Score:getCurrentScore()
return self.score_count.current_score
end
function Score:pauseTimer()
self.timer.active = false
end
function Score:startTimer()
self.timer.active = true
end
function Score:resetMultiplier()
self.score_count.multiplier.multiplier = self.score_count.multiplier.minimum_multiplier
end
function Score:resetTimer()
self.timer.current_time = 0
end
function Score:resetScoreCount()
self:updateScoreCount()
self.score_count.current_score = 0
end
function Score:updateScoreCount()
local point = poll_score(self)
while point ~= nil do
self.score_count.current_score = self.score_count.current_score + point
point = poll_score(self)
end
end
function Score:drawScoreCount()
local old_font = love.graphics.getFont()
love.graphics.setFont(self.timer.font)
love.graphics.printf(self.score_count.prefix .. string.format("%05d", self.score_count.current_score), self.score_count.position.x,
self.score_count.position.y, self.score_count.overflow_limit, self.score_count.text_align)
love.graphics.setFont(old_font)
end
function Score:drawMultiplier()
local old_font = love.graphics.getFont()
love.graphics.setFont(self.timer.font)
love.graphics.printf(string.format("%i", self.score_count.multiplier.multiplier) .. self.score_count.multiplier.multiplier_suffix, self.score_count.multiplier.position.x,
self.score_count.multiplier.position.y, self.score_count.overflow_limit, self.score_count.text_align)
love.graphics.setFont(old_font)
end