-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.js
executable file
·85 lines (63 loc) · 1.88 KB
/
Entity.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
// ======
// ENTITY
// ======
/*
Provides a set of common functions which can be "inherited" by all other
game Entities.
JavaScript's prototype-based inheritance system is unusual, and requires
some care in use. In particular, this "base" should only provide shared
functions... shared data properties are potentially quite confusing.
*/
"use strict";
/* jshint browser: true, devel: true, globalstrict: true */
/*
0 1 2 3 4 5 6 7 8
12345678901234567890123456789012345678901234567890123456789012345678901234567890
*/
function Entity() {
/*
// Diagnostics to check inheritance stuff
this._entityProperty = true;
console.dir(this);
*/
};
Entity.prototype.setup = function (descr) {
// Apply all setup properies from the (optional) descriptor
for (var property in descr) {
this[property] = descr[property];
}
// Get my (unique) spatial ID
this._spatialID = spatialManager.getNewSpatialID();
// I am not dead yet!
this._isDeadNow = false;
};
Entity.prototype.setPos = function (cx, cy) {
this.cx = cx;
this.cy = cy;
};
Entity.prototype.getPos = function () {
return {posX : this.cx, posY : this.cy};
};
Entity.prototype.getRadius = function () {
return 0;
};
Entity.prototype.getSpatialID = function () {
return this._spatialID;
};
Entity.prototype.kill = function () {
this._isDeadNow = true;
};
Entity.prototype.findHitEntity = function () {
var pos = this.getPos();
return spatialManager.findEntityInRange(
pos.posX, pos.posY, this.getRadius()
);
};
// This is just little "convenience wrapper"
Entity.prototype.isColliding = function () {
return this.findHitEntity();
};
Entity.prototype.wrapPosition = function () {
this.cx = util.wrapRange(this.cx, 0, g_canvas.width);
this.cy = util.wrapRange(this.cy, 0, g_canvas.height);
};