Strata - v1.4.10
    Preparing search index...

    Function createPredatorPreset

    • Create hunting predators that patrol or wander, then pursue detected prey.

      Predator entities intelligently search for prey and initiate high-speed pursuits when targets are detected. They can patrol fixed routes or wander freely, making them perfect for wolves, enemies, sharks, or any aggressive hunting character.

      The preset includes state management for switching between hunting and pursuit behaviors, with configurable detection ranges and speeds.

      Parameters

      Returns AIPresetResult

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

      const wolf = createPredatorPreset({
      pursuitSpeed: 15,
      detectionRadius: 25,
      maxSpeed: 12
      });

      // In game loop
      function update(delta) {
      wolf.update(delta, {
      preyPosition: nearestPrey?.position
      });
      }
      const patrollingPredator = createPredatorPreset({
      patrolWaypoints: [
      [0, 0, 0],
      [20, 0, 0],
      [20, 0, 20],
      [0, 0, 20]
      ],
      pursuitSpeed: 16,
      detectionRadius: 20
      });

      // Patrols until prey detected, then gives chase
      patrollingPredator.update(delta, { preyPosition: rabbit.position });
      import { createPredatorPreset } from '@jbcom/strata/presets/ai';

      // Create wolf pack
      const pack = Array.from({ length: 5 }, () =>
      createPredatorPreset({
      pursuitSpeed: 14,
      detectionRadius: 30, // Wide detection for coordination
      maxSpeed: 12,
      maxForce: 15
      })
      );

      // Spawn in formation
      pack.forEach((wolf, i) => {
      const angle = (i / pack.length) * Math.PI * 2;
      wolf.vehicle.position.set(
      Math.cos(angle) * 5,
      0,
      Math.sin(angle) * 5
      );
      });

      function updatePack(delta, preyPos) {
      // All wolves target same prey
      pack.forEach(wolf => {
      wolf.update(delta, { preyPosition: preyPos });
      });
      }
      const shark = createPredatorPreset({
      pursuitSpeed: 18,
      detectionRadius: 35,
      maxSpeed: 15,
      mass: 3 // Heavy, momentum-based movement
      });

      function updateShark(delta, fishPos) {
      shark.update(delta, { preyPosition: fishPos });

      // Keep shark underwater
      shark.vehicle.position.y = Math.max(
      shark.vehicle.position.y,
      -10 // Maximum depth
      );
      shark.vehicle.position.y = Math.min(
      shark.vehicle.position.y,
      -2 // Minimum depth (below surface)
      );
      }
      const ambushPredator = createPredatorPreset({
      pursuitSpeed: 20, // Very fast when attacking
      detectionRadius: 15, // Close range only
      maxSpeed: 10
      });

      let isHidden = true;

      function updateAmbushPredator(delta, preyPos) {
      const distance = ambushPredator.vehicle.position.distanceTo(preyPos);

      if (isHidden && distance < 8) {
      // Surprise attack!
      isHidden = false;
      ambushPredator.vehicle.maxSpeed = 20;
      playSound('roar');
      } else if (!isHidden && distance > 30) {
      // Return to hiding
      isHidden = true;
      ambushPredator.vehicle.maxSpeed = 2;
      }

      ambushPredator.update(delta, {
      preyPosition: !isHidden ? preyPos : undefined
      });
      }
      const territorialPredator = createPredatorPreset({
      patrolWaypoints: territoryBoundary,
      pursuitSpeed: 14,
      detectionRadius: 25
      });

      const territoryCenter = new YUKA.Vector3(0, 0, 0);
      const territoryRadius = 30;

      function updateTerritorialPredator(delta, preyPos) {
      const distFromCenter = territorialPredator.vehicle.position
      .distanceTo(territoryCenter);

      // Only pursue if prey is in territory
      const preyInTerritory = preyPos &&
      preyPos.distanceTo(territoryCenter) < territoryRadius;

      territorialPredator.update(delta, {
      preyPosition: preyInTerritory ? preyPos : undefined
      });

      // Return to territory if too far
      if (distFromCenter > territoryRadius + 10) {
      const returnVector = territoryCenter.clone()
      .subtract(territorialPredator.vehicle.position)
      .normalize()
      .multiplyScalar(3);

      territorialPredator.vehicle.velocity.add(returnVector);
      }
      }
      const bossPredator = createPredatorPreset({
      pursuitSpeed: 12,
      detectionRadius: 40,
      maxSpeed: 12,
      mass: 5 // Very heavy, hard to evade
      });

      let bossPhase = 1;
      let health = 100;

      function updateBossPredator(delta, playerPos) {
      // Phase transitions
      if (health < 60 && bossPhase === 1) {
      bossPhase = 2;
      bossPredator.vehicle.maxSpeed = 16;
      console.log('Boss enraged!');
      } else if (health < 30 && bossPhase === 2) {
      bossPhase = 3;
      bossPredator.vehicle.maxSpeed = 20;
      spawnMinions();
      console.log('Boss desperate!');
      }

      bossPredator.update(delta, { preyPosition: playerPos });

      // Special abilities per phase
      if (bossPhase === 3 && Math.random() < 0.02) {
      performSpecialAttack();
      }
      }
      const predators = Array.from({ length: 3 }, () =>
      createPredatorPreset({ pursuitSpeed: 14, detectionRadius: 25 })
      );

      const prey = Array.from({ length: 20 }, () =>
      createPreyPreset({ fleeSpeed: 12, fleeDistance: 12 })
      );

      function updateEcosystem(delta) {
      predators.forEach(predator => {
      // Find nearest prey
      let nearest = null;
      let nearestDist = Infinity;

      prey.forEach(p => {
      const dist = predator.vehicle.position.distanceTo(
      p.vehicle.position
      );
      if (dist < nearestDist) {
      nearestDist = dist;
      nearest = p;
      }
      });

      predator.update(delta, {
      preyPosition: nearest?.vehicle.position
      });
      });

      prey.forEach(p => {
      // Find nearest predator
      let nearest = null;
      let nearestDist = Infinity;

      predators.forEach(predator => {
      const dist = p.vehicle.position.distanceTo(
      predator.vehicle.position
      );
      if (dist < nearestDist) {
      nearestDist = dist;
      nearest = predator;
      }
      });

      p.update(delta, {
      threatPosition: nearest?.vehicle.position
      });
      });
      }