-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathentity.js
53 lines (48 loc) · 1.53 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
"use strict";
var util = require('util');
var events = require('events');
var Vector = require('./vector');
var Entity = function Entity(position, velocity) {
events.EventEmitter.call(this);
this.position = position || Vector.zero;
this.velocity = velocity || Vector.zero;
this.forces = {};
this._id = -1;
};
util.inherits(Entity, events.EventEmitter);
Entity.allowInteraction = function(a, b) {
var allow = true;
var cancelled = function(x) { if(x) { allow = false; } else return !allow; }
a.emit('interaction', b, cancelled)
b.emit('interaction', a, cancelled)
return allow;
}
Object.defineProperty(Entity.prototype, 'id', {
get: function() { return 'e' + this._id; }
});
Object.defineProperty(Entity.prototype, 'acceleration', {
get: function() {
return Object.reduce(this.forces, function sumVectors(total, current) {
if(current instanceof Vector)
return current.isFinite() ? total.plusEquals(current) : total;
else if(current instanceof Array)
return current.reduce(sumVectors, total);
else if(current instanceof Object)
return Object.reduce(current, sumVectors, total);
else
return total;
}, Vector.zero).overEquals(this.mass);
}
});
Entity.prototype.update = function(dt) {
this.velocity.plusEquals(this.acceleration.times(dt))
this.velocity.forceMaxLength(1000);
this.position.plusEquals(this.velocity.times(dt))
return this;
};
Entity.prototype.clearForces = function(dt) {
this.forces = {}
}
Entity.prototype.mass = 1;
Entity.prototype.touches = function() { return false; };
module.exports = Entity;