Added Player States

This commit is contained in:
OfficialCHenry 2022-04-03 20:31:06 -04:00
parent 11dffbd718
commit 5bc079756d
10 changed files with 199 additions and 2 deletions

View File

@ -12,10 +12,10 @@ import Walk from "./PlayerStates/Walk";
import Debug from "../../Wolfie2D/Debug/Debug"; import Debug from "../../Wolfie2D/Debug/Debug";
export enum PlayerStates { export enum PlayerStates {
IDLE = "idle", IDLE = "idle",
WALK = "walk", WALK = "walk",
RUN = "run",
JUMP = "jump", JUMP = "jump",
FALL = "fall", FALL = "fall",
PREVIOUS = "previous" PREVIOUS = "previous"

View File

@ -0,0 +1,16 @@
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import InAir from "./InAir";
export default class Fall extends InAir {
owner: AnimatedSprite;
onEnter(options: Record<string, any>): void {
this.owner.animation.play("FALL", true);
}
onExit(): Record<string, any> {
this.owner.animation.stop();
return {};
}
}

View File

@ -0,0 +1,34 @@
import Input from "../../../Wolfie2D/Input/Input";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import { PlayerStates } from "../PlayerController";
import OnGround from "./OnGround";
export default class Idle extends OnGround {
owner: AnimatedSprite;
onEnter(options: Record<string, any>): void {
this.parent.speed = this.parent.MIN_SPEED;
}
update(deltaT: number): void {
super.update(deltaT);
this.owner.animation.playIfNotAlready("IDLE", true);
let dir = this.getInputDirection();
if(!dir.isZero() && dir.y === 0){
this.finished(PlayerStates.WALK);
}
this.parent.velocity.x = 0;
this.owner.move(this.parent.velocity.scaled(deltaT));
}
onExit(): Record<string, any> {
this.owner.animation.stop();
return {};
}
}

View File

@ -0,0 +1,20 @@
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import { PlayerStates } from "../PlayerController";
import PlayerState from "./PlayerState";
export default abstract class InAir extends PlayerState {
update(deltaT: number): void {
super.update(deltaT);
let dir = this.getInputDirection();
this.parent.velocity.x += dir.x * this.parent.speed/3.5 - 0.3*this.parent.velocity.x;
this.owner.move(this.parent.velocity.scaled(deltaT));
if(this.owner.onGround){
this.finished(PlayerStates.PREVIOUS);
}
}
}

View File

@ -0,0 +1,37 @@
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import { GameEventType } from "../../../Wolfie2D/Events/GameEventType";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import { EaseFunctionType } from "../../../Wolfie2D/Utils/EaseFunctions";
import { Player_Events } from "../../sword_enums";
import { PlayerStates } from "../PlayerController";
import InAir from "./InAir";
export default class Jump extends InAir {
owner: AnimatedSprite;
onEnter(options: Record<string, any>): void {
this.emitter.fireEvent(GameEventType.PLAY_SOUND, {key: "jump", loop: false, holdReference: false});
}
update(deltaT: number): void {
super.update(deltaT);
this.owner.animation.play("JUMP", true);
if(this.owner.onCeiling){
this.parent.velocity.y = 0;
}
// If we're falling, go to the fall state
if(this.parent.velocity.y >= 0){
this.finished(PlayerStates.FALL);
}
}
onExit(): Record<string, any> {
this.owner.animation.stop();
return {};
}
}

View File

@ -0,0 +1,51 @@
import State from "../../../Wolfie2D/DataTypes/State/State";
import StateMachine from "../../../Wolfie2D/DataTypes/State/StateMachine";
import Vec2 from "../../../Wolfie2D/DataTypes/Vec2";
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import Input from "../../../Wolfie2D/Input/Input";
import GameNode from "../../../Wolfie2D/Nodes/GameNode";
import Timer from "../../../Wolfie2D/Timing/Timer";
import { Player_Events } from "../../sword_enums";
import PlayerController from "../PlayerController";
export default abstract class PlayerState extends State {
owner: GameNode;
gravity: number = 1000; //TODO - can change later
parent: PlayerController;
positionTimer: Timer;
constructor(parent: StateMachine, owner: GameNode){
super(parent);
this.owner = owner;
this.positionTimer = new Timer(250);
this.positionTimer.start();
}
handleInput(event: GameEvent): void {
}
/**
* Get the inputs from the keyboard, or Vec2.Zero if nothing is being pressed
*/
getInputDirection(): Vec2 {
let direction = Vec2.ZERO;
direction.x = (Input.isPressed("left") ? -1 : 0) + (Input.isPressed("right") ? 1 : 0);
direction.y = (Input.isJustPressed("jump") ? -1 : 0);
return direction;
}
update(deltaT: number): void {
// Do gravity
if (this.positionTimer.isStopped()){
this.emitter.fireEvent(Player_Events.PLAYER_MOVE, {position: this.owner.position.clone()});
this.positionTimer.start();
}
this.parent.velocity.y += this.gravity*deltaT;
}
}

View File

@ -0,0 +1,34 @@
import Input from "../../../Wolfie2D/Input/Input";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import { Player_Events } from "../../sword_enums";
import { PlayerStates } from "../PlayerController";
import OnGround from "./OnGround";
export default class Walk extends OnGround {
owner: AnimatedSprite;
onEnter(options: Record<string, any>): void {
this.parent.speed = this.parent.MIN_SPEED;
}
update(deltaT: number): void {
super.update(deltaT);
this.owner.animation.playIfNotAlready("WALK", true);
let dir = this.getInputDirection();
if(dir.isZero()){
this.finished(PlayerStates.IDLE);
}
this.parent.velocity.x = dir.x * this.parent.speed
this.owner.move(this.parent.velocity.scaled(deltaT));
}
onExit(): Record<string, any> {
this.owner.animation.stop();
return {};
}
}

View File

@ -25,7 +25,7 @@ export default class MainMenu extends Scene {
loadScene(): void { loadScene(): void {
// Load the menu song // Load the menu song
//this.load.audio("menu", "hw5_assets/music/menu.mp3"); //this.load.audio("menu", "shattered_sword_assets/music/menu.mp3");
} }
//TODO //TODO

View File

@ -4,7 +4,12 @@ export enum Player_Events {
PLAYER_ATTACK = "PlayerAttack", PLAYER_ATTACK = "PlayerAttack",
PLAYER_DASH = "PlayerDash", PLAYER_DASH = "PlayerDash",
PLAYER_HEAL = "PlayerHeal", PLAYER_HEAL = "PlayerHeal",
LEVEL_START = "LevelStart",
LEVEL_END = "LevelEnd",
PLAYER_KILLED = "PlayerKilled", PLAYER_KILLED = "PlayerKilled",
ENEMY_KILLED = "EnemyKilled",
PLAYER_HIT_ENEMY = "PlayerHitEnemy",
BOSS_KILLED = "BossKilled",
} }
export enum Damage_Type { export enum Damage_Type {
NORMAL_DAMAGE = "NormalDamage", NORMAL_DAMAGE = "NormalDamage",