Skip to content

Commit

Permalink
Added working menu (though bug on how the options are displayed), and…
Browse files Browse the repository at this point in the history
… some controller input
  • Loading branch information
zkfazal committed Apr 17, 2024
1 parent af67157 commit 8c542aa
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 42 deletions.
42 changes: 29 additions & 13 deletions public/bundle.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -88401,6 +88401,7 @@ function isCollidingObject(obj) {\r
this.controlManager = new controlManager_default();
}
boot() {
console.log("This is: ", this);
this.eventEmitter = this.systems.events;
this.events = new Phaser.Events.EventEmitter();
this.game.events.on(Phaser.Core.Events.PRE_STEP, this.preupdate, this);
Expand Down Expand Up @@ -89188,9 +89189,9 @@ function isCollidingObject(obj) {\r
this.load.scenePlugin("mergedInput", main_default);
}
create() {
this.player1 = this.mergedInput.addPlayer(0);
this.player2 = this.mergedInput.addPlayer(1);
this.mergedInput.defineKey(0, "UP", "W").defineKey(0, "DOWN", "S").defineKey(0, "LEFT", "A").defineKey(0, "RIGHT", "D").defineKey(1, "UP", "UP").defineKey(1, "DOWN", "DOWN").defineKey(1, "LEFT", "LEFT").defineKey(1, "RIGHT", "RIGHT").defineKey(0, "RC_S", "ONE").defineKey(0, "RC_E", "TWO").defineKey(0, "RC_W", "THREE").defineKey(0, "RC_N", "FOUR").defineKey(0, "LB", "FIVE").defineKey(0, "RB", "SIX").defineKey(0, "LT", "SEVEN").defineKey(0, "RT", "EIGHT").defineKey(0, "START", "NINE").defineKey(0, "SELECT", "ZERO").defineKey(0, "B12", "ESC").defineKey(1, "B0", "NUMPAD_ONE").defineKey(1, "B1", "NUMPAD_TWO").defineKey(1, "B2", "NUMPAD_THREE").defineKey(1, "B3", "NUMPAD_FOUR").defineKey(1, "B4", "NUMPAD_FIVE").defineKey(1, "B5", "NUMPAD_SIX").defineKey(1, "B6", "NUMPAD_SEVEN").defineKey(1, "B7", "NUMPAD_EIGHT").defineKey(1, "B8", "NUMPAD_NINE").defineKey(1, "B9", "NUMPAD_ZERO");
this.player1 = this.mergedInput?.addPlayer(0);
this.player2 = this.mergedInput?.addPlayer(1);
this.mergedInput?.defineKey(0, "UP", "W").defineKey(0, "DOWN", "S").defineKey(0, "LEFT", "A").defineKey(0, "RIGHT", "D").defineKey(1, "UP", "UP").defineKey(1, "DOWN", "DOWN").defineKey(1, "LEFT", "LEFT").defineKey(1, "RIGHT", "RIGHT").defineKey(0, "RC_S", "ONE").defineKey(0, "RC_E", "TWO").defineKey(0, "RC_W", "THREE").defineKey(0, "RC_N", "FOUR").defineKey(0, "LB", "FIVE").defineKey(0, "RB", "SIX").defineKey(0, "LT", "SEVEN").defineKey(0, "RT", "EIGHT").defineKey(0, "START", "NINE").defineKey(0, "SELECT", "ZERO").defineKey(0, "B12", "ESC").defineKey(1, "B0", "NUMPAD_ONE").defineKey(1, "B1", "NUMPAD_TWO").defineKey(1, "B2", "NUMPAD_THREE").defineKey(1, "B3", "NUMPAD_FOUR").defineKey(1, "B4", "NUMPAD_FIVE").defineKey(1, "B5", "NUMPAD_SIX").defineKey(1, "B6", "NUMPAD_SEVEN").defineKey(1, "B7", "NUMPAD_EIGHT").defineKey(1, "B8", "NUMPAD_NINE").defineKey(1, "B9", "NUMPAD_ZERO");
}
};

Expand Down Expand Up @@ -89272,8 +89273,11 @@ function isCollidingObject(obj) {\r
background;
logo;
title;
menuOptions = [];
selectedMenuOption = 0;
mergedInput;
inputController;
enterKey;
player1;
player2;
players;
Expand All @@ -89295,13 +89299,14 @@ function isCollidingObject(obj) {\r
});
this.add.sprite(0, 0, "mainmenu_001").setOrigin(0, 0).play("mainmenu");
this.inputController = this.scene.get("InputController");
this.enterKey = this.input.keyboard?.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);
this.player1 = this.inputController.player1;
this.player2 = this.inputController.player2;
this.players = [this.player1, this.player2];
this.inputController.mergedInput?.events.on("gamepad_keydown", ({key, player}) => {
console.log(this.inputController.mergedInput?.getPlayerIndexFromKey(key));
let pressedButton = this.inputController.mergedInput?.getPlayerButtonFromKey(key);
console.log(key, player, pressedButton);
console.log("Key is:", key, player, pressedButton);
});
console.log(width / 12, width / 4 + 50, width / 3 + 25, width / 3 + 100, height);
this.add.text(width / 2, height / 6, "Brutal Brawl", {
Expand All @@ -89312,37 +89317,51 @@ function isCollidingObject(obj) {\r
strokeThickness: 8,
align: "center"
}).setOrigin(0.5, 0.5).setShadow(15, 15, "#111111", 4, true, true);
this.add.text(width / 2, height / 2, "Classic", {
const classicOption = this.add.text(width / 2, height / 2, "Classic", {
fontFamily: "dragon_woff",
fontSize: 70,
color: "#800000",
stroke: "#111111",
strokeThickness: 2,
align: "center"
}).setOrigin(0.5, 0.5);
this.add.text(width / 2, height / 2 + 75, "Survive", {
this.menuOptions.push(classicOption);
const surviveOption = this.add.text(width / 2, height / 2 + 75, "Survive", {
fontFamily: "dragon_woff",
fontSize: 70,
color: "#000000",
stroke: "#111111",
strokeThickness: 0,
align: "center"
}).setOrigin(0.5, 0.5);
this.add.text(width / 2, height / 2 + 150, "Options", {
this.menuOptions.push(surviveOption);
const optionsOption = this.add.text(width / 2, height / 2 + 150, "Options", {
fontFamily: "dragon_woff",
fontSize: 70,
color: "#000000",
stroke: "#111111",
strokeThickness: 0,
align: "center"
}).setOrigin(0.5, 0.5);
console.log(this.input.gamepad?.getAll());
this.menuOptions.push(optionsOption);
this.selectButton(0);
}
selectButton(index) {
const currentOption = this.menuOptions[this.selectedMenuOption];
currentOption.setColor("#000000");
currentOption.setStroke("#111111", 0);
const newOption = this.menuOptions[index];
newOption.setColor("#800000");
currentOption.setStroke("#111111", 2);
this.selectedMenuOption = index;
}
selectNextButton(change = 1) {
let index = (this.selectedMenuOption + change + this.menuOptions.length) % this.menuOptions.length;
this.selectButton(index);
}
confirmSelection() {
console.log("uwu");
this.scene.start("Game");
}
update(time, delta) {
if (this.player2.interaction_mapped.isPressed(["DOWN", "LC_S"]) || this.player1.interaction_mapped.isPressed(["DOWN", "LC_S"])) {
Expand All @@ -89351,6 +89370,8 @@ function isCollidingObject(obj) {\r
} else if (this.player2.interaction_mapped.isPressed(["UP", "LC_N"]) || this.player1.interaction_mapped.isPressed(["UP", "LC_N"])) {
console.log("up");
this.selectNextButton(-1);
} else if (this.player2.interaction_mapped.isPressed(["RC_S"]) || this.player1.interaction_mapped.isPressed(["RC_S"]) || Phaser.Input.Keyboard.JustDown(this.enterKey)) {
this.confirmSelection();
}
if (this.player1.direction.LEFT > 0 || this.player2.direction.LEFT > 0) {
console.log("left");
Expand Down Expand Up @@ -89413,11 +89434,6 @@ function isCollidingObject(obj) {\r
plugin: import_phaser_matter_collision_plugin.default,
key: "matterCollision",
mapping: "matterCollision"
},
{
key: "mergedInput",
plugin: main_default,
mapping: "mergedInput"
}
],
global: [
Expand Down
9 changes: 4 additions & 5 deletions src/controller/InputController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import MergedInput from "../../node_modules/phaser3-merged-input/src/main";
import MergedInput from 'phaser3-merged-input';

export class InputController extends Phaser.Scene {
debugScene?: Phaser.Scene;
Expand Down Expand Up @@ -33,15 +33,14 @@ export class InputController extends Phaser.Scene {
*/
create?() {
// Setup player objects
this.player1 = this.mergedInput.addPlayer(0);
this.player2 = this.mergedInput.addPlayer(1);
this.player1 = this.mergedInput?.addPlayer(0);
this.player2 = this.mergedInput?.addPlayer(1);

/**
* Define keys (player, action, key, append)
* This scene would also be a good place to handle updates of key definitions, so that a player can redefine keys or gamepad mappings.
*/
this.mergedInput
.defineKey(0, 'UP', 'W')
this.mergedInput?.defineKey(0, 'UP', 'W')
.defineKey(0, 'DOWN', 'S')
.defineKey(0, 'LEFT', 'A')
.defineKey(0, 'RIGHT', 'D')
Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ const config: Types.Core.GameConfig = {
key: "matterCollision", // Where to store in Scene.Systems, e.g. scene.sys.matterCollision
mapping: "matterCollision" // Where to store in the Scene, e.g. scene.matterCollision
},
{
key: "mergedInput",
plugin: MergedInput,
mapping: "mergedInput",
},
// {
// key: "mergedInput",
// plugin: MergedInput,
// mapping: "mergedInput",
// },
],
global: [
{
Expand Down
52 changes: 33 additions & 19 deletions src/scenes/MainMenu.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Scene, GameObjects } from 'phaser';
import MergedInput from 'phaser3-merged-input';
import MergedInput, { Player } from 'phaser3-merged-input';
import { InputController } from '../controller/InputController';

export class MainMenu extends Scene
{
background: GameObjects.Image;
logo: GameObjects.Image;
title: GameObjects.Text;
private menuOptions: GameObjects.Text[] = [];
private selectedMenuOption = 0;
public mergedInput?: MergedInput;
inputController: InputController;
player1: any;
player2: any;
players: any[];
private enterKey: Phaser.Input.Keyboard.Key;
player1: Player;
player2: Player;
players: Player[];

constructor ()
{
Expand All @@ -20,12 +23,6 @@ export class MainMenu extends Scene

create ()
{
/*this.input.once('pointerdown', () => {
this.scene.start('Game');
});*/

const { width, height } = this.scale;

this.anims.create(
Expand All @@ -44,6 +41,7 @@ export class MainMenu extends Scene

// Get the input controller scene, which contains our player objects
this.inputController = this.scene.get('InputController');
this.enterKey = this.input.keyboard?.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER)!;

// Get our player objects
this.player1 = this.inputController.player1;
Expand All @@ -61,7 +59,7 @@ export class MainMenu extends Scene
console.log(this.inputController.mergedInput?.getPlayerIndexFromKey(key));
let pressedButton = this.inputController.mergedInput?.getPlayerButtonFromKey(key);

console.log(key, player, pressedButton);
console.log("Key is:", key, player, pressedButton);

});

Expand All @@ -75,44 +73,56 @@ export class MainMenu extends Scene
align: 'center'
}).setOrigin(0.5, 0.5).setShadow(15, 15, "#111111", 4, true, true);

this.add.text(width/2, height/2, 'Classic', {
const classicOption = this.add.text(width/2, height/2, 'Classic', {
fontFamily: 'dragon_woff', fontSize: 70, color: '#800000',
stroke: '#111111', strokeThickness: 2,
align: 'center'
}).setOrigin(0.5, 0.5);
this.menuOptions.push(classicOption);

this.add.text(width/2, height/2+75, 'Survive', {
const surviveOption = this.add.text(width/2, height/2+75, 'Survive', {
fontFamily: 'dragon_woff', fontSize: 70, color: '#000000',
stroke: '#111111', strokeThickness: 0,
align: 'center'
}).setOrigin(0.5, 0.5);
this.menuOptions.push(surviveOption);

this.add.text(width/2, height/2+150, 'Options', {
const optionsOption = this.add.text(width/2, height/2+150, 'Options', {
fontFamily: 'dragon_woff', fontSize: 70, color: '#000000',
stroke: '#111111', strokeThickness: 0,
align: 'center'
}).setOrigin(0.5, 0.5);
this.menuOptions.push(optionsOption);

console.log(this.input.gamepad?.getAll());
this.selectButton(0);
}

selectButton(index: number)
{
// TODO
const currentOption = this.menuOptions[this.selectedMenuOption];
currentOption.setColor('#000000');
currentOption.setStroke('#111111', 0);

const newOption = this.menuOptions[index];
newOption.setColor('#800000');
currentOption.setStroke('#111111', 2);

this.selectedMenuOption = index;
}

selectNextButton(change = 1)
{
// TODO
let index = ( (this.selectedMenuOption + change ) + this.menuOptions.length ) % this.menuOptions.length;
this.selectButton(index)
}

confirmSelection()
{
// TODO
console.log('uwu')
this.scene.start('Game');
}

update(time: number, delta: number): void {
//console.log(this.player1, this.player2);
if (this.player2.interaction_mapped.isPressed(['DOWN', 'LC_S']) || this.player1.interaction_mapped.isPressed(['DOWN', 'LC_S'])) {
console.log("down");
this.selectNextButton(1)
Expand All @@ -121,6 +131,10 @@ export class MainMenu extends Scene
console.log("up");
this.selectNextButton(-1)
}
else if (this.player2.interaction_mapped.isPressed(['RC_S']) || this.player1.interaction_mapped.isPressed(['RC_S'])
|| Phaser.Input.Keyboard.JustDown(this.enterKey)) {
this.confirmSelection();
}

if( (this.player1.direction.LEFT > 0) || (this.player2.direction.LEFT > 0) ){
console.log("left");
Expand Down

0 comments on commit 8c542aa

Please sign in to comment.