Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5001ad6

Browse filesBrowse files
committed
format
1 parent a25227f commit 5001ad6
Copy full SHA for 5001ad6

File tree

Expand file treeCollapse file tree

8 files changed

+252
-80
lines changed
Filter options
Expand file treeCollapse file tree

8 files changed

+252
-80
lines changed

‎README.md

Copy file name to clipboard
+186-5Lines changed: 186 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,189 @@
1-
# React + Vite
1+
# Phaser Vue Template
22

3-
This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
3+
This is a Phaser 3 project template that uses the Vue framework and Vite for bundling. It includes a bridge for Vue to Phaser game communication, hot-reloading for quick development workflow and scripts to generate production-ready builds.
44

5-
Currently, two official plugins are available:
5+
### Versions
66

7-
- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
8-
- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
7+
This template has been updated for:
8+
9+
- [Phaser 3.70.0](https://github.com/phaserjs/phaser)
10+
- [Vue 3.4.15](https://github.com/vuejs)
11+
- [Vite 5.0.11](https://github.com/vitejs/vite)
12+
13+
![screenshot](screenshot.png)
14+
15+
## Requirements
16+
17+
[Node.js](https://nodejs.org) is required to install dependencies and run scripts via `npm`.
18+
19+
## Available Commands
20+
21+
| Command | Description |
22+
|---------|-------------|
23+
| `npm install` | Install project dependencies |
24+
| `npm run dev` | Launch a development web server |
25+
| `npm run build` | Create a production build in the `dist` folder |
26+
27+
## Writing Code
28+
29+
After cloning the repo, run `npm install` from your project directory. Then, you can start the local development server by running `npm run dev`.
30+
31+
The local development server runs on `http://localhost:8080` by default. Please see the Vite documentation if you wish to change this, or add SSL support.
32+
33+
Once the server is running you can edit any of the files in the `src` folder. Vite will automatically recompile your code and then reload the browser.
34+
35+
## Template Project Structure
36+
37+
We have provided a default project structure to get you started. This is as follows:
38+
39+
- `index.html` - A basic HTML page to contain the game.
40+
- `src` - Contains the Vue source code.
41+
- `src/main.js` - The main **Vue** entry point. This bootstraps the Vue application.
42+
- `src/App.vue` - The main Vue component.
43+
- `src/game/PhaserGame.vue` - The Vue component that initializes the Phaser Game and serve like a bridge between Vue and Phaser.
44+
- `src/game/EventBus.js` - A simple event bus to communicate between Vue and Phaser.
45+
- `src/game` - Containts the game source code.
46+
- `src/game/main.js` - The main **game** entry point. This contains the game configuration and start the game.
47+
- `src/game/scenes/` - The Phaser Scenes are in this folder.
48+
- `public/style.css` - Some simple CSS rules to help with page layout.
49+
- `public/assets` - Contains the static assets used by the game.
50+
51+
## Vue Bridge
52+
53+
The `PhaserGame.vue` component is the bridge between Vue and Phaser. It initializes the Phaser game and passes events between the two.
54+
55+
To communicate between Vue and Phaser, you can use the **EventBus.js** file. This is a simple event bus that allows you to emit and listen for events from both Vue and Phaser.
56+
57+
```js
58+
// In Vue
59+
import { EventBus } from './EventBus';
60+
61+
// Emit an event
62+
EventBus.emit('event-name', data);
63+
64+
// In Phaser
65+
// Listen for an event
66+
EventBus.on('event-name', (data) => {
67+
// Do something with the data
68+
});
69+
```
70+
71+
In addition to this, the `PhaserGame` component exposes the Phaser game instance along with the most recently active Phaser Scene. You can pick these up from Vue via `(defineExpose({ scene, game }))`.
72+
73+
Once exposed, you can access them like any regular state reference.
74+
75+
## Phaser Scene Handling
76+
77+
In Phaser, the Scene is the lifeblood of your game. It is where you sprites, game logic and all of the Phaser systems live. You can also have multiple scenes running at the same time. This template provides a way to obtain the current active scene from Vue.
78+
79+
You can get the current Phaser Scene from the component event `"current-active-scene"`. In order to do this, you need to emit the event `"current-scene-ready"` from the Phaser Scene class. This event should be emitted when the scene is ready to be used. You can see this done in all of the Scenes in our template.
80+
81+
**Important**: When you add a new Scene to your game, make sure you expose to to Vue by emitting the `"current-scene-ready"` event via the `EventBus`, like this:
82+
83+
84+
```js
85+
class MyScene extends Phaser.Scene
86+
{
87+
constructor ()
88+
{
89+
super('MyScene');
90+
}
91+
92+
create ()
93+
{
94+
// Your Game Objects and logic here
95+
96+
// At the end of create method:
97+
EventBus.emit('current-scene-ready', this);
98+
}
99+
}
100+
```
101+
102+
You don't have to emit this event if you don't need to access the specific scene from Vue. Also, you don't have to emit it at the end of `create`, you can emit it at any point. For example, should your Scene be waiting for a network request or API call to complete, it could emit the event once that data is ready.
103+
104+
### Vue Component Example
105+
106+
Here's an example of how to access Phaser data for use in a Vue Component:
107+
108+
```js
109+
// In a parent component
110+
<script setup>
111+
import { ref, toRaw } from 'vue';
112+
113+
const phaserRef = ref();
114+
const game = toRaw(phaserRef.value.game);
115+
const scene = toRaw(phaserRef.value.scene);
116+
117+
const onCurrentActiveScene = (scene) => {
118+
119+
// This is invoked
120+
121+
}
122+
123+
</script>
124+
<template>
125+
<PhaserGame ref="phaserRef" @current-active-scene="onCurrentActiveScene" />
126+
</template>
127+
```
128+
129+
In the code above, you can get a reference to the current Phaser Game instance and the current Scene by calling `ref()`.
130+
131+
From this state reference, the game instance is available via `toRaw(phaserRef.value.game)` and the most recently active Scene via `toRaw(phaserRef.value.scene)`
132+
133+
The `onCurrentActiveScene` callback will also be invoked whenever the the Phaser Scene changes, as long as you emit the event via the EventBus, as outlined above.
134+
135+
## Handling Assets
136+
137+
Vite supports loading assets via JavaScript module `import` statements.
138+
139+
This template provides support for both embedding assets and also loading them from a static folder. To embed an asset, you can import it at the top of the JavaScript file you are using it in:
140+
141+
```js
142+
import logoImg from './assets/logo.png'
143+
```
144+
145+
To load static files such as audio files, videos, etc place them into the `public/assets` folder. Then you can use this path in the Loader calls within Phaser:
146+
147+
```js
148+
preload ()
149+
{
150+
// This is an example of an imported bundled image.
151+
// Remember to import it at the top of this file
152+
this.load.image('logo', logoImg);
153+
154+
// This is an example of loading a static image
155+
// from the public/assets folder:
156+
this.load.image('background', 'assets/bg.png');
157+
}
158+
```
159+
160+
When you issue the `npm run build` command, all static assets are automatically copied to the `dist/assets` folder.
161+
162+
## Deploying to Production
163+
164+
After you run the `npm run build` command, your code will be built into a single bundle and saved to the `dist` folder, along with any other assets your project imported, or stored in the public assets folder.
165+
166+
In order to deploy your game, you will need to upload *all* of the contents of the `dist` folder to a public facing web server.
167+
168+
## Customizing the Template
169+
170+
### Vite
171+
172+
If you want to customize your build, such as adding plugin (i.e. for loading CSS or fonts), you can modify the `vite/config.*.mjs` file for cross-project changes, or you can modify and/or create new configuration files and target them in specific npm tasks inside of `package.json`. Please see the [Vite documentation](https://vitejs.dev/) for more information.
173+
174+
## Join the Phaser Community!
175+
176+
We love to see what developers like you create with Phaser! It really motivates us to keep improving. So please join our community and show-off your work 😄
177+
178+
**Visit:** The [Phaser website](https://phaser.io) and follow on [Phaser Twitter](https://twitter.com/phaser_)<br />
179+
**Play:** Some of the amazing games [#madewithphaser](https://twitter.com/search?q=%23madewithphaser&src=typed_query&f=live)<br />
180+
**Learn:** [API Docs](https://newdocs.phaser.io), [Support Forum](https://phaser.discourse.group/) and [StackOverflow](https://stackoverflow.com/questions/tagged/phaser-framework)<br />
181+
**Discord:** Join us on [Discord](https://discord.gg/phaser)<br />
182+
**Code:** 2000+ [Examples](https://labs.phaser.io)<br />
183+
**Read:** The [Phaser World](https://phaser.io/community/newsletter) Newsletter<br />
184+
185+
Created by [Phaser Studio](mailto:support@phaser.io). Powered by coffee, anime, pixels and love.
186+
187+
The Phaser logo and characters are &copy; 2011 - 2024 Phaser Studio Inc.
188+
189+
All rights reserved.

‎src/App.jsx

Copy file name to clipboardExpand all lines: src/App.jsx
+12-6Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Phaser from 'phaser';
22
import { useRef, useState } from 'react';
33
import { PhaserGame } from './game/PhaserGame';
44

5-
function App() {
5+
function App ()
6+
{
67
const [logoPosition, setLogoPosition] = useState({ x: 0, y: 0 });
78
const [canMoveLogo, setCanMoveLogo] = useState(true);
89

@@ -18,25 +19,30 @@ function App() {
1819
}
1920

2021
const changeScene = () => {
21-
if (scene) {
22+
if (scene)
23+
{
2224
scene.changeScene();
2325
}
2426
}
2527

2628
const moveLogo = () => {
27-
if (scene && scene.scene.key === 'MainMenu') {
28-
scene.moveLogo(({x, y}) => {
29+
if (scene && scene.scene.key === 'MainMenu')
30+
{
31+
scene.moveLogo(({ x, y }) => {
32+
2933
setLogoPosition({ x, y });
34+
3035
});
3136
}
3237
}
3338

3439
const addStars = () => {
35-
if (scene) {
40+
if (scene)
41+
{
3642
// Add more stars
3743
const x = Phaser.Math.Between(100, scene.scale.width - 100);
3844
const y = Phaser.Math.Between(100, scene.scale.height - 100);
39-
45+
4046
scene.add.image(x, y, 'star');
4147
}
4248
}

‎src/game/PhaserGame.jsx

Copy file name to clipboardExpand all lines: src/game/PhaserGame.jsx
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,32 @@ import { forwardRef, useEffect, useLayoutEffect, useRef } from 'react';
33
import StartGame from "./main"
44
import { EventBus } from './EventBus';
55

6-
export const PhaserGame = forwardRef(function PhaserGame ({ currentActiveScene }, ref) {
6+
export const PhaserGame = forwardRef(function PhaserGame ({ currentActiveScene }, ref)
7+
{
78
const game = useRef();
89

910
useLayoutEffect(() => {
10-
if (game.current === undefined) {
11+
if (game.current === undefined)
12+
{
1113
game.current = StartGame("game-container");
1214
ref.current = { game: game.current, scene: null };
1315
}
14-
16+
1517
return () => {
16-
if (game.current) {
18+
if (game.current)
19+
{
1720
game.current.destroy(true);
1821
game.current = undefined;
1922
}
2023
}
2124
}, [ref]);
2225

2326
useEffect(() => {
24-
EventBus.on('current-scene-ready', (scene_instance) => {
25-
if (currentActiveScene instanceof Function) {
26-
currentActiveScene(scene_instance);
27-
ref.current.scene = scene_instance;
27+
EventBus.on('current-scene-ready', (currentScene) => {
28+
if (currentActiveScene instanceof Function)
29+
{
30+
currentActiveScene(currentScene);
31+
ref.current.scene = currentScene;
2832
}
2933
});
3034
return () => {
@@ -38,7 +42,6 @@ export const PhaserGame = forwardRef(function PhaserGame ({ currentActiveScene }
3842

3943
});
4044

41-
// Add prop validation
4245
PhaserGame.propTypes = {
43-
currentActiveScene: PropTypes.func
46+
currentActiveScene: PropTypes.func // Callback function to get the current active scene
4447
}

‎src/game/main.js

Copy file name to clipboard
+7-11Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import Phaser from 'phaser';
21
import { Boot } from './scenes/Boot';
32
import { Game } from './scenes/Game';
43
import { GameOver } from './scenes/GameOver';
54
import { MainMenu } from './scenes/MainMenu';
5+
import Phaser from 'phaser';
66
import { Preloader } from './scenes/Preloader';
77

88
// Find out more information about the Game Config at:
@@ -13,10 +13,6 @@ const config = {
1313
height: 768,
1414
parent: 'game-container',
1515
backgroundColor: '#028af8',
16-
scale: {
17-
// mode: Phaser.Scale.FIT,
18-
// autoCenter: Phaser.Scale.CENTER_BOTH
19-
},
2016
scene: [
2117
Boot,
2218
Preloader,
@@ -25,11 +21,11 @@ const config = {
2521
GameOver
2622
]
2723
};
28-
let game_instance = null;
29-
const start_game = (parent) => {
30-
game_instance = new Phaser.Game({...config, parent: parent});
31-
return game_instance;
24+
25+
const StartGame = (parent) => {
26+
27+
return new Phaser.Game({...config, parent: parent});
28+
3229
}
3330

34-
export { game_instance }
35-
export default start_game;
31+
export default StartGame;

‎src/game/scenes/Game.js

Copy file name to clipboardExpand all lines: src/game/scenes/Game.js
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Scene } from 'phaser';
21
import { EventBus } from '../EventBus';
2+
import { Scene } from 'phaser';
33

44
export class Game extends Scene
55
{
@@ -18,16 +18,12 @@ export class Game extends Scene
1818
fontFamily: 'Arial Black', fontSize: 38, color: '#ffffff',
1919
stroke: '#000000', strokeThickness: 8,
2020
align: 'center'
21-
}).setOrigin(0.5);
22-
23-
this.input.once('pointerdown', () => {
24-
this.changeScene();
25-
});
21+
}).setOrigin(0.5).setDepth(100);
2622

2723
EventBus.emit('current-scene-ready', this);
2824
}
2925

30-
changeScene()
26+
changeScene ()
3127
{
3228
this.scene.start('GameOver');
3329
}

‎src/game/scenes/GameOver.js

Copy file name to clipboardExpand all lines: src/game/scenes/GameOver.js
+3-7Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Scene } from 'phaser';
21
import { EventBus } from '../EventBus';
2+
import { Scene } from 'phaser';
33

44
export class GameOver extends Scene
55
{
@@ -18,16 +18,12 @@ export class GameOver extends Scene
1818
fontFamily: 'Arial Black', fontSize: 64, color: '#ffffff',
1919
stroke: '#000000', strokeThickness: 8,
2020
align: 'center'
21-
}).setOrigin(0.5);
22-
23-
this.input.once('pointerdown', () => {
24-
this.changeScene();
25-
});
21+
}).setOrigin(0.5).setDepth(100);
2622

2723
EventBus.emit('current-scene-ready', this);
2824
}
2925

30-
changeScene()
26+
changeScene ()
3127
{
3228
this.scene.start('MainMenu');
3329
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.