feat: implemented InputWrapper and replace Input

This commit is contained in:
Renge 2022-04-17 15:42:12 -04:00
parent dca4b275bb
commit f584280f50
12 changed files with 231 additions and 106 deletions

View File

@ -12,11 +12,11 @@ import Walk from "./PlayerStates/Walk";
import Debug from "../../Wolfie2D/Debug/Debug";
import Item from "../GameSystems/items/Item";
import InventoryManager from "../GameSystems/InventoryManager";
import Input from "../../Wolfie2D/Input/Input";
import BattlerAI from "../AI/BattlerAI";
import MathUtils from "../../Wolfie2D/Utils/MathUtils";
import Weapon from "../GameSystems/items/Weapon";
import AnimatedSprite from "../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import InputWrapper from "../Tools/InputWrapper";
export enum PlayerType {
@ -104,15 +104,6 @@ export default class PlayerController extends StateMachineAI implements BattlerA
}
}
/**
* Returns three legal random generate buffs based on current state
* @returns Three buffs
*/
static getBuffs(): Buffs {
// TODO
return undefined;
}
//TODO - balance buff value generation
/**
* returns an array of three randomly generated buffs
@ -241,7 +232,7 @@ export default class PlayerController extends StateMachineAI implements BattlerA
Debug.log("player Coords:", "Player Coords:" +this.owner.position );
//testing the attacks here, may be moved to another place later
if(Input.isJustPressed("attack")){
if(InputWrapper.isAttackJustPressed()){
let item = this.inventory.getItem();
(<AnimatedSprite>this.owner).animation.playIfNotAlready("ATTACK", true);
//TODO - get proper look direction

View File

@ -1,7 +1,7 @@
import GameEvent from "../../../Wolfie2D/Events/GameEvent";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import InAir from "./InAir";
import Input from "../../../Wolfie2D/Input/Input";
import InputWrapper from "../../Tools/InputWrapper";
export default class Fall extends InAir {
owner: AnimatedSprite;
@ -15,7 +15,7 @@ export default class Fall extends InAir {
//TODO - testing doublejump, may have to move to InAir instead
// If we jump, move to the Jump state, give a burst of upwards velocity
if( this.parent.airjumps>0 && Input.isJustPressed("jump")){
if( this.parent.airjumps>0 && InputWrapper.isJumpJustPressed()){
this.parent.airjumps --;
this.finished("jump");
this.parent.velocity.y = -600; // basically jump height

View File

@ -1,4 +1,3 @@
import Input from "../../../Wolfie2D/Input/Input";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import { PlayerStates } from "../PlayerController";
import OnGround from "./OnGround";

View File

@ -3,9 +3,9 @@ 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 InputWrapper from "../../Tools/InputWrapper";
import { PlayerStates } from "../PlayerController";
import InAir from "./InAir";
import Input from "../../../Wolfie2D/Input/Input";
export default class Jump extends InAir {
owner: AnimatedSprite;
@ -27,7 +27,7 @@ export default class Jump extends InAir {
//TODO - testing doublejump, may have to move to InAir instead
// If we jump, move to the Jump state, give a burst of upwards velocity
if( this.parent.airjumps>0 && Input.isJustPressed("jump")){
if( this.parent.airjumps>0 && InputWrapper.isJumpJustPressed()){
this.parent.airjumps --;
this.finished("jump");
this.parent.velocity.y = -600; // basically jump height

View File

@ -1,7 +1,7 @@
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 InputWrapper from "../../Tools/InputWrapper";
import PlayerState from "./PlayerState";
export default class OnGround extends PlayerState {
@ -26,7 +26,7 @@ export default class OnGround extends PlayerState {
// If we jump, move to the Jump state, give a burst of upwards velocity
if(Input.isJustPressed("jump")){
if(InputWrapper.isJumpJustPressed()){
this.finished("jump");
this.parent.velocity.y = -600; // basically jump height

View File

@ -2,10 +2,10 @@ 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 InputWrapper from "../../Tools/InputWrapper";
import PlayerController from "../PlayerController";
@ -32,8 +32,8 @@ export default abstract class PlayerState extends State {
*/
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);
direction.x = (InputWrapper.isLeftPressed() ? -1 : 0) + (InputWrapper.isRightPressed() ? 1 : 0);
direction.y = (InputWrapper.isJumpJustPressed() ? -1 : 0);
return direction;
}

View File

@ -1,6 +1,6 @@
import Input from "../../../Wolfie2D/Input/Input";
import AnimatedSprite from "../../../Wolfie2D/Nodes/Sprites/AnimatedSprite";
import { Player_Events } from "../../sword_enums";
import InputWrapper from "../../Tools/InputWrapper";
import { PlayerStates } from "../PlayerController";
import OnGround from "./OnGround";
@ -26,7 +26,7 @@ export default class Walk extends OnGround {
this.parent.velocity.x = dir.x * (this.parent.speed + this.parent.CURRENT_BUFFS.speed);
//TODO - decide how to implement dash - could be a flash - maybe allow in air as well
if(Input.isJustPressed("dash")){
if(InputWrapper.isDashJustPressed()){
//play dash anim maybe
//TODO - might give buffed speed stat to dash speed
this.parent.velocity.x = dir.x * 1000; //give sidewards velocity

View File

@ -2,7 +2,6 @@ import AABB from "../../Wolfie2D/DataTypes/Shapes/AABB";
import Vec2 from "../../Wolfie2D/DataTypes/Vec2";
import Debug from "../../Wolfie2D/Debug/Debug";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
import Input from "../../Wolfie2D/Input/Input";
import { TweenableProperties } from "../../Wolfie2D/Nodes/GameNode";
import { GraphicType } from "../../Wolfie2D/Nodes/Graphics/GraphicTypes";
import Point from "../../Wolfie2D/Nodes/Graphics/Point";
@ -16,7 +15,7 @@ import Color from "../../Wolfie2D/Utils/Color";
import { EaseFunctionType } from "../../Wolfie2D/Utils/EaseFunctions";
import PlayerController from "../Player/PlayerController";
import MainMenu from "./MainMenu";
import { Player_Events, Statuses } from "../sword_enums";
import { GameState, Player_Events, Statuses } from "../sword_enums";
import RegistryManager from "../../Wolfie2D/Registry/RegistryManager";
import WeaponType from "../GameSystems/items/WeaponTypes/WeaponType";
import Weapon from "../GameSystems/items/Weapon";
@ -30,6 +29,8 @@ import Button from "../../Wolfie2D/Nodes/UIElements/Button";
import { Buff } from "../Player/PlayerController";
import CanvasNode from "../../Wolfie2D/Nodes/CanvasNode";
import { Enemy } from "../Tools/RandomMapGenerator";
import Stack from "../../Wolfie2D/DataTypes/Stack";
import InputWrapper from "../Tools/InputWrapper";
@ -75,6 +76,8 @@ export default class GameLevel extends Scene {
// A list of enemies
protected enemies: Array<AnimatedSprite>;
protected gameStateStack: Stack<GameState>;
//buffs layer
buffLayer: Layer;
buffButton1 : Button;
@ -170,49 +173,62 @@ export default class GameLevel extends Scene {
//TODO - uncomment when done testing
// Initially disable player movement
//Input.disableInput();
Input.enableInput();
this.gameStateStack = new Stack();
this.setGameState(GameState.GAMING);
InputWrapper.enableInput();
}
updateScene(deltaT: number){
// Handle events and update the UI if needed
while(this.receiver.hasNextEvent()){
let event = this.receiver.getNextEvent();
switch(event.type){
case Player_Events.ENEMY_KILLED:
if (this.gameStateStack.peek() == GameState.GAMING) {
switch(event.type){
case Player_Events.ENEMY_KILLED:
let node = this.sceneGraph.getNode(event.data.get("owner"));//get enemy id
//remove enemy from enemies
this.enemies = this.enemies.filter(item => item !== event.data.get("ai"));
this.battleManager.removeEnemy(event.data.get("ai"));
node.destroy();
//TODO - this is for testing, add some chance here later
this.emitter.fireEvent(Player_Events.GIVE_BUFF);
break;
let node = this.sceneGraph.getNode(event.data.get("owner"));//get enemy id
//remove enemy from enemies
this.enemies = this.enemies.filter(item => item !== event.data.get("ai"));
this.battleManager.removeEnemy(event.data.get("ai"));
node.destroy();
//TODO - this is for testing, add some chance here later
this.emitter.fireEvent(Player_Events.GIVE_BUFF);
break;
case Player_Events.GIVE_BUFF:
this.buffs = PlayerController.generateBuffs();
this.buffButton1.text = "Increase "+this.buffs[0].type.toString() + " by "+this.buffs[0].value;
this.buffButton2.text = "Increase "+this.buffs[1].type + " by "+this.buffs[1].value;
this.buffButton3.text = "Increase "+this.buffs[2].type + " by "+this.buffs[2].value;
case Player_Events.GIVE_BUFF:
this.buffs = PlayerController.generateBuffs();
this.buffButton1.text = "Increase "+this.buffs[0].type.toString() + " by "+this.buffs[0].value;
this.buffButton2.text = "Increase "+this.buffs[1].type + " by "+this.buffs[1].value;
this.buffButton3.text = "Increase "+this.buffs[2].type + " by "+this.buffs[2].value;
//pause game here
this.buffLayer.enable();
//pause game here
this.setGameState(GameState.BUFF);
this.buffLayer.enable();
break;
}
}
break;
case "buff1":
(<PlayerController>this.player._ai).addBuff(this.buffs[0]);
this.buffLayer.disable();
break;
case "buff2":
(<PlayerController>this.player._ai).addBuff(this.buffs[1]);
this.buffLayer.disable();
break;
case "buff3":
(<PlayerController>this.player._ai).addBuff(this.buffs[2]);
this.buffLayer.disable();
break;
else if (this.gameStateStack.peek() == GameState.BUFF) {
switch(event.type){
case "buff1":
(<PlayerController>this.player._ai).addBuff(this.buffs[0]);
this.buffLayer.disable();
this.setGameState();
break;
case "buff2":
(<PlayerController>this.player._ai).addBuff(this.buffs[1]);
this.buffLayer.disable();
this.setGameState();
break;
case "buff3":
(<PlayerController>this.player._ai).addBuff(this.buffs[2]);
this.buffLayer.disable();
this.setGameState();
break;
}
}
}
@ -231,7 +247,7 @@ export default class GameLevel extends Scene {
this.playerFalloff(viewportCenter, baseViewportSize);
//TODO - this is for testing
if(Input.isJustPressed("spawn")){
if(InputWrapper.isSpawnJustPressed()){
console.log("trying to spawn enemy");
this.addEnemy("test_dummy",this.player.position,{player: this.player,
health :100,
@ -245,6 +261,18 @@ export default class GameLevel extends Scene {
}
// TODO put UI changes in here
protected setGameState(gameState?: GameState) {
if (gameState) {
this.gameStateStack.push(gameState);
InputWrapper.setState(gameState);
}
else {
this.gameStateStack.pop();
InputWrapper.setState(this.gameStateStack.peek());
}
}
/**
* Initialzes the layers
*/
@ -603,7 +631,7 @@ export default class GameLevel extends Scene {
GameLevel.livesCount += amt;
this.livesCountLabel.text = "Lives: " + GameLevel.livesCount;
if (GameLevel.livesCount == 0){
Input.disableInput();
InputWrapper.disableInput();
this.player.disablePhysics();
this.emitter.fireEvent(GameEventType.PLAY_SOUND, {key: "player_death", loop: false, holdReference: false});
this.player.tweens.play("death");
@ -617,7 +645,7 @@ export default class GameLevel extends Scene {
GameLevel.livesCount = 3;
this.emitter.fireEvent(GameEventType.STOP_SOUND, {key: "level_music"});
this.sceneManager.changeToScene(MainMenu, {});
Input.enableInput();
InputWrapper.enableInput();
}
@ -637,3 +665,4 @@ export default class GameLevel extends Scene {
}

View File

@ -7,7 +7,7 @@ import Color from "../../Wolfie2D/Utils/Color";
import Layer from "../../Wolfie2D/Scene/Layer";
import Sprite from "../../Wolfie2D/Nodes/Sprites/Sprite";
import { GameEventType } from "../../Wolfie2D/Events/GameEventType";
import Input from "../../Wolfie2D/Input/Input";
import InputWrapper from "../Tools/InputWrapper";
enum Mode {
@ -20,7 +20,6 @@ export default class SceneWithStory extends Scene {
private currentMode: Mode = Mode.GAME_MODE;
private storytextLabel: Label;
private storyLayer: Layer;
private primary: Layer;
private story: Story;
private storyProgress: number;
private storySprites: Array<Sprite>;
@ -29,12 +28,8 @@ export default class SceneWithStory extends Scene {
private currentContent: string;
startScene(): void {
// The code below are for testing only. Please comment them when submit
this.primary = this.addUILayer("primary");
const center = this.viewport.getCenter();
const loadStory = this.add.uiElement(UIElementType.BUTTON, "primary", { position: new Vec2(center.x, center.y), text: "LoadStory" });
loadStory.size.set(200, 50);
@ -56,21 +51,12 @@ export default class SceneWithStory extends Scene {
* @param storyPath The path to the story JSON
*/
async storyLoader(storyPath: string) {
// I may want to load multiple stories in a single scene, but this
// Layer with name story already exists
// so can i detect whether this layer exists?
const response = await (await fetch(storyPath)).json();
this.story = <Story>response;
console.log("story:", this.story);
if (this.story.bgm) {
this.storyBGMs = new Array;
this.story.bgm.forEach((bgm) => {
// this.load.audio(bgm.key, bgm.path);
// console.log("audio:", bgm.key, "path:", bgm.path);
// this.load.loadResourcesFromQueue(() => {
// console.log("finished loading audio");
// this.emitter.fireEvent(GameEventType.PLAY_SOUND, { key: bgm.key, loop: false, holdReference: true });
// });
if (this.load.getAudio(bgm.key)) {
this.emitter.fireEvent(GameEventType.PLAY_SOUND, { key: bgm.key, loop: false, holdReference: true });
@ -115,13 +101,6 @@ export default class SceneWithStory extends Scene {
this.story.texts[this.storyProgress].actions.forEach(action => {
switch (action.type) {
case "loadSprite":
// this.load.image(action.key, action.path);
// this.load.loadResourcesFromQueue(() => {
// tmp = this.add.sprite(action.key, "story");
// tmp.position.set(action.positon[0], action.positon[1]);
// tmp.scale.set(action.scale[0], action.scale[1]);
// this.storySprites.push(tmp);
// });
if (this.load.getImage(action.key)) {
tmp = this.add.sprite(action.key, "story");
tmp.position.set(action.positon[0], action.positon[1]);
@ -137,15 +116,6 @@ export default class SceneWithStory extends Scene {
})
}
break;
// case "loadAnimatedSprite":
// this.load.spritesheet(action.key, action.path);
// this.load.loadResourcesFromQueue(() => {
// tmp = this.add.animatedSprite(action.key, "story");
// tmp.position.set(action.positon[0], action.positon[1]);
// tmp.scale.set(action.scale[0], action.scale[1]);
// this.storySprites.push(tmp);
// });
// break;
case "moveSprite":
tmp = this.storySprites.find(function (sprite) {
return sprite.imageId === action.key;
@ -208,7 +178,7 @@ export default class SceneWithStory extends Scene {
}
}
// Testing code
if (Input.isMouseJustPressed() && this.currentMode === Mode.STORY_MODE) {
if (InputWrapper.isNextJustPressed() && this.currentMode === Mode.STORY_MODE) {
this.updateStory();
}
}

View File

@ -10,7 +10,7 @@ import Color from "../../Wolfie2D/Utils/Color";
import Layer from "../../Wolfie2D/Scene/Layer";
import Label from "../../Wolfie2D/Nodes/UIElements/Label";
import Levels from "./Levels";
import Input from "../../Wolfie2D/Input/Input";
import InputWrapper from "../Tools/InputWrapper";
export default class MainMenu extends Scene {
@ -73,7 +73,7 @@ export default class MainMenu extends Scene {
while(this.receiver.hasNextEvent()){
let event = this.receiver.getNextEvent();
console.log(event);
if (Input.isMouseJustPressed(0)) { //if left click
if (InputWrapper.isMouseJustPressed(0)) { //if left click
this.sceneManager.changeToScene(MainMenu, {}, {});
}

View File

@ -1,10 +1,58 @@
import Input from "../../Wolfie2D/Input/Input";
import {GameState} from "../sword_enums";
export default class InputWrapper {
private static isInStoryMode: boolean;
private static gameState: GameState = GameState.GAMING;
constructor() {
InputWrapper.isInStoryMode = false;
static isUpPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isPressed("up")) {
return true;
}
return false;
}
static isDownPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isPressed("down")) {
return true;
}
return false;
}
static isLeftPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isPressed("left")) {
return true;
}
return false;
}
static isRightPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isPressed("right")) {
return true;
}
return false;
}
static isJumpJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isJustPressed("jump")) {
return true;
}
return false;
}
/**
@ -12,15 +60,96 @@ export default class InputWrapper {
* @returns True if the attack key is pressed, false otherwise
*/
static isAttackJustPressed(): boolean {
if (InputWrapper.isInStoryMode) {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
// TODO
return undefined;
if (Input.isJustPressed("attack")) {
return true;
}
return false;
}
static setStoryMode(storyMode: boolean): void {
InputWrapper.isInStoryMode = storyMode;
static isDashJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isJustPressed("dash")) {
return true;
}
return false;
}
static isSkillJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isJustPressed("skill")) {
return true;
}
return false;
}
static isInventoryJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isJustPressed("attack")) {
return true;
}
return false;
}
static isSpawnJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING) {
return false;
}
if (Input.isJustPressed("spawn")) {
return true;
}
return false;
}
static isPauseJustPressed(): boolean {
if (InputWrapper.gameState != GameState.GAMING && InputWrapper.gameState != GameState.PAUSE) {
return false;
}
if (Input.isJustPressed("pause")) {
return true;
}
return false;
}
static isNextJustPressed(): boolean {
if (InputWrapper.gameState != GameState.STORY) {
return false;
}
if (Input.isJustPressed("attack") || Input.isMouseJustPressed()) {
return true;
}
return false;
}
static isMouseJustPressed(mouseButton?: number): boolean{
return Input.isMouseJustPressed(mouseButton);
}
static disableInput() {
Input.disableInput();
}
static enableInput() {
Input.enableInput();
}
static setState(gameState: GameState): void {
InputWrapper.gameState = gameState;
}
}

View File

@ -24,3 +24,10 @@ export enum Statuses {
CAN_RETREAT = "CAN_RETREAT",
REACHED_GOAL = "GOAL"
}
export enum GameState {
GAMING = "gaming",
STORY = "story",
BUFF = "buff",
PAUSE = "pause"
}