avocado-old/packages/entity/traits/spawner.trait.js

172 lines
3.8 KiB
JavaScript
Raw Normal View History

import {compose, merge} from '@avocado/core';
2019-04-21 01:30:03 -05:00
import {Vector} from '@avocado/math';
import {StateProperty, Trait} from '../trait';
import {Entity} from '..';
const decorate = compose(
StateProperty('isSpawning', {
track: true,
}),
StateProperty('maxSpawns'),
);
export class Spawner extends decorate(Trait) {
static defaultParams() {
return {
spawns: {},
};
}
static defaultState() {
return {
isSpawning: true,
maxSpawns: Infinity,
};
}
2019-05-05 04:26:35 -05:00
static type() {
return 'spawner';
}
destroy() {
2019-10-08 02:29:24 -05:00
while (this.children.length > 0) {
2019-10-09 03:46:19 -05:00
const child = this.children.pop();
if (child) {
this.removeChild(child);
}
2019-10-08 02:29:24 -05:00
}
}
constructor(entity, params, state) {
super(entity, params, state);
2019-04-21 01:30:03 -05:00
this.children = [];
2019-10-08 02:29:24 -05:00
this.childrenListeners = new Map();
2019-05-04 14:06:47 -05:00
this.spawnJSONs = this.params.spawns;
2019-04-21 01:30:03 -05:00
}
2019-10-08 03:24:20 -05:00
augmentJSONWithPosition(json, position) {
if (!json.traits) {
json.traits = {};
}
if (!json.traits.positioned) {
json.traits.positioned = {};
}
if (!json.traits.positioned.state) {
json.traits.positioned.state = {};
}
json.traits.positioned.state.x = position[0];
json.traits.positioned.state.y = position[1];
return json;
}
destinationEntityList() {
if (
this.entity.is('listed')
&& this.entity.list
) {
return this.entity.list;
}
if (
this.entity.wielder
&& this.entity.wielder.is('listed')
&& this.entity.wielder.list
) {
return this.entity.wielder.list;
}
}
2019-10-01 20:37:08 -05:00
maySpawn() {
if (this.maxSpawns <= this.children.length) {
return false;
}
2019-10-08 03:24:20 -05:00
if (!this.destinationEntityList()) {
return false;
}
2019-10-01 20:37:08 -05:00
return true;
}
2019-10-08 02:29:24 -05:00
removeChild(child) {
const index = this.children.indexOf(child);
if (-1 !== index) {
this.children.splice(index, 1);
const listener = this.childrenListeners.get(child);
child.off('destroy', listener);
this.childrenListeners.delete(child);
}
}
2019-04-21 01:30:03 -05:00
listeners() {
return {
2019-10-10 01:27:02 -05:00
isDyingChanged: (_, isDying) => {
this.isSpawning = !isDying;
2019-04-21 01:30:03 -05:00
},
};
}
methods() {
return {
killAllChildren: () => {
2019-10-01 01:46:13 -05:00
// Juggle children since this may cause splices and mess up the array.
const children = this.children.slice(0);
for (let i = 0; i < children.length; i++) {
children[i].destroyGently();
2019-04-21 01:30:03 -05:00
}
},
spawn: (key, json = {}) => {
2019-10-01 20:37:08 -05:00
if (!this.maySpawn()) {
2019-04-21 01:30:03 -05:00
return;
}
const spawnJSON = this.spawnJSONs[key];
if (!spawnJSON) {
return;
}
2019-10-08 03:24:20 -05:00
return this.entity.spawnRaw(merge({}, spawnJSON, json));
},
spawnAt: (key, position, json = {}) => {
if (!this.maySpawn()) {
return;
}
json = this.augmentJSONWithPosition(json, position);
return this.entity.spawn(key, json);
},
spawnRaw: (json) => {
if (!this.maySpawn()) {
return;
}
// Add null to children to prevent race.
const childIndex = this.children.length;
this.children.push(null);
2019-10-08 03:24:20 -05:00
const list = this.destinationEntityList();
return Entity.loadOrInstance(json).then((child) => {
2019-10-08 02:29:24 -05:00
this.children[childIndex] = child;
2019-10-08 03:24:20 -05:00
// Listen for destroy event.
2019-10-08 02:29:24 -05:00
const listener = this.removeChild.bind(this, child);
this.childrenListeners.set(child, listener);
child.once('destroy', listener);
2019-10-08 03:24:20 -05:00
// Add child to list.
2019-10-08 02:29:24 -05:00
list.addEntity(child);
return child;
});
2019-04-21 01:30:03 -05:00
},
2019-10-08 03:24:20 -05:00
spawnRawAt: (position, json = {}) => {
if (!this.maySpawn()) {
return;
}
2019-10-08 03:24:20 -05:00
json = this.augmentJSONWithPosition(json, position);
return this.entity.spawnRaw(json);
},
2019-04-21 01:30:03 -05:00
};
}
}