public bool ApplyMove(Player player, Vector2Int dest)
{
PositionInfo posInfo = player.Info.PosInfo;
if (posInfo.PosX < MinX || posInfo.PosX > MaxX)
return false;
if (posInfo.PosY < MinY || posInfo.PosY > MaxY)
return false;
if (CanGo(dest, true) == false)
return false;
{
int x = posInfo.PosX - MinX;
int y = MaxY - posInfo.PosY;
if(_players[y,x] == player) // 다른 플레이어 null로 만드는거 방지
_players[y, x] = null;
}
{
int x = dest.x - MinX;
int y = MaxY - dest.y;
_players[y, x] = player;
}
// 실제 좌표 이동
posInfo.PosX = dest.x;
posInfo.PosY = dest.y;
return true;
}
실제 좌표를 이동하기 전에 받아온 플레이어 유효한 영역 안에 있는지, 또 가고 싶은 좌표에 갈 수는 있는지, 현재 위치가 내 위치가 맞는지 체크해서 다른 플레이어 좌표를 null로 만드는 것을 방지하고, 목적지에 도착지 좌표를 갱신한 다음, 목적지 좌표를 player의 PosInfo에 갱신하도록 하였다.
public Vector2Int CellPos
{
get
{
return new Vector2Int(Info.PosInfo.PosX, Info.PosInfo.PosY);
}
set
{
Info.PosInfo.PosX = value.x;
Info.PosInfo.PosY = value.y;
}
}
public Vector2Int GetFrontCellPos(MoveDir dir)
{
Vector2Int cellPos = CellPos;
switch (dir)
{
case MoveDir.Up:
cellPos += Vector2Int.up;
break;
case MoveDir.Down:
cellPos += Vector2Int.down;
break;
case MoveDir.Left:
cellPos += Vector2Int.left;
break;
case MoveDir.Right:
cellPos += Vector2Int.right;
break;
}
return cellPos;
}
그리고 클라이언트 CreatureController에 정의해두었던 GetFrontCellPos를 Server::Player에 이동시켜주었다.
public void HandleSkill(Player player, C_Skill skillPacket)
{
...
// 데미지 판정
Vector2Int skillPos = player.GetFrontCellPos(info.PosInfo.MoveDir);
Player target = _map.Find(skillPos);
if(target != null)
{
Console.WriteLine("Hit Player!");
}
public Player Find(Vector2Int cellPos)
{
if (cellPos.x < MinX || cellPos.x > MaxX)
return null;
if (cellPos.y < MinY || cellPos.y > MaxY)
return null;
int x = cellPos.x - MinX;
int y = MaxY - cellPos.y;
return _players[y, x];
}
여기서 실행하면 바라보는 방향 앞에 아무도 없는데도 불구하고 hit player!가 뜨는 버그가 생기는데 이는 클라이언트에서 GetDirInput() 에서 키보드 입력을 받지 않으면 Dir에 None이 입력이 되고 서버에서 GetFrontCellPos로 dir가 넘어갈 때 None이 없기 때문에 결국 자기 자신의 좌표를 넘겨주기 때문에 생기는 버그였다. 따라서 protocol.proto를 아래와 같이 수정하고 관련된 코드를 제거/수정하는 것으로 버그를 해결했다.
enum MoveDir {
UP = 0;
DOWN = 1;
LEFT = 2;
RIGHT = 3;
}