-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgraphics.lua
669 lines (565 loc) · 21.3 KB
/
graphics.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
------------
--- Graphics Module
--- load the game graphic
-- @module graphics
require("input")
require("constants")
--- upload image file and returns drawn image
-- @function load_img
-- @param image_path image archive
-- @return draw_image drawing image
function load_img(image_path)
local img
assert(image_path)
-- if the path doesn't exist, creates the path
function creates_image()
img = love.image.newImageData("assets/"..(CONFIG_TABLE.assets_dir or
default_assets_dir).."/"..image_path)
end
if pcall(creates_image) then
if CONFIG_TABLE.assets_dir and CONFIG_TABLE.assets_dir ~= default_assets_dir then
print("loaded custom asset: "..CONFIG_TABLE.assets_dir.."/"..image_path)
end
else
img = love.image.newImageData("assets/"..default_assets_dir.."/"..
image_path)
end
local draw_image = love.graphics.newImage(img)
draw_image:setFilter("nearest","nearest")
return assert(draw_image)
end
--- receives an image and draws it
-- @function draw
-- @param img
-- @param x position on the x_axis
-- @param y position on the y_axis
-- @param rot rotation
-- @param x_scale
-- @param y_scale
-- @return nil
function draw(img, x, y, rot, x_scale, y_scale)
assert(img)
assert(x)
assert(y)
rot = rot or 0
x_scale = x_scale or 1
y_scale = y_scale or 1
gfx_q:push({love.graphics.draw, {
img,
x*GFX_SCALE,
y*GFX_SCALE,
rot,
x_scale*GFX_SCALE,
y_scale*GFX_SCALE
}
})
end
--- draws the menu
-- @function menu_draw
-- @param img
-- @param x position on the x_axis
-- @param y position on the y_axis
-- @param rot rotation
-- @param x_scale
-- @param y_scale
-- @return nil
function menu_draw(img, x, y, rot, x_scale,y_scale)
assert(img)
assert(x)
assert(y)
assert(rot)
assert(x_scale > 0, "x_scale can't be negative")
assert(y_scale > 0, "y_scale can't be negative")
rot = rot or 0
x_scale = x_scale or 1
y_scale = y_scale or 1
gfx_q:push({love.graphics.draw, {
img,
x,
y,
rot,
x_scale,
y_scale
}
})
end
--- draws the menu quad, the menu of right-cick
-- @function menu_drawq
-- @param img
-- @param quad quad panel
-- @param x position on the x_axis
-- @param y position on the y_axis
-- @param rot rotation
-- @param x_scale
-- @param y_scale
-- @return nil
function menu_drawq(img, quad, x, y, rot, x_scale, y_scale)
assert(img)
assert(quad)
assert(x)
assert(y)
assert(rot)
assert(x_scale > 0, "x_scale can't be negative")
assert(y_scale > 0, "y_scale can't be negative")
rot = rot or 0
x_scale = x_scale or 1
y_scale = y_scale or 1
gfx_q:push({love.graphics.draw, {
img,
quad,
x,
y,
rot,
x_scale,
y_scale
}
})
end
--- generates rectangles
-- @function grectangle
-- @param mode
-- @param x position on the x_axis
-- @param y position on the y_axis
-- @param width_rectangle
-- @param height_rectangle
-- @return nil
function grectangle(mode, x, y, width_rectangle, height_rectangle)
assert(mode)
assert(x)
assert(y)
assert(width_rectangle > 0, "x_scale can't be negative")
assert(height_rectangle > 0, "y_scale can't be negative")
gfx_q:push({love.graphics.rectangle, {
mode,
x,
y,
width_rectangle,
height_rectangle
}
})
end
--- print message on screen
-- @function gprint
-- @param str message that will be printed
-- @param x position on the x_axis
-- @param y position on the y_axis
-- @return nil
function gprint(str, x, y)
assert(str)
gfx_q:push({love.graphics.print, {
str,
x,
y
}
})
end
--- equals the colors received if the first ones are equal to zero
-- @function set_color
-- @param r red
-- @param g green
-- @param b blue
-- @param a alpha
-- @return nil
function set_color(r, g, b, a)
assert(r)
assert(g)
assert(b)
-- current state of red, green, blue and alpha
local r_current = 0
local g_current = 0
local b_current = 0
local a_current = 0
local MAX_ALPHA
a = a or MAX_ALPHA
-- only do it if this color isn't the same as the previous one
if r_current ~= r or g_current ~= g or b_current ~= b or a_current ~= a then
r_current, g_current, b_current, a_current = r, g, b, a
gfx_q:push({love.graphics.setColor, {r, g, b, a}})
else
-- nothing to do
end
end
local floor = math.floor
--- initiates the graphics of the game
-- @function graphics_init
-- @param nil
-- @return nil
function graphics_init()
IMG_panels = {}
for i = 1, 8 do
IMG_panels[i]={}
for j = 1, 7 do
IMG_panels[i][j]=load_img("panel"..tostring(i)..tostring(j)..".png")
end
end
IMG_panels[9]={}
for j = 1,7 do
IMG_panels[9][j] = load_img("panel00.png")
end
local g_parts = {
"topleft", "botleft", "topright", "botright",
"top", "bot", "left", "right", "face", "pop",
"doubleface", "filler1", "filler2", "flash",
"portrait"
}
IMG_garbage = {}
for _, key in ipairs(characters) do
local imgs = {}
IMG_garbage[key] = imgs
for _, part in ipairs(g_parts) do
imgs[part] = load_img(""..key.."/"..part..".png")
end
end
local IMG_metal_flash = load_img("garbageflash.png")
local IMG_metal = load_img("metalmid.png")
local IMG_metal_l = load_img("metalend0.png")
local IMG_metal_r = load_img("metalend1.png")
IMG_cards = {}
IMG_cards[true] = {}
IMG_cards[false] = {}
for i = 4, 66 do
IMG_cards[false][i] = load_img("combo"..tostring(floor(i/10))..
tostring(i%10)..".png")
end
for i = 2, 13 do
IMG_cards[true][i] = load_img("chain"..tostring(floor(i/10))..
tostring(i%10)..".png")
end
for i = 14, 99 do
IMG_cards[true][i] = load_img("chain00.png")
end
IMG_character_icons = {}
for k, name in ipairs(characters) do
IMG_character_icons[name] = load_img(""..name.."/icon.png")
end
local MAX_SUPPORTED_PLAYERS = 2
IMG_char_sel_cursors = {}
for player_num = 1, MAX_SUPPORTED_PLAYERS do
IMG_char_sel_cursors[player_num] = {}
for position_num = 1, 2 do
local image_name = "char_sel_cur_"..player_num.."P_pos"..
position_num..".png"
IMG_char_sel_cursors[player_num][position_num] = load_img(image_name)
end
end
IMG_char_sel_cursor_halves = {left={}, right={}}
for player_num = 1, MAX_SUPPORTED_PLAYERS do -- position cursor
IMG_char_sel_cursor_halves.left[player_num] = {}
for position_num = 1, 2 do
local cur_width_height = IMG_char_sel_cursors[player_num][position_num]:getDimensions()
local half_width, half_height = cur_width_height/2, cur_width_height/2
IMG_char_sel_cursor_halves["left"][player_num][position_num] =
love.graphics.newQuad(0, 0, half_width, cur_width_height, cur_width_height,
cur_width_height)
end
IMG_char_sel_cursor_halves.right[player_num] = {}
for position_num = 1, 2 do
local cur_width_height = IMG_char_sel_cursors[player_num][position_num]:getDimensions()
local half_width, half_height = cur_width_height/2, cur_width_height/2
IMG_char_sel_cursor_halves.right[player_num][position_num] =
love.graphics.newQuad(half_width,0,half_width,cur_width_height,
cur_width_height, cur_width_height)
end
end
character_display_names = {} -- players names
for k, original_name in ipairs(characters) do -- show names
local file_name = "assets/"..CONFIG_TABLE.assets_dir.."/"..original_name
.."/name.txt"
local name_txt_file = love.filesystem.newFile(file_name)
local open_success, err = name_txt_file:open("r")
local display_name = name_txt_file:read(name_txt_file:getSize())
name_txt_file:close()
if display_name then
character_display_names[original_name] = display_name
else
character_display_names[original_name] = original_name
end
end
for k, v in pairs(character_display_names) do
print(k.." = "..v)
end
character_display_names_to_original_names = {}
for k, v in pairs(character_display_names) do
character_display_names_to_original_names[v] = k
end
end
--- subscribe the method draw_cards of Stack class for draw the cards
-- @function Stack.draw_cards
-- @param self object
-- @return nil
function Stack.draw_cards(self)
assert(self)
for i = self.card_q.first,self.card_q.last do
local card = self.card_q[i]
if card_animation[card.frame] then
local draw_x = (card.x-1) * 16 + self.pos_x
local draw_y = (11-card.y) * 16 + self.pos_y + self.displacement
- card_animation[card.frame]
draw(IMG_cards[card.chain][card.n], draw_x, draw_y)
else
--nothing to do
end
end
end
--- subscribe the method render of Stack class for render
-- @function Stack.render
-- @param self object
-- @return nil
function Stack.render(self)
assert(self)
local ceil = math.ceil --rounding
local mouse_x, mouse_y -- coordinates of mouse
if CONFIG_TABLE.debug_mode then
mouse_x, mouse_y = love.mouse.getPosition()
mouse_x = mouse_y / GFX_SCALE
mouse_y = mouse_y / GFX_SCALE
else
--nothing to do
end
if P1 == self then
draw(IMG_garbage[self.character].portrait, self.pos_x, self.pos_y)
else
draw(IMG_garbage[self.character].portrait, self.pos_x+96, self.pos_y, 0, -1)
end
local shake_idx = #shake_arr - self.shake_time
local shake = ceil((shake_arr[shake_idx] or 0) * 13)
for row = 0, self.height do
for col = 1, self.width do
local panel = self.panels[row][col]
local draw_x = (col-1) * 16 + self.pos_x
local draw_y = (11-(row)) * 16 + self.pos_y + self.displacement - shake
if panel.color ~= 0 and panel.state ~= "popped" then
local draw_frame = 1
if panel.garbage then
local imgs = {flash=IMG_metal_flash}
if not panel.metal then
imgs = IMG_garbage[self.garbage_target.character]
end
if panel.x_offset == 0 and panel.y_offset == 0 then
-- draw the entire block
if panel.metal then
draw(IMG_metal_l, draw_x, draw_y)
draw(IMG_metal_r, draw_x+16*(panel.width-1)+8,draw_y)
for i = 1, 2*(panel.width-1) do
draw(IMG_metal, draw_x+8*i, draw_y)
end
else
local height, width = panel.height, panel.width
-- highest possible height
local top_y = draw_y - (height-1) * 16
-- verifies that the resulting height is odd,
local odd = ((height-(height%2))/2)%2==0
for i = 0, height-1 do
for j = 1, width-1 do
draw((odd or height<3) and imgs.filler1 or
imgs.filler2, draw_x+16*j-8, top_y+16*i)
odd = not odd
end
end
if height % 2 == 1 then
draw(imgs.face, draw_x+8*(width-1),
top_y+16*((height-1)/2))
else
draw(imgs.doubleface, draw_x+8*(width-1),
top_y+16*((height-2)/2))
end
draw(imgs.left, draw_x, top_y, 0, 1, height*16)
draw(imgs.right, draw_x+16*(width-1)+8, top_y, 0,
1, height*16)
draw(imgs.top, draw_x, top_y, 0, width*16)
draw(imgs.bot, draw_x, draw_y+14, 0, width*16)
draw(imgs.topleft, draw_x, top_y)
draw(imgs.topright, draw_x+16*width-8, top_y)
draw(imgs.botleft, draw_x, draw_y+13)
draw(imgs.botright, draw_x+16*width-8, draw_y+13)
end
end
if panel.state == "matched" then
local flash_time = panel.initial_time - panel.timer
if flash_time >= self.FRAMECOUNT_FLASH then
if panel.timer > panel.pop_time then
if panel.metal or flash_time % 2 == 1 then
draw(IMG_metal_l, draw_x, draw_y)
draw(IMG_metal_r, draw_x+8, draw_y)
else
draw(imgs.pop, draw_x, draw_y)
end
elseif panel.y_offset == -1 then
draw(IMG_panels[panel.color][
garbage_bounce_table[panel.timer] or 1],
draw_x, draw_y)
end
else
draw(imgs.flash, draw_x, draw_y)
end
end
-- this adds the drawing of state flags to garbage panels
if CONFIG_TABLE.debug_mode then
gprint(panel.state, draw_x*3, draw_y*3)
gprint(panel.chaining and "chaining" or "nah",
draw_x*3, draw_y*3+30)
if panel.match_anyway ~= nil then
gprint(tostring(panel.match_anyway), draw_x*3,
draw_y*3+10)
if panel.debug_tag then
gprint(tostring(panel.debug_tag),
draw_x*3, draw_y*3+20)
end
end
else
-- nothing to do
end
else
if panel.state == "matched" then
local flash_time = self.FRAMECOUNT_MATCH - panel.timer
if flash_time >= self.FRAMECOUNT_FLASH then
draw_frame = 6
elseif flash_time % 2 == 1 then
draw_frame = 1
else
draw_frame = 5
end
elseif panel.state == "popping" then
draw_frame = 6
elseif panel.state == "landing" then
draw_frame = bounce_table[panel.timer + 1]
elseif panel.state == "swapping" then
if panel.is_swapping_from_left then
draw_x = draw_x - panel.timer * 4
else
draw_x = draw_x + panel.timer * 4
end
elseif panel.state == "dimmed" then
draw_frame = 7
elseif self.danger_col[col] then
draw_frame = danger_bounce_table[wrap(1,self.danger_timer+1+floor((col-1)/2),
#danger_bounce_table)]
else
draw_frame = 1
end
draw(IMG_panels[panel.color][draw_frame], draw_x, draw_y)
if CONFIG_TABLE.debug_mode then
gprint(panel.state, draw_x*3, draw_y*3)
if panel.match_anyway ~= nil then
gprint(tostring(panel.match_anyway), draw_x*3,
draw_y*3+10)
if panel.debug_tag then
gprint(tostring(panel.debug_tag), draw_x*3,
draw_y*3+20)
end
end
gprint(panel.chaining and "chaining" or
"nah", draw_x*3, draw_y*3+30)
end
end
end
if CONFIG_TABLE.debug_mode and mx >= draw_x and mx < draw_x + 16 and
my >= draw_y and my < draw_y + 16 then
mouse_panel = {row, col, panel}
draw(IMG_panels[4][1], draw_x+16/3, draw_y+16/3, 0, 0.33333333,
0.3333333)
else
-- nothing to do
end
end
end
local IMG_frame = load_img("frame.png")
local IMG_wall = load_img("wall.png")
draw(IMG_frame, self.pos_x-4, self.pos_y-4)
draw(IMG_wall, self.pos_x, self.pos_y - shake + self.height*16)
if self.mode == "puzzle" then
gprint("Moves: "..self.puzzle_moves, self.score_x, 100)
gprint("Frame: "..self.CLOCK, self.score_x, 130)
else
gprint("Score: "..self.score, self.score_x, 100)
gprint("Speed: "..self.speed, self.score_x, 130)
gprint("Frame: "..self.CLOCK, self.score_x, 145)
if self.mode == "time" then
local time_left = 120 - self.CLOCK/60
local mins = floor(time_left/60) -- currents minutes
local secs = floor(time_left%60) -- currents seconde
gprint("Time: "..string.format("%01d:%02d",mins,secs),
self.score_x, 160)
elseif self.level then
gprint("Level: "..self.level, self.score_x, 160)
end
gprint("Health: "..self.health, self.score_x, 175)
gprint("Shake: "..self.shake_time, self.score_x, 190)
gprint("Stop: "..self.stop_time, self.score_x, 205)
gprint("Pre stop: "..self.pre_stop_time, self.score_x, 220)
if CONFIG_TABLE.debug_mode then
gprint("cleared: "..(self.panels_cleared or 0), self.score_x, 265)
gprint("metal q: "..(self.metal_panels_queued or 0), self.score_x, 280)
if self.danger then
gprint("danger", self.score_x,235)
end
if self.danger_music then
gprint("danger music", self.score_x, 250)
end
if self.input_state then
local iraise, iswap, iup, idown, ileft, iright = unpack(base64decode[self.input_state])
local inputs_to_print = "inputs:"
if iraise then
inputs_to_print = inputs_to_print.."\nraise"
elseif iswap then
inputs_to_print = inputs_to_print.."\nswap"
elseif iup then
inputs_to_print = inputs_to_print.."\nup"
elseif idown then
inputs_to_print = inputs_to_print.."\ndown"
elseif ileft then
inputs_to_print = inputs_to_print.."\nleft"
elseif iright then
inputs_to_print = inputs_to_print.."\nright"
end
else
-- nothing to do
end
gprint(inputs_to_print, self.score_x, 295)
end
if match_type then
gprint(match_type, 375, 15)
else
-- nothing to do
end
end
self:draw_cards()
self:render_cursor()
end
--- scales letterbox
-- @function scale_letterbox
-- @param width
-- @param height
-- @param w_ratio width ratio
-- @param h_ratio height ratio
-- @return ratio_letterbox
function scale_letterbox(width, height, w_ratio, h_ratio)
assert(width > 0, "width can't be negative or zero")
assert(height > 0, "height can't be negative or zero")
local height_ratio = height / h_ratio
local width_ratio = width / w_ratio
if height_ratio > width_ratio then
local scaled_height = h_ratio * width_ratio
return 0, (height - scaled_height) / 2, width, scaled_height
else
-- nothing to do
end
local scaled_width = w_ratio * height_ratio
return assert((width - scaled_width) / 2, 0, scaled_width, height)
end
--- subscribe the method draw_cards of Stack class for render the cursor
-- @function Stack.render_cursor
-- @param self object
-- @return nil
function Stack.render_cursor(self)
local index = (floor(self.CLOCK/16)%2)+1
local x = self.pos_x - 4
local y = self.pos_y - 4
local col = (self.cur_col - 1) * 16
local row = (11 - self.cur_row) * 16
local IMG_cursor = {
load_img("cur0.png"),
load_img("cur1.png")
}
draw(IMG_cursor[index], col+x, row+y+self.displacement)
end