Skip to content

Instantly share code, notes, and snippets.

@Wolwer1nE
Created December 16, 2020 09:27
Show Gist options
  • Save Wolwer1nE/c0d1cc504c5a7a3542cb352c6de4dd0c to your computer and use it in GitHub Desktop.
Save Wolwer1nE/c0d1cc504c5a7a3542cb352c6de4dd0c to your computer and use it in GitHub Desktop.
import Steering from "./steering.js";
import Vector2 from 'phaser/src/math/Vector2'
export default class PatrollingLeftRight extends Steering {
constructor (owner, ownerSpeed= 80, force = 1) {
super(owner, force);
this.ownerSpeed = ownerSpeed;
this.lastX = owner.x;
this.currentDist = 0;
this.maxDist = Phaser.Math.RND.between(50, 150 );
this.direction = 1;
this.started = false;
}
seek(owner, maxSpeed) {
const desiredVelocity = new Vector2(owner.x+maxSpeed*this.derection, 0)
.normalize().scale(maxSpeed);
return desiredVelocity;
}
calculateImpulse (currentBody)
{
let pursuitMen;
pursuitMen = this.owner;
this.started= true;
const dx =Math.abs( this.lastX - pursuitMen.x);
this.currentDist += dx;
if ((dx < 2)||(this.currentDist>this.maxDist))
{ this.derection *= -1;
this.currentDist = 0;
};
this.lastX = pursuitMen.x;
return this.seek(pursuitMen, this.ownerSpeed);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment