humus-old/common/combat/vulnerable.trait.js
2019-11-03 10:43:05 -06:00

290 lines
7.1 KiB
JavaScript

import {Trait} from '@avocado/entity';
import {
behaviorItemFromJSON,
buildInvoke,
buildTraversal,
Context,
} from '@avocado/behavior';
import {hasGraphics, TextNodeRenderer} from '@avocado/graphics';
import {randomNumber, Vector} from '@avocado/math';
import {DamagePacket} from './damage.packet';
export class Vulnerable extends Trait {
static defaultParams() {
const emitDamage = buildInvoke(['entity', 'emitDamageParticle'], [
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._isInvulnerable = false;
this.locks = new Map();
this._tookDamageActions = behaviorItemFromJSON(
this.params.tookDamageActions
);
}
destroy() {
this.locks.clear();
this.damageTickingPromises = [];
}
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);
}
}
}
cleanPackets() {
this.damages = [];
}
damageTextSize(amount) {
const biggest = 16;
const smallest = biggest / 2;
const step = biggest / 6;
if (amount > 999) {
return biggest;
}
else if (amount > 99) {
return smallest + (step * 2);
}
else if (amount > 9) {
return smallest + step;
}
else {
return smallest;
}
}
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 {
isDyingChanged: (_, isDying) => {
this._isInvulnerable = isDying;
},
tookDamage: (damage) => {
if (AVOCADO_SERVER) {
this.damages.push(damage);
this.setDirty();
}
},
};
}
methods() {
return {
emitDamageParticle: (position, damage) => {
const {amount, damageSpec, isDamage} = damage;
const fill = isDamage ? '#FF0000' : '#00FF77';
this.entity.emitParticleJson({
traits: {
darkened: {
params: {
isDarkened: false,
},
},
emitted: {
params: {
alpha: {
start: 1,
end: 0,
},
force: [0, 1],
velocity: [
randomNumber(-0.5, 0.5),
randomNumber(-1.25, -0.75)
],
position,
rotation: {
start: 0,
add: {
min: -0.5,
max: 0.5,
},
},
scale: {
start: 1,
end: 1.25,
},
},
},
existent: {},
layered: {},
listed: {},
positioned: {},
roomed: {},
textual: {
state: {
text: amount,
textStyle: {
fill,
fontFamily: 'joystix',
fontSize: this.damageTextSize(amount),
strokeThickness: 2,
},
},
},
visible: {
state: {
zIndex: 65535,
}
},
},
}).then((particle) => {
this.entity.list.addEntity(particle);
});
},
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);
}
}
}
}
}