-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleEnemy.js
55 lines (45 loc) · 1.51 KB
/
SimpleEnemy.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
'use strict';
/* Simple enemy flys across the screen and changes direction when it hits a
* wall. */
class SimpleEnemy extends Entity {
constructor(x, y, s) {
super(x, y, s);
/* 1 is right, -1 is left */
this.dir = random([-1, 1]);
this.walkingForce = 0.5;
this.forwardWalkingForce = createVector(this.walkingForce, 0);
this.reverseWalkingForce = createVector(-this.walkingForce, 0);
this.flyingFrames = new Keyframes([
utils.makeRect( 0, 0),
utils.makeRect(tilesize, 0)
], random(60, 80));
this.flyingFrames.start();
}
update(game) {
/* Move back and forth */
if (this.dir === 1) {
if (this.velocity.x >= -this.walkingForce) {
this.applyForce(this.reverseWalkingForce);
}
} else {
if (this.velocity.x <= this.walkingForce) {
this.applyForce(this.forwardWalkingForce);
}
}
super.update(game);
this.flyingFrames.update();
}
draw(camera) {
let dx = this.position.x;
let dy = this.position.y;
let cf = this.flyingFrames.getCurrent();
/* flip the image to correct direction */
push();
if (this.velocity.x < 0 || cos(this.angle) < 0) {
scale(-1, 1);
dx = (dx * -1) - tilesize;
}
image(assets.getImage("worker_bee"), floor(dx), dy, tilesize, tilesize, cf.x, cf.y, tilesize, tilesize);
pop();
}
}