Skip to content

Commit

Permalink
[fix] boostcampwm-2022#137 충둘시 팅겨나오며 다른 유저의 방향이 바뀌는 문제 해결
Browse files Browse the repository at this point in the history
- direction을 받고 있기 때문에 받은 값을 사용해서 방향을 결정
  • Loading branch information
JJongBin committed Dec 8, 2022
1 parent 4b8b63f commit 35ecb10
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
4 changes: 4 additions & 0 deletions frontend/src/component/Game/Phaser/Player/myPlayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
changePosition,
changeState,
calcMoveToPos,
changeDirection,
} from '../../util';
import { Player } from './player';

Expand Down Expand Up @@ -71,6 +72,9 @@ export class MyPlayer extends Player {
const move: any = calcMoveToPos(this, this.heldDirection);
this.getBody().setVelocity(move.x * this.speed, move.y * this.speed);

const direction = move.x > 0 ? 'right' : 'left';
changeDirection(this, direction);

changePosition(this, move.x * this.speed, move.y * this.speed);
} else {
this.checkAndSetState('wait');
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/component/Game/Phaser/Player/otherPlayer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { changePosition, changeState } from '../../util';
import { changeDirection, changePosition, changeState } from '../../util';
import { Player } from './player';

export class OtherPlayer extends Player {
constructor(scene: Phaser.Scene, data: any) {
super(scene, data.x, data.y, data.id, data.characterName, data.nickname);
}
update(state: string, x: number, y: number) {
update(state: string, x: number, y: number, direction: string) {
const prevState = this.state;
this.state = state;

changeDirection(this, direction);

if (this.x !== x || this.y !== y)
changePosition(this, x - this.x, y - this.y);
if (prevState !== this.state) changeState(this);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/component/Game/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export default class Game extends Phaser.Scene {
const id = data.id.toString().trim();

if (!this.otherPlayer[id]) return;
this.otherPlayer[id].update(data.state, data.x, data.y);
const { state, x, y, direction } = data;
this.otherPlayer[id].update(state, x, y, direction);
});

this.socket.on('userLeaved', (data: any) => {
Expand Down
9 changes: 3 additions & 6 deletions frontend/src/component/Game/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@ export const changeState = (player: any) => {
}
};

export const changeDirection = (player: any, moveX: number) => {
export const changeDirection = (player: any, direction: string) => {
if (!player.character || !player.hair) return;
if (player.direction === 'left' && moveX <= 0) return;
if (player.direction === 'right' && moveX >= 0) return;
if (player.direction === direction) return;

player.character.toggleFlipX();
player.hair.toggleFlipX();
player.dust.toggleFlipX();

player.direction = player.direction === 'left' ? 'right' : 'left';
player.direction = direction;
};

export const calcMoveToPos = (player: any, dir: string[]) => {
Expand Down Expand Up @@ -57,8 +56,6 @@ export const changePosition = (player: any, x: number, y: number) => {
player.x += x;
player.y += y;

changeDirection(player, x);

player.character.setPosition(player.x, player.y);
player.hair.setPosition(player.x, player.y);

Expand Down

0 comments on commit 35ecb10

Please sign in to comment.