feat: implemented snake ai

This commit is contained in:
Renge 2022-04-24 22:26:35 -04:00
parent 6f995ae005
commit da99dc9883
5 changed files with 62 additions and 14 deletions

View File

@ -12,6 +12,7 @@ export default class Alert extends EnemyState {
let position = this.parent.getPlayerPosition();
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) {
this.finished(EnemyStates.ATTACK);
}
@ -21,7 +22,6 @@ export default class Alert extends EnemyState {
this.finished(EnemyStates.PATROL);
}
this.parent.direction = this.parent.velocity.x >= 0 ? 1 : -1;
if (!this.canWalk()) {
this.parent.velocity.x = 0;
}

View File

@ -0,0 +1,47 @@
import EnemyAI, { EnemyStates } from "../EnemyAI";
import EnemyState from "./EnemyState";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import Shape from "../../../Wolfie2D/DataTypes/Shapes/Shape";
import Vec2 from "../../../Wolfie2D/DataTypes/Vec2";
import AABB from "../../../Wolfie2D/DataTypes/Shapes/AABB";
import Sprite from "../../../Wolfie2D/Nodes/Sprites/Sprite";
export default class Attack extends EnemyState {
protected charged: string;
protected attacked: string;
onEnter(options: Record<string, any>): void {
this.parent.attackTimer.start();
this.parent.velocity.x = 0;
this.charged = this.owner.id+"charged";
this.attacked = this.owner.id+"attacked";
// TODO replace DYING with CHARGING
(<AnimatedSprite>this.owner).animation.play("DYING", false, this.charged);
this.receiver.subscribe(this.charged);
this.receiver.subscribe(this.attacked);
}
update(deltaT: number): void {
while (this.receiver.hasNextEvent()) {
let event = this.receiver.getNextEvent().type;
switch (event) {
case this.charged:
(<AnimatedSprite>this.owner).animation.play("ATTACK", false, this.attacked);
(<AABB>this.owner.collisionShape).halfSize.x += 3.5;
break;
case this.attacked:
(<AABB>this.owner.collisionShape).halfSize.x -= 3.5;
this.finished(EnemyStates.ALERT);
break;
}
}
console.log(this.parent.direction);
(<Sprite>this.owner).invertX = this.parent.direction === 1 ? true : false ;
}
onExit(): Record<string, any> {
(<AnimatedSprite>this.owner).animation.stop();
return null;
}
}

View File

@ -0,0 +1,10 @@
import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import EnemyAI, { EnemyStates } from "./EnemyAI";
import SnakeAttack from "./EnemyStates/SnakeAttack";
export default class SnakeAI extends EnemyAI {
initializeAI(owner: AnimatedSprite, options: Record<string, any>): void {
super.initializeAI(owner, options);
this.addState(EnemyStates.ATTACK, new SnakeAttack(this, owner));
}
}

View File

@ -20,6 +20,7 @@ import WeaponType from "../GameSystems/items/WeaponTypes/WeaponType";
import Weapon from "../GameSystems/items/Weapon";
import BattleManager from "../GameSystems/BattleManager";
import EnemyAI from "../AI/EnemyAI";
import SnakeAI from "../AI/SnakeAI";
import BattlerAI from "../AI/BattlerAI";
import InventoryManager from "../GameSystems/InventoryManager";
import Item from "../GameSystems/items/Item";
@ -878,7 +879,7 @@ export default class GameLevel extends Scene {
for (let enemy of enemies) {
switch (enemy.type) {
case "Snake": //Snake enemies drop from sky("trees")? or could just be very abundant
this.addEnemy("Snake", enemy.position.scale(32), EnemyAI, {
this.addEnemy("Snake", enemy.position.scale(32), SnakeAI, {
player: this.player,
health: 50,
tilemap: "Main",

View File

@ -1,18 +1,8 @@
import { TiledTilemapData } from "../../Wolfie2D/DataTypes/Tilesets/TiledData";
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import Debug from "../../Wolfie2D/Debug/Debug";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
import RandomMapGenerator from "../Tools/RandomMapGenerator";
import GameLevel from "./GameLevel";
import Label from "../../Wolfie2D/Nodes/UIElements/Label";
import Color from "../../Wolfie2D/Utils/Color";
import { UIElementType } from "../../Wolfie2D/Nodes/UIElements/UIElementTypes";
import { Statuses } from "../sword_enums";
import AABB from "../../Wolfie2D/DataTypes/Shapes/AABB";
import EnemyAI from "../AI/EnemyAI";
import BattlerAI from "../AI/BattlerAI";
import SnakeAI from "../AI/SnakeAI";
import Porcelain from "./Porcelain";
import MainMenu from "./MainMenu";
export default class Tutorial extends GameLevel {
loadScene(): void {
@ -39,7 +29,7 @@ export default class Tutorial extends GameLevel {
//spawn snake()
if(Math.random() < .0001){
console.log("RANDOM SNAKE!");
this.addEnemy("Snake", this.player.position.clone().add(new Vec2(0,-320)), EnemyAI, {
this.addEnemy("Snake", this.player.position.clone().add(new Vec2(0,-320)), SnakeAI, {
player: this.player,
health: 50,
tilemap: "Main",