-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bullet.js
executable file
·119 lines (87 loc) · 2.77 KB
/
Bullet.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
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
// ======
// BULLET
// ======
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
// A generic contructor which accepts an arbitrary descriptor object
function Bullet(descr) {
// Common inherited setup logic from Entity
this.setup(descr);
// Make a noise when I am created (i.e. fired)
//this.fireSound.volume = 0.2;
//this.fireSound.play();
/*
// Diagnostics to check inheritance stuff
this._bulletProperty = true;
console.dir(this);
*/
}
Bullet.prototype = new Entity();
// HACKED-IN AUDIO (no preloading)
Bullet.prototype.laserAlien = new Audio("sounds/laserAlien.wav");
Bullet.prototype.laserAlien.volume = 0.2;
Bullet.prototype.laserPlayer = new Audio("sounds/laserPlayer.wav");
Bullet.prototype.laserPlayer.volume = 0.2;
// Initial, inheritable, default values
Bullet.prototype.rotation = 0;
Bullet.prototype.cx = 200;
Bullet.prototype.cy = 200;
Bullet.prototype.velX = 1;
Bullet.prototype.velY = 1;
// Convert times from milliseconds to "nominal" time units.
//Bullet.prototype.lifeSpan = 3000 / NOMINAL_UPDATE_INTERVAL;
Bullet.prototype.update = function (du) {
// TODO: YOUR STUFF HERE! --- Unregister and check for death
spatialManager.unregister(this);
if (this._isDeadNow) return entityManager.KILL_ME_NOW;
if (this.cy < 0 || this.cy > g_canvas.height) return entityManager.KILL_ME_NOW;
if (this.velY === 0) {
let shipCoord = entityManager.getShipCoords();
this.cx = shipCoord.x;
return;
}
this.cy += this.velY * du;
//
// Handle collisions
//
var hitEntity = this.findHitEntity();
if (hitEntity) {
var canTakeHit = hitEntity.takeBulletHit;
if (canTakeHit &&
this.type === "enemyBullet" &&
hitEntity.type === "Ship") {
hitEntity.takeBulletHit(this);
this.kill();
}
if (canTakeHit &&
this.type === "playerBullet" &&
hitEntity.type === "Alien") {
hitEntity.takeBulletHit(this);
this.kill();
}
}
spatialManager.register(this);
};
Bullet.prototype.getRadius = function () {
return 4;
};
Bullet.prototype.takeBulletHit = function () {
this.kill();
};
Bullet.prototype.render = function (ctx) {
if (this.type === "playerBullet") {
g_sprites.playerBullet.drawCentredAt(
ctx, this.cx, this.cy, this.rotation
);
}
if (this.type === "enemyBullet") {
g_sprites.enemyBullet.drawCentredAt(
ctx, this.cx, this.cy, this.rotation
);
}
ctx.globalAlpha = 1;
};