humus-old/common/combat/vulnerable.trait.js
2019-09-30 22:28:55 -05:00

265 lines
6.4 KiB
JavaScript

import * as I from 'immutable';
import {Trait} from '@avocado/entity';
import {
behaviorItemFromJSON,
buildInvoke,
buildTraversal,
Context,
} from '@avocado/behavior';
import {hasGraphics, TextNodeRenderer} from '@avocado/graphics';
import {Vector} from '@avocado/math';
import {DamagePacket} from './damage.packet';
import {DamageEmitter} from './emitter';
export class Vulnerable extends Trait {
static defaultParams() {
const emitDamage = buildInvoke(['entity', 'emitParticle'], [
'damage',
buildTraversal(['entity', 'position']),
buildTraversal(['damage']),
]);
const playDamagingSound = buildInvoke(['damage', 'from', 'playSound'], [
buildTraversal(['damage', 'from', 'damagingSound']),
]);
return {
tookDamageActions: {
type: 'actions',
traversals: [
emitDamage,
playDamagingSound,
],
},
vulnerabilities: undefined,
}
}
static type() {
return 'vulnerable';
}
constructor(entity, params, state) {
super(entity, params, state);
this.damageId = 0;
this.damageTickingPromises = [];
this.damages = [];
this._hasAddedEmitter = false;
this._hasAddedEmitterRenderer = false;
this._isHydrating = false;
this._isInvulnerable = false;
this.locks = new Map();
const tookDamageActionsJSON = this.params.tookDamageActions;
this._tookDamageActions = behaviorItemFromJSON(tookDamageActionsJSON);
}
destroy() {
this.locks.clear();
this.damageTickingPromises = [];
}
hydrate() {
if (AVOCADO_CLIENT) {
this._isHydrating = true;
this.addEmitter();
this.addEmitterRenderer();
}
}
acceptDamage(damage) {
const context = new Context({
damage,
entity: this.entity,
});
const tickingPromise = this._tookDamageActions.tickingPromise(
context
);
tickingPromise.then(() => {
context.destroy();
});
this.damageTickingPromises.push(tickingPromise);
}
acceptPacket(packet) {
if (packet instanceof DamagePacket) {
for (let i = 0; i < packet.data.length; ++i) {
const damage = packet.data[i];
if (this.entity.is('listed') && this.entity.list) {
damage.from = this.entity.list.findEntity(damage.from);
}
else {
damage.from = undefined;
}
this.acceptDamage(damage);
}
}
}
addEmitter() {
if (!this._isHydrating) {
return;
}
if (this._hasAddedEmitter) {
return;
}
if (!this.entity.is('emitter')) {
return;
}
this.entity.addEmitter('damage', new DamageEmitter());
this._hasAddedEmitter = true;
}
addEmitterRenderer() {
if (!this._isHydrating) {
return;
}
if (this._hasAddedEmitterRenderer) {
return;
}
if (!this.entity.is('emitter')) {
return;
}
if (!this.entity.is('staged') || !this.entity.stage) {
return;
}
const renderer = new TextNodeRenderer('.damage', this.entity.stage);
this.entity.addEmitterRenderer('damage', renderer);
this._hasAddedEmitterRenderer = true;
}
cleanPackets() {
this.damages = [];
}
get isInvulnerable() {
return this._isInvulnerable;
}
set isInvulnerable(isInvulnerable) {
this._isInvulnerable = isInvulnerable;
}
packets(informed) {
if (this.damages.length > 0) {
return new DamagePacket(this.damages);
}
}
listeners() {
return {
dying: () => {
this._isInvulnerable = true;
},
stageChanged: () => {
this.addEmitterRenderer();
},
tookDamage: (damage) => {
if (AVOCADO_SERVER) {
this.damages.push(damage);
}
},
traitsChanged: () => {
this.addEmitter();
this.addEmitterRenderer();
},
};
}
methods() {
return {
takeDamageFrom: (entity) => {
const damageSpecs = entity.damageSpecs;
for (let i = 0; i < damageSpecs.length; ++i) {
const damageSpec = damageSpecs[i];
let power = damageSpec.power;
// Check if vulnerable to this affinity.
if (this.params.vulnerabilities) {
if (damageSpec.affinity in this.params.vulnerabilities) {
power *= this.params.vulnerabilities[damageSpec.affinity];
}
}
if (this.locks.has(damageSpec)) {
continue;
}
this.locks.set(damageSpec, damageSpec.lock);
// Give any knockback.
if (damageSpec.knockback) {
if (
this.entity.is('mobile')
&& this.entity.is('positioned')
&& entity.is('positioned')
) {
const difference = Vector.sub(
this.entity.position,
entity.position,
);
const unit = Vector.normalize(difference)
const knockback = Vector.scale(unit, damageSpec.knockback);
this.entity.applyMovement(knockback);
}
}
const variance = Math.random() * damageSpec.variance * 2 - damageSpec.variance;
const difference = power * variance;
// Account for variance past 0, so track if it's damage or not.
let amount = Math.round(power + difference);
let isDamage;
if (power < 0) {
isDamage = false;
if (amount > 0) {
amount = 0;
}
}
else {
isDamage = true;
if (amount < 0) {
amount = 0;
}
}
amount = Math.abs(amount);
const damage = {
id: this.damageId++,
isDamage,
amount,
damageSpec,
from: entity.numericUid,
};
this.entity.emit('tookDamage', damage);
}
},
};
}
tick(elapsed) {
for (let i = 0; i < this.damageTickingPromises.length; ++i) {
this.damageTickingPromises[i].tick(elapsed);
}
if (AVOCADO_SERVER) {
const keyIterator = this.locks.keys();
for (
let thisKey = keyIterator.next();
thisKey.done !== true;
thisKey = keyIterator.next()
) {
const {value: key} = thisKey;
const remaining = this.locks.get(key) - elapsed;
if (remaining <= 0) {
this.locks.delete(key);
}
else {
this.locks.set(key, remaining);
}
}
}
}
}