Skip to content

Commit fc217b9

Browse files
committed
init
1 parent 793f65b commit fc217b9

29 files changed

+2160
-1
lines changed

.gitignore

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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-*

.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
engine-strict=true

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Phaser
3+
Copyright (c) 2024 Phaser Studio Inc
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+187
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
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.80.1](https://github.com/phaserjs/phaser)
10+
- [Svelte 4.2.7](https://github.com/sveltejs/kit)
11+
- [Vite 5.1.4](https://github.com/vitejs/vite)
12+
- [TypeScript 5.3.3](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 `dist` folder |
27+
28+
## Writing Code
29+
30+
After cloning the repo, run `npm install` from your project directory. Then, you can start the local development server by running `npm run dev`.
31+
32+
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.
33+
34+
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.
35+
36+
## Template Project Structure
37+
38+
We have provided a default project structure to get you started. This is as follows:
39+
40+
- `src` - Contains the Svelte source code.
41+
- `src/app.html` - The html Svelte container.
42+
- `src/app.d.ts` - Global TypeScript declarations, provide types information.
43+
- `src/routes/+layout.svelte` - Svelte layout component. Here, the page title and the global styles are defined.
44+
- `src/+page.svelte` - Svelte page that integrates the functionality of the game created with Phaser.
45+
- `src/game` - Containts the game source code.
46+
- `src/game/PhaserGame.svelte` - The Svelte component that initializes the Phaser Game and serve like a bridge between Svelte and Phaser.
47+
48+
- `src/game/EventBus.ts` - A simple event bus to communicate between Svelte and Phaser.
49+
- `src/game/main.ts` - The main **game** entry point. This contains the game configuration and start the game.
50+
- `src/game/scenes/` - The Phaser Scenes are in this folder.
51+
- `static/assets` - Contains the static assets used by the game.
52+
53+
## Svelte Bridge
54+
55+
The `PhaserGame.svelte` component is the bridge between Svelte and Phaser. It initializes the Phaser game and passes events between the two.
56+
57+
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.
58+
59+
```js
60+
// In Svelte
61+
import { EventBus } from './EventBus';
62+
63+
// Emit an event
64+
EventBus.emit('event-name', data);
65+
66+
// In Phaser
67+
// Listen for an event
68+
EventBus.on('event-name', (data) => {
69+
// Do something with the data
70+
});
71+
```
72+
73+
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`.
74+
75+
Once exposed, you can access them like any regular reference.
76+
77+
## Phaser Scene Handling
78+
79+
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.
80+
81+
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.
82+
83+
**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:
84+
85+
86+
```js
87+
class MyScene extends Phaser.Scene
88+
{
89+
constructor ()
90+
{
91+
super('MyScene');
92+
}
93+
94+
create ()
95+
{
96+
// Your Game Objects and logic here
97+
98+
// At the end of create method:
99+
EventBus.emit('current-scene-ready', this);
100+
}
101+
}
102+
```
103+
104+
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.
105+
106+
### Svelte Component Example
107+
108+
Here's an example of how to access Phaser data for use in a Svelte Component:
109+
110+
```typescript
111+
// In a parent component
112+
<script lang="ts">
113+
import type { Scene } from "phaser";
114+
import PhaserGame, { type TPhaserRef } from "game/PhaserGame.svelte"; // We provide the type TPhaserRef but this route is an example. You should use the correct path to the PhaserGame component.
115+
116+
let phaserRef: TPhaserRef = { game: null, scene: null};
117+
118+
const onCurrentActiveScene = (scene) => {
119+
120+
// This is invoked
121+
122+
}
123+
124+
</script>
125+
126+
<PhaserGame phaserRef={phaserRef} currentActiveScene={onCurrentActiveScene} />
127+
```
128+
129+
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.
130+
131+
From this reference, the game instance is available via `phaserRef.game` and the most recently active Scene via `phaserRef.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+
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:
138+
139+
```js
140+
preload ()
141+
{
142+
// This is an example of loading a static image
143+
// from the static/assets folder:
144+
this.load.image('background', 'assets/bg.png');
145+
}
146+
```
147+
148+
When you issue the `npm run build` command, all static assets are automatically copied to the `build/assets` folder.
149+
150+
## Deploying to Production
151+
152+
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.
153+
154+
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.
155+
156+
## Customizing the Template
157+
158+
### Vite
159+
160+
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.
161+
162+
## Warning
163+
164+
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).
165+
166+
Phaser needs to run on the client, therefore in the file `src/routes/+layout.js` we have added the line:
167+
```javascript
168+
export const ssr = false;
169+
```
170+
Please do not modify this line unless you know what you are doing and can resolve all related issues with SSR.
171+
172+
## Join the Phaser Community!
173+
174+
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 😄
175+
176+
**Visit:** The [Phaser website](https://phaser.io) and follow on [Phaser Twitter](https://twitter.com/phaser_)<br />
177+
**Play:** Some of the amazing games [#madewithphaser](https://twitter.com/search?q=%23madewithphaser&src=typed_query&f=live)<br />
178+
**Learn:** [API Docs](https://newdocs.phaser.io), [Support Forum](https://phaser.discourse.group/) and [StackOverflow](https://stackoverflow.com/questions/tagged/phaser-framework)<br />
179+
**Discord:** Join us on [Discord](https://discord.gg/phaser)<br />
180+
**Code:** 2000+ [Examples](https://labs.phaser.io)<br />
181+
**Read:** The [Phaser World](https://phaser.io/community/newsletter) Newsletter<br />
182+
183+
Created by [Phaser Studio](mailto:[email protected]). Powered by coffee, anime, pixels and love.
184+
185+
The Phaser logo and characters are &copy; 2011 - 2024 Phaser Studio Inc.
186+
187+
All rights reserved.

0 commit comments

Comments
 (0)