avocado-old/packages/physics/traits/emitted.trait.js
2020-06-23 11:19:59 -05:00

156 lines
3.4 KiB
JavaScript

import {compose} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/entity';
// import {Proton, TextNode} from '@avocado/graphics';
import {Range, Vector} from '@avocado/math';
const decorate = compose(
);
export default class Emitted extends decorate(Trait) {
static behaviorTypes() {
return {
particle: {
type: 'particle',
label: 'Create particle.',
},
};
}
static defaultParams() {
return {
alpha: {
start: 1,
end: 1,
},
force: [0, 0],
listed: true,
mass: 1,
position: null,
rotation: {
start: 0,
add: 0,
},
scale: {
start: 1,
end: 1,
},
transient: true,
ttl: 2,
velocity: {
angle: 0,
magnitude: 0,
},
};
}
static describeParams() {
return {
alpha: {
type: 'object',
label: 'Alpha',
},
force: {
type: 'vector',
label: 'Force',
},
listed: {
type: 'bool',
label: 'Is listed',
},
mass: {
type: 'number',
label: 'Mass',
},
position: {
type: 'vector',
label: 'Position',
},
rotation: {
type: 'object',
label: 'Rotation',
},
scale: {
type: 'object',
label: 'Scale',
},
transient: {
type: 'bool',
label: 'Kill entity after particle finishes',
},
ttl: {
type: 'number',
label: 'Time to live in seconds',
},
velocity: {
type: 'object',
label: 'Velocity',
},
};
}
static type() {
return 'emitted';
}
constructor(entity, params, state) {
super(entity, params, state);
this.alphaStart = new Range(this.params.alpha.start);
this.alphaEnd = new Range(this.params.alpha.end);
this.force = new Vector.Range(this.params.force);
this.mass = this.params.mass;
if (null !== this.params.position) {
this.position = new Vector.Range(this.params.position);
}
else {
this.position = null;
}
this.rotationStart = new Range(this.params.rotation.start);
this.rotationAdd = new Range(this.params.rotation.add);
this.scaleStart = new Range(this.params.scale.start);
this.scaleEnd = new Range(this.params.scale.end);
this.ttl = this.params.ttl;
this.velocityAngle = new Range(this.params.velocity.angle);
this.velocityMagnitude = new Range(this.params.velocity.magnitude);
}
get isTransientParticle() {
return !!this.params.transient;
}
methods() {
return {
particle: () => {
const position = null === this.position ? null : this.position.value();
const force = this.force.value();
return {
alpha: {
start: this.alphaStart.value(),
end: this.alphaEnd.value(),
},
force,
listed: this.params.listed,
mass: this.mass,
position,
rotation: {
start: this.rotationStart.value(),
add: this.rotationAdd.value(),
},
scale: {
start: this.scaleStart.value(),
end: this.scaleEnd.value(),
},
ttl: this.ttl,
velocity: {
angle: this.velocityAngle.value(),
magnitude: this.velocityMagnitude.value(),
},
};
},
};
}
}