-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCannonball.js
55 lines (43 loc) · 1.28 KB
/
Cannonball.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';
class Cannonball extends Entity {
constructor(x, y) {
super(x, y, tilesize / 2);
/* Has not been shot yet */
this.fired = false;
/* The cannonball will richochet a limited number of times */
this.totalBounces = 2;
this.bouncesLeft = this.totalBounces;
/* Shoot for magnitude */
this.shootForceMag = 10;
}
reset() {
this.fired = false;
this.bouncesLeft = this.totalBounces;
}
/* Shoot the cannonball */
shoot(from, to) {
let bulletOffset = createVector(this.w / 2, this.h / 2);
let mouse = createVector(to.x, to.y);
from.add(bulletOffset);
mouse.sub(bulletOffset);
this.position.set(from.x - this.w, from.y - this.h);
this.velocity.mult(0);
let shootForce = p5.Vector.sub(mouse, this.position);
shootForce.setMag(this.shootForceMag);
this.applyForce(shootForce);
this.fired = true;
}
hasBounced() {
return this.bouncesLeft < this.totalBounces;
}
update(game) {
if (this.fired) {
super.update(game);
}
}
draw() {
if (this.fired) {
assets.drawImage("seed_spit", this.position.x, this.position.y);
}
}
}