refactor: damaging particles

This commit is contained in:
cha0s 2019-11-09 16:46:03 -06:00
parent ea504785dc
commit 2d5cd2acfb
4 changed files with 133 additions and 14 deletions

View File

@ -1,7 +1,8 @@
import * as I from 'immutable'; import * as I from 'immutable';
import {compose} from '@avocado/core'; import {compose, merge} from '@avocado/core';
import {StateProperty, Trait} from '@avocado/entity'; import {StateProperty, Trait} from '@avocado/entity';
import {fromRad, normalizeAngleRange, Vector} from '@avocado/math';
import {AFFINITY_PHYSICAL} from './constants'; import {AFFINITY_PHYSICAL} from './constants';
@ -70,6 +71,46 @@ export class Damaging extends decorate(Trait) {
} }
} }
hooks() {
return {
particles: () => {
return {
damaging: {
traits: {
emitted: {
params: {
alpha: {
start: 1,
end: .2,
},
rotation: {
start: 0,
add: {
min: -0.5,
max: 0.5,
},
},
scale: {
start: 1,
end: 1.25,
},
ttl: .2,
},
},
existent: {},
layered: {},
listed: {},
positioned: {},
roomed: {},
visible: {},
},
},
};
},
};
}
listeners() { listeners() {
const listeners = {}; const listeners = {};
if (AVOCADO_SERVER) { if (AVOCADO_SERVER) {
@ -83,6 +124,35 @@ export class Damaging extends decorate(Trait) {
methods() { methods() {
return { return {
emitDamagingParticles: (other, json = {}) => {
const diff = Vector.sub(this.entity.position, other.position);
const velocityAngle = Vector.toAngle(Vector.normalize(diff));
const [fromAngle, toAngle] = normalizeAngleRange(
velocityAngle - Math.PI / 8,
velocityAngle + Math.PI / 8,
);
this.entity.emitParticle('damaging', merge(
{},
{
traits: {
emitted: {
params: {
position: other.position,
velocity: {
angle: {
min: 450 - fromRad(fromAngle),
max: 450 - fromRad(toAngle),
},
magnitude: 0.5,
},
},
},
},
},
json,
));
},
setDoesDamage: (entity) => { setDoesDamage: (entity) => {
const index = this._doesNotDamage.indexOf(entity); const index = this._doesNotDamage.indexOf(entity);
if (-1 !== index) { if (-1 !== index) {

View File

@ -62,6 +62,9 @@ export class Vulnerable extends Trait {
context.destroy(); context.destroy();
}); });
this.damageTickingPromises.push(tickingPromise); this.damageTickingPromises.push(tickingPromise);
if (damage.from) {
damage.from.emitDamagingParticles(this.entity);
}
} }
acceptPacket(packet) { acceptPacket(packet) {
@ -134,10 +137,16 @@ export class Vulnerable extends Trait {
end: 0, end: 0,
}, },
force: [0, 1], force: [0, 1],
velocity: [ velocity: {
randomNumber(-0.5, 0.5), angle: {
randomNumber(-1.25, -0.75) min: 337.5,
], max: 382.5,
},
magnitude: {
min: -1.25,
max: -0.75,
},
},
rotation: { rotation: {
start: 0, start: 0,
add: { add: {
@ -171,10 +180,16 @@ export class Vulnerable extends Trait {
start: 1, start: 1,
end: 0.4, end: 0.4,
}, },
force: [0, 4], force: [0, 3],
velocity: { velocity: {
min: [-0.5, -1.25], angle: {
max: [0.5, -0.75], min: 316,
max: 405,
},
magnitude: {
min: 0.7,
max: 0.9,
},
}, },
scale: { scale: {
start: 1, start: 1,

View File

@ -56,10 +56,16 @@ export class Lootable extends decorate(Trait) {
json.traits.emitted = { json.traits.emitted = {
params: { params: {
force: [0, 8], force: [0, 8],
velocity: [ velocity: {
randomNumber(-0.5, 0.5), angle: {
randomNumber(-1.25, -0.75) min: 316,
], max: 405,
},
magnitude: {
min: 0.7,
max: 0.9,
},
},
position, position,
transient: false, transient: false,
ttl: 0.25, ttl: 0.25,

View File

@ -65,7 +65,7 @@ export function rockProjectileJSON() {
['Math', 'Vector', 'sub'], ['Math', 'Vector', 'sub'],
[ [
buildTraversal(['entity', 'position']), buildTraversal(['entity', 'position']),
buildTraversal(['obstacle', 'position']), buildTraversal(['other', 'position']),
], ],
), ),
0.2, 0.2,
@ -136,7 +136,7 @@ export function rockProjectileJSON() {
buildInvoke( buildInvoke(
['entity', 'context', 'add'], ['entity', 'context', 'add'],
[ [
'obstacle', 'other',
buildTraversal(['other']), buildTraversal(['other']),
], ],
), ),
@ -167,6 +167,34 @@ export function rockProjectileJSON() {
directionCount: 4, directionCount: 4,
}, },
}, },
emitter: {
params: {
particles: {
damaging: {
rate: 0.0125,
count: 5,
traits: {
primitive: {
params: {
primitives: [
{
type: 'circle',
radius: 0.5,
line: {
rgba: [128, 128, 128],
},
fill: {
rgba: [128, 128, 128],
},
},
],
},
},
},
},
},
},
},
existent: { existent: {
state: { state: {
name: 'Rock (projectile)', name: 'Rock (projectile)',