Strata - v1.4.10
    Preparing search index...

    Function createPreyPreset

    • Create prey animals that wander peacefully and flee from threats.

      Prey entities exhibit natural wildlife behavior - wandering randomly within an area until a threat approaches, then fleeing at high speed. Perfect for rabbits, deer, fish, neutral NPCs, or any character that should avoid danger.

      The preset includes intelligent state management that transitions between calm wandering and panicked fleeing, creating believable animal behavior.

      Parameters

      Returns AIPresetResult

      import { createPreyPreset } from '@jbcom/strata/presets/ai';

      const rabbit = createPreyPreset({
      wanderRadius: 3,
      fleeDistance: 10,
      fleeSpeed: 12,
      maxSpeed: 6
      });

      // In game loop
      function update(delta) {
      // Rabbit flees from nearest predator
      rabbit.update(delta, {
      threatPosition: nearestPredator?.position
      });
      }
      import { createPreyPreset, createPredatorPreset } from '@jbcom/strata/presets/ai';

      // Create multiple prey animals
      const rabbits = Array.from({ length: 10 }, () =>
      createPreyPreset({
      wanderRadius: 4,
      fleeDistance: 12,
      fleeSpeed: 14
      })
      );

      // Spawn them in world
      rabbits.forEach((rabbit, i) => {
      rabbit.vehicle.position.set(
      Math.random() * 50 - 25,
      0,
      Math.random() * 50 - 25
      );
      });

      // Update with predator awareness
      const wolf = createPredatorPreset({ pursuitSpeed: 15 });

      function updateEcosystem(delta) {
      rabbits.forEach(rabbit => {
      rabbit.update(delta, {
      threatPosition: wolf.vehicle.position
      });
      });

      wolf.update(delta, {
      preyPosition: rabbits[0].vehicle.position
      });
      }
      // Timid prey - flees early
      const mouse = createPreyPreset({
      wanderRadius: 2,
      fleeDistance: 15, // Notices threats from far away
      fleeSpeed: 10,
      maxSpeed: 5
      });

      // Bold prey - flees late
      const boar = createPreyPreset({
      wanderRadius: 5,
      fleeDistance: 5, // Only flees when threat is close
      fleeSpeed: 8,
      maxSpeed: 4,
      mass: 2 // Heavier, slower to change direction
      });

      // Fast prey - quick escape
      const gazelle = createPreyPreset({
      wanderRadius: 6,
      fleeDistance: 12,
      fleeSpeed: 18, // Very fast escape
      maxSpeed: 8
      });
      const deer = createPreyPreset({
      wanderRadius: 4,
      fleeDistance: 10,
      fleeSpeed: 14
      });

      let stamina = 100;
      let isExhausted = false;

      function updateDeer(delta, predatorPos) {
      const isFleeing = deer.vehicle.position.distanceTo(predatorPos) < 10;

      if (isFleeing && !isExhausted) {
      stamina -= delta * 20; // Drain stamina while fleeing
      if (stamina <= 0) {
      isExhausted = true;
      deer.vehicle.maxSpeed = 4; // Too tired to run fast
      }
      } else {
      stamina = Math.min(100, stamina + delta * 5); // Recover
      if (stamina > 50) {
      isExhausted = false;
      deer.vehicle.maxSpeed = 14;
      }
      }

      deer.update(delta, { threatPosition: predatorPos });
      }
      // Small fish flee as a group
      const fishSchool = Array.from({ length: 20 }, () =>
      createPreyPreset({
      wanderRadius: 2,
      fleeDistance: 8,
      fleeSpeed: 10,
      maxSpeed: 5
      })
      );

      function updateFishSchool(delta, sharkPos) {
      fishSchool.forEach(fish => {
      // All fish flee from same threat
      fish.update(delta, { threatPosition: sharkPos });

      // Keep fish underwater
      fish.vehicle.position.y = Math.max(
      fish.vehicle.position.y,
      -5 // Min depth
      );
      });
      }
      const prey = createPreyPreset({
      wanderRadius: 3,
      fleeDistance: 10,
      fleeSpeed: 12
      });

      const safeZoneCenter = new YUKA.Vector3(0, 0, 0);
      const safeZoneRadius = 15;

      function isInSafeZone(position) {
      return position.distanceTo(safeZoneCenter) < safeZoneRadius;
      }

      function updatePrey(delta, threatPos) {
      const inSafeZone = isInSafeZone(prey.vehicle.position);

      // Only flee if threat is present AND we're not in safe zone
      const shouldFlee = !inSafeZone && threatPos;

      prey.update(delta, {
      threatPosition: shouldFlee ? threatPos : undefined
      });

      // If fleeing, add steering toward safe zone
      if (shouldFlee) {
      const toSafety = safeZoneCenter.clone()
      .subtract(prey.vehicle.position)
      .normalize()
      .multiplyScalar(2); // Seek safety

      prey.vehicle.velocity.add(toSafety);
      }
      }
      // Create herd that flees together
      const herd = Array.from({ length: 15 }, () =>
      createPreyPreset({
      wanderRadius: 4,
      fleeDistance: 12,
      fleeSpeed: 12
      })
      );

      function updateHerd(delta, predatorPos) {
      // Calculate average herd position
      const avgPos = new YUKA.Vector3();
      herd.forEach(animal => avgPos.add(animal.vehicle.position));
      avgPos.divideScalar(herd.length);

      herd.forEach(animal => {
      const distToHerd = animal.vehicle.position.distanceTo(avgPos);

      // Lone animals are more vulnerable
      const effectiveFleeDistance = distToHerd > 10 ? 15 : 12;

      animal.update(delta, { threatPosition: predatorPos });
      });
      }