|
@ -9,6 +9,7 @@ |
|
|
import Matchmaking from "./Matchmaking.svelte"; |
|
|
import Matchmaking from "./Matchmaking.svelte"; |
|
|
import type { MatchmakingDto } from "./dtos/MatchmakingDto"; |
|
|
import type { MatchmakingDto } from "./dtos/MatchmakingDto"; |
|
|
import { store } from "../../Auth"; |
|
|
import { store } from "../../Auth"; |
|
|
|
|
|
import ColorPicker from "./ColorPicker.svelte"; |
|
|
|
|
|
|
|
|
const SERVER_URL = `ws://${import.meta.env.VITE_HOST}:${ |
|
|
const SERVER_URL = `ws://${import.meta.env.VITE_HOST}:${ |
|
|
import.meta.env.VITE_BACK_PORT |
|
|
import.meta.env.VITE_BACK_PORT |
|
@ -23,19 +24,23 @@ |
|
|
let loggedIn: boolean = false; |
|
|
let loggedIn: boolean = false; |
|
|
let socket: WebSocket; |
|
|
let socket: WebSocket; |
|
|
let username: string = $store.username; |
|
|
let username: string = $store.username; |
|
|
|
|
|
let elementsColor: string = "#FFFFFF"; |
|
|
|
|
|
let backgroundColor: string = "#000000"; |
|
|
|
|
|
let game: Game; |
|
|
|
|
|
|
|
|
function setupSocket( |
|
|
function setupSocket( |
|
|
canvas: HTMLCanvasElement, |
|
|
canvas: HTMLCanvasElement, |
|
|
context: CanvasRenderingContext2D |
|
|
context: CanvasRenderingContext2D |
|
|
) { |
|
|
) { |
|
|
socket = new WebSocket(SERVER_URL); |
|
|
socket = new WebSocket(SERVER_URL); |
|
|
const game = new Game(canvas, context); |
|
|
game = new Game(canvas, context, elementsColor, backgroundColor); |
|
|
socket.onmessage = function (e) { |
|
|
socket.onmessage = function (e) { |
|
|
const event_json = JSON.parse(e.data); |
|
|
const event_json = JSON.parse(e.data); |
|
|
const event = event_json.event; |
|
|
const event = event_json.event; |
|
|
const data = event_json.data; |
|
|
const data = event_json.data; |
|
|
|
|
|
|
|
|
if (event == GAME_EVENTS.START_GAME) { |
|
|
if (event == GAME_EVENTS.START_GAME) { |
|
|
|
|
|
matchmaking = false; |
|
|
game.start(socket); |
|
|
game.start(socket); |
|
|
} else if (event == GAME_EVENTS.GAME_TICK) { |
|
|
} else if (event == GAME_EVENTS.GAME_TICK) { |
|
|
game.update(data); |
|
|
game.update(data); |
|
@ -97,6 +102,12 @@ |
|
|
const data: MatchmakingDto = { matchmaking: false }; |
|
|
const data: MatchmakingDto = { matchmaking: false }; |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.MATCHMAKING, data)); |
|
|
socket.send(formatWebsocketData(GAME_EVENTS.MATCHMAKING, data)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
$: { |
|
|
|
|
|
if (game !== undefined) { |
|
|
|
|
|
game.updateColors(elementsColor, backgroundColor) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
</script> |
|
|
</script> |
|
|
|
|
|
|
|
|
<div> |
|
|
<div> |
|
@ -120,11 +131,16 @@ |
|
|
<button on:click={() => (spectateWindow = true)} |
|
|
<button on:click={() => (spectateWindow = true)} |
|
|
>Spectate a friend</button |
|
|
>Spectate a friend</button |
|
|
> |
|
|
> |
|
|
|
|
|
<label for="colorPicker">Elements color:</label> |
|
|
|
|
|
<ColorPicker bind:color={elementsColor} /> |
|
|
|
|
|
<label for="colorPicker">Background color:</label> |
|
|
|
|
|
<ColorPicker bind:color={backgroundColor} /> |
|
|
|
|
|
|
|
|
{#if matchmaking} |
|
|
{#if matchmaking} |
|
|
<div on:click={stopMatchmaking} on:keydown={stopMatchmaking}> |
|
|
<div on:click={stopMatchmaking} on:keydown={stopMatchmaking}> |
|
|
<Matchmaking {stopMatchmaking} /> |
|
|
<Matchmaking {stopMatchmaking} /> |
|
|
</div> |
|
|
</div> |
|
|
|
|
|
<button on:click={() => console.log(elementsColor)}>tet</button> |
|
|
{:else if createMatchWindow} |
|
|
{:else if createMatchWindow} |
|
|
<div |
|
|
<div |
|
|
on:click={() => (createMatchWindow = false)} |
|
|
on:click={() => (createMatchWindow = false)} |
|
|