-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity.js
65 lines (53 loc) · 1.64 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
(function () {
'use strict';
function Entity(x, y, width, height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.index = 0;
this.score = 0;
this.isGone = false;
}
Entity.prototype.move = function (initX, finalX, height) {
this.y += this.speedY;
if (this.y > height && !this.isGone) {
this.isGone = true;
this.emitEvent('entityIsGone', { index: this.index, score: this.score });
}
};
Entity.prototype.draw = function (ctx) {
throw Error('Draw must be implemented by Entity child');
};
Entity.prototype.crashWith = function (other) {
if (!(other instanceof Entity))
throw Error('Argument must be an Entity');
var myPosition = this.getPosition();
var otherPosition = other.getPosition();
var diff = 5;
return (myPosition.bottom - otherPosition.top > diff)
&& (otherPosition.bottom - myPosition.top > diff)
&& (myPosition.right - otherPosition.left > diff)
&& (otherPosition.right - myPosition.left > diff);
};
Entity.prototype.getPosition = function () {
return {
left: this.x,
right: this.x + this.width,
top: this.y,
bottom: this.y + this.height
};
};
Entity.prototype.crashAction = function () {
throw Error('CrashAction must be implemented by Entity child');
};
Entity.prototype.emitEvent = function (eventName, detail) {
var event = new CustomEvent(eventName, { detail: detail });
document.dispatchEvent(event);
};
// static properties
Entity.obstacles = 0;
Entity.prototype.speedX = 0;
Entity.prototype.speedY = 0;
window.Entity = Entity;
})();