Skip to content

Commit

Permalink
Merge branch 'feat/game-server-front' of github.com:iaurg/42-transcen…
Browse files Browse the repository at this point in the history
…dence into feat/game-server-front
  • Loading branch information
iaurg committed Jul 4, 2023
2 parents aa91c4b + 9bd9a32 commit d998aba
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions backend/src/game/game.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export class GameGateway implements OnGatewayConnection, OnGatewayDisconnect {
const game = this.gameLobby.joinPlayer2(client);
this.gamesPlaying[game.gameId] = game;
this.gameServer.to(game.gameId).emit('gameCreated', game.gameId);
this.gameService.restartBall(game);
this.startGame(client, game.gameId);
}
}
Expand Down
22 changes: 16 additions & 6 deletions backend/src/game/game.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ export class GameService {
private PLAYER2 = '2';
private PADDLE_WIDTH = 10;
private PADDLE_HEIGHT = 30;
private MAX_SCORE = 5;
private MAX_SCORE = 50;
private BALL_SPEED = 6;

updateBallPosition(gameDto: GameDto) {
gameDto.ball.x += gameDto.ball.dx;
Expand Down Expand Up @@ -100,13 +101,22 @@ export class GameService {
restartBall(gameDto: GameDto) {
gameDto.ball.x = gameDto.canvas.width / 2;
gameDto.ball.y = gameDto.canvas.height / 2;
gameDto.ball.dx = this.randomDirection();
gameDto.ball.dy = this.randomDirection();

const directionY = Math.random() < 0.5 ? 1 : -1;
const directionX = Math.random() < 0.5 ? 1 : -1;
const angle = this.getRandomAngle();

gameDto.ball.dx = this.BALL_SPEED * Math.cos(angle) * directionX;
gameDto.ball.dy = this.BALL_SPEED * Math.sin(angle) * directionY;
}

private randomDirection(): number {
const random = Math.random();
return random < 0.5 ? 4 : -4;
private getRandomAngle() {
const angles = [25, 35, 45, 55, 65];
const randomIndex = Math.floor(Math.random() * angles.length);
const randomDegree = angles[randomIndex];

const randomAngle = (randomDegree * Math.PI) / 180;
return randomAngle;
}

isGameFinished(gameDto: GameDto): boolean {
Expand Down
8 changes: 4 additions & 4 deletions backend/src/game/lobby/game.lobby.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ export class GameLobbyService {
},
player2: undefined,
ball: {
x: 400,
y: 300,
dx: 4,
dy: 4,
x: 0,
y: 0,
dx: 0,
dy: 0,
radius: 5,
},
canvas: {
Expand Down

0 comments on commit d998aba

Please sign in to comment.