Skip to content

Commit

Permalink
created a quitremotegame function, added shaderpass to composer, clea…
Browse files Browse the repository at this point in the history
…ned up overall game engine code
  • Loading branch information
trsctr committed Aug 20, 2024
1 parent b39889e commit 7c505a1
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 287 deletions.
26 changes: 20 additions & 6 deletions Frontend/src/js/pong/classes/Ball.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import * as THREE from 'three';

import { BALL_SIZE } from '../constants.js';

import { vertexShader } from '../shaders/vertexShader.js';
import { scoreBoardShader } from '../shaders/scoreBoardShader.js';

class Ball {
constructor(scene){
this.geometry = new THREE.SphereGeometry(BALL_SIZE);
this.geometry = new THREE.TorusGeometry(BALL_SIZE, 2.5);
this.material = new THREE.MeshPhongMaterial( {color: 0xf0f0f0, emissive: 0x00000, specular: 0x111111, shininess: 100} );

this.mesh = new THREE.Mesh(this.geometry, this.material);
this.mesh.position.set(0, 0, 0);
//this.mesh.rotation.x = THREE.MathUtils.degToRad(90)
this.mesh.rotation.x = THREE.MathUtils.degToRad(90)
this.scene = scene// console.log(this.direction);
this.dx = 0;
this.dy = 0;
}
updatePosition(position) {
this.mesh.position.set(position.x, this.mesh.position.y, position.z);
// Update bounding sphere position if needed
}
addToScene(){
this.scene.add(this.mesh);
}

removeFromScene(){
this.scene.remove(this.mesh);
if (this.geometry) {
this.geometry.dispose();
}
if (this.material) {
this.material.dispose();
}
this.mesh.traverse(child => {
if (child.isMesh) {
if (child.material.map) {
child.material.map.dispose();
}

}});
}
}

export default Ball;
20 changes: 16 additions & 4 deletions Frontend/src/js/pong/classes/GameSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import Ball from './Ball.js';
import PlayingField from './PlayingField.js';
import ScoreBoard from './ScoreBoard.js';
import { LEFT_PADDLE_START, RIGHT_PADDLE_START } from '../constants.js';
import { changeCameraAngle, endGame } from '../pong.js';
import { endGame } from '../pong.js';
import { globalState } from '../globalState.js';
import { sendQuit } from '../eventhandlers.js';

class GameSession {
constructor() {
Expand Down Expand Up @@ -110,7 +111,6 @@ class GameSession {
globalState.playingFieldMaterial.uniforms.ballDx.value = -1.0;
}
if (data.bounce === true) {
globalState.rgbShift.enabled = true;
if (data.hitpos < 0.1) {
globalState.rgbShift.uniforms.amount.value = 0.3
globalState.glitchPass.enabled = true;
Expand All @@ -120,9 +120,8 @@ class GameSession {
}
}
else {
globalState.rgbShift.enabled = false;
globalState.glitchPass.enabled = false;
globalState.rgbShift.uniforms.amount.value = 0.0;
globalState.rgbShift.uniforms.amount.value = 0.0015;
}
}

Expand Down Expand Up @@ -165,6 +164,19 @@ class GameSession {
console.log('Disconnected from server');
}
}

clearResources() {
this.leftPaddle.removeFromScene();
this.rightPaddle.removeFromScene();
this.playingField.removeFromScene();
this.ball.removeFromScene();
this.scoreBoard.removeFromScene();
this.disconnect();
}

quitGame() {
sendQuit(this.gameId, this.localPlayerId);
}
}

export default GameSession;
4 changes: 0 additions & 4 deletions Frontend/src/js/pong/classes/Paddle.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@ class Paddle {
}
}

if (this.boundingBox) {
this.boundingBox.dispose();
}

// Dispose of textures if any
this.mesh.traverse(child => {
if (child.isMesh) {
Expand Down
18 changes: 18 additions & 0 deletions Frontend/src/js/pong/classes/PlayingField.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ class PlayingField {
this.upperWall.addToScene()
this.lowerWall.addToScene()
}
removeFromScene() {
this.scene.remove(this.planeMesh);
this.upperWall.removeFromScene()
this.lowerWall.removeFromScene()
if (this.planeGeometry) {
this.planeGeometry.dispose();
}
if (globalState.playingFieldMaterial) {
globalState.playingFieldMaterial.dispose();
}
this.planeMesh.traverse(child => {
if (child.isMesh) {
if (child.material.map) {
child.material.map.dispose();
}
}
});
}
}

export default PlayingField;
47 changes: 31 additions & 16 deletions Frontend/src/js/pong/classes/ScoreBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ScoreBoard {
this.player1Mesh = null;
this.player2Mesh = null;
this.goalMesh = null;
this.gameStartMesh = null;
this.messageMesh = null;
this.font = new FontLoader().parse(fontJson);

// Array to store material instances
Expand Down Expand Up @@ -105,30 +105,42 @@ class ScoreBoard {

showGameStartText() {
this.clearScores();
this.gameStartMesh = this.createText('GAME START', 80, 0x00FF00, 0.3, 15.0)
this.gameStartMesh.position.set(0, 90.0, 300.0);
this.messageMesh = this.createText('GAME START', 80, 0x00FF00, 0.3, 15.0)
this.messageMesh.position.set(0, 90.0, 300.0);
if (globalState.invertedView === true) {
this.gameStartMesh.rotation.y = THREE.MathUtils.degToRad(180);
this.messageMesh.rotation.y = THREE.MathUtils.degToRad(180);
}
else {
this.gameStartMesh.position.z = -300.0
this.messageMesh.position.z = -300.0
}
this.scene.add(this.gameStartMesh);
this.scene.add(this.messageMesh);
}

showWaitText() {
this.clearScores();
this.gameStartMesh = this.createText('Waiting for your opponent..', 50, 0x4455FF, 1.4, 4.0);
this.gameStartMesh.position.set(0, 90.0, 300.0);
this.messageMesh = this.createText('Waiting for your opponent..', 50, 0x4455FF, 1.4, 4.0);
this.messageMesh.position.set(0, 90.0, 300.0);
if (globalState.invertedView === true) {
this.gameStartMesh.rotation.y = THREE.MathUtils.degToRad(180);
this.messageMesh.rotation.y = THREE.MathUtils.degToRad(180);
}
else {
this.gameStartMesh.position.z = -300.0
this.messageMesh.position.z = -300.0
}
this.scene.add(this.gameStartMesh);
this.scene.add(this.messageMesh);
}

showOpponentQuit() {
this.clearScores();
this.messageMesh = this.createText('Your opponent has quit the game', 50, 0x2222FF, 1.4, 4.0);
this.messageMesh.position.set(0, 90.0, 300.0);
if (globalState.invertedView === true) {
this.messageMesh.rotation.y = THREE.MathUtils.degToRad(180);
}
else {
this.messageMesh.position.z = -300.0
}
this.scene.add(this.messageMesh);
}

clearScores() {
if (this.player1Mesh) {
Expand All @@ -149,15 +161,18 @@ class ScoreBoard {
this.goalMesh.material.dispose();
this.goalMesh = null;
}
if (this.gameStartMesh) {
this.scene.remove(this.gameStartMesh);
this.gameStartMesh.geometry.dispose();
this.gameStartMesh.material.dispose();
this.gameStartMesh = null;
if (this.messageMesh) {
this.scene.remove(this.messageMesh);
this.messageMesh.geometry.dispose();
this.messageMesh.material.dispose();
this.messageMesh = null;
}
// Clear the stored materials
this.materials = [];
}
removeFromScene() {
this.clearScores();
}
}

export default ScoreBoard;
17 changes: 17 additions & 0 deletions Frontend/src/js/pong/classes/Wall.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ class Wall {
this.scene.add(this.mesh);
this.scene.add(this.edgesMesh);
}
removeFromScene() {
this.scene.remove(this.mesh);
this.scene.remove(this.edgesMesh);
if (this.geometry) {
this.geometry.dispose();
}
if (this.material) {
this.material.dispose();
}
this.mesh.traverse(child => {
if (child.isMesh) {
if (child.material.map) {
child.material.map.dispose();
}
}
});
}
}

export default Wall;
89 changes: 89 additions & 0 deletions Frontend/src/js/pong/controls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { PADDLE_SPEED } from './constants.js';
import { globalState } from './globalState.js';


const keys = {};
document.addEventListener('keydown', (event) => {
keys[event.key] = true;
});
document.addEventListener('keyup', (event) => {
keys[event.key] = false;
});

export function localGameControls(gameSession) {
let leftDeltaZ = 0;
let rightDeltaZ = 0;
if (gameSession.paused === true) {
return;
}
if ((keys['w'] || keys['W']) && !gameSession.leftPaddle.intersectsWall(gameSession.playingField.upperWall.boundingBox)) {
leftDeltaZ -= PADDLE_SPEED;
}
if ((keys['s'] || keys['S']) && !gameSession.leftPaddle.intersectsWall(gameSession.playingField.lowerWall.boundingBox)) {
leftDeltaZ += PADDLE_SPEED;
}
if ((keys['ArrowUp']) && !gameSession.rightPaddle.intersectsWall(gameSession.playingField.upperWall.boundingBox)) {
rightDeltaZ -= PADDLE_SPEED;
}
if ((keys['ArrowDown']) && !gameSession.rightPaddle.intersectsWall(gameSession.playingField.lowerWall.boundingBox)) {
rightDeltaZ += PADDLE_SPEED;
}
gameSession.leftPaddle.move(leftDeltaZ)
gameSession.rightPaddle.move(rightDeltaZ);

if (leftDeltaZ !== 0 || rightDeltaZ !== 0) {
let emitData = {
'type': 'move_paddle',
'game_id': gameSession.gameId,
'player1_id': gameSession.player1Id,
'p1_delta_z': leftDeltaZ,
'player2_id': gameSession.player2Id,
'p2_delta_z': rightDeltaZ
};
gameSession.sendMovement(emitData);
}
}

export function remoteGameControls(gameSession) {
let deltaZ = 0;

if (gameSession.paused === true) {
return;
}

let playerPaddle;
if (globalState.invertedView)
playerPaddle = gameSession.rightPaddle;
else
playerPaddle = gameSession.leftPaddle;
// Capture input for the local player’s paddle

if (globalState.invertedView) {
if ((keys['s'] || keys['S']) && !playerPaddle.intersectsWall(gameSession.playingField.upperWall.boundingBox)) {
deltaZ -= PADDLE_SPEED;
}
else if ((keys['w'] || keys['W']) && !playerPaddle.intersectsWall(gameSession.playingField.lowerWall.boundingBox)){
deltaZ += PADDLE_SPEED;
}
//deltaZ *= -1
}
else {
if ((keys['w'] || keys['W']) && !playerPaddle.intersectsWall(gameSession.playingField.upperWall.boundingBox)) {
deltaZ -= PADDLE_SPEED;
}
else if ((keys['s'] || keys['S']) && !playerPaddle.intersectsWall(gameSession.playingField.lowerWall.boundingBox)){
deltaZ += PADDLE_SPEED;
}
}
playerPaddle.move(deltaZ)
if (deltaZ !== 0) {
let emitData = {
'type': 'move_paddle',
'game_id': gameSession.gameId,
'player_id': gameSession.localPlayerId,
'delta_z': deltaZ
};
gameSession.sendMovement(emitData);
}
}

4 changes: 4 additions & 0 deletions Frontend/src/js/pong/eventhandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ export const initializeEventHandlers = (gameSession) => {
export const sendMovement = (data) => {
socket.emit('move_paddle', data);
};

export const sendQuit = (gameId, playerId) => {
socket.emit('quit_game', {game_id: gameId, player_id: playerId});
};
Loading

0 comments on commit 7c505a1

Please sign in to comment.