-
Notifications
You must be signed in to change notification settings - Fork 1
/
Sprite.js
122 lines (114 loc) · 4.28 KB
/
Sprite.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class Sprite {
constructor(config) {
this.image = new Image();
this.image.src = config.src;
this.image.onload = () => {
this.isLoaded = true;
}
this.closed = false;
//Shadow
this.shadow = new Image();
this.shadow.src = "./images/shadow.png";
this.shadow.onload = () => {
this.isShadowLoaded = true;
}
this.useShadow = true;
//Configure animation state
this.animations = config.animations || {
"idle-down": [[0, 0]],
"idle-up": [[0, 1]],
"idle-right": [[0, 3]],
"idle-left": [[0, 2]],
"walk-down": [[3,0],[1,0],[2,0],[0,0]],
"walk-up": [[0,1],[2,1],[1,1],[3,1]],
"walk-right": [[0,3],[2,3],[1,3],[3,3]],
"walk-left": [[0,2],[2,2],[1,2],[3,2]]
}
this.currentAnimation = config.currentAnimation || "walk-down";
this.currentAnimationFrame = 0;
this.animationFrameLimit = config.animationFrameLimit || 8;
this.animationFrameProgress = this.animationFrameLimit
this.gameObject = config.gameObject;
this.message = null;
}
get frame() {
return this.animations[this.currentAnimation][this.currentAnimationFrame]
}
setAnimation(key) {
if (this.currentAnimation !== key) {
this.currentAnimation = key;
this.currentAnimationFrame = 0;
this.animationFrameProgress = this.animationFrameLimit;
}
}
updateAnimationProgress() {
if (this.animationFrameProgress > 0) {
this.animationFrameProgress -= 1;
return;
}
this.animationFrameProgress = this.animationFrameLimit;
this.currentAnimationFrame += 1;
if(this.animations[this.currentAnimation][this.currentAnimationFrame] == undefined) {
this.currentAnimationFrame = 0;
}
}
checkForMessage() {
if (this.gameObject.x == utils.withGrid(15) && this.gameObject.y == utils.withGrid(7)) {
if (this.closed == false) {
this.closed == true;
this.message = new TextMessage({text: "1st answer to question", container: document.querySelector(".screen-container")});
this.message.init();
}
} else if (this.gameObject.x == utils.withGrid(8) && this.gameObject.y == utils.withGrid(3)) {
if (this.closed == false) {
this.closed == true;
this.message = new TextMessage({text: "2nd answer to question", container: document.querySelector(".screen-container")});
this.message.init();
}
} else if (this.gameObject.x == utils.withGrid(9) && this.gameObject.y == utils.withGrid(6)) {
if (this.closed == false) {
this.closed == true;
this.message = new TextMessage({text: "3rd answer to question", container: document.querySelector(".screen-container")});
this.message.init();
}
} else if (this.gameObject.x == utils.withGrid(10) && this.gameObject.y == utils.withGrid(4)) {
if (this.closed == false) {
this.closed == true;
this.message = new TextMessage({text: "4th answer to question", container: document.querySelector(".screen-container")});
this.message.init();
}
} else if (this.gameObject.x == utils.withGrid(13) && this.gameObject.y == utils.withGrid(2)) {
if (this.closed == false) {
this.closed == true;
this.message = new TextMessage({text: "5th answer to question", container: document.querySelector(".screen-container")});
this.message.init();
}
} else {
this.closed == false
if (this.message != null) {
console.log("here")
this.message.done();
} this.message = null;
}
}
draw(ctx, cameraPerson) {
const x = this.gameObject.x + utils.withGrid(10.5) - cameraPerson.x;
const y = this.gameObject.y + utils.withGrid(6) - cameraPerson.y;
this.isShadowLoaded && ctx.drawImage(
this.shadow,
0,0,
48,48,
x+8, y+1.5,
48,48)
const [frameX, frameY] = this.frame;
this.isLoaded && ctx.drawImage(
this.image,
frameX * 48,frameY * 48,
48,48,
x,y,
48,48
)
this.updateAnimationProgress();
this.checkForMessage();
}
}