generated from yandeu/phaser-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
269 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./useGame"; | ||
export * from "./useSettings"; |
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,80 @@ | ||
import { MainScene } from "~/scenes"; | ||
import type { Spaceship } from "~/objects"; | ||
import { produce } from "immer"; | ||
import { create } from "zustand"; | ||
|
||
import { game } from "~/game/core/game"; | ||
import type { Game } from "~/game/core/game"; | ||
|
||
interface GameStore { | ||
game: Game | null; | ||
mode: "mainMenu" | "singleplayer" | "multiplayer"; | ||
computed: { | ||
isLoading: boolean; | ||
isLoaded: boolean; | ||
player: Spaceship | null; | ||
scene: MainScene | null; | ||
}; | ||
loadSingleplayer: (settings) => Promise<void>; | ||
loadMultiplayer: (settings) => Promise<void>; | ||
loadMainMenu: () => void; | ||
} | ||
|
||
export const useGame = create<GameStore>((set, get) => ({ | ||
game: null, | ||
mode: "mainMenu", | ||
|
||
// https://github.com/pmndrs/zustand/issues/132 | ||
computed: { | ||
get isLoading() { | ||
return get().mode !== "mainMenu" && !get().computed.isLoaded; | ||
}, | ||
get isLoaded() { | ||
return !!get().game?.player; | ||
}, | ||
|
||
get player() { | ||
return get().game?.player; | ||
}, | ||
get scene() { | ||
return get().game?.scene; | ||
}, | ||
}, | ||
|
||
loadSingleplayer: async (settings) => { | ||
set( | ||
produce((state) => { | ||
state.mode = "singleplayer"; | ||
}) | ||
); | ||
const singleplayerGame = await game.init(settings); | ||
|
||
set( | ||
produce((state) => { | ||
state.game = singleplayerGame; | ||
}) | ||
); | ||
}, | ||
loadMultiplayer: async (settings) => { | ||
set( | ||
produce((state) => { | ||
state.mode = "multiplayer"; | ||
}) | ||
); | ||
const multiplayerGame = await game.init(settings); | ||
set( | ||
produce((state) => { | ||
state.game = multiplayerGame; | ||
}) | ||
); | ||
}, | ||
loadMainMenu: () => { | ||
get().game.destroy(); | ||
set( | ||
produce((state) => { | ||
state.game = null; | ||
state.mode = "mainMenu"; | ||
}) | ||
); | ||
}, | ||
})); |
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
Oops, something went wrong.