This repository was archived by the owner on May 17, 2024. It is now read-only.
File tree Expand file tree Collapse file tree 9 files changed +72
-71
lines changed
dev/tutorials/colyseus-phaser-server-client-setup-template/src Expand file tree Collapse file tree 9 files changed +72
-71
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import Phaser from 'phaser'
22
33import Bootstrap from './scenes/Bootstrap' ;
44import Game from './scenes/Game' ;
5+ import FirstGame from './scenes/FirstGame' ;
56
67const config : Phaser . Types . Core . GameConfig = {
78 type : Phaser . AUTO ,
Original file line number Diff line number Diff line change 11import Phaser from 'phaser'
2-
2+ import Server from '../services/Server'
33
44//holds logic for switching between scenes
55
66export default class Bootstrap extends Phaser . Scene {
7+
8+ private server ! : Server
9+
710 constructor ( )
811 {
912 super ( 'bootstrap' )
1013 }
1114
12- preload ( )
13- {
14-
15+ init ( ) {
16+ this . server = new Server ( )
1517 }
1618
19+
1720 create ( )
1821 {
19- console . log ( 'bootstrap scene' )
20- this . scene . launch ( 'game' )
22+ //launch game scene from here and pass server instance to it
23+ this . scene . launch ( 'game' , {
24+ server : this . server
25+ } )
2126 }
2227}
Original file line number Diff line number Diff line change @@ -29,13 +29,22 @@ export default class FirstGame extends Phaser.Scene
2929 console . log ( room . sessionId )
3030
3131 // onMessage handler for "keydown" message that we created in the server "MyRoom" class
32- room . onMessage ( 'keydown ' , ( message ) => {
32+ room . onMessage ( 'action ' , ( message ) => {
3333 console . log ( message )
3434 } )
3535
3636 // on key down send the key to the server
3737 this . input . keyboard . on ( 'keydown' , ( event : KeyboardEvent ) => {
38- room . send ( 'keydown' , event . key )
38+ // translate key to action and send to server
39+ // ClientInputHandler
40+ const action_map : Record < string , integer > = {
41+ 'w' : 0 ,
42+ 'a' : 1 ,
43+ 's' : 2 ,
44+ 'd' : 3 ,
45+ }
46+
47+ room . send ( 'action' , action_map [ event . key ] )
3948 } )
4049 }
4150
Original file line number Diff line number Diff line change 11import Phaser from "phaser" ;
2+ import type Server from '../services/Server' ;
23
34export default class Game extends Phaser . Scene {
45 constructor ( ) {
@@ -15,9 +16,11 @@ export default class Game extends Phaser.Scene {
1516
1617 }
1718
18- create ( )
19+ create ( data : { server : Server } )
1920 {
20- console . log ( "game scene" ) ;
21+ //game recieves server instance from bootstrap scene
22+ const { server } = data
23+ server . join ( )
2124 }
2225
2326 update ( )
Original file line number Diff line number Diff line change 1+ import { Client } from 'colyseus.js'
2+
3+ export default class Server {
4+
5+ private client : Client
6+
7+ // create a client instance
8+ constructor ( ) {
9+ this . client = new Client ( 'ws://localhost:2567' )
10+ console . log ( this . client )
11+ }
12+
13+ async join ( ) {
14+ // join room tic-tac-toe
15+ const room = await this . client . joinOrCreate ( 'tic-tac-toe' )
16+ console . log ( room )
17+ }
18+ }
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ import { monitor } from "@colyseus/monitor";
55 * Import your Room files
66 */
77import { MyRoom } from "./rooms/MyRoom" ;
8-
8+ import { TicTacToe } from "./rooms/TicTacToe" ;
99export default Arena ( {
1010 getId : ( ) => "Your Colyseus App" ,
1111
@@ -14,6 +14,9 @@ export default Arena({
1414 * Define your room handlers:
1515 */
1616 gameServer . define ( 'my_room' , MyRoom ) ;
17+
18+ //create a tic-tac-toe room
19+ gameServer . define ( 'tic-tac-toe' , TicTacToe ) ;
1720
1821 } ,
1922
Original file line number Diff line number Diff line change 1+ import { Room } from "colyseus" ;
2+ import { TicTacToeState } from "./schema/TicTacToeState" ;
3+
4+
5+ // export default didnt work here but classic export did
6+ export class TicTacToe extends Room < TicTacToeState > {
7+
8+ onCreate ( ) {
9+ // set initial room state
10+ this . setState ( new TicTacToeState ( ) )
11+ }
12+
13+ }
Original file line number Diff line number Diff line change 1+ import { Schema , type } from '@colyseus/schema' ;
2+
3+ export class TicTacToeState extends Schema {
4+
5+ //state setup for tic tac toe game
6+ @type ( 'string' )
7+ name = "TicTacToeState" ;
8+
9+ }
You can’t perform that action at this time.
0 commit comments