humus-old/common/combat/emitter.js
2019-04-30 14:43:51 -05:00

103 lines
2.0 KiB
JavaScript

import {Proton, TextNode} from '@avocado/graphics';
class DamageTextNode extends TextNode {
constructor(damage) {
const {amount} = damage;
super(amount);
this.damage = damage;
}
sizeInPx() {
const {amount} = this.damage;
// Big numbers are literally big.
if (amount > 999) {
return 12;
}
else if (amount > 99) {
return 10;
}
else if (amount > 9) {
return 8;
}
else {
return 6;
}
}
spanClassName() {
const {damageSpec, isDamage} = this.damage;
let className = super.spanClassName();
className += ' affinity-' + damageSpec.affinity;
if (isDamage) {
className += ' is-damage';
}
else {
className += ' is-healing';
}
return className;
}
}
export class DamageEmitter {
constructor() {
const proton = new Proton();
const emitter = new Proton.Emitter();
proton.addEmitter(emitter);
this.emitter = emitter;
this.proton = proton;
}
addRenderer(renderer) {
this.renderer = renderer;
this.proton.addRender(this.renderer);
}
destroy() {
this.proton.destroy();
}
emit(position, damage) {
// BUGS
const pz = new Proton.PointZone();
pz.x = position[0];
pz.y = -position[1];
const initializers = [
new Proton.Body(new DamageTextNode(damage)),
new Proton.Position(pz),
new Proton.Mass(1),
new Proton.Life(2),
new Proton.Velocity(
new Proton.Span(50, 90),
new Proton.Vector3D(0, 5, 0),
27.5
),
];
// Heh, bugs.
const rot = new Proton.Rotate(0, 0, 0);
rot.a = new Proton.Span(-.006, .006);
const behaviors = [
new Proton.Alpha(1, .25),
new Proton.Scale(.8, 1.2),
new Proton.Force(0, -0.5, 0),
rot,
];
this.emitter.createParticle(initializers, behaviors);
}
hasParticles() {
return this.particleCount > 0;
}
get particleCount() {
return this.emitter.particles.length;
}
tick(elapsed) {
this.proton.tick(elapsed);
}
}