Added Player States

This commit is contained in:
OfficialCHenry 2022-04-03 17:43:50 -04:00
parent 9b335ba6ea
commit 87ff0d5ec7
8 changed files with 109 additions and 2 deletions

View File

@ -3,6 +3,23 @@ import GameNode, { TweenableProperties } from "../../Wolfie2D/Nodes/GameNode";
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
import OrthogonalTilemap from "../../Wolfie2D/Nodes/Tilemaps/OrthogonalTilemap";
import { Player_Events } from "../sword_enums";
import Fall from "./PlayerStates/Fall";
import Idle from "./PlayerStates/Idle";
import InAir from "./PlayerStates/InAir";
import Jump from "./PlayerStates/Jump";
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"
}
export enum BuffType {
ATK = "attack",
@ -22,6 +39,9 @@ type Buffs = [
export default class PlayerController extends StateMachineAI {
protected owner: GameNode;
velocity: Vec2 = Vec2.ZERO;
speed: number = 200;
MIN_SPEED: number = 200;
MAX_SPEED: number = 300;
BASE_HP: number = 100;
MAX_HP: number = 100;
@ -32,6 +52,7 @@ export default class PlayerController extends StateMachineAI {
BASE_DEF: number = 100;
MAX_DEF: number = 100;
CURRENT_DEF: number = 100;
tilemap: OrthogonalTilemap;
CURRENT_BUFFS: {
atk: 0;
@ -39,8 +60,8 @@ export default class PlayerController extends StateMachineAI {
def: 0;
speed: 0;
}
velocity: Vec2 = Vec2.ZERO;
tilemap: OrthogonalTilemap;
/**
* Returns three legal random generate buffs based on current state
@ -58,4 +79,55 @@ export default class PlayerController extends StateMachineAI {
setBuff(buff: Buff): void {
// TODO
}
//TODO - get the correct tilemap
initializeAI(owner: GameNode, options: Record<string, any>){
this.owner = owner;
this.initializePlatformer();
this.tilemap = this.owner.getScene().getTilemap(options.tilemap) as OrthogonalTilemap;
}
initializePlatformer(): void {
this.speed = 400;
let idle = new Idle(this, this.owner);
this.addState(PlayerStates.IDLE, idle);
let walk = new Walk(this, this.owner);
this.addState(PlayerStates.WALK, walk);
let jump = new Jump(this, this.owner);
this.addState(PlayerStates.JUMP, jump);
let fall = new Fall(this, this.owner);
this.addState(PlayerStates.FALL, fall);
this.initialize(PlayerStates.IDLE);
}
changeState(stateName: string): void {
// If we jump or fall, push the state so we can go back to our current state later
// unless we're going from jump to fall or something
if((stateName === PlayerStates.JUMP || stateName === PlayerStates.FALL) && !(this.stack.peek() instanceof InAir)){
this.stack.push(this.stateMap.get(stateName));
}
super.changeState(stateName);
}
update(deltaT: number): void {
super.update(deltaT);
if(this.currentState instanceof Jump){
Debug.log("playerstate", "Player State: Jump");
} else if (this.currentState instanceof Walk){
Debug.log("playerstate", "Player State: Walk");
} else if (this.currentState instanceof Idle){
Debug.log("playerstate", "Player State: Idle");
} else if(this.currentState instanceof Fall){
Debug.log("playerstate", "Player State: Fall");
}
}
}

View File

@ -0,0 +1,35 @@
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import Input from "../../../Wolfie2D/Input/Input";
import Sprite from "../../../Wolfie2D/Nodes/Sprites/Sprite";
import MathUtils from "../../../Wolfie2D/Utils/MathUtils";
import PlayerState from "./PlayerState";
export default class OnGround extends PlayerState {
onEnter(options: Record<string, any>): void {}
update(deltaT: number): void {
if(this.parent.velocity.y > 0){
this.parent.velocity.y = 0;
}
super.update(deltaT);
let direction = this.getInputDirection();
if(direction.x !== 0){
(<Sprite>this.owner).invertX = MathUtils.sign(direction.x) < 0;
}
// If we jump, move to the Jump state, give a burst of upwards velocity
if(Input.isJustPressed("jump")){
this.finished("jump");
this.parent.velocity.y = -500;
} else if(!this.owner.onGround){
this.finished("fall");
}
}
onExit(): Record<string, any> {
return {};
}
}