Patrol
Patrol walks an entity back and forth on a straight line, a set distance each way from where it started. It fits an enemy on its beat and a platform that goes to and fro.
PhysicsPlugin installs PatrolPlugin. To install it alone, import it from @shell/patrol.
import { Define } from '@shell/ecs';import { CircleCollider, KinematicBody, Patrol } from '@shell/physics';import { Position } from '@shell/transform';
const EnemyPrefab = Define.prefab() .with(KinematicBody) .with(Position, { x: 400, y: 300, z: 0 }) .with(CircleCollider, { radius: 16 }) .with(Patrol, { speed: 100, distance: 150 });| Field | Type | Default | Description |
|---|---|---|---|
speed | number | 100 | Units per second along the line. 0 stops it |
distance | number | 100 | How far it walks each way from where it started |
angle | number | 0 | The line, in radians: 0 is left and right, Math.PI / 2 up and down |
Patrol requires Position and Velocity. Its other fields (originX, originY, started, direction, expectedOffset) are the system’s own state, hidden in the editor.
How it walks
Section titled “How it walks”PatrolSystem runs each fixed step, before velocity is integrated. On its first step it takes the entity’s Position as the middle of its beat. After that it measures how far along the line the entity is from there, and turns at distance on either side.
It sets only the speed along its line and leaves the speed across it. A patroller with Force on a level line still falls, and one that is pushed sideways keeps that speed.
When it is stopped
Section titled “When it is stopped”A patroller that came less than half as far in a step as it was sent turns around. A dynamic patroller therefore turns at a wall, and at anything as heavy as itself or heavier. It turns a step or two after meeting it.
A kinematic patroller is stopped by nothing here, so it turns only at the ends of its distance.
Stopping and starting
Section titled “Stopping and starting”Set speed to 0 to stop a patroller. It goes on the way it went when speed is set again.
entity.getComponent(Patrol)!.speed = 0;Removing Patrol leaves the entity its last Velocity, so it keeps moving.
Limitations
Section titled “Limitations”- One straight line. There are no waypoints and no path.
- The beat is measured from where the entity is on its first fixed step, so it is placed at the middle of its beat, not at an end.
- Being stopped is a guess from how far it came, not read from contacts.