-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathassets.js
54 lines (46 loc) · 1.27 KB
/
assets.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
const sprites = {};
let assetsStillLoading = 0;
function assetsLoadingLoop (callback) {
if (assetsStillLoading) {
requestAnimationFrame(assetsLoadingLoop.bind(this, callback));
} else {
callback();
}
}
function loadSprite(fileName) {
assetsStillLoading++;
const spriteImage = new Image();
spriteImage.src = ".\\assets\\" + fileName;
spriteImage.onload = function () {
assetsStillLoading--;
}
return spriteImage;
}
function loadAssets (callback) {
// loading assets
sprites.background = loadSprite('spr_background.png');
sprites.stick = loadSprite('spr_stick.png');
sprites.whiteBall = loadSprite('spr_ball_white.png');
sprites.redBall = loadSprite('spr_ball_red.png');
sprites.yellowBall = loadSprite('spr_ball_yellow.png');
sprites.blackBall = loadSprite('spr_ball_black.png');
assetsLoadingLoop(callback);
}
function getBallSpriteByColor (color) {
switch (color) {
case Color.red:
return sprites.redBall;
case Color.yellow:
return sprites.yellowBall;
case Color.black:
return sprites.blackBall;
case Color.white:
return sprites.whiteBall;
}
}
const Color = {
red: 1,
yellow: 2,
black: 3,
white: 4
}