-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
206 lines (158 loc) · 4.37 KB
/
main.lua
File metadata and controls
206 lines (158 loc) · 4.37 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
local utf8 = require("utf8")
local push = require("deps.push")
local Words = require("words")
local font
local width = 240
local height = 200
function love.load()
local window_width = love.graphics:getWidth()
local window_height = love.graphics:getHeight()
love.graphics.setDefaultFilter("nearest", "nearest")
push:setupScreen(width, height, window_width, window_height, {pixelperfect = true})
font = love.graphics.newImageFont("imagefont.png",
" abcdefghijklmnopqrstuvwxyz" ..
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0" ..
"123456789.,!?-+/():;%&`'*#=[]\"")
font:setFilter("nearest", "nearest")
love.graphics.setFont(font)
love.keyboard.setKeyRepeat(true)
end
local Toasts = {
---@type {
--- text: love.Text,
--- expiry: integer,
---}[]
items = {}
}
function Toasts:push(text_content, expire_after)
local text = love.graphics.newText(font)
text:setf(text_content, width, "center")
self.items[#self.items+1] = {
text = text,
expiry = os.time() + (expire_after or 3)
}
end
Words.selector = Words.char5
local State = {
word = Words:random(),
text = {
field_i = 1,
fields = {},
},
---@type "playing"|"win"|"lose"
gamestate = "playing",
Toasts = Toasts
}
---@param i integer?
function State.text:get(i)
return string.lower(self.fields[i or self.field_i] or "")
end
---@param text string
---@param i integer?
function State.text:set(text, i)
self.fields[i or self.field_i] = text:sub(1, #State.word)
end
---@param text string
---@param i integer?
function State.text:add(text, i)
self:set(self:get(i) .. text, i)
end
function State.text:get_i()
return self.field_i
end
---@param i integer
function State.text:set_i(i)
self.field_i = i
end
local time = os.time()
function love.update(dt)
time = os.time()
for i, toast in pairs(State.Toasts.items) do
if time >= toast.expiry then
table.remove(State.Toasts.items, i)
end
end
if State.gamestate == "playing" and State.text:get_i() > 5 then
State.gamestate = "lose"
end
end
function love.keypressed(key)
if State.gamestate ~= "playing" then
return
end
if key == "backspace" then
local byteoffset = utf8.offset(State.text:get(), -1)
if byteoffset then
State.text:set(string.sub(State.text:get(), 1, byteoffset - 1))
end
elseif key == "return" then
if not Words:inside(State.text:get()) then
State.Toasts:push("invalid word")
return
end
if State.text:get() == State.word then
State.gamestate = "win"
local i = State.text:get_i() + 1
State.text:set_i(i)
else
local i = State.text:get_i() + 1
State.text:set_i(i)
end
end
end
function love.textinput(t)
if State.gamestate ~= "playing" then
return
end
State.text:add(t)
end
function love.draw()
push:start()
for i = 1, 5, 1 do
local text = State.text:get(i)
local text_table = {}
local ci = 0
for wc in State.word:gmatch("(.)") do
ci = ci + 1
local tc = text:sub(ci, ci)
if i == State.text:get_i() then
if tc ~= "" then
table.insert(text_table, { 1, 1, 1, 1 })
table.insert(text_table, tc)
else
table.insert(text_table, { 0.4, 0.4, 0.4, 1 })
table.insert(text_table, "-")
end
goto continue
elseif i < State.text:get_i() then
if wc == tc then
table.insert(text_table, { 0, 1, 0, 1 }) -- gren
table.insert(text_table, tc)
elseif State.word:find(tc) then
table.insert(text_table, { 1, 1, 0, 1 }) -- yello
table.insert(text_table, tc)
else
-- make em vars for teh colors
table.insert(text_table, { 0.4, 0.4, 0.4, 1 }) -- gray
table.insert(text_table, tc)
end
end
::continue::
end
love.graphics.printf(text_table, width / 2 - 80, 15 * (i - 1), 160)
end
local accum_height = 0
for i, toast in pairs(State.Toasts.items) do
if i <= 3 then
accum_height = accum_height + toast.text:getHeight()
love.graphics.draw(toast.text, 0, height - accum_height)
accum_height = accum_height + 5
end
end
if State.gamestate == "win" then
love.graphics.printf("yu winener", 0, height - 40, width, "center")
elseif State.gamestate == "lose" then
love.graphics.printf("yU FAKINGv LOSER\n IT WAS `"..State.word.."`", 0, height - 40, width, "center")
end
push:finish()
end