diff --git a/common/combat/alive.trait.js b/common/combat/alive.trait.js index b0a4ca0..b700a10 100644 --- a/common/combat/alive.trait.js +++ b/common/combat/alive.trait.js @@ -1,4 +1,10 @@ -import {behaviorItemFromJSON, createContext} from '@avocado/behavior'; +import { + behaviorItemFromJSON, + buildCondition, + buildInvoke, + buildTraversal, + createContext, +} from '@avocado/behavior'; import {compose} from '@avocado/core'; import {StateProperty, Trait} from '@avocado/entity'; @@ -14,97 +20,30 @@ const decorate = compose( export class Alive extends decorate(Trait) { static defaultParams() { + const playDeathSound = buildInvoke(['entity', 'playSound'], [ + buildTraversal(['entity', 'deathSound']), + ]); + const squeeze = buildInvoke(['entity', 'transition'], [ + { + opacity: 0, + visibleScaleX: .3, + visibleScaleY: 3, + }, + 0.2, + ]); + const isLifeGone = buildCondition('<=', [ + buildTraversal(['entity', 'life']), + 0, + ]); return { deathActions: { type: 'actions', traversals: [ - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'playSound', - }, - { - type: 'invoke', - args: [ - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'deathSound', - }, - ], - }, - ], - }, - ], - }, - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'transition', - }, - { - type: 'invoke', - args: [ - { - type: 'literal', - value: { - opacity: 0, - visibleScaleX: .3, - visibleScaleY: 3, - }, - }, - { - type: 'literal', - value: 0.2, - }, - ], - }, - ], - }, - ], - }, - // By default, die when life <= 0. - deathCondition: { - type: 'condition', - operator: '<=', - operands: [ - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'life', - }, - ], - }, - { - type: 'literal', - value: 0, - }, + playDeathSound, + squeeze, ], }, + deathCondition: isLifeGone, deathSound: 'deathSound', }; } diff --git a/common/combat/vulnerable.trait.js b/common/combat/vulnerable.trait.js index e43d8c5..deb6361 100644 --- a/common/combat/vulnerable.trait.js +++ b/common/combat/vulnerable.trait.js @@ -1,7 +1,12 @@ import * as I from 'immutable'; import {Trait} from '@avocado/entity'; -import {behaviorItemFromJSON, createContext} from '@avocado/behavior'; +import { + behaviorItemFromJSON, + buildInvoke, + buildTraversal, + createContext, +} from '@avocado/behavior'; import {hasGraphics, TextNodeRenderer} from '@avocado/graphics'; import {DamageEmitter} from './emitter'; @@ -9,93 +14,20 @@ 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: [ - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'emitParticle', - }, - { - type: 'invoke', - args: [ - { - type: 'literal', - value: 'damage', - }, - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'entity', - }, - { - type: 'key', - key: 'position', - }, - ], - }, - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'damage', - }, - ], - }, - ], - }, - ], - }, - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'damage', - }, - { - type: 'key', - key: 'from', - }, - { - type: 'key', - key: 'playSound', - }, - { - type: 'invoke', - args: [ - { - type: 'traversal', - steps: [ - { - type: 'key', - key: 'damage', - }, - { - type: 'key', - key: 'from', - }, - { - type: 'key', - key: 'damagingSound', - }, - ], - }, - ], - }, - ], - }, + emitDamage, + playDamagingSound, ], }, }