drawing now seperate from gameLoop
+ refactoring + added soundMutedFlag
This commit is contained in:
@@ -1,17 +1,17 @@
|
||||
|
||||
import {pong, gc} from "./global.js"
|
||||
import * as ev from "../shared_js/class/Event.js"
|
||||
import {matchmaking, matchmakingComplete, startGame} from "./pong.js";
|
||||
import * as en from "../shared_js/enums.js"
|
||||
import { RacketClient } from "./class/RectangleClient.js";
|
||||
import { sleep } from "./utils.js";
|
||||
import * as c from "./constants.js"
|
||||
import {soundRoblox} from "./audio.js"
|
||||
import { gc } from "./global.js"
|
||||
import * as ev from "../shared_js/class/Event.js"
|
||||
import * as en from "../shared_js/enums.js"
|
||||
import { matchmaking, matchmakingComplete, startGame } from "./pong.js";
|
||||
import { RacketClient } from "./class/RectangleClient.js";
|
||||
import { repeatInput } from "./handleInput.js";
|
||||
import { soundRoblox } from "./audio.js"
|
||||
import { sleep } from "./utils.js";
|
||||
|
||||
const wsPort = 8042;
|
||||
const wsUrl = "ws://" + document.location.hostname + ":" + wsPort + "/pong";
|
||||
const socket = new WebSocket(wsUrl, "json");
|
||||
export const socket = new WebSocket(wsUrl, "json");
|
||||
|
||||
class ClientInfo {
|
||||
id = "";
|
||||
@@ -66,16 +66,13 @@ function inGameListener(event: MessageEvent)
|
||||
const data: ev.ServerEvent = JSON.parse(event.data);
|
||||
switch (data.type) {
|
||||
case en.EventTypes.gameUpdate:
|
||||
console.log("gameUpdate");
|
||||
// setTimeout(gameUpdate, 1000, data as ev.EventGameUpdate); // artificial latency for testing purpose
|
||||
gameUpdate(data as ev.EventGameUpdate);
|
||||
break;
|
||||
case en.EventTypes.scoreUpdate:
|
||||
console.log("scoreUpdate");
|
||||
scoreUpdate(data as ev.EventScoreUpdate);
|
||||
break;
|
||||
case en.EventTypes.matchEnd:
|
||||
console.log("matchEnd");
|
||||
matchEnd(data as ev.EventMatchEnd);
|
||||
break;
|
||||
}
|
||||
@@ -83,6 +80,7 @@ function inGameListener(event: MessageEvent)
|
||||
|
||||
function gameUpdate(data: ev.EventGameUpdate)
|
||||
{
|
||||
console.log("gameUpdate");
|
||||
gc.playerLeft.pos.y = Math.floor(data.playerLeft.y);
|
||||
gc.playerRight.pos.y = Math.floor(data.playerRight.y);
|
||||
gc.ball.pos.x = Math.floor(data.ball.x);
|
||||
@@ -94,6 +92,7 @@ function gameUpdate(data: ev.EventGameUpdate)
|
||||
|
||||
function scoreUpdate(data: ev.EventScoreUpdate)
|
||||
{
|
||||
console.log("scoreUpdate");
|
||||
if (clientInfo.side === en.PlayerSide.left && data.scoreRight > gc.scoreRight.value) {
|
||||
soundRoblox.play();
|
||||
}
|
||||
@@ -106,6 +105,7 @@ function scoreUpdate(data: ev.EventScoreUpdate)
|
||||
|
||||
function matchEnd(data: ev.EventMatchEnd)
|
||||
{
|
||||
console.log("matchEnd");
|
||||
if (data.winner === clientInfo.side) {
|
||||
alert("WIN"); // placeholder TODO draw
|
||||
}
|
||||
@@ -113,5 +113,3 @@ function matchEnd(data: ev.EventMatchEnd)
|
||||
alert("LOSE"); // placeholder TODO draw
|
||||
}
|
||||
}
|
||||
|
||||
export {socket}
|
||||
|
||||
Reference in New Issue
Block a user