diff --git a/src/shattered_sword/AI/EnemyAI.ts b/src/shattered_sword/AI/EnemyAI.ts index 3779494..0c69eca 100644 --- a/src/shattered_sword/AI/EnemyAI.ts +++ b/src/shattered_sword/AI/EnemyAI.ts @@ -9,6 +9,7 @@ import Weapon from "../GameSystems/items/Weapon"; import BattlerAI from "./BattlerAI"; import Patrol from "./EnemyStates/Patrol"; +import Alert from "./EnemyStates/Alert"; import { GameState, Statuses } from "../sword_enums"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; @@ -32,6 +33,8 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI { /** The default movement speed of this AI */ speed: number = 20; + maxSpeed: number = 40; + /** The weapon this AI has */ weapon: Weapon; @@ -81,7 +84,7 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI { //add states // Patrol mode this.addState(EnemyStates.PATROL, new Patrol(this, owner)); - // this.addState(EnemyStates.ALERT,) + this.addState(EnemyStates.ALERT, new Alert(this, owner)); this.maxHealth = options.health; diff --git a/src/shattered_sword/AI/EnemyStates/Alert.ts b/src/shattered_sword/AI/EnemyStates/Alert.ts new file mode 100644 index 0000000..d51876d --- /dev/null +++ b/src/shattered_sword/AI/EnemyStates/Alert.ts @@ -0,0 +1,33 @@ +import EnemyAI, { EnemyStates } from "../EnemyAI"; +import EnemyState from "./EnemyState"; +import Sprite from "../../../Wolfie2D/Nodes/Sprites/Sprite"; +import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; + +export default class Alert extends EnemyState { + onEnter(options: Record): void { + (this.owner).animation.playIfNotAlready("IDLE", true); + } + + update(deltaT: number): void { + if(!this.canWalk()){ + this.parent.direction *= -1; + } + + let position = this.parent.getPlayerPosition(); + if (position) { + this.parent.velocity.x = this.parent.maxSpeed * Math.sign(position.x - this.owner.position.x); + } + else { + this.parent.velocity.x = 0; + this.finished(EnemyStates.PATROL); + } + + (this.owner).invertX = this.parent.velocity.x > 0 ? true : false ; + super.update(deltaT); + } + + onExit(): Record { + (this.owner).animation.stop(); + return null; + } +} \ No newline at end of file diff --git a/src/shattered_sword/AI/EnemyStates/Patrol.ts b/src/shattered_sword/AI/EnemyStates/Patrol.ts index 173159a..245b809 100644 --- a/src/shattered_sword/AI/EnemyStates/Patrol.ts +++ b/src/shattered_sword/AI/EnemyStates/Patrol.ts @@ -24,6 +24,9 @@ export default class Patrol extends EnemyState { this.parent.velocity.x = this.parent.direction * this.parent.speed; (this.owner).invertX = this.parent.direction === 1 ? true : false ; + if (this.parent.getPlayerPosition()) { + this.finished(EnemyStates.ALERT); + } super.update(deltaT); }