diff --git a/src/shattered_sword/Player/PlayerController.ts b/src/shattered_sword/Player/PlayerController.ts index a9373d4..87da100 100644 --- a/src/shattered_sword/Player/PlayerController.ts +++ b/src/shattered_sword/Player/PlayerController.ts @@ -12,10 +12,10 @@ import Walk from "./PlayerStates/Walk"; import Debug from "../../Wolfie2D/Debug/Debug"; + export enum PlayerStates { IDLE = "idle", WALK = "walk", - RUN = "run", JUMP = "jump", FALL = "fall", PREVIOUS = "previous" diff --git a/src/shattered_sword/Player/PlayerStates/Attack.ts b/src/shattered_sword/Player/PlayerStates/Attack.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/shattered_sword/Player/PlayerStates/Fall.ts b/src/shattered_sword/Player/PlayerStates/Fall.ts index e69de29..04c6c60 100644 --- a/src/shattered_sword/Player/PlayerStates/Fall.ts +++ b/src/shattered_sword/Player/PlayerStates/Fall.ts @@ -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): void { + this.owner.animation.play("FALL", true); + } + + onExit(): Record { + this.owner.animation.stop(); + return {}; + } +} \ No newline at end of file diff --git a/src/shattered_sword/Player/PlayerStates/Idle.ts b/src/shattered_sword/Player/PlayerStates/Idle.ts index e69de29..214e80c 100644 --- a/src/shattered_sword/Player/PlayerStates/Idle.ts +++ b/src/shattered_sword/Player/PlayerStates/Idle.ts @@ -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): 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 { + this.owner.animation.stop(); + return {}; + } +} \ No newline at end of file diff --git a/src/shattered_sword/Player/PlayerStates/InAir.ts b/src/shattered_sword/Player/PlayerStates/InAir.ts index e69de29..ff3ceb2 100644 --- a/src/shattered_sword/Player/PlayerStates/InAir.ts +++ b/src/shattered_sword/Player/PlayerStates/InAir.ts @@ -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); + } + } +} \ No newline at end of file diff --git a/src/shattered_sword/Player/PlayerStates/Jump.ts b/src/shattered_sword/Player/PlayerStates/Jump.ts index e69de29..54ff2c7 100644 --- a/src/shattered_sword/Player/PlayerStates/Jump.ts +++ b/src/shattered_sword/Player/PlayerStates/Jump.ts @@ -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): 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 { + this.owner.animation.stop(); + return {}; + } +} \ No newline at end of file diff --git a/src/shattered_sword/Player/PlayerStates/PlayerState.ts b/src/shattered_sword/Player/PlayerStates/PlayerState.ts index e69de29..cf46c36 100644 --- a/src/shattered_sword/Player/PlayerStates/PlayerState.ts +++ b/src/shattered_sword/Player/PlayerStates/PlayerState.ts @@ -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; + } +} \ No newline at end of file diff --git a/src/shattered_sword/Player/PlayerStates/Walk.ts b/src/shattered_sword/Player/PlayerStates/Walk.ts index e69de29..28fbc20 100644 --- a/src/shattered_sword/Player/PlayerStates/Walk.ts +++ b/src/shattered_sword/Player/PlayerStates/Walk.ts @@ -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): 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 { + this.owner.animation.stop(); + return {}; + } +} \ No newline at end of file diff --git a/src/shattered_sword/Scenes/MainMenu.ts b/src/shattered_sword/Scenes/MainMenu.ts index 7c964ef..8fb16dc 100644 --- a/src/shattered_sword/Scenes/MainMenu.ts +++ b/src/shattered_sword/Scenes/MainMenu.ts @@ -25,7 +25,7 @@ export default class MainMenu extends Scene { loadScene(): void { // Load the menu song - //this.load.audio("menu", "hw5_assets/music/menu.mp3"); + //this.load.audio("menu", "shattered_sword_assets/music/menu.mp3"); } //TODO diff --git a/src/shattered_sword/sword_enums.ts b/src/shattered_sword/sword_enums.ts index a694489..e1ec18f 100644 --- a/src/shattered_sword/sword_enums.ts +++ b/src/shattered_sword/sword_enums.ts @@ -4,7 +4,12 @@ export enum Player_Events { PLAYER_ATTACK = "PlayerAttack", PLAYER_DASH = "PlayerDash", PLAYER_HEAL = "PlayerHeal", + LEVEL_START = "LevelStart", + LEVEL_END = "LevelEnd", PLAYER_KILLED = "PlayerKilled", + ENEMY_KILLED = "EnemyKilled", + PLAYER_HIT_ENEMY = "PlayerHitEnemy", + BOSS_KILLED = "BossKilled", } export enum Damage_Type { NORMAL_DAMAGE = "NormalDamage",