Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/excaliburjs/sample-platformer. Sample Platformer built in Excalibur excaliburjs.com/sample-platformer
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import * as ex from "excalibur";import { PatrolComponent } from "./behaviors/patrol";import { Config } from "./config";import { collisionGroups, makeCharacterCollider } from "./physics";import { NpcGraphics } from "./resources";
export class NPC extends ex.Actor { constructor( x: number, y: number, public patrolLeft: number, public patrolRight: number ) { super({ pos: new ex.Vector(x, y), collisionGroup: collisionGroups.player, collisionType: ex.CollisionType.Active, collider: makeCharacterCollider(), });
this.addComponent( new PatrolComponent({ delay: Config.npcPatrolDelay, speed: Config.npcPatrolSpeed, from: ex.vec(x + patrolLeft, y), to: ex.vec(x + patrolRight, y), }) ); }
onInitialize() { // Set the z-index to be behind everything this.z = -1;
this.graphics.add("idle", NpcGraphics.idle); this.graphics.add("left", NpcGraphics.left); this.graphics.add("right", NpcGraphics.right); this.graphics.use("idle"); }
onPostUpdate() { if (this.vel.x < 0) { this.graphics.use("left"); } else if (this.vel.x > 0) { this.graphics.use("right"); } else if (this.vel.x === 0) { this.graphics.use("idle"); } }}