-
Notifications
You must be signed in to change notification settings - Fork 68
/
highscore_entry.lua
191 lines (175 loc) · 4.9 KB
/
highscore_entry.lua
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
179
180
181
182
183
184
185
186
187
188
189
190
191
highscore_entry = {}
local lg = love.graphics
function highscore_entry.enter()
state = STATE_HIGHSCORE_ENTRY
highscore_entry.name = "_____"
highscore_entry.selection = 1
highscore_entry.position = 1
local scores = highscores[level]
local rank = 0
for i=1,10 do
if scores[i] then
if scores[i].score < score then
rank = i
break
end
else
rank = i
break
end
end
if rank == 0 then
highscore_list.enter(level,rank)
else
highscore_entry.rank = rank
end
end
function highscore_entry.update(dt)
updateKeys()
end
function highscore_entry.draw()
lg.push()
lg.scale(config.scale)
lg.printf("NEW HIGHSCORE!", 0, 32, WIDTH, "center")
lg.printf("PLEASE ENTER YOUR NAME", 0, 48, 256, "center")
local char = 1
for iy = 1,3 do
for ix = 1,10 do
if highscore_entry.selection == char then
lg.rectangle("fill", 33+ix*16, 66+iy*16, 14, 14)
lg.setColor(0,0,0,1)
lg.print(KEYBOARD:sub(char,char), 37+ix*16, 70+iy*16)
lg.setColor(1,1,1,1)
else
lg.print(KEYBOARD:sub(char,char), 37+ix*16, 70+iy*16)
end
char = char + 1
end
end
for i=1,5 do
lg.print(highscore_entry.name:sub(i,i), 86+i*14, 158)
end
lg.pop()
end
function highscore_entry.addChar(c)
local char = c or KEYBOARD:sub(highscore_entry.selection,highscore_entry.selection)
local head = highscore_entry.name:sub(1,highscore_entry.position-1)
local tail = highscore_entry.name:sub(highscore_entry.position+1, 5)
highscore_entry.name = head .. char .. tail
highscore_entry.position = cap(highscore_entry.position + 1, 1, 6)
playSound("confirm")
end
function highscore_entry.delete()
highscore_entry.position = cap(highscore_entry.position - 1, 1, 6)
local head = highscore_entry.name:sub(1,highscore_entry.position-1)
highscore_entry.name = head .. string.rep("_", 6-highscore_entry.position)
playSound("blip")
end
function highscore_entry.confirm()
local newname = highscore_entry.name:gsub("_"," ")
local entry = {name=newname, score=score}
table.insert(highscores[level], highscore_entry.rank, entry)
highscore_list.enter(level,highscore_entry.rank)
playSound("confirm")
end
function highscore_entry.keypressed(k)
if k == "right" then
if highscore_entry.selection % 10 == 0 then
highscore_entry.selection = highscore_entry.selection - 9
else
highscore_entry.selection = highscore_entry.selection + 1
end
playSound("blip")
elseif k == "left" then
if highscore_entry.selection % 10 == 1 then
highscore_entry.selection = highscore_entry.selection + 9
else
highscore_entry.selection = highscore_entry.selection - 1
end
playSound("blip")
elseif k == "down" then
if highscore_entry.selection >= 21 then
highscore_entry.selection = highscore_entry.selection - 20
else
highscore_entry.selection = highscore_entry.selection + 10
end
playSound("blip")
elseif k == "up" then
if highscore_entry.selection <= 10 then
highscore_entry.selection = highscore_entry.selection + 20
else
highscore_entry.selection = highscore_entry.selection - 10
end
playSound("blip")
elseif k == "return" then
if highscore_entry.selection <= 28 then
if highscore_entry.position <= 5 then
highscore_entry.addChar()
end
elseif highscore_entry.selection == 29 then
highscore_entry.delete()
else
highscore_entry.confirm()
end
elseif k == "backspace" then
highscore_entry.delete()
end
end
function highscore_entry.textinput(k)
local uni = k:byte()
if (uni >= 97 and uni <= 122) or k == " " or k == "-" then
if highscore_entry.position <= 5 then
if k == " " then
highscore_entry.addChar("_")
else
highscore_entry.addChar(string.upper(k))
end
end
highscore_entry.selection = 30
end
end
function highscore_entry.action(k)
if k == "right" then
if highscore_entry.selection % 10 == 0 then
highscore_entry.selection = highscore_entry.selection - 9
else
highscore_entry.selection = highscore_entry.selection + 1
end
playSound("blip")
elseif k == "left" then
if highscore_entry.selection % 10 == 1 then
highscore_entry.selection = highscore_entry.selection + 9
else
highscore_entry.selection = highscore_entry.selection - 1
end
playSound("blip")
elseif k == "down" then
if highscore_entry.selection >= 21 then
highscore_entry.selection = highscore_entry.selection - 20
else
highscore_entry.selection = highscore_entry.selection + 10
end
playSound("blip")
elseif k == "up" then
if highscore_entry.selection <= 10 then
highscore_entry.selection = highscore_entry.selection + 20
else
highscore_entry.selection = highscore_entry.selection - 10
end
playSound("blip")
elseif k == "jump" then
if highscore_entry.selection <= 28 then
if highscore_entry.position <= 5 then
highscore_entry.addChar()
end
elseif highscore_entry.selection == 29 then
highscore_entry.delete()
else
highscore_entry.confirm()
end
elseif k == "pause" then
highscore_entry.confirm()
elseif k == "action" then
highscore_entry.delete()
end
end