-
Notifications
You must be signed in to change notification settings - Fork 0
/
bacon.js
44 lines (41 loc) · 1.42 KB
/
bacon.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
// Module for collectible bacon
import {randInt} from './randint.js';
import {Player} from './player.js';
export class Bacon {
constructor(game){
this.game = game;
this.image = document.getElementById('bacon');
this.width = 33;
this.height = 36;
this.x = ((this.game.width) + ((randInt(3)) * this.width));
this.y = this.game.height - (this.height * (randInt(3))+this.height);
this.frameX = 0;
this.frameY = 0;
this.fps = 20;
this.frameInterval = 1000/this.fps;
this.frameTimer = 0;
this.speedX = -3.5;
this.maxFrame = 0;
this.markedForDeletion = false;
this.player = Player;
this.player.x = Player.x;
this.player.y = Player.y;
}
update(deltaTime){
// Controls bacon's movement across screen
this.x += this.speedX;
if (this.frameTimer > this.frameInterval){
this.frameTimer = 0;
if (this.frameX < this.maxFrame) this.frameX++;
else this.frameX = 0;
} else {
this.frameTimer += deltaTime;
}
// Check if bacon is off screen
if (this.x + this.width < 0) this.markedForDeletion = true;
}
draw(context){
// Bacon image
context.drawImage(this.image, this.frameX * this.width, this.frameY * this.height, this.width, this.height, this.x, this.y, this.width, this.height);
}
}