|
@ -2,12 +2,14 @@ |
|
|
import { GAME_EVENTS } from './constants'; |
|
|
import { GAME_EVENTS } from './constants'; |
|
|
import { Game } from './Game'; |
|
|
import { Game } from './Game'; |
|
|
import { formatWebsocketData } from './utils'; |
|
|
import { formatWebsocketData } from './utils'; |
|
|
import { onMount } from 'svelte' |
|
|
|
|
|
|
|
|
|
|
|
const FPS = 144; |
|
|
const FPS = 144; |
|
|
const SERVER_URL = 'ws://localhost:3001'; |
|
|
const SERVER_URL = 'ws://localhost:3001'; |
|
|
|
|
|
|
|
|
|
|
|
let connected: boolean = false; |
|
|
let socket: WebSocket; |
|
|
let socket: WebSocket; |
|
|
|
|
|
let username: string = 'John'; |
|
|
|
|
|
let otherUsername: string = 'Garfield'; |
|
|
|
|
|
|
|
|
//Get canvas and its context |
|
|
//Get canvas and its context |
|
|
window.onload = () => { |
|
|
window.onload = () => { |
|
@ -19,6 +21,7 @@ |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function setupSocket(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) { |
|
|
function setupSocket(canvas: HTMLCanvasElement, context: CanvasRenderingContext2D) { |
|
|
socket = new WebSocket(SERVER_URL); |
|
|
socket = new WebSocket(SERVER_URL); |
|
|
const game = new Game(canvas, context); |
|
|
const game = new Game(canvas, context); |
|
@ -32,25 +35,62 @@ |
|
|
} else if (event == GAME_EVENTS.GAME_TICK) { |
|
|
} else if (event == GAME_EVENTS.GAME_TICK) { |
|
|
game.update(data); |
|
|
game.update(data); |
|
|
} else if (event == GAME_EVENTS.GET_GAME_INFO) { |
|
|
} else if (event == GAME_EVENTS.GET_GAME_INFO) { |
|
|
|
|
|
if (data && data.gameId != game.id) { |
|
|
game.setInfo(data); |
|
|
game.setInfo(data); |
|
|
setInterval(() => { |
|
|
setInterval(() => { |
|
|
game.draw(); |
|
|
game.draw(); |
|
|
}, 1000 / FPS); |
|
|
}, 1000 / FPS); |
|
|
console.log('Game loaded!'); |
|
|
console.log('Game updated!'); |
|
|
|
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
console.log('Unknown event from server: ' + event); |
|
|
console.log('Unknown event from server: ' + event); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
socket.onopen = () => { |
|
|
socket.onopen = () => { |
|
|
console.log('Connected to game server!'); |
|
|
connected = true; |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO)); |
|
|
}; |
|
|
|
|
|
socket.onclose = () => { |
|
|
|
|
|
connected = false; |
|
|
|
|
|
setupSocket(canvas, context); |
|
|
}; |
|
|
}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function updateGameInfo() { |
|
|
|
|
|
socket.send(formatWebsocketData(GAME_EVENTS.GET_GAME_INFO)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function connectToServer() { |
|
|
|
|
|
socket.send(formatWebsocketData(GAME_EVENTS.REGISTER_PLAYER, { playerName: username })); |
|
|
|
|
|
setInterval(() => { |
|
|
|
|
|
updateGameInfo(); |
|
|
|
|
|
}, 1000); |
|
|
|
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
<div> |
|
|
<div> |
|
|
<button on:click={() => socket.send(formatWebsocketData(GAME_EVENTS.START_GAME))}>Start game</button> |
|
|
{#if connected} |
|
|
|
|
|
Your name: |
|
|
|
|
|
<input bind:value={username} /> |
|
|
|
|
|
<br /> |
|
|
|
|
|
<button on:click={connectToServer}> Connect </button> |
|
|
|
|
|
<br /> |
|
|
|
|
|
Other player name: |
|
|
|
|
|
<input bind:value={otherUsername} /> |
|
|
|
|
|
<br /> |
|
|
|
|
|
<button |
|
|
|
|
|
on:click={() => { |
|
|
|
|
|
socket.send(formatWebsocketData(GAME_EVENTS.CREATE_GAME, { playerNames: [username, otherUsername] })); |
|
|
|
|
|
updateGameInfo(); |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
Create game vs {otherUsername} |
|
|
|
|
|
</button> |
|
|
|
|
|
<br /> |
|
|
|
|
|
<button on:click={() => socket.send(formatWebsocketData(GAME_EVENTS.READY))}>Ready</button> |
|
|
<br /> |
|
|
<br /> |
|
|
<br /> |
|
|
<br /> |
|
|
|
|
|
{:else} |
|
|
|
|
|
Connecting to game server... |
|
|
|
|
|
{/if} |
|
|
<canvas id="pong_canvas" /> |
|
|
<canvas id="pong_canvas" /> |
|
|
</div> |
|
|
</div> |
|
|