-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.js
71 lines (62 loc) · 1.87 KB
/
classes.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
// creation of class so that player could move
class Sprite {
constructor({ position, image , frames = {max:1} , sprites}) {
this.position = position;
this.image = image;
// this is done in order to add moving animation as there are four characters bundled together
this.frames = {...frames , val:0 , elapsed:0}
this.image.onload = () =>{
this.width = this.image.width/this.frames.max
this.heigth = this.image.height
}
this.moving = false;
this.sprites = sprites;
}
draw() { // this method is used to draw the bg image which was done earlier statically
context.drawImage(
this.image,
// the below 4 line are about cropping image
this.frames.val * this.width, // x position of the image from where to crop
0,
this.image.width / this.frames.max,
this.image.height,
// these are the placing of the image
this.position.x,
this.position.y,
this.image.width / this.frames.max,
this.image.height
);
if(this.moving){
if(this.frames.max > 1)
{
this.frames.elapsed+=1;
}
if(this.frames.elapsed % 10 === 0)
{
// we need this value till 3 as the next image when multiplied will be empty space
if(this.frames.val < this.frames.max -1 )
{
this.frames.val++;
}
else
{
this.frames.val = 0;
}
}
}
}
}
class boundary{
static width = 66;
static height = 66;
constructor({position}){
this.position = position;
this.width = 66;
this.height = 66;
}
draw()
{
context.fillStyle = "rgba(255,0,0,0)";
context.fillRect(this.position.x , this.position.y , this.width , this.height);
}
}