## Credit Rule: https://www.scribd.com/document/718603193/Pikachu # Phaser Next.js Template This is a Phaser 3 project template that uses the Next.js framework. It includes a bridge for React to Phaser game communication, hot-reloading for quick development workflow and scripts to generate production-ready builds. ### Versions This template has been updated for: - [Phaser 3.90.0](https://github.com/phaserjs/phaser) - [Next.js 15.3.1](https://github.com/vercel/next.js) - [TypeScript 5](https://github.com/microsoft/TypeScript)  ## Requirements [Node.js](https://nodejs.org) and npm are required to install dependencies and run scripts. ## Available Commands | Command | Description | |---------|-------------| | `npm install` | Install project dependencies | | `npm run dev` | Launch a development web server | | `npm run build` | Create a production build in the `dist` folder | | `npm run dev-nolog` | Launch a development web server without sending anonymous data (see "About log.js" below) | | `npm run build-nolog` | Create a production build in the `dist` folder without sending anonymous data (see "About log.js" below) | ## Writing Code After cloning the repo, run `npm install` from your project directory. Then, you can start the local development server by running `npm run dev`. The local development server runs on `http://localhost:8080` by default. Please see the Next.js documentation if you wish to change this, or add SSL support. Once the server is running you can edit any of the files in the `src` folder. Next.js will automatically recompile your code and then reload the browser. ## Template Project Structure We have provided a default project structure to get you started. This is as follows: | Path | Description | |-------------------------------|-----------------------------------------------------------------------------| | `src/pages/_document.tsx` | A basic Next.js component entry point. It is used to define the `` and `
` tags and other globally shared UI. | | `src` | Contains the Next.js client source code. | | `src/styles/globals.css` | Some simple global CSS rules to help with page layout. You can enable Tailwind CSS here. | | `src/page/_app.tsx` | The main Next.js component. | | `src/App.tsx` | Middleware component used to run Phaser in client mode. | | `src/PhaserGame.tsx` | The React component that initializes the Phaser Game and serves as a bridge between React and Phaser. | | `src/game/EventBus.ts` | A simple event bus to communicate between React and Phaser. | | `src/game` | Contains the game source code. | | `src/game/main.tsx` | The main **game** entry point. This contains the game configuration and starts the game. | | `src/game/scenes/` | The Phaser Scenes are in this folder. | | `public/favicon.png` | The default favicon for the project. | | `public/assets` | Contains the static assets used by the game. | ## React Bridge The `PhaserGame.tsx` component is the bridge between React and Phaser. It initializes the Phaser game and passes events between the two. To communicate between React 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 React and Phaser. ```js // In React import { EventBus } from './EventBus'; // Emit an event EventBus.emit('event-name', data); // In Phaser // Listen for an event EventBus.on('event-name', (data) => { // Do something with the data }); ``` In addition to this, the `PhaserGame` component exposes the Phaser game instance along with the most recently active Phaser Scene using React forwardRef. Once exposed, you can access them like any regular react reference. ## Phaser Scene Handling 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 React. 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. **Important**: When you add a new Scene to your game, make sure you expose to React by emitting the `"current-scene-ready"` event via the `EventBus`, like this: ```ts class MyScene extends Phaser.Scene { constructor () { super('MyScene'); } create () { // Your Game Objects and logic here // At the end of create method: EventBus.emit('current-scene-ready', this); } } ``` You don't have to emit this event if you don't need to access the specific scene from React. 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. ### React Component Example Here's an example of how to access Phaser data for use in a React Component: ```ts import { useRef } from 'react'; import { IRefPhaserGame } from "./game/PhaserGame"; // In a parent component const ReactComponent = () => { const phaserRef = useRef