-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsprite.js
More file actions
122 lines (104 loc) · 3.47 KB
/
sprite.js
File metadata and controls
122 lines (104 loc) · 3.47 KB
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(sprite_json, x, y, start_state) {
this.sprite_json = sprite_json;
this.x = x;
this.y = y;
this.state = start_state;
this.root_e = "TenderBud";
this.cur_frame = 0;
this.x_v = 20;
this.y_v = 20;
this.inBounds;
this.canvasWidth = canvas.width;
this.canvasHeight = canvas.height;
this.idleTimer;
this.time = 500; //after 0.5 secs then goes idle
}
draw() {
const ctx = canvas.getContext('2d');
if (this.cur_frame < 0 || this.cur_frame >= this.sprite_json[this.root_e][this.state].length) {
this.cur_frame = 0;
}
var currentFrameData = this.sprite_json[this.root_e][this.state][this.cur_frame];
if (!currentFrameData['img']) {
const imagePath = `Penguins/${this.root_e}/${this.state}/${this.cur_frame}.png`;
currentFrameData['img'] = new Image();
currentFrameData['img'].src = imagePath;
}
ctx.drawImage(currentFrameData['img'], this.x, this.y);
this.cur_frame = (this.cur_frame + 1) % this.sprite_json[this.root_e][this.state].length;
if (!this.inBounds) { //if it a hit a boundary
if (this.x >= (this.canvasWidth - currentFrameData['w'])) { //if it hit the right
this.bound_hit('E');
} else if (this.x <= 0) { //if it hit the left
this.bound_hit('W');
} else if (this.y >= (this.canvasHeight - currentFrameData['h'])) { //if it hit the bottom
this.bound_hit('S');
} else if (this.y <= 0) { //if it hit the top
this.bound_hit('N');
}
}
}
set_idle_state() {
const idle_state = ["idle", "idleBackAndForth", "idleBreathing", "idleFall", "idleLayDown", "idleLookAround", "idleLookDown", "idleLookLeft", "idleLookRight", "idleLookUp", "idleSit", "idleSpin", "idleWave"];
const random = Math.floor(Math.random() * idle_state.length);
this.state = idle_state[random];
console.log(this.state);
this.stopTimer(); //stops timer til user presses something
}
bound_hit(side) {
if (side === 'E') {
this.x -= 60;
console.log("hit the right bounds");
} else if (side === 'W') {
this.x += 40;
console.log("hit the left bounds");
} else if (side === 'S') {
this.y -= 20;
console.log("hit the down bounds");
} else if (side === 'N') {
this.y += 20;
console.log("hit the up bounds");
}
this.set_idle_state();
}
startTimer() {
if (this.idleTimer) {
clearTimeout(this.idleTimer);
}
this.idleTimer = setTimeout(() => {
this.set_idle_state();
}, this.time);
}
stopTimer() {
if (this.idleTimer) {
clearTimeout(this.idleTimer);
this.idleTimer = null;
}
}
//mover functions, starts timer when called
moveLeft() {
this.x -= this.x_v;
this.state = "walk_W";
console.log(this.state);
this.startTimer();
}
moveRight() {
this.x += this.x_v;
this.state = "walk_E";
console.log(this.state);
this.startTimer();
}
moveUp() {
this.y -= this.y_v;
this.state = "walk_N";
console.log(this.state);
this.startTimer();
}
moveDown() {
this.y += this.y_v;
this.state = "walk_S";
console.log(this.state);
this.startTimer();
}
}