Ninety-six monsters in one draw, chasing two players
Open the live demo · Read the source · View on GitHub
A horde whose every step is decided by flutter3d_sim does not need a scene
node per monster. InstancedActorComponent draws a simulated actor as a slot
of a shared InstancedMeshNode, and ActorSystemComponent steps the whole
system towards several players at once, so each monster goes for the one it
can reach first.
Step 1: One batch for the horde #
The mesh and the material are the batch's; the colour is each slot's own, which is how a monster shows who it is after.
/// Every monster's mesh and material, drawn in one call however many there
/// are; the colour is each slot's own.
late final InstancedMeshNode batch = InstancedMeshNode(
DeviceMesh.upload(
device,
CuboidShape(size: Vector3(0.6, 1.0, 0.6)).build(),
),
RenderMaterial(name: 'monster', baseColor: LinearColor.white),
capacity: _monsters,
name: 'horde',
);
Step 2: A brain that chases a focus #
The system tells each monster which focus is its own on every step, and
steerTowardsFocus walks it there. The brain only remembers the answer so
the page can colour it.
/// Walks at whichever player the system says it can reach first, and
/// remembers which one that was.
final class _Chase extends Brain {
int attended = -1;
@override
void act(Mind it) {
attended = it.focusIndex;
it.steerTowardsFocus();
}
}
Step 3: Several foci #
foci is read fresh on every step: here, where the two players are now. With
several, each actor attends to the one nearest by walking where the level has
a flow field, and by straight line where it has none, as in this empty yard.
The component is also what steps the actors, so it is the clock their slots
draw between.
// One system, stepped towards both players: each monster goes for the
// one it can reach first. The system is a step clock too, and every
// monster is drawn between its last two steps by it.
final ActorSystemComponent stepper = ActorSystemComponent(
system: system,
foci: () => <FocusPoint>[
for (final MeshNode hero in heroes)
FocusPoint(at: hero.readPosition()..y = 0.9, body: null),
],
);
Step 4: An actor per monster, a slot per actor #
Each monster is an actor in the system and a component in the game. The component takes a slot when it is added, gives it back when it goes, and goes itself when the simulation removes its actor.
// Two rings of monsters, each an actor in the system and a slot in the
// batch; no scene node of their own.
final List<Component> slots = <Component>[];
for (var i = 0; i < _monsters; i++) {
final double radius = i.isEven ? 9.0 : 11.0;
final double angle = i * 2.0 * math.pi / _monsters;
final _Chase brain = _Chase();
final Actor actor = system.spawn(
body: CharacterController(
world: world,
position: Vector3(
radius * math.cos(angle),
0.9,
radius * math.sin(angle),
),
settings: const MovementSettings(walkSpeed: 2.2),
),
brain: brain,
);
monsters.add((actor, brain));
slots.add(
_Monster(actor: actor, batch: batch, stepper: stepper, brain: brain),
);
}
addAll(<Component>[stepper, ...slots]);
Drawn with stepper, a monster is placed between where it was before the
last step and where it is now, so the horde moves smoothly at any frame rate.
The slot's colour follows the player the brain last went for: orange for the
player on the left, blue for the one on the right, and a monster changes
colour as the two circle and the other one comes nearer.
/// A monster drawn through its slot, between its steps, in the colour of
/// the player its brain went for.
final class _Monster extends InstancedActorComponent {
_Monster({
required super.actor,
required super.batch,
required ActorSystemComponent super.stepper,
required this.brain,
}) : super(lift: -0.4);
final _Chase brain;
int _shown = -1;
@override
void update(double dt) {
super.update(dt);
final InstanceHandle? at = slot;
if (at == null || brain.attended == _shown || brain.attended < 0) return;
_shown = brain.attended;
at.setColor(_playerColours[_shown].toLinearColor());
}
}