Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

1.8 KB
Raw Blame
import { defineSystem, Query, Mut, registerAction, registerCondition } from 'esengine';
import { setNavDestination, Perception, Status } from 'esengine/ai';
import { Facing, Swing } from '../components';

// Leaves for assets/ai/wisp.esbt. The tree is authored as data and resolves
// these names from the engine's AI registry, so the brain's shape is editable
// without touching code and the code stays a vocabulary rather than a plan.

registerCondition('seesPlayer', (ctx) => ctx.has(Perception) && ctx.get(Perception).visible);

registerCondition('inStrikeRange', (ctx) => {
    if (!ctx.has(Perception) || !ctx.has(Swing)) return false;
    const perception = ctx.get(Perception);
    // Short of the full reach, so the wisp closes in rather than poking from the
    // edge of its own arc.
    return perception.visible && perception.distance <= ctx.get(Swing).reach * 0.75;
});

registerAction('strike', (ctx) => {
    if (!ctx.has(Swing)) return Status.Failure;
    ctx.set(Swing, { ...ctx.get(Swing), pending: true });
    return Status.Success;
});

registerAction('chase', (ctx) => {
    if (!ctx.has(Perception)) return Status.Failure;
    const perception = ctx.get(Perception);
    if (!perception.visible) return Status.Failure;
    setNavDestination(ctx.world, ctx.entity, { x: perception.targetX, y: perception.targetY });
    return Status.Running;
});

registerAction('hover', () => Status.Running);

/** Whoever can see turns to what they see, so their arc points at it. */
export const perceiverFacingSystem = defineSystem(
    [Query(Mut(Facing), Perception)],
    (perceivers) => {
        for (const [, facing, perception] of perceivers) {
            if (!perception.visible) continue;
            facing.x = perception.dirX;
            facing.y = perception.dirY;
        }
    },
    { name: 'PerceiverFacingSystem' },
);