-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Abstractolotl/scene-refactor
Add base classes for scenes & wrap game objects
- Loading branch information
Showing
13 changed files
with
222 additions
and
200 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { Scene } from "phaser"; | ||
import AzNopolyGame from "../game"; | ||
import { SceneSwitcher } from "../scene-switcher"; | ||
import { PacketType, PlayerPacket, SceneChangePacket } from "../types/client"; | ||
|
||
|
||
export class BaseScene extends Phaser.Scene { | ||
|
||
protected aznopoly: AzNopolyGame; | ||
private packetListener: [string, EventListener][] = []; | ||
private sync: boolean; | ||
|
||
constructor(aznopoly: AzNopolyGame, synced: boolean = true) { | ||
super(); | ||
this.aznopoly = aznopoly; | ||
this.sync = synced; | ||
} | ||
|
||
init() { | ||
if(!this.aznopoly.client) return; | ||
|
||
if (this.sync) { | ||
if (!this.aznopoly.isHost) { | ||
SceneSwitcher.updateScene(this.aznopoly, this.scene.key); | ||
} else { | ||
SceneSwitcher.waitForPlayers(this.aznopoly, this.scene.key).then(() => { | ||
this.onAllPlayerReady(); | ||
}); | ||
} | ||
} | ||
this.addPacketListener(PacketType.SCENE_CHANGE, this.onChangeScene.bind(this)); | ||
} | ||
|
||
protected onAllPlayerReady() { | ||
|
||
} | ||
|
||
protected addPacketListener<T extends PlayerPacket>(type: string, callback: (packet: T) => void) { | ||
const listener: EventListener = (event: Event) => { | ||
const packet = (event as CustomEvent<T>).detail; | ||
callback(packet); | ||
}; | ||
this.packetListener.push([type, listener]); | ||
this.aznopoly.client.addEventListener(type, listener); | ||
} | ||
|
||
protected cleanPacketListeners() { | ||
this.packetListener.forEach(([type, listener]) => { | ||
this.aznopoly.client.removeEventListener(type, listener); | ||
}); | ||
} | ||
|
||
private onChangeScene(packet: SceneChangePacket) { | ||
if (!this.aznopoly.isPlayerHost(packet.sender)) return; | ||
this.scene.start(packet.data.scene); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.