summaryrefslogtreecommitdiff
path: root/client/src/akkamon/scenes/WorldScene.ts
diff options
context:
space:
mode:
authorMike Vink <mike1994vink@gmail.com>2021-07-28 16:52:08 +0200
committerMike Vink <mike1994vink@gmail.com>2021-07-28 16:52:08 +0200
commit9e675c3652eb7a16ce5c2a865c030c76653c921e (patch)
tree6ae24d621eb38b832ba6d6595d2ad87b6f1ec5aa /client/src/akkamon/scenes/WorldScene.ts
parent4d84c12df52c89abb7d3ba9f565753116b99dbf0 (diff)
feat(): refactor after playing with mixins
Diffstat (limited to 'client/src/akkamon/scenes/WorldScene.ts')
-rw-r--r--client/src/akkamon/scenes/WorldScene.ts167
1 files changed, 167 insertions, 0 deletions
diff --git a/client/src/akkamon/scenes/WorldScene.ts b/client/src/akkamon/scenes/WorldScene.ts
new file mode 100644
index 0000000..fd866aa
--- /dev/null
+++ b/client/src/akkamon/scenes/WorldScene.ts
@@ -0,0 +1,167 @@
+import Phaser from 'phaser';
+
+import { client } from '../../app';
+
+import { PauseMenu, AkkamonMenu } from '../scenes/UIElement';
+
+import type {
+ BasePhaserScene
+} from '../PhaserTypes';
+
+import {
+ Stack,
+ baseStack
+} from '../DataWrappers';
+
+export let TILE_SIZE = 32;
+
+export interface WorldScene extends Phaser.Scene {
+
+ client: typeof client;
+
+ map?: Phaser.Tilemaps.Tilemap
+ spawnPoint?: Phaser.Types.Tilemaps.TiledObject;
+ spawnPointTilePos?: {
+ x: number,
+ y: number
+ };
+
+
+ create: () => void
+
+ menus: Stack<AkkamonMenu>;
+
+ menuTakesUIControl: (input: Phaser.Input.InputPlugin, menu: AkkamonMenu) => void
+
+ isUsingGridControls: () => void
+
+ pushMenu: (menu: AkkamonMenu) => void
+ popMenu: () => void
+
+ traverseMenusBackwards: () => void
+
+ getPlayerPixelPosition: () => Phaser.Math.Vector2
+
+ getRemotePlayerNames: () => string[]
+
+ requestBattle: (remotePlayerData: string | string[]) => void
+
+ clearMenus: () => void
+}
+
+
+export function createWorldScene<PhaserScene extends BasePhaserScene>(scene: PhaserScene, sceneKey: string, mapKey: string, tileSetKey: string) {
+ return class WorldScene extends scene implements WorldScene {
+ map?: Phaser.Tilemaps.Tilemap;
+
+ client = client;
+
+ menus = baseStack<AkkamonMenu>();
+
+ spawnPoint?: Phaser.Types.Tilemaps.TiledObject;
+ spawnPointTilePos?: {
+ x: number,
+ y: number
+ };
+
+ constructor(... params: any[]) {
+ super(sceneKey);
+ }
+
+ create() {
+ this.map = this.make.tilemap({ key: mapKey });
+ // Parameters are the name you gave the tileset in Tiled and then the key of the tileset image in
+ // Phaser's cache (i.e. the name you used in preload)
+ const tileset = this.map.addTilesetImage(tileSetKey, "tiles");
+ // Parameters: layer name (or index) from Tiled, tileset, x, y
+ const belowLayer = this.map.createLayer("Below Player", tileset, 0, 0);
+ const worldLayer = this.map.createLayer("World", tileset, 0, 0);
+ const aboveLayer = this.map.createLayer("Above Player", tileset, 0, 0);
+ // By default, everything gets depth sorted on the screen in the order we created things. Here, we
+ // want the "Above Player" layer to sit on top of the player, so we explicitly give it a depth.
+ // Higher depths will sit on top of lower depth objects.
+ aboveLayer.setDepth(10);
+
+ this.spawnPoint = this.map.findObject("Objects", obj => obj.name === "Spawn Point");
+
+ var tilePos = new Phaser.Math.Vector2(
+ Math.floor(this.spawnPoint.x! / TILE_SIZE),
+ Math.floor(this.spawnPoint.y! / TILE_SIZE),
+ );
+ this.spawnPointTilePos = tilePos;
+
+ this.client.requestInitWorldScene(
+ this
+ );
+
+ let akey = this.input.keyboard.addKey('a');
+ akey.on('down', () => {
+ if (this.menus.isEmpty()) {
+ this.pushMenu(new PauseMenu(this));
+ }
+ });
+
+ }
+
+ menuTakesUIControl(input: Phaser.Input.InputPlugin, menu: AkkamonMenu): void {
+ this.client.setUIControls(input, menu);
+ }
+
+ isUsingGridControls() {
+ this.client.setGridControls();
+ }
+
+ pushMenu(menu: AkkamonMenu) {
+ this.menus.push(menu);
+ this.menuTakesUIControl(this.input, menu);
+ console.log("New menu stack:");
+ console.log(this.menus);
+ }
+
+ popMenu() {
+ return this.menus.pop();
+ }
+
+ traverseMenusBackwards() {
+ this.popMenu();
+ if (!this.menus.isEmpty()) {
+ this.menuTakesUIControl(this.input, this.menus.peek()!);
+ } else {
+ this.isUsingGridControls();
+ }
+ console.log("menu stack after traversing back:");
+ console.log(this.menus);
+ }
+
+ getPlayerPixelPosition(): Phaser.Math.Vector2 {
+ return this.client.requestPlayerPixelPosition();
+ }
+
+ getRemotePlayerNames(): Array<string> {
+ let remotePlayerData = this.client.requestRemotePlayerData();
+ if (remotePlayerData.size === 0) {
+ return ['Nobody Online'];
+ } else {
+ return Array.from(remotePlayerData.keys());
+ }
+ }
+
+ requestBattle(remotePlayerName: string | string[]): void {
+ this.client.sendInteractionRequest({
+ type: "battle",
+ requestingTrainerId: this.client.getSessionTrainerId()!,
+ receivingTrainerIds: Array.isArray(remotePlayerName) ? remotePlayerName : [remotePlayerName]
+ });
+ }
+
+ clearMenus() {
+ if (this.menus.size() > 0) {
+ this.menus.pop()!.destroyGroup();
+ console.log("stack while clearing menus:");
+ console.log(this.menus.cloneData());
+ this.clearMenus();
+ }
+ }
+
+ }
+}