refactor: behavior builders

This commit is contained in:
cha0s 2019-04-21 21:33:11 -05:00
parent c4d731a739
commit fc7777a1fe
2 changed files with 41 additions and 170 deletions

View File

@ -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',
};
}

View File

@ -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,
],
},
}