Skip to content

Commit

Permalink
Change direction name
Browse files Browse the repository at this point in the history
  • Loading branch information
takumihara committed Mar 5, 2024
1 parent 8092cd6 commit e4ca99c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 29 deletions.
14 changes: 6 additions & 8 deletions backend/src/events/events.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,32 +189,30 @@ export class EventsGateway implements OnGatewayDisconnect {
}

@UseGuards(UserGuardWs)
@SubscribeMessage('left')
async left(
@SubscribeMessage('up')
async up(
@MessageBody() data: string,
@ConnectedSocket() client: Socket,
): Promise<void> {
const roomId = client.handshake.query['game_id'] as string;
if (!isPlayer(this.players, roomId, client.id)) return;

this.logger.log(`left: ${client.id}`);

this.broadcastToRooms(client, 'left', {
this.broadcastToRooms(client, 'up', {
playerNumber: this.players[roomId][client.id],
});
return;
}

@UseGuards(UserGuardWs)
@SubscribeMessage('right')
async right(
@SubscribeMessage('down')
async down(
@MessageBody() data: string,
@ConnectedSocket() client: Socket,
): Promise<void> {
const roomId = client.handshake.query['game_id'] as string;
if (!isPlayer(this.players, roomId, client.id)) return;

this.broadcastToRooms(client, 'right', {
this.broadcastToRooms(client, 'down', {
playerNumber: this.players[roomId][client.id],
});
return;
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/lib/hooks/game/useGameKeyboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ export default function useGameKeyboard(getGame: () => PongGame) {
};
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key == "ArrowDown") {
game.setMovingDirection("right");
game.setMovingDirection("down");
} else if (event.key == "ArrowUp") {
game.setMovingDirection("left");
game.setMovingDirection("up");
}
};

Expand Down
20 changes: 10 additions & 10 deletions frontend/app/lib/hooks/game/useGameSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,19 @@ export default function useGameSocket(
setStartDisabled(true);
};

const handleRight = ({ playerNumber }: HandleActionProps) => {
const handleDown = ({ playerNumber }: HandleActionProps) => {
if (userMode !== "player" && playerNumber == 1) {
game.movePlayer1Left();
game.movePlayer1Down();
} else {
game.movePlayer2Left();
game.movePlayer2Up();
}
};

const handleLeft = ({ playerNumber }: HandleActionProps) => {
const handleUp = ({ playerNumber }: HandleActionProps) => {
if (userMode !== "player" && playerNumber == 1) {
game.movePlayer1Right();
game.movePlayer1Up();
} else {
game.movePlayer2Right();
game.movePlayer2Down();
}
};

Expand Down Expand Up @@ -222,8 +222,8 @@ export default function useGameSocket(

socket.on("connect", handleConnect);
socket.on("start", handleStart);
socket.on("right", handleRight);
socket.on("left", handleLeft);
socket.on("down", handleDown);
socket.on("up", handleUp);
socket.on("bounce", handleBounce);
socket.on("collide", handleCollide);
socket.on("update-status", handleUpdateStatus);
Expand All @@ -232,8 +232,8 @@ export default function useGameSocket(
return () => {
socket.off("connect", handleConnect);
socket.off("start", handleStart);
socket.off("right", handleRight);
socket.off("left", handleLeft);
socket.off("down", handleDown);
socket.off("up", handleUp);
socket.off("bounce", handleBounce);
socket.off("collide", handleCollide);
socket.off("update-status", handleUpdateStatus);
Expand Down
18 changes: 9 additions & 9 deletions frontend/app/pong/[id]/PongGame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TARGET_FRAME_MS,
} from "./const";

type movingDirectionType = "none" | "left" | "right";
type movingDirectionType = "none" | "up" | "down";
type onActionType = (action: string) => void;
type userModeType = "player" | "viewer";

Expand Down Expand Up @@ -120,16 +120,16 @@ export class PongGame {
this.elapsed = this.updatedAt === undefined ? 0 : now - this.updatedAt;
this.updatedAt = now;
if (this.userMode === "player") {
if (this.movingDirection === "left") {
if (this.movingDirection === "up") {
this.player1.clear(this.ctx);
this.player1.moveTop();
this.player1.draw(this.ctx);
this.onAction && this.onAction("left");
} else if (this.movingDirection === "right") {
this.onAction && this.onAction("up");
} else if (this.movingDirection === "down") {
this.player1.clear(this.ctx);
this.player1.moveDown();
this.player1.draw(this.ctx);
this.onAction && this.onAction("right");
this.onAction && this.onAction("down");
}
}
if (this.isPlaying) {
Expand Down Expand Up @@ -207,25 +207,25 @@ export class PongGame {
return this.ball.bounceOffPaddle(this.player2);
};

movePlayer1Left = () => {
movePlayer1Up = () => {
this.player1.clear(this.ctx);
this.player1.moveTop();
this.player1.draw(this.ctx);
};

movePlayer1Right = () => {
movePlayer1Down = () => {
this.player1.clear(this.ctx);
this.player1.moveDown();
this.player1.draw(this.ctx);
};

movePlayer2Left = () => {
movePlayer2Up = () => {
this.player2.clear(this.ctx);
this.player2.moveTop();
this.player2.draw(this.ctx);
};

movePlayer2Right = () => {
movePlayer2Down = () => {
this.player2.clear(this.ctx);
this.player2.moveDown();
this.player2.draw(this.ctx);
Expand Down

0 comments on commit e4ca99c

Please sign in to comment.