fix: enemy play walk animation

This commit is contained in:
Renge 2022-04-24 20:47:24 -04:00
parent 1726ba3653
commit 94da5fef26
4 changed files with 41 additions and 19 deletions

View File

@ -54,7 +54,7 @@
] ]
}, },
{ {
"name": "walk", "name": "WALK",
"repeat": true, "repeat": true,
"frames": [ "frames": [
{ {

View File

@ -4,12 +4,11 @@ import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import GameNode from "../../Wolfie2D/Nodes/GameNode"; import GameNode from "../../Wolfie2D/Nodes/GameNode";
import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite"; import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import OrthogonalTilemap from "../../Wolfie2D/Nodes/Tilemaps/OrthogonalTilemap"; import OrthogonalTilemap from "../../Wolfie2D/Nodes/Tilemaps/OrthogonalTilemap";
import NavigationPath from "../../Wolfie2D/Pathfinding/NavigationPath";
import Weapon from "../GameSystems/items/Weapon";
import BattlerAI from "./BattlerAI"; import BattlerAI from "./BattlerAI";
import Patrol from "./EnemyStates/Patrol"; import Patrol from "./EnemyStates/Patrol";
import Alert from "./EnemyStates/Alert"; import Alert from "./EnemyStates/Alert";
import Attack from "./EnemyStates/Attack";
import { GameState, Statuses } from "../sword_enums"; import { GameState, Statuses } from "../sword_enums";
import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite"; import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
@ -35,24 +34,12 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI {
maxSpeed: number = 40; maxSpeed: number = 40;
/** The weapon this AI has */
weapon: Weapon;
/** A reference to the player object */ /** A reference to the player object */
player: GameNode; player: GameNode;
// The current known position of the player // The current known position of the player
playerPos: Vec2; playerPos: Vec2;
// The last known position of the player
lastPlayerPos: Vec2;
// Path to player
//path: NavigationPath;
// Path away from player
retreatPath: NavigationPath;
tilemap: OrthogonalTilemap; tilemap: OrthogonalTilemap;
velocity: Vec2 = Vec2.ZERO; velocity: Vec2 = Vec2.ZERO;
@ -85,13 +72,12 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI {
// Patrol mode // Patrol mode
this.addState(EnemyStates.PATROL, new Patrol(this, owner)); this.addState(EnemyStates.PATROL, new Patrol(this, owner));
this.addState(EnemyStates.ALERT, new Alert(this, owner)); this.addState(EnemyStates.ALERT, new Alert(this, owner));
this.addState(EnemyStates.ATTACK, new Attack(this, owner));
this.maxHealth = options.health; this.maxHealth = options.health;
this.CURRENT_HP = options.health; this.CURRENT_HP = options.health;
this.weapon = options.weapon;
this.player = options.player; this.player = options.player;
//TODO - get correct tilemap //TODO - get correct tilemap
@ -190,7 +176,6 @@ export default class EnemyAI extends StateMachineAI implements BattlerAI {
} }
} }
} }
return pos; return pos;
} }

View File

@ -5,13 +5,16 @@ import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
export default class Alert extends EnemyState { export default class Alert extends EnemyState {
onEnter(options: Record<string, any>): void { onEnter(options: Record<string, any>): void {
(<AnimatedSprite>this.owner).animation.playIfNotAlready("IDLE", true); (<AnimatedSprite>this.owner).animation.playIfNotAlready("WALK", true);
} }
update(deltaT: number): void { update(deltaT: number): void {
let position = this.parent.getPlayerPosition(); let position = this.parent.getPlayerPosition();
if (position) { if (position) {
this.parent.velocity.x = this.parent.maxSpeed * Math.sign(position.x - this.owner.position.x); this.parent.velocity.x = this.parent.maxSpeed * Math.sign(position.x - this.owner.position.x);
if (this.parent.attackTimer.isStopped()) {
this.finished(EnemyStates.ATTACK);
}
} }
else { else {
this.parent.velocity.x = 0; this.parent.velocity.x = 0;

View File

@ -0,0 +1,34 @@
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<string, any>): void {
(<AnimatedSprite>this.owner).animation.playIfNotAlready("WALK", true);
}
update(deltaT: number): void {
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.parent.direction = this.parent.velocity.x >= 0 ? 1 : -1;
if (!this.canWalk()) {
this.parent.velocity.x = 0;
}
(<Sprite>this.owner).invertX = this.parent.direction === 1 ? true : false ;
super.update(deltaT);
}
onExit(): Record<string, any> {
(<AnimatedSprite>this.owner).animation.stop();
return null;
}
}