-
Notifications
You must be signed in to change notification settings - Fork 0
/
draw_canvas.js
104 lines (93 loc) · 2.52 KB
/
draw_canvas.js
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
// Globals
let ctx = null;
let enemy_sprite = null;
let player_sprite = null;
let target_sprite = null;
let poop_sprite = null;
let game_state_icon = null;
let tile_size = 0;
function initializeCanvas() {
ctx = canvas.getContext('2d');
function loadSymbol(code) {
let img = new Image();
let DOMURL = window.URL || window.webkitURL || window;
let data = '<svg xmlns="http://www.w3.org/2000/svg"' +
' width="20" height="20">' +
'<foreignObject font-size="15" width="20" height="20"> ' +
code +
' </foreignObject>' +
'</svg>';
let svg = new Blob([data], {type: 'image/svg+xml'});
let url = DOMURL.createObjectURL(svg);
img.src = url;
return img;
}
// Load Sprites & Icons
enemy_sprite = loadSymbol('💀');
player_sprite = loadSymbol('😃');
target_sprite = loadSymbol('⭐');
poop_sprite = loadSymbol('💩');
game_state_icon = {
"Launch": loadSymbol('▶'),
// "Play": null,
"Paused": loadSymbol('⏸'),
"Restart" : loadSymbol('🔄')
}
}
function drawSprite(sprite, x, y) {
ctx.drawImage(
sprite,
tile_size * x + 0.5 * sprite.width,
tile_size * y + 0.5 * sprite.height,
sprite.width,
sprite.height);
}
function drawCanvas() {
ctx.fillStyle = '#44444488';
ctx.fillRect(0, 0, canvas.width, canvas.height);
// Score
ctx.font = "100px Arial";
ctx.fillStyle = "#888";
let score_string = score.toString();
let score_width = ctx.measureText(score_string).width;
ctx.fillText(
score_string,
canvas.width/2 - score_width/2,
100);
// Player
let ps = (game_state != "Restart") ? player_sprite : poop_sprite;
drawSprite(ps, player.x, player.y);
// Target
drawSprite(target_sprite, target.x, target.y);
// Enemies
for (let enemy of enemies) {
drawSprite(enemy_sprite, enemy.x, enemy.y);
}
// Particles
for (let particle of particles) {
let scale = 0.75 * 1/(Math.max(1, particle.vy - 1.2*PARTICLE_SPEED));
let width = scale * target_sprite.width;
let height = scale * target_sprite.height;
ctx.drawImage(
target_sprite,
tile_size * particle.x + 0.5 * width,
tile_size * particle.y + 0.5 * height,
width,
height);
}
if (game_state != "Play") {
ctx.fillStyle = "#aaa";
ctx.globalAlpha = 0.5;
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.globalAlpha = 1.0;
let icon = game_state_icon[game_state];
let icon_width = icon.width * 5;
let icon_height = icon.height * 5;
ctx.drawImage(
icon,
canvas.width/2 - icon_width/2,
canvas.height/2 - icon_height/2,
icon_width,
icon_height);
}
}