├── .npmrc ├── screenshot.png ├── src ├── lib │ └── index.ts ├── game │ ├── EventBus.ts │ ├── scenes │ │ ├── Boot.ts │ │ ├── GameOver.ts │ │ ├── Game.ts │ │ ├── Preloader.ts │ │ └── MainMenu.ts │ └── main.ts ├── routes │ ├── +layout.js │ ├── +layout.svelte │ └── +page.svelte ├── app.d.ts ├── app.html └── PhaserGame.svelte ├── static ├── favicon.png └── assets │ ├── bg.png │ ├── logo.png │ └── star.png ├── .gitignore ├── tsconfig.json ├── vite ├── config.dev.mjs └── config.prod.mjs ├── .editorconfig ├── svelte.config.js ├── tsconfig.app.json ├── tsconfig.node.json ├── log.js ├── LICENSE ├── package.json └── README.md /.npmrc: -------------------------------------------------------------------------------- 1 | engine-strict=true 2 | -------------------------------------------------------------------------------- /screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-svelte/HEAD/screenshot.png -------------------------------------------------------------------------------- /src/lib/index.ts: -------------------------------------------------------------------------------- 1 | // place files you want to import through the `$lib` alias in this folder. 2 | -------------------------------------------------------------------------------- /static/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-svelte/HEAD/static/favicon.png -------------------------------------------------------------------------------- /static/assets/bg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-svelte/HEAD/static/assets/bg.png -------------------------------------------------------------------------------- /static/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-svelte/HEAD/static/assets/logo.png -------------------------------------------------------------------------------- /static/assets/star.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/phaserjs/template-svelte/HEAD/static/assets/star.png -------------------------------------------------------------------------------- /src/game/EventBus.ts: -------------------------------------------------------------------------------- 1 | import { Events } from 'phaser'; 2 | 3 | // Used to emit events between components, HTML and Phaser scenes 4 | export const EventBus = new Events.EventEmitter(); -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /build 4 | /.svelte-kit 5 | /package 6 | .env 7 | .env.* 8 | !.env.example 9 | vite.config.js.timestamp-* 10 | vite.config.ts.timestamp-* 11 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "files": [], 3 | "references": [ 4 | { 5 | "path": "./tsconfig.app.json" 6 | }, 7 | { 8 | "path": "./tsconfig.node.json" 9 | } 10 | ] 11 | } -------------------------------------------------------------------------------- /vite/config.dev.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import { sveltekit } from '@sveltejs/kit/vite'; 3 | 4 | // https://vitejs.dev/config/ 5 | export default defineConfig({ 6 | plugins: [ 7 | sveltekit(), 8 | ], 9 | server: { 10 | port: 8080 11 | } 12 | }) 13 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # top-most EditorConfig file 4 | root = true 5 | 6 | [*] 7 | indent_style = space 8 | indent_size = 4 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = false 12 | insert_final_newline = false -------------------------------------------------------------------------------- /src/routes/+layout.js: -------------------------------------------------------------------------------- 1 | /* 2 | Sveltekit renders the page in the server this causes some errors with native browser objects like window, document, HTMLVideoElement etc. 3 | This line of code is to tell sveltekit to not render the page in the server and only in the client. 4 | */ 5 | export const ssr = false; -------------------------------------------------------------------------------- /src/app.d.ts: -------------------------------------------------------------------------------- 1 | // See https://kit.svelte.dev/docs/types#app 2 | // for information about these interfaces 3 | declare global { 4 | namespace App { 5 | // interface Error {} 6 | // interface Locals {} 7 | // interface PageData {} 8 | // interface PageState {} 9 | // interface Platform {} 10 | } 11 | } 12 | 13 | export {}; 14 | -------------------------------------------------------------------------------- /src/app.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | %sveltekit.head% 8 | 9 | 10 |
%sveltekit.body%
11 | 12 | 13 | -------------------------------------------------------------------------------- /src/routes/+layout.svelte: -------------------------------------------------------------------------------- 1 | 2 | Phaser Svelte Template 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /svelte.config.js: -------------------------------------------------------------------------------- 1 | import adapter from '@sveltejs/adapter-static'; 2 | import { vitePreprocess } from '@sveltejs/vite-plugin-svelte'; 3 | 4 | /** @type {import('@sveltejs/kit').Config} */ 5 | const config = { 6 | // Consult https://kit.svelte.dev/docs/integrations#preprocessors 7 | // for more information about preprocessors 8 | preprocess: vitePreprocess(), 9 | kit: { 10 | adapter: adapter({ 11 | precompress: false, 12 | fallback: 'index.html' 13 | }) 14 | } 15 | }; 16 | 17 | export default config; 18 | -------------------------------------------------------------------------------- /src/game/scenes/Boot.ts: -------------------------------------------------------------------------------- 1 | import { Scene } from 'phaser'; 2 | 3 | export class Boot extends Scene 4 | { 5 | constructor () 6 | { 7 | super('Boot'); 8 | } 9 | 10 | preload () 11 | { 12 | // The Boot Scene is typically used to load in any assets you require for your Preloader, such as a game logo or background. 13 | // The smaller the file size of the assets, the better, as the Boot Scene itself has no preloader. 14 | 15 | this.load.image('background', 'assets/bg.png'); 16 | } 17 | 18 | create () 19 | { 20 | this.scene.start('Preloader'); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /tsconfig.app.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "@tsconfig/svelte/tsconfig.json", 3 | "compilerOptions": { 4 | "target": "ESNext", 5 | "useDefineForClassFields": true, 6 | "module": "ESNext", 7 | "resolveJsonModule": true, 8 | /** 9 | * Typecheck JS in `.svelte` and `.js` files by default. 10 | * Disable checkJs if you'd like to use dynamic types in JS. 11 | * Note that setting allowJs false does not prevent the use 12 | * of JS in `.svelte` files. 13 | */ 14 | "allowJs": true, 15 | "checkJs": true, 16 | "isolatedModules": true, 17 | "moduleDetection": "force" 18 | }, 19 | "include": [ 20 | "src/**/*.ts", 21 | "src/**/*.js", 22 | "src/**/*.svelte" 23 | ] 24 | } -------------------------------------------------------------------------------- /tsconfig.node.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo", 4 | "target": "ES2022", 5 | "lib": [ 6 | "ES2023" 7 | ], 8 | "module": "ESNext", 9 | "skipLibCheck": true, 10 | /* Bundler mode */ 11 | "moduleResolution": "bundler", 12 | "allowImportingTsExtensions": true, 13 | "isolatedModules": true, 14 | "moduleDetection": "force", 15 | "noEmit": true, 16 | /* Linting */ 17 | "strict": true, 18 | "noUnusedLocals": true, 19 | "noUnusedParameters": true, 20 | "noFallthroughCasesInSwitch": true, 21 | "noUncheckedSideEffectImports": true 22 | }, 23 | "include": [ 24 | "vite.config.ts" 25 | ] 26 | } -------------------------------------------------------------------------------- /src/game/main.ts: -------------------------------------------------------------------------------- 1 | import { Boot } from './scenes/Boot'; 2 | import { GameOver } from './scenes/GameOver'; 3 | import { Game as MainGame } from './scenes/Game'; 4 | import { MainMenu } from './scenes/MainMenu'; 5 | import { AUTO, Game } from 'phaser'; 6 | import { Preloader } from './scenes/Preloader'; 7 | 8 | // Find out more information about the Game Config at: 9 | // https://docs.phaser.io/api-documentation/typedef/types-core#gameconfig 10 | const config: Phaser.Types.Core.GameConfig = { 11 | type: AUTO, 12 | width: 1024, 13 | height: 768, 14 | parent: 'game-container', 15 | backgroundColor: '#028af8', 16 | scene: [ 17 | Boot, 18 | Preloader, 19 | MainMenu, 20 | MainGame, 21 | GameOver 22 | ] 23 | }; 24 | 25 | const StartGame = (parent: string) => { 26 | 27 | return new Game({ ...config, parent }); 28 | 29 | } 30 | 31 | export default StartGame; 32 | -------------------------------------------------------------------------------- /log.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import https from 'https'; 3 | 4 | const main = async () => { 5 | const args = process.argv.slice(2); 6 | const packageData = JSON.parse(fs.readFileSync('./package.json', 'utf8')); 7 | const event = args[0] || 'unknown'; 8 | const phaserVersion = packageData.dependencies.phaser; 9 | 10 | const options = { 11 | hostname: 'gryzor.co', 12 | port: 443, 13 | path: `/v/${event}/${phaserVersion}/${packageData.name}`, 14 | method: 'GET' 15 | }; 16 | 17 | try { 18 | const req = https.request(options, (res) => { 19 | res.on('data', () => {}); 20 | res.on('end', () => { 21 | process.exit(0); 22 | }); 23 | }); 24 | 25 | req.on('error', (error) => { 26 | process.exit(1); 27 | }); 28 | 29 | req.end(); 30 | } catch (error) { 31 | // Silence is the canvas where the soul paints its most profound thoughts. 32 | process.exit(1); 33 | } 34 | } 35 | 36 | main(); 37 | -------------------------------------------------------------------------------- /src/game/scenes/GameOver.ts: -------------------------------------------------------------------------------- 1 | import { EventBus } from '../EventBus'; 2 | import { Scene } from 'phaser'; 3 | 4 | export class GameOver extends Scene 5 | { 6 | camera: Phaser.Cameras.Scene2D.Camera; 7 | background: Phaser.GameObjects.Image; 8 | gameOverText : Phaser.GameObjects.Text; 9 | 10 | constructor () 11 | { 12 | super('GameOver'); 13 | } 14 | 15 | create () 16 | { 17 | this.camera = this.cameras.main 18 | this.camera.setBackgroundColor(0xff0000); 19 | 20 | this.background = this.add.image(512, 384, 'background'); 21 | this.background.setAlpha(0.5); 22 | 23 | this.gameOverText = this.add.text(512, 384, 'Game Over', { 24 | fontFamily: 'Arial Black', fontSize: 64, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | this.scene.start('MainMenu'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /vite/config.prod.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'vite'; 2 | import { sveltekit } from '@sveltejs/kit/vite'; 3 | 4 | const MESSAGE_INTERVAL_MS = 1000000; 5 | const lastMessageTime = process.env.LAST_MESSAGE_TIME || 0; 6 | 7 | const now = Date.now(); 8 | 9 | if (now - lastMessageTime > MESSAGE_INTERVAL_MS) { 10 | process.stdout.write(`Building for production...\n`); 11 | const line = "---------------------------------------------------------"; 12 | const msg = `❤️❤️❤️ Tell us about your game! - games@phaser.io ❤️❤️❤️`; 13 | process.stdout.write(`${line}\n${msg}\n${line}\n`); 14 | process.env.LAST_MESSAGE_TIME = now; 15 | } 16 | 17 | export default defineConfig({ 18 | base: './', 19 | plugins: [ 20 | sveltekit(), 21 | ], 22 | logLevel: 'error', 23 | build: { 24 | minify: 'terser', 25 | terserOptions: { 26 | compress: { 27 | passes: 2 28 | }, 29 | mangle: true, 30 | format: { 31 | comments: false 32 | } 33 | } 34 | } 35 | }); 36 | 37 | -------------------------------------------------------------------------------- /src/game/scenes/Game.ts: -------------------------------------------------------------------------------- 1 | import { EventBus } from '../EventBus'; 2 | import { Scene } from 'phaser'; 3 | 4 | export class Game extends Scene 5 | { 6 | camera: Phaser.Cameras.Scene2D.Camera; 7 | background: Phaser.GameObjects.Image; 8 | gameText: Phaser.GameObjects.Text; 9 | 10 | constructor () 11 | { 12 | super('Game'); 13 | } 14 | 15 | create () 16 | { 17 | this.camera = this.cameras.main; 18 | this.camera.setBackgroundColor(0x00ff00); 19 | 20 | this.background = this.add.image(512, 384, 'background'); 21 | this.background.setAlpha(0.5); 22 | 23 | this.gameText = this.add.text(512, 384, 'Make something fun!\nand share it with us:\nsupport@phaser.io', { 24 | fontFamily: 'Arial Black', fontSize: 38, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | this.scene.start('GameOver'); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/PhaserGame.svelte: -------------------------------------------------------------------------------- 1 | 11 | 12 | 45 | 46 |
-------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 Phaser Studio Inc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "template-svelte", 3 | "description": "A Phaser 3 project template that demonstrates Svelte communication and uses Vite for bundling.", 4 | "version": "1.2.0", 5 | "repository": { 6 | "type": "git", 7 | "url": "git+https://github.com/phaserjs/template-svelte.git" 8 | }, 9 | "author": "Phaser Studio (https://phaser.io/)", 10 | "license": "MIT", 11 | "licenseUrl": "http://www.opensource.org/licenses/mit-license.php", 12 | "bugs": { 13 | "url": "https://github.com/phaserjs/template-svelte/issues" 14 | }, 15 | "homepage": "https://github.com/phaserjs/template-svelte#readme", 16 | "keywords": [ 17 | "phaser", 18 | "phaser3", 19 | "svelte", 20 | "vite" 21 | ], 22 | "scripts": { 23 | "dev": "node log.js dev & vite --config vite/config.dev.mjs", 24 | "build": "node log.js build & vite build --config vite/config.prod.mjs", 25 | "dev-nolog": "vite --config vite/config.dev.mjs", 26 | "build-nolog": "vite build --config vite/config.prod.mjs", 27 | "preview": "vite preview --outDir build" 28 | }, 29 | "devDependencies": { 30 | "@tsconfig/svelte": "^5.0.4", 31 | "@sveltejs/adapter-auto": "6.0.0", 32 | "@sveltejs/adapter-static": "3.0.8", 33 | "@sveltejs/kit": "2.20.7", 34 | "@sveltejs/vite-plugin-svelte": "5.0.3", 35 | "svelte": "5.23.1", 36 | "svelte-check": "^4.0.2", 37 | "terser": "5.39.0", 38 | "tslib": "2.7.0", 39 | "typescript": "5.7.2", 40 | "vite": "6.3.1" 41 | }, 42 | "type": "module", 43 | "dependencies": { 44 | "phaser": "^3.90.0" 45 | } 46 | } -------------------------------------------------------------------------------- /src/game/scenes/Preloader.ts: -------------------------------------------------------------------------------- 1 | import { Scene } from 'phaser'; 2 | 3 | export class Preloader extends Scene 4 | { 5 | constructor () 6 | { 7 | super('Preloader'); 8 | } 9 | 10 | init () 11 | { 12 | // We loaded this image in our Boot Scene, so we can display it here 13 | this.add.image(512, 384, 'background'); 14 | 15 | // A simple progress bar. This is the outline of the bar. 16 | this.add.rectangle(512, 384, 468, 32).setStrokeStyle(1, 0xffffff); 17 | 18 | // This is the progress bar itself. It will increase in size from the left based on the % of progress. 19 | const bar = this.add.rectangle(512-230, 384, 4, 28, 0xffffff); 20 | 21 | // Use the 'progress' event emitted by the LoaderPlugin to update the loading bar 22 | this.load.on('progress', (progress: number) => { 23 | 24 | // Update the progress bar (our bar is 464px wide, so 100% = 464px) 25 | bar.width = 4 + (460 * progress); 26 | 27 | }); 28 | } 29 | 30 | preload () 31 | { 32 | // Load the assets for the game - Replace with your own assets 33 | this.load.setPath('assets'); 34 | 35 | this.load.image('logo', 'logo.png'); 36 | this.load.image('star', 'star.png'); 37 | } 38 | 39 | create () 40 | { 41 | // When all the assets have loaded, it's often worth creating global objects here that the rest of the game can use. 42 | // For example, you can define global animations here, so we can use them in other scenes. 43 | 44 | // Move to the MainMenu. You could also swap this for a Scene Transition, such as a camera fade. 45 | this.scene.start('MainMenu'); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/game/scenes/MainMenu.ts: -------------------------------------------------------------------------------- 1 | import { type GameObjects, Scene } from 'phaser'; 2 | 3 | import { EventBus } from '../EventBus'; 4 | 5 | export class MainMenu extends Scene 6 | { 7 | background: GameObjects.Image; 8 | logo: GameObjects.Image; 9 | title: GameObjects.Text; 10 | logoTween: Phaser.Tweens.Tween | null; 11 | 12 | constructor () 13 | { 14 | super('MainMenu'); 15 | } 16 | 17 | create () 18 | { 19 | this.background = this.add.image(512, 384, 'background'); 20 | 21 | this.logo = this.add.image(512, 300, 'logo').setDepth(100); 22 | 23 | this.title = this.add.text(512, 460, 'Main Menu', { 24 | fontFamily: 'Arial Black', fontSize: 38, color: '#ffffff', 25 | stroke: '#000000', strokeThickness: 8, 26 | align: 'center' 27 | }).setOrigin(0.5).setDepth(100); 28 | 29 | EventBus.emit('current-scene-ready', this); 30 | } 31 | 32 | changeScene () 33 | { 34 | if (this.logoTween) 35 | { 36 | this.logoTween.stop(); 37 | this.logoTween = null; 38 | } 39 | 40 | this.scene.start('Game'); 41 | } 42 | 43 | moveLogo (vueCallback: ({ x, y }: { x: number, y: number }) => void) 44 | { 45 | if (this.logoTween) 46 | { 47 | if (this.logoTween.isPlaying()) 48 | { 49 | this.logoTween.pause(); 50 | } 51 | else 52 | { 53 | this.logoTween.play(); 54 | } 55 | } 56 | else 57 | { 58 | this.logoTween = this.tweens.add({ 59 | targets: this.logo, 60 | x: { value: 750, duration: 3000, ease: 'Back.easeInOut' }, 61 | y: { value: 80, duration: 1500, ease: 'Sine.easeOut' }, 62 | yoyo: true, 63 | repeat: -1, 64 | onUpdate: () => { 65 | if (vueCallback) 66 | { 67 | vueCallback({ 68 | x: Math.floor(this.logo.x), 69 | y: Math.floor(this.logo.y) 70 | }); 71 | } 72 | } 73 | }); 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /src/routes/+page.svelte: -------------------------------------------------------------------------------- 1 | 84 | 85 |
86 | 87 |
88 |
89 | 90 |
91 |
92 | 93 |
94 |
95 | Sprite Position: 96 |
{JSON.stringify(spritePosition, null, 2)}
97 |
98 |
99 | 100 |
101 |
102 |
103 | 104 | 146 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Phaser Svelte Template 2 | 3 | This is a Phaser 3 project template that uses the Svelte framework, TypeScript and Vite for bundling. It includes a bridge for Svelte to Phaser game communication, hot-reloading for quick development workflow and scripts to generate production-ready builds. 4 | 5 | ### Versions 6 | 7 | This template has been updated for: 8 | 9 | - [Phaser 3.90.0](https://github.com/phaserjs/phaser) 10 | - [Svelte 5.23.1](https://github.com/sveltejs/kit) 11 | - [Vite 6.3.1](https://github.com/vitejs/vite) 12 | - [TypeScript 5.7.2](https://github.com/microsoft/TypeScript) 13 | 14 | ![screenshot](screenshot.png) 15 | 16 | ## Requirements 17 | 18 | [Node.js](https://nodejs.org) is required to install dependencies and run scripts via `npm`. 19 | 20 | ## Available Commands 21 | 22 | | Command | Description | 23 | |---------|-------------| 24 | | `npm install` | Install project dependencies | 25 | | `npm run dev` | Launch a development web server | 26 | | `npm run build` | Create a production build in the `build` folder | 27 | | `npm run dev-nolog` | Launch a development web server without sending anonymous data (see "About log.js" below) | 28 | | `npm run build-nolog` | Create a production build in the `dist` folder without sending anonymous data (see "About log.js" below) | 29 | 30 | ## Writing Code 31 | 32 | After cloning the repo, run `npm install` from your project directory. Then, you can start the local development server by running `npm run dev`. 33 | 34 | 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. 35 | 36 | 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. 37 | 38 | ## Template Project Structure 39 | 40 | We have provided a default project structure to get you started. This is as follows: 41 | 42 | | Path | Description | 43 | |-------------------------------|-----------------------------------------------------------------------------| 44 | | `src` | Contains the Svelte source code. | 45 | | `src/app.html` | The HTML Svelte container. | 46 | | `src/app.d.ts` | Global TypeScript declarations, providing type information. | 47 | | `src/routes/+layout.svelte` | Svelte layout component where the page title and global styles are defined.| 48 | | `src/+page.svelte` | Svelte page that integrates the functionality of the game created with Phaser.| 49 | | `src/PhaserGame.svelte` | The Svelte component that initializes the Phaser game and acts as a bridge between Svelte and Phaser.| 50 | | `src/game` | Contains the game source code. | 51 | | `src/game/EventBus.ts` | A simple event bus to communicate between Svelte and Phaser. | 52 | | `src/game/main.ts` | The main **game** entry point containing the game configuration and starting the game.| 53 | | `src/game/scenes/` | Folder containing the Phaser Scenes. | 54 | | `static/assets` | Contains the static assets used by the game. | 55 | 56 | ## Svelte Bridge 57 | 58 | The `PhaserGame.svelte` component is the bridge between Svelte and Phaser. It initializes the Phaser game and passes events between the two. 59 | 60 | To communicate between Svelte and Phaser, you can use the **EventBus.ts** file. This is a simple event bus that allows you to emit and listen for events from both Svelte and Phaser. 61 | 62 | ```js 63 | // In Svelte 64 | import { EventBus } from './EventBus'; 65 | 66 | // Emit an event 67 | EventBus.emit('event-name', data); 68 | 69 | // In Phaser 70 | // Listen for an event 71 | EventBus.on('event-name', (data) => { 72 | // Do something with the data 73 | }); 74 | ``` 75 | 76 | 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 Svelte via `phaserRef prop`. 77 | 78 | Once exposed, you can access them like any regular reference. 79 | 80 | ## Phaser Scene Handling 81 | 82 | 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 Svelte. 83 | 84 | 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. 85 | 86 | **Important**: When you add a new Scene to your game, make sure you expose to Svelte by emitting the `"current-scene-ready"` event via the `EventBus`, like this: 87 | 88 | 89 | ```js 90 | class MyScene extends Phaser.Scene 91 | { 92 | constructor () 93 | { 94 | super('MyScene'); 95 | } 96 | 97 | create () 98 | { 99 | // Your Game Objects and logic here 100 | 101 | // At the end of create method: 102 | EventBus.emit('current-scene-ready', this); 103 | } 104 | } 105 | ``` 106 | 107 | You don't have to emit this event if you don't need to access the specific scene from Svelte. 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. 108 | 109 | ### Svelte Component Example 110 | 111 | Here's an example of how to access Phaser data for use in a Svelte Component: 112 | 113 | ```js 114 | // In a parent component 115 | 128 | 129 | 130 | ``` 131 | 132 | In the code above, you can get a reference to the current Phaser Game instance and the current Scene by creating a reference with a variable `let phaserRef` and assign to PhaserGame component. 133 | 134 | From this reference, the game instance is available via `phaserRef.game` and the most recently active Scene via `phaserRef.scene`. 135 | 136 | 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. 137 | 138 | ## Handling Assets 139 | 140 | To load your static games files such as audio files, images, videos, etc place them into the `static/assets` folder. Then you can use this path in the Loader calls within Phaser: 141 | 142 | ```js 143 | preload () 144 | { 145 | // This is an example of loading a static image 146 | // from the static/assets folder: 147 | this.load.image('background', 'assets/bg.png'); 148 | } 149 | ``` 150 | 151 | When you issue the `npm run build` command, all static assets are automatically copied to the `build/assets` folder. 152 | 153 | ## Deploying to Production 154 | 155 | After you run the `npm run build` command, your code will be built into a single bundle and saved to the `build` folder, along with any other assets your project imported, or stored in the public assets folder. 156 | 157 | In order to deploy your game, you will need to upload *all* of the contents of the `build` folder to a public facing web server. 158 | 159 | ## Customizing the Template 160 | 161 | ### Vite 162 | 163 | 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. 164 | 165 | ## Warning 166 | 167 | Normally, SvelteKit renders your page on the server first and sends that HTML to the client where it's hydrated. If you set ssr to false, it renders an empty 'shell' page instead. This is useful if your page is unable to be rendered on the server (because you use browser-only globals like document for example). 168 | 169 | Phaser needs to run on the client, therefore in the file `src/routes/+layout.js` we have added the line: 170 | ```javascript 171 | export const ssr = false; 172 | ``` 173 | Please do not modify this line unless you know what you are doing and can resolve all related issues with SSR. 174 | 175 | ## About log.js 176 | 177 | If you inspect our node scripts you will see there is a file called `log.js`. This file makes a single silent API call to a domain called `gryzor.co`. This domain is owned by Phaser Studio Inc. The domain name is a homage to one of our favorite retro games. 178 | 179 | We send the following 3 pieces of data to this API: The name of the template being used (vue, react, etc). If the build was 'dev' or 'prod' and finally the version of Phaser being used. 180 | 181 | At no point is any personal data collected or sent. We don't know about your project files, device, browser or anything else. Feel free to inspect the `log.js` file to confirm this. 182 | 183 | Why do we do this? Because being open source means we have no visible metrics about which of our templates are being used. We work hard to maintain a large and diverse set of templates for Phaser developers and this is our small anonymous way to determine if that work is actually paying off, or not. In short, it helps us ensure we're building the tools for you. 184 | 185 | However, if you don't want to send any data, you can use these commands instead: 186 | 187 | Dev: 188 | 189 | ```bash 190 | npm run dev-nolog 191 | ``` 192 | 193 | Build: 194 | 195 | ```bash 196 | npm run build-nolog 197 | ``` 198 | 199 | Or, to disable the log entirely, simply delete the file `log.js` and remove the call to it in the `scripts` section of `package.json`: 200 | 201 | Before: 202 | 203 | ```json 204 | "scripts": { 205 | "dev": "node log.js dev & dev-template-script", 206 | "build": "node log.js build & build-template-script" 207 | }, 208 | ``` 209 | 210 | After: 211 | 212 | ```json 213 | "scripts": { 214 | "dev": "dev-template-script", 215 | "build": "build-template-script" 216 | }, 217 | ``` 218 | 219 | Either of these will stop `log.js` from running. If you do decide to do this, please could you at least join our Discord and tell us which template you're using! Or send us a quick email. Either will be super-helpful, thank you. 220 | 221 | ## Join the Phaser Community! 222 | 223 | 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 😄 224 | 225 | **Visit:** The [Phaser website](https://phaser.io) and follow on [Phaser Twitter](https://twitter.com/phaser_)
226 | **Play:** Some of the amazing games [#madewithphaser](https://twitter.com/search?q=%23madewithphaser&src=typed_query&f=live)
227 | **Learn:** [API Docs](https://newdocs.phaser.io), [Support Forum](https://phaser.discourse.group/) and [StackOverflow](https://stackoverflow.com/questions/tagged/phaser-framework)
228 | **Discord:** Join us on [Discord](https://discord.gg/phaser)
229 | **Code:** 2000+ [Examples](https://labs.phaser.io)
230 | **Read:** The [Phaser World](https://phaser.io/community/newsletter) Newsletter
231 | 232 | Created by [Phaser Studio](mailto:support@phaser.io). Powered by coffee, anime, pixels and love. 233 | 234 | The Phaser logo and characters are © 2011 - 2025 Phaser Studio Inc. 235 | 236 | All rights reserved. 237 | --------------------------------------------------------------------------------