-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.js
65 lines (60 loc) · 1.81 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
class Sprite {
constructor(animation, x, y){
this.x = x;
this.y = y;
this.attacking = false;
this.attackTime = 0;
this.attackDuration = 0;
this.animation = animation;
this.index = 0;
this.xSpeed = 0;
this.dir = "right";
}
displayAnimation (){
if (this.attacking){
this.show("attack");
this.animate("attack");
}
else if (this.xSpeed == 0){
this.show("idle");
this.animate("idle");
} else {
this.show("run");
this.animate("run");
}
}
show(actionToShow){
let tempX = this.x;
if (this.dir == "left"){
push();
scale(-1, 1);
tempX = -tempX;
}
if (actionToShow == "attack"){
let len = this.animation.attack.length;
let index = floor(this.index) % len;
image(this.animation.attack[index], tempX, this.y);
} else if (actionToShow == "run"){
let len = this.animation.run.length;
let index = floor(this.index) % len;
image(this.animation.run[index], tempX, this.y);
} else if (actionToShow == "idle"){
let len = this.animation.idle.length;
let index = floor(this.index) % len;
image(this.animation.idle[index], tempX, this.y);
}
if (this.dir == "left"){
pop();
}
}
animate(actionToShow){
if (actionToShow == "attack"){
this.animateSpeed = 0.2
} else if (actionToShow == "run"){
this.animateSpeed = 0.3
} else {
this.animateSpeed = 0.3
}
this.index += this.animateSpeed;
}
}