Skip to content

Commit

Permalink
fixed: powerups spawn also for the right user
Browse files Browse the repository at this point in the history
  • Loading branch information
halil-kalyoncu committed Oct 14, 2023
1 parent 33c1161 commit 16648db
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 61 deletions.
12 changes: 3 additions & 9 deletions nestjs/conf/src/game/gateway/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,15 +305,6 @@ export class EventsGateway {
}
}

// @SubscribeMessage('removePowerUp')
// removePowerUp(@ConnectedSocket() socket: Socket, @MessageBody() id: number) {
// const room = this.rooms.get(socket.data.match.id);
// let index = room.powerups.findIndex((powerup) => powerup.id == id);
// if (index != -1) {
// room.powerups.splice(index, 1);
// }
// }

@SubscribeMessage('maxWaitingTimeReached')
async maxWaitingTimeReached(@ConnectedSocket() socket: Socket) {
const room = this.rooms.get(socket.data.match.id);
Expand Down Expand Up @@ -380,16 +371,19 @@ export class EventsGateway {
);
if (powerupNames.length > 0) {
room.powerupInterval = setInterval(async () => {
let powerupID = Math.floor(Date.now());
let powerUpIndex = Math.floor(Math.random() * powerupNames.length);
let x =
Math.floor(Math.random() * (room.ball.fieldWidth - 70 - 70 + 1)) + 70;
let y = -70;
const newPowerUpData = {
powerupID,
powerUp: powerupNames[powerUpIndex],
x: x,
y: y,
};
this.server.to(room.socketIds[0]).emit('newPowerUp', newPowerUpData);
this.server.to(room.socketIds[1]).emit('newPowerUp', newPowerUpData);
}, 10000);
}
}
Expand Down
29 changes: 14 additions & 15 deletions nestjs/conf/src/game/service/ball.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,6 @@ export class Ball {
let nextBallX = this.x + this.dx;
let nextBallY = this.y + this.dy;

// if (this.scoreGoal(room, nextBallX, server)) {
// room.paddleA.resetHeight();
// room.paddleB.resetHeight();
// server.to(room.socketIds[0]).emit('resetPaddle');
// server.to(room.socketIds[1]).emit('resetPaddle');
// this.resetBall();
// this.pause = true;
// if (this.timeoutId) {
// clearTimeout(this.timeoutId);
// }
// this.timeoutId = setTimeout(() => {
// this.pause = false;
// }, 3000);
// } else
for (let powerup of room.powerups) {
if (this.handlePowerUpCollision(nextBallX, nextBallY, powerup)) {
let target;
Expand Down Expand Up @@ -198,7 +184,20 @@ export class Ball {
}
}

if (nextBallX + this.wid > this.fieldWidth) this.dx = -this.dx;
if (this.scoreGoal(room, nextBallX, server)) {
room.paddleA.resetHeight();
room.paddleB.resetHeight();
server.to(room.socketIds[0]).emit('resetPaddle');
server.to(room.socketIds[1]).emit('resetPaddle');
this.resetBall();
this.pause = true;
if (this.timeoutId) {
clearTimeout(this.timeoutId);
}
this.timeoutId = setTimeout(() => {
this.pause = false;
}, 3000);
} else if (nextBallX + this.wid > this.fieldWidth) this.dx = -this.dx;
else if (nextBallY + this.hgt > this.fieldHeight || nextBallY < 0)
this.dy = -this.dy;
else if (this.handleBallCollision(nextBallX, nextBallY, room, 'A')) {
Expand Down
115 changes: 78 additions & 37 deletions vue/conf/src/components/game/GameField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
import { faArrowUp, faArrowDown, faMagnet } from '@fortawesome/free-solid-svg-icons'
import { useNotificationStore } from '../../stores/notification'
import type { MatchI } from '../../model/match/match.interface'
import { match } from 'assert'
library.add(faArrowUp, faArrowDown, faMagnet)
const accessToken = localStorage.getItem('ponggame') ?? ''
Expand Down Expand Up @@ -136,7 +137,6 @@ const keyHookDown = (e: KeyboardEvent) => {
const keyHookUp = (e: KeyboardEvent) => {
if (!socket || !socket.value) {
notificationStore.showNotification(`Error: Connection problems`, true)
return
}
Expand Down Expand Up @@ -216,6 +216,10 @@ const formattedTimer = computed(() => {
})
const startTimer = () => {
if (!socket || !socket.value) {
return
}
timerId = setInterval(() => {
waitingTime.value++
if (waitingTime.value >= maxWaitingTime) {
Expand Down Expand Up @@ -243,6 +247,10 @@ const setEventListeners = () => {
}
socket.value.on('paddleMove', ({ playerId, newPos }: { playerId: string; newPos: number }) => {
if (matchResult.value) {
return
}
if (playerId === 'left') {
paddleA.value?.setY(newPos)
} else {
Expand All @@ -251,47 +259,60 @@ const setEventListeners = () => {
})
socket.value.on('ballPosition', ({ x, y }: { x: number; y: number }) => {
if (matchResult.value) {
return
}
ball.value?.setX(x)
ball.value?.setY(y)
ballCoordinates.value = { x, y }
})
socket.value.on('newPowerUp', (PowerUp: { powerUp: string; x: number; y: number }) => {
const newPowerUp = {
id: Math.floor(Date.now()),
x: PowerUp.x,
y: PowerUp.y,
type: 'null',
index: 0,
color: 'white',
wid: 70,
hgt: 70
}
if (PowerUp.powerUp == 'increasePaddleHeight') {
newPowerUp.color = 'white'
newPowerUp.index = 0
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'decreasePaddleHeight') {
newPowerUp.color = 'red'
newPowerUp.index = 1
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'magnet') {
newPowerUp.color = 'green'
newPowerUp.index = 2
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'slowBall') {
newPowerUp.color = 'blue'
newPowerUp.index = 3
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'fastBall') {
newPowerUp.color = 'yellow'
newPowerUp.index = 4
newPowerUp.type = PowerUp.powerUp
}
socket.value.on(
'newPowerUp',
(PowerUp: { powerupID: number; powerUp: string; x: number; y: number }) => {
if (!socket || !socket.value || matchResult.value) {
return
}
socket.value?.emit('spawnPowerUp', newPowerUp)
PowerUps.value?.push(newPowerUp)
})
const newPowerUp = {
id: PowerUp.powerupID,
x: PowerUp.x,
y: PowerUp.y,
type: 'null',
index: 0,
color: 'white',
wid: 70,
hgt: 70
}
if (PowerUp.powerUp == 'increasePaddleHeight') {
newPowerUp.color = 'white'
newPowerUp.index = 0
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'decreasePaddleHeight') {
newPowerUp.color = 'red'
newPowerUp.index = 1
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'magnet') {
newPowerUp.color = 'green'
newPowerUp.index = 2
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'slowBall') {
newPowerUp.color = 'blue'
newPowerUp.index = 3
newPowerUp.type = PowerUp.powerUp
} else if (PowerUp.powerUp == 'fastBall') {
newPowerUp.color = 'yellow'
newPowerUp.index = 4
newPowerUp.type = PowerUp.powerUp
}
if (side.value! == 'left') {
socket.value?.emit('spawnPowerUp', newPowerUp)
}
PowerUps.value?.push(newPowerUp)
}
)
socket.value.on('powerUpMove', ({ id, y }: { id: number; y: number }) => {
let powerUp = null
Expand All @@ -307,10 +328,13 @@ const setEventListeners = () => {
return
}
if (matchResult.value) {
return
}
let index = PowerUps.value?.findIndex((powerup) => powerup.id == id)
if (index != -1) {
PowerUps.value?.splice(index, 1)
// socket.value.emit('removePowerUp', id)
}
})
Expand All @@ -329,6 +353,10 @@ const setEventListeners = () => {
})
socket.value.on('activatePowerUp', ({ player, type }: { player: string; type: string }) => {
if (!socket || !socket.value || matchResult.value) {
return
}
let target
if (player == 'left') target = paddleA.value
else target = paddleB.value
Expand All @@ -355,11 +383,19 @@ const setEventListeners = () => {
})
socket.value.on('scoreGoal', (payload: string) => {
if (matchResult.value) {
return
}
if (payload == 'playerA') playerAScore.value++
else playerBScore.value++
})
socket.value.on('resetPaddle', () => {
if (matchResult.value) {
return
}
paddleA.value?.resetHgt()
paddleB.value?.resetHgt()
})
Expand Down Expand Up @@ -392,10 +428,15 @@ onBeforeUnmount(() => {
})
function update() {
if (!socket || !socket.value) {
return
}
if (isPaused.value) {
requestAnimationFrame(update)
return
}
if (side.value && side.value == 'left') {
if (paddleA.value && isMovingUp.value) {
if (socket.value) {
Expand Down

0 comments on commit 16648db

Please sign in to comment.