|
1 |
| -# React + Vite |
| 1 | +# Phaser Vue Template |
2 | 2 |
|
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. |
4 | 4 |
|
5 |
| -Currently, two official plugins are available: |
| 5 | +### Versions |
6 | 6 |
|
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 | + |
| 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 © 2011 - 2024 Phaser Studio Inc. |
| 188 | + |
| 189 | +All rights reserved. |
0 commit comments