Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

Commit 999d76f

Browse files
Merge branch 'dev' of https://github.com/cryptoblivious/Last-Stand into dev
2 parents 0ffbefa + 82c890d commit 999d76f

File tree

9 files changed

+72
-71
lines changed

9 files changed

+72
-71
lines changed

dev/tutorials/colyseus-phaser-server-client-setup-template/src/client/FirstGame.ts

Lines changed: 0 additions & 60 deletions
This file was deleted.

dev/tutorials/colyseus-phaser-server-client-setup-template/src/client/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Phaser from 'phaser'
22

33
import Bootstrap from './scenes/Bootstrap';
44
import Game from './scenes/Game';
5+
import FirstGame from './scenes/FirstGame';
56

67
const config: Phaser.Types.Core.GameConfig = {
78
type: Phaser.AUTO,
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import Phaser from 'phaser'
2-
2+
import Server from '../services/Server'
33

44
//holds logic for switching between scenes
55

66
export 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
}

dev/tutorials/colyseus-phaser-server-client-setup-template/src/client/scenes/FirstGame.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff 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

dev/tutorials/colyseus-phaser-server-client-setup-template/src/client/scenes/Game.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Phaser from "phaser";
2+
import type Server from '../services/Server';
23

34
export 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()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

dev/tutorials/colyseus-phaser-server-client-setup-template/src/server/arena.config.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { monitor } from "@colyseus/monitor";
55
* Import your Room files
66
*/
77
import { MyRoom } from "./rooms/MyRoom";
8-
8+
import { TicTacToe } from "./rooms/TicTacToe";
99
export 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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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+
}

0 commit comments

Comments
 (0)