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

251 lines
5.3 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'),
);
2020-06-15 17:26:20 -05:00
export default class Spawner extends decorate(Trait) {
2019-04-21 01:30:03 -05:00
2020-06-19 17:59:14 -05:00
static behaviorContextTypes() {
2020-06-19 15:31:35 -05:00
return {
2020-06-19 22:01:45 -05:00
killAllChildren: {
type: 'void',
label: 'Kill all spawned children',
},
2020-06-19 15:31:35 -05:00
spawn: {
type: 'entity',
label: 'Spawn $1 with $2 extensions.',
args: [
['key', {
type: 'string',
}],
['json', {
type: 'object',
}],
],
},
spawnAt: {
type: 'entity',
label: 'Spawn $1 as $2 with $3 extensions.',
args: [
['key', {
type: 'string',
}],
['position', {
type: 'vector',
}],
['json', {
type: 'object',
}],
],
},
spawnRaw: {
type: 'entity',
label: 'Spawn $1.',
args: [
['json', {
type: 'object',
}],
]
},
spawnRawAt: {
type: 'entity',
label: 'Spawn $1 at $2.',
args: [
['position', {
type: 'vector',
}],
['json', {
type: 'object',
}],
]
},
};
}
2019-04-21 01:30:03 -05:00
static defaultParams() {
return {
spawns: {},
};
}
2020-06-19 22:01:45 -05:00
static describeParams() {
return {
spawns: {
type: 'object',
label: 'Entities that may be spawned',
},
};
}
2019-04-21 01:30:03 -05:00
static defaultState() {
return {
isSpawning: true,
maxSpawns: Infinity,
};
}
2020-06-19 22:01:45 -05:00
static describeState() {
return {
isSpawning: {
type: 'bool',
label: 'Is spawning',
},
maxSpawns: {
type: 'number',
label: 'Maximum concurrent spawns',
},
};
}
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
};
}
}