humus-old/common/combat/emitter.js

103 lines
2.0 KiB
JavaScript
Raw Normal View History

2019-04-30 14:43:51 -05:00
import {Proton, TextNode} from '@avocado/graphics';
2019-04-19 12:16:28 -05:00
class DamageTextNode extends TextNode {
constructor(damage) {
2019-04-24 02:46:08 -05:00
const {amount} = damage;
2019-04-19 12:16:28 -05:00
super(amount);
2019-04-24 02:46:08 -05:00
this.damage = damage;
}
sizeInPx() {
const {amount} = this.damage;
2019-04-19 12:16:28 -05:00
// Big numbers are literally big.
if (amount > 999) {
2019-04-24 02:46:08 -05:00
return 12;
2019-04-19 12:16:28 -05:00
}
else if (amount > 99) {
2019-04-24 02:46:08 -05:00
return 10;
2019-04-19 12:16:28 -05:00
}
else if (amount > 9) {
2019-04-24 02:46:08 -05:00
return 8;
2019-04-19 12:16:28 -05:00
}
else {
2019-04-24 02:46:08 -05:00
return 6;
2019-04-19 12:16:28 -05:00
}
2019-04-24 02:46:08 -05:00
}
spanClassName() {
const {damageSpec, isDamage} = this.damage;
let className = super.spanClassName();
2019-04-28 22:35:20 -05:00
className += ' affinity-' + damageSpec.affinity;
2019-04-19 12:16:28 -05:00
if (isDamage) {
2019-04-24 02:46:08 -05:00
className += ' is-damage';
2019-04-19 12:16:28 -05:00
}
else {
2019-04-24 02:46:08 -05:00
className += ' is-healing';
2019-04-19 12:16:28 -05:00
}
2019-04-24 02:46:08 -05:00
return className;
2019-04-19 12:16:28 -05:00
}
}
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);
}
2019-04-25 03:43:31 -05:00
destroy() {
this.proton.destroy();
}
2019-04-19 12:16:28 -05:00
emit(position, damage) {
// BUGS
const pz = new Proton.PointZone();
pz.x = position[0];
pz.y = -position[1];
const initializers = [
2019-04-19 12:16:28 -05:00
new Proton.Body(new DamageTextNode(damage)),
new Proton.Position(pz),
new Proton.Mass(1),
new Proton.Life(2),
new Proton.Velocity(
2019-04-28 22:35:20 -05:00
new Proton.Span(50, 90),
new Proton.Vector3D(0, 5, 0),
27.5
),
];
// Heh, bugs.
const rot = new Proton.Rotate(0, 0, 0);
2019-04-29 11:01:52 -05:00
rot.a = new Proton.Span(-.006, .006);
const behaviors = [
2019-04-19 18:33:56 -05:00
new Proton.Alpha(1, .25),
2019-04-19 03:12:50 -05:00
new Proton.Scale(.8, 1.2),
2019-04-28 22:35:20 -05:00
new Proton.Force(0, -0.5, 0),
rot,
];
this.emitter.createParticle(initializers, behaviors);
}
2019-04-19 14:51:05 -05:00
hasParticles() {
2019-04-25 03:43:31 -05:00
return this.particleCount > 0;
}
get particleCount() {
return this.emitter.particles.length;
2019-04-19 14:51:05 -05:00
}
tick(elapsed) {
this.proton.tick(elapsed);
}
}