-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_ui.lua
More file actions
440 lines (355 loc) · 10.7 KB
/
Copy pathgame_ui.lua
File metadata and controls
440 lines (355 loc) · 10.7 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
UI_X = 8
UI_Y = 12
function draw_ui()
local x = UI_X
local y = UI_Y
local scrnw,scrnh = screen_size()
local xb = GRID_X - 8
local midx = lerp(x, xb, 0.5)
font("big")
-- Timer
local str = max(ceil(game_timer), 0)..'" before end of game'
draw_text(str, GRID_X + GRID_W/2, 2, 1, 0, 22, 23)
-- Current scores
local strs = {}
local total_owned = 0
for i = 1,4 do
total_owned = total_owned + faction_tiles[i]
end
for i=1,4 do
local c = faction_color[i]
local xx = midx + (i-2.5)*22
local n = flr((faction_tiles[i]/total_owned)*1000)/10
local str = n.."%"
draw_text2(str, xx, y-(i%2)*12+6, 1, 0, c, c_lit[c])
end
y = y + 16
line(midx - 48, y, midx+48, y, 22)
y = y + 9
-- Available resources
local fac = my_faction or 1
local c = faction_color[fac]
local cl,cd = c_lit[c], c_drk[c]
xx = midx
draw_text2(faction_res[fac].." $", xx, y, 1, 0, c, cl)
y = y + 11
local n = group_size("unit"..fac)
draw_text2(n.." unit"..(n>1 and "s" or ""), xx, y, 1, 0, c, cl)
y = y + 12
line(midx - 48, y, midx+48, y, 22)
y = y + 41
-- Selected unit panel
if selected then
-- selected:draw(midx,y)
local w,h = 40, 40
local sx,sy = board_to_screen(selected.x, selected.y)
palt(13,false)
draw_to(slct_surf)
draw_surface(render_canvas, 0, 0, sx-w/2, sy-h/2, w, h)
draw_to()
draw_surface(slct_surf, midx-w/2, y-h/2)
palt(13,true)
--draw_frame(320, midx-w/2-4, y-h/2-4, midx+w/2+4, y+h/2+4, true)
draw_frame(320, midx-w/2, y-h/2, midx+w/2, y+h/2)
end
if selected and selected.faction == my_faction then
--font("small")
y = task_log_y+6
function log_task(task)
local info = task_lib[task.type]
local sp = info.sprite
pal(0, 22) spr(sp, x+12, y+2+2)
pal(0, 21) spr(sp, x+12, y+2+1)
pal(0, 0) spr(sp, x+12, y+2)
local str
if info.cost then
str = task.type.."( "..info.cost.."$ )"
else
str = task.type.."()"
end
draw_text2(str, x+20, y, 0, 0, 21, 22)
y = y + 12
end
if selected.task then
--local anim = {'\\','|','/','-','>','|','<','-'}
--local anim = {'\\','|','/','-','>','|','>','-'}
local anim = {'>','|','>','-'}
local c = anim[flr(t*9)%#anim+1]
draw_text2(c, x+2, y, 1, 21, 22, 23)
log_task(selected.task)
end
for t in all(selected.task_queue) do
log_task(t)
end
end
end
tooltips = true
function init_control_ui()
if select_buttons then
for s in all(select_buttons) do
deregister_object(s)
end
end
local x = UI_X
local y = UI_Y + 61
local scrnw,scrnh = screen_size()
local xb = GRID_X - 8
local midx = lerp(x, xb, 0.5)
local w = 96
local h = 14
create_button(midx-w/2, y-8, w, h, nil, {"toggle tooltips"}, function() tooltips = not tooltips end, 'h')
y = y + 8
local w,h = 32, 24
select_buttons = {
create_button(x, y+h/2, w, h, nil, {"prev", "unit"}, function() select_next_entity("unit", true) end, 'q'),
create_button(xb-w, y+h/2, w, h, nil, {"next", "unit"}, function() select_next_entity("unit") end, 'e'),
-- create_button(x, y+h+4, w, h, nil, {"prev", "build."}, function() select_next_entity("building", true) end, 'z'),
-- create_button(xb-w, y+h+4, w, h, nil, {"next", "build."}, function() select_next_entity("building") end, 'x')
}
end
function select_next_entity(ty, prev)
local oi
if selected then oi = selected.id
else oi = 1 end
local i = oi
local k = entity_id
local di = prev and -2 or 0
i = (i+di)%k+1
while oi ~= i do
local s = entities[i]
if s and s.faction == my_faction and s.type == ty then
selected = s
refresh_control_ui(s)
sfx("select")
return
end
i = (i+di)%k+1
end
end
holding = {}
function refresh_control_ui(s)
eradicate_group("control_ui")
if not s then return end
if s.faction ~= my_faction then return end
local x = UI_X
local y = UI_Y + 118
local scrnw,scrnh = screen_size()
local xb = GRID_X - 8
local midx = lerp(x, xb, 0.5)
if s.type == "unit" then
local walk = function(dir)
if holding.build_wall then
client_add_task(s, "build_wall")
elseif holding.juice then
client_add_task(s, "juice")
end
client_add_task(s, "walk_"..dir)
end
local w,h = 16, 16
create_button(x+w/2+1, y, w, h, 162, nil, function() walk("up") end, 'w', "control_ui", "walk")
create_button(x, y+h+2, w, h, 160, nil, function() walk("left") end, 'a', "control_ui", "walk")
create_button(x+w+2, y+h+2, w, h, 161, nil, function() walk("right") end, 'd', "control_ui", "walk")
create_button(x+w/2+1, y+2*h+4, w, h, 163, nil, function() walk("down") end, 's', "control_ui", "walk")
local x = x + w*2 + 8
local w = 80
create_button(x, y, w, h, 169, {"juice"}, function() client_add_task(s, "juice") end, 'r', "control_ui", "juice")
-- create_button(x, y, w, h, 166, {"wall"}, function() client_add_task(s, "build_wall") end, 'r', "control_ui")
create_button(x, y+h+2, w, h, 166, {"wall"}, function() client_add_task(s, "build_wall") end, 'f', "control_ui", "build_wall")
-- create_button(x, y+h+2, w, h, 166, {"$$$ factory"}, function() client_add_task(s, "build_prod", {produce = "resource"}) end, 'f', "control_ui")
create_button(x, y+2*h+4, w, h, 170, {"duplicate"}, function() client_add_task(s, "duplicate") end, 'v', "control_ui", "duplicate")
-- create_button(x, y+2*h+4, w, h, 166, {"unit factory"}, function() client_add_task(s, "build_prod", {produce = "unit"}) end, 'v', "control_ui")
y = y+3*h+10
x = x - 8 - 32
local w = 120
create_button(x, y, w, h, 174, {"cancel last task"}, function() client_add_task(s, -1) end, 'backspace', "control_ui")
create_button(x, y+h+2, w, h, 175, {"clear task queue"}, function() client_add_task(s, -2) end, 'tab', "control_ui")
y = y + 2*h + 8
task_log_y = y
elseif s.type == "building" then
if s.produce == "unit" then
local w,h = 76, 16
create_button(x, y, w, h, 166, {"create unit"}, function() client_add_task(s, "prod_unit") end, 'a', "control_ui")
y = y + h + 10
end
task_log_y = y
end
end
function update_button(s)
local cx, cy = cursor.x, cursor.y
local opressed = s.pressed
local callb = false
if cx > s.x and cx < s.x+s.w and cy > s.y and cy < s.y+s.h then
s.hovered = true
if mouse_btn(0) then
s.pressed = true
else
s.pressed = false
end
if mouse_btnr(0) then
s.callback()
callb = true
end
else
s.hovered = false
s.pressed = false
end
if btn(s.key) then
s.pressed = true
end
if btnr(s.key) then
s.callback()
callb = true
end
if s.pressed and not opressed then
sfx("button", nil, nil, 0.5)
end
if callb then
sfx("button", nil, nil, 1)
if s.cost and faction_res[my_faction] < s.cost then
sfx("nomoney")
end
end
if s.task_type then
holding[s.task_type] = s.pressed
end
end
function draw_button(s)
local fac = my_faction or 1
local fc = faction_color[fac]
local lit
if s.pressed then lit = -1
elseif s.hovered then lit = 1 end
local c,cl,cd
if lit then
c, cl, cd = lighter(fc, lit), lighter(fc, lit+1), lighter(fc, lit-1)
else
c, cl, cd = fc, c_lit[fc], c_drk[fc]
end
local yy = s.y + (s.pressed and 3 or s.hovered and 1 or 0)
local hh = s.h
--rectfill(s.x, yy, s.x+s.w, yy+hh, c)
clip(s.x, s.y, s.w, s.h)
faction_pal(fac, lit)
pal(23, lighter(fc, (lit or 0)+2))
draw_frame(256, s.x, yy, s.x+s.w, yy+hh, true)
faction_pal()
pal(23,23)
clip()
-- line(s.x+1, s.y+s.h, s.x+s.w-1, s.y+s.h, 0)
sspr(8, 152, 8, 2, s.x+1, s.y+s.h, s.w-2, 2, 0, 0, 0)
hh = hh-3
font("small")
if s.sprite then
if s.strs then
pal(0,c)
spr(s.sprite, s.x+8, yy+hh/2-1)
--pal(0,cl)
--spr(s.sprite, s.x+8, yy+hh/2+1)
pal(0,0)
spr(s.sprite, s.x+8, yy+hh/2)
local x = s.x + 16
local y = yy + hh/2 - (#s.strs-1)*0.5*8 - 2
for _,str in ipairs(s.strs) do
draw_text(str, x, y-1, 0, c)
--draw_text(str, x, y+1, 0, cl)
draw_text(str, x, y, 0, 0)
y = y + 8
end
if s.cost then
local x = s.x+s.w-2
local y = yy+hh/2 - 2
local str = s.cost.."$"
draw_text(str, x, y-1, 2, c)
draw_text(str, x, y, 2, 0)
end
else
local x = s.x + s.w/2
local y = yy + hh/2
local sp = s.sprite
if s.task_type == "walk" then
if holding.build_wall then
sp = 166
elseif holding.juice then
sp = 169
end
end
pal(0, c)
spr(sp, x, y-1)
--pal(0,cl)
--spr(s.sprite, x, y+1)
pal(0, 0)
spr(sp, x, y)
end
else
local x = s.x + s.w/2
local y = yy + hh/2 - (#s.strs - 1) * 0.5 * 8 - 2
for _,str in ipairs(s.strs) do
draw_text(str, x, y-1, 1, c)
--draw_text(str, x, y+1, 1, cl)
draw_text(str, x, y, 1, 0)
y = y + 8
end
end
-- if s.hovered and s.key_str then
-- font("small")
-- local x = lerp(8, GRID_X-8, 0.5)
-- local y = 77
-- local str = "Shortcut: ['"..string.upper(s.key_str).."']"
-- draw_text(str, x, y+1, 1, 22)
-- draw_text(str, x, y, 1, 20)
-- end
end
function draw_tooltip()
if not tooltips then return end
for s in group("ui_button") do
if s.hovered then
local strs = { "Shortkey: ['"..string.upper(s.key_str).."']" }
if s.task_type == "juice" or s.task_type == "build_wall" then
add(strs, "Can be held to")
add(strs, "use with movement")
end
font("small")
local w = 0
for str in all(strs) do
w = max(w, str_width(str))
end
w = w + 8
local h = #strs * 10 + 8
local x = flr(cursor.x + 3)
local y = flr(cursor.y - h - 3)
draw_frame(328, x-4, y-4, x+w+4, y+h+4, true)
x = x + w/2
y = y + 4 + 2
for str in all(strs) do
draw_text(str, x, y, 1, 0, 22, 23)
y = y + 10
end
end
end
end
function create_button(x, y, w, h, s, strs, callback, key, reg, task_type)
local s = {
x = x,
y = y,
w = w,
h = h,
sprite = s,
strs = strs,
callback = callback,
task_type = task_type,
hovered = false,
pressed = false,
update = update_button,
draw = draw_button,
regs = {"to_update", "to_draw3", "ui_button", reg}
}
if task_type then
s.cost = task_lib[task_type].cost
end
if key then
s.key_str = key
s.key = get_key_id(key)
end
register_object(s)
return s
end