2019-03-24 01:16:24 -05:00
|
|
|
import {compose, virtualize} from '@avocado/core';
|
2019-03-24 04:03:15 -05:00
|
|
|
import {Vector} from '@avocado/math';
|
2019-03-24 01:16:24 -05:00
|
|
|
|
|
|
|
const decorate = compose(
|
|
|
|
virtualize([
|
|
|
|
'addBody',
|
|
|
|
'createBody',
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
|
2019-03-27 16:18:27 -05:00
|
|
|
export class AbstractWorld extends decorate(class {}) {
|
2019-03-24 01:16:24 -05:00
|
|
|
|
|
|
|
constructor() {
|
2019-03-27 16:18:27 -05:00
|
|
|
super();
|
2019-03-24 01:16:24 -05:00
|
|
|
this.entities = new Map();
|
2019-04-16 14:04:11 -05:00
|
|
|
this.entitiesList = [];
|
2019-03-24 01:16:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
associateBodyWithEntity(body, entity) {
|
|
|
|
this.entities.set(body, entity);
|
2019-04-16 14:04:11 -05:00
|
|
|
this.entitiesList.push(entity);
|
2019-03-24 01:16:24 -05:00
|
|
|
body.position = entity.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeBody(body) {
|
2019-04-16 14:04:11 -05:00
|
|
|
if (this.entities.has(body)) {
|
|
|
|
const entity = this.entities.get(body);
|
|
|
|
this.entities.delete(body);
|
|
|
|
const index = this.entitiesList.indexOf(entity);
|
|
|
|
if (-1 !== index) {
|
|
|
|
this.entitiesList.splice(index, 1);
|
|
|
|
}
|
|
|
|
}
|
2019-03-24 01:16:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
tick(elapsed) {
|
|
|
|
// Propagate position updates.
|
2019-04-16 14:04:11 -05:00
|
|
|
for (let i = 0; i < this.entitiesList.length; ++i) {
|
|
|
|
const entity = this.entitiesList[i];
|
2019-04-16 18:02:07 -05:00
|
|
|
const entityPosition = entity.position;
|
|
|
|
const bodyPosition = entity.body.position;
|
2019-04-16 14:04:11 -05:00
|
|
|
// Hot.
|
2019-04-16 18:02:07 -05:00
|
|
|
if (
|
|
|
|
entityPosition[0] === bodyPosition[0]
|
|
|
|
&& entityPosition[1] === bodyPosition[1]
|
|
|
|
) {
|
2019-03-24 04:03:15 -05:00
|
|
|
continue;
|
|
|
|
}
|
2019-03-24 01:16:24 -05:00
|
|
|
entity.position = entity.body.position;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|