-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRock.js
117 lines (86 loc) · 2.63 KB
/
Rock.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
// ====
// ROCK
// ====
"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 Rock(descr) {
// Common inherited setup logic from Entity
this.setup(descr);
this.randomisePosition();
this.randomiseVelocity();
// Default sprite and scale, if not otherwise specified
this.sprite = this.sprite || g_sprites.rock;
this.scale = this.scale || 1;
/*
// Diagnostics to check inheritance stuff
this._rockProperty = true;
console.dir(this);
*/
};
Rock.prototype = new Entity();
Rock.prototype.randomisePosition = function () {
// Rock randomisation defaults (if nothing otherwise specified)
this.cx = this.cx || Math.random() * g_canvas.width;
this.cy = this.cy || Math.random() * g_canvas.height;
this.rotation = this.rotation || 0;
};
Rock.prototype.randomiseVelocity = function () {
var MIN_SPEED = 20,
MAX_SPEED = 70;
var speed = util.randRange(MIN_SPEED, MAX_SPEED) / SECS_TO_NOMINALS;
var dirn = Math.random() * consts.FULL_CIRCLE;
this.velX = this.velX || speed * Math.cos(dirn);
this.velY = this.velY || speed * Math.sin(dirn);
var MIN_ROT_SPEED = 0.5,
MAX_ROT_SPEED = 2.5;
this.velRot = this.velRot ||
util.randRange(MIN_ROT_SPEED, MAX_ROT_SPEED) / SECS_TO_NOMINALS;
};
Rock.prototype.update = function (du) {
// TODO: YOUR STUFF HERE! --- Unregister and check for death
this.cx += this.velX * du;
this.cy += this.velY * du;
this.rotation += 1 * this.velRot;
this.rotation = util.wrapRange(this.rotation,
0, consts.FULL_CIRCLE);
this.wrapPosition();
// TODO: YOUR STUFF HERE! --- (Re-)Register
};
Rock.prototype.getRadius = function () {
return this.scale * (this.sprite.width / 2) * 0.9;
};
// HACKED-IN AUDIO (no preloading)
Rock.prototype.splitSound = new Audio(
"sounds/rockSplit.ogg");
Rock.prototype.evaporateSound = new Audio(
"sounds/rockEvaporate.ogg");
Rock.prototype.takeWireHit = function () {
this.kill();
if (this.scale > 0.25) {
this._spawnFragment();
this._spawnFragment();
this.splitSound.play();
} else {
this.evaporateSound.play();
}
};
Rock.prototype._spawnFragment = function () {
entityManager.generateRock({
cx : this.cx,
cy : this.cy,
scale : this.scale /2
});
};
Rock.prototype.render = function (ctx) {
var origScale = this.sprite.scale;
// pass my scale into the sprite, for drawing
this.sprite.scale = this.scale;
this.sprite.drawWrappedCentredAt(
ctx, this.cx, this.cy, this.rotation
);
};