diff --git a/src/shattered_sword/AI/EnemyAI.ts b/src/shattered_sword/AI/EnemyAI.ts index 569098f..9bad659 100644 --- a/src/shattered_sword/AI/EnemyAI.ts +++ b/src/shattered_sword/AI/EnemyAI.ts @@ -8,7 +8,7 @@ import BattlerAI from "./BattlerAI"; import Patrol from "./EnemyStates/Patrol"; import Alert from "./EnemyStates/Alert"; -import Attack from "./EnemyStates/Attack"; +import SlimeAttack from "./EnemyStates/SlimeAttack"; import { GameState, Statuses } from "../sword_enums"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; @@ -73,7 +73,7 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI { // Patrol mode this.addState(EnemyStates.PATROL, new Patrol(this, owner)); this.addState(EnemyStates.ALERT, new Alert(this, owner)); - this.addState(EnemyStates.ATTACK, new Attack(this, owner)); + this.addState(EnemyStates.ATTACK, new SlimeAttack(this, owner)); this.maxHealth = options.health; @@ -142,6 +142,10 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI { player.damage(10); } + canAttack(position: Vec2): boolean { + return this.attackTimer.isStopped() && this.owner.position.distanceTo(position)<=32; + } + //TODO - need to modify for side view isPlayerVisible(pos: Vec2): Vec2{ //Check ifplayer is visible, taking into account walls diff --git a/src/shattered_sword/AI/EnemyStates/Alert.ts b/src/shattered_sword/AI/EnemyStates/Alert.ts index 2803807..e42cf86 100644 --- a/src/shattered_sword/AI/EnemyStates/Alert.ts +++ b/src/shattered_sword/AI/EnemyStates/Alert.ts @@ -13,7 +13,7 @@ export default class Alert extends EnemyState { if (position) { this.parent.velocity.x = this.parent.maxSpeed * Math.sign(position.x - this.owner.position.x); this.parent.direction = this.parent.velocity.x >= 0 ? 1 : -1; - if (this.parent.attackTimer.isStopped() && this.owner.position.distanceTo(position)<=32) { + if (this.parent.canAttack(position)) { this.finished(EnemyStates.ATTACK); } } diff --git a/src/shattered_sword/AI/EnemyStates/Attack.ts b/src/shattered_sword/AI/EnemyStates/Attack.ts index db0cc12..19e57d9 100644 --- a/src/shattered_sword/AI/EnemyStates/Attack.ts +++ b/src/shattered_sword/AI/EnemyStates/Attack.ts @@ -20,17 +20,6 @@ export default class Attack extends EnemyState { } update(deltaT: number): void { - while (this.receiver.hasNextEvent()) { - let event = this.receiver.getNextEvent().type; - switch (event) { - case this.charged: - (this.owner).animation.play("ATTACK", false, this.attacked); - break; - case this.attacked: - this.finished(EnemyStates.ALERT); - break; - } - } super.update(deltaT); } diff --git a/src/shattered_sword/AI/EnemyStates/SlimeAttack.ts b/src/shattered_sword/AI/EnemyStates/SlimeAttack.ts new file mode 100644 index 0000000..35e0421 --- /dev/null +++ b/src/shattered_sword/AI/EnemyStates/SlimeAttack.ts @@ -0,0 +1,20 @@ +import EnemyAI, { EnemyStates } from "../EnemyAI"; +import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; +import Attack from "./Attack"; + +export default class SlimeAttack extends Attack { + update(deltaT: number): void { + while (this.receiver.hasNextEvent()) { + let event = this.receiver.getNextEvent().type; + switch (event) { + case this.charged: + (this.owner).animation.play("ATTACK", false, this.attacked); + break; + case this.attacked: + this.finished(EnemyStates.ALERT); + break; + } + } + super.update(deltaT); + } +} \ No newline at end of file diff --git a/src/shattered_sword/AI/EnemyStates/SnakeAttack.ts b/src/shattered_sword/AI/EnemyStates/SnakeAttack.ts index d9372cd..bd822e5 100644 --- a/src/shattered_sword/AI/EnemyStates/SnakeAttack.ts +++ b/src/shattered_sword/AI/EnemyStates/SnakeAttack.ts @@ -5,13 +5,6 @@ import Sprite from "../../../Wolfie2D/Nodes/Sprites/Sprite"; import Attack from "./Attack"; export default class SnakeAttack extends Attack { - protected charged: string; - protected attacked: string; - - onEnter(options: Record): void { - super.onEnter(options); - } - update(deltaT: number): void { while (this.receiver.hasNextEvent()) { let event = this.receiver.getNextEvent().type; @@ -28,9 +21,4 @@ export default class SnakeAttack extends Attack { } (this.owner).invertX = this.parent.direction === 1 ? true : false ; } - - onExit(): Record { - (this.owner).animation.stop(); - return null; - } } \ No newline at end of file diff --git a/src/shattered_sword/AI/EnemyStates/TigerAttack.ts b/src/shattered_sword/AI/EnemyStates/TigerAttack.ts new file mode 100644 index 0000000..d44d6f4 --- /dev/null +++ b/src/shattered_sword/AI/EnemyStates/TigerAttack.ts @@ -0,0 +1,39 @@ +import EnemyAI, { EnemyStates } from "../EnemyAI"; +import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; +import AABB from "../../../Wolfie2D/DataTypes/Shapes/AABB"; +import Sprite from "../../../Wolfie2D/Nodes/Sprites/Sprite"; +import Attack from "./Attack"; + +export default class TigerAttack extends Attack { + protected velocity: number; + protected distance: number; + protected attacking: boolean = false; + + onEnter(options: Record): void { + super.onEnter(options); + this.velocity = 0; + } + + update(deltaT: number): void { + if (this.attacking && this.owner.onGround) { + this.emitter.fireEvent(this.attacked); + } + while (this.receiver.hasNextEvent()) { + let event = this.receiver.getNextEvent().type; + switch (event) { + case this.charged: + (this.owner).animation.play("ATTACK", true); + this.velocity = (this.parent.getPlayerPosition().x - this.owner.position.x)/2; + this.parent.direction = this.velocity >= 0 ? 1 : 0; + this.attacking = true; + break; + case this.attacked: + this.finished(EnemyStates.ALERT); + break; + } + } + this.parent.velocity.x = this.velocity; + (this.owner).invertX = this.parent.direction === 1 ? true : false ; + super.update(deltaT); + } +} \ No newline at end of file diff --git a/src/shattered_sword/AI/TigerAI.ts b/src/shattered_sword/AI/TigerAI.ts new file mode 100644 index 0000000..eac328a --- /dev/null +++ b/src/shattered_sword/AI/TigerAI.ts @@ -0,0 +1,19 @@ +import Vec2 from "../../Wolfie2D/DataTypes/Vec2"; +import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; +import EnemyAI, { EnemyStates } from "./EnemyAI"; +import TigerAttack from "./EnemyStates/TigerAttack"; + +export default class TigerAI extends EnemyAI { + initializeAI(owner: AnimatedSprite, options: Record): void { + super.initializeAI(owner, options); + this.addState(EnemyStates.ATTACK, new TigerAttack(this, owner)); + } + + canAttack(position: Vec2): boolean { + return this.attackTimer.isStopped(); + } + + getPlayerPosition(): Vec2 { + return this.player.position; + } +} \ No newline at end of file