feat: implemented snake ai
This commit is contained in:
parent
6f995ae005
commit
da99dc9883
|
@ -12,6 +12,7 @@ export default class Alert extends EnemyState {
|
||||||
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);
|
||||||
|
this.parent.direction = this.parent.velocity.x >= 0 ? 1 : -1;
|
||||||
if (this.parent.attackTimer.isStopped() && this.owner.position.distanceTo(position)<=32) {
|
if (this.parent.attackTimer.isStopped() && this.owner.position.distanceTo(position)<=32) {
|
||||||
this.finished(EnemyStates.ATTACK);
|
this.finished(EnemyStates.ATTACK);
|
||||||
}
|
}
|
||||||
|
@ -21,7 +22,6 @@ export default class Alert extends EnemyState {
|
||||||
this.finished(EnemyStates.PATROL);
|
this.finished(EnemyStates.PATROL);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.parent.direction = this.parent.velocity.x >= 0 ? 1 : -1;
|
|
||||||
if (!this.canWalk()) {
|
if (!this.canWalk()) {
|
||||||
this.parent.velocity.x = 0;
|
this.parent.velocity.x = 0;
|
||||||
}
|
}
|
||||||
|
|
47
src/shattered_sword/AI/EnemyStates/SnakeAttack.ts
Normal file
47
src/shattered_sword/AI/EnemyStates/SnakeAttack.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
10
src/shattered_sword/AI/SnakeAI.ts
Normal file
10
src/shattered_sword/AI/SnakeAI.ts
Normal 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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import WeaponType from "../GameSystems/items/WeaponTypes/WeaponType";
|
||||||
import Weapon from "../GameSystems/items/Weapon";
|
import Weapon from "../GameSystems/items/Weapon";
|
||||||
import BattleManager from "../GameSystems/BattleManager";
|
import BattleManager from "../GameSystems/BattleManager";
|
||||||
import EnemyAI from "../AI/EnemyAI";
|
import EnemyAI from "../AI/EnemyAI";
|
||||||
|
import SnakeAI from "../AI/SnakeAI";
|
||||||
import BattlerAI from "../AI/BattlerAI";
|
import BattlerAI from "../AI/BattlerAI";
|
||||||
import InventoryManager from "../GameSystems/InventoryManager";
|
import InventoryManager from "../GameSystems/InventoryManager";
|
||||||
import Item from "../GameSystems/items/Item";
|
import Item from "../GameSystems/items/Item";
|
||||||
|
@ -878,7 +879,7 @@ export default class GameLevel extends Scene {
|
||||||
for (let enemy of enemies) {
|
for (let enemy of enemies) {
|
||||||
switch (enemy.type) {
|
switch (enemy.type) {
|
||||||
case "Snake": //Snake enemies drop from sky("trees")? or could just be very abundant
|
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,
|
player: this.player,
|
||||||
health: 50,
|
health: 50,
|
||||||
tilemap: "Main",
|
tilemap: "Main",
|
||||||
|
|
|
@ -1,18 +1,8 @@
|
||||||
import { TiledTilemapData } from "../../Wolfie2D/DataTypes/Tilesets/TiledData";
|
|
||||||
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
|
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
|
||||||
import Debug from "../../Wolfie2D/Debug/Debug";
|
|
||||||
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
|
|
||||||
import RandomMapGenerator from "../Tools/RandomMapGenerator";
|
import RandomMapGenerator from "../Tools/RandomMapGenerator";
|
||||||
import GameLevel from "./GameLevel";
|
import GameLevel from "./GameLevel";
|
||||||
import Label from "../../Wolfie2D/Nodes/UIElements/Label";
|
import SnakeAI from "../AI/SnakeAI";
|
||||||
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 Porcelain from "./Porcelain";
|
import Porcelain from "./Porcelain";
|
||||||
import MainMenu from "./MainMenu";
|
|
||||||
|
|
||||||
export default class Tutorial extends GameLevel {
|
export default class Tutorial extends GameLevel {
|
||||||
loadScene(): void {
|
loadScene(): void {
|
||||||
|
@ -39,7 +29,7 @@ export default class Tutorial extends GameLevel {
|
||||||
//spawn snake()
|
//spawn snake()
|
||||||
if(Math.random() < .0001){
|
if(Math.random() < .0001){
|
||||||
console.log("RANDOM SNAKE!");
|
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,
|
player: this.player,
|
||||||
health: 50,
|
health: 50,
|
||||||
tilemap: "Main",
|
tilemap: "Main",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user