클론코딩/애니팡
애니팡 클론코딩 (2) - 보드 구현
정주찬
2024. 1. 23. 18:42
보드 구현
보드를 구현하기 위한 위치 저장소
- Vector2[,]를 사용했는데 보드의 크기가 (7,7)로 정해져 있다는 조건 하에 배열을 선택했다.
보드판의 실제 객체들
- 딕셔너리로 구현했고 키값은 Vector2Int타입으로 받았는데 위의 배열의 인덱스로 사용하려는 의도였다.
- Value 값은 터치를 받을 CharacterTile 타입으로 받았다.
private Vector2[,] _boardPosArr;
private Dictionary<Vector2Int, CharacterTile> _board = new Dictionary<Vector2Int, CharacterTile>();
터치
터치 받기
- IBeginDragHandler를 사용 했는데 애니팡 게임에서 드래그가 일어나마자 반응하는 것을 구현하기 위해 해당 인터페이스를 사용했다.
- enum 타입의 Direction으로 상하좌우를 구분하게 했다.
public void OnBeginDrag(PointerEventData eventData)
{
bool isLeft = eventData.delta.x < 0;
bool isDown = eventData.delta.y < 0;
float deltaXAbs = isLeft ? -eventData.delta.x : eventData.delta.x;
float deltaYAbs = isDown ? -eventData.delta.y : eventData.delta.y;
if (deltaXAbs > deltaYAbs)
{
if (isLeft)
_dragInputted.Invoke(Direction.Left, _index);
else
_dragInputted.Invoke(Direction.Right, _index);
}
else
{
if (isDown)
_dragInputted.Invoke(Direction.Down, _index);
else
_dragInputted.Invoke(Direction.Up, _index);
}
}
public enum Direction
{
Right,
Left,
Up,
Down
}
드래그 콜백
Action<Direction, Vector2Int>
- 드래그가 일어났을 때 콜백으로 다음 객체와의 위치 교환을 하기 위해 방향 정보와 인덱스를 받았다.
class CharacterTile
{
private Action<Direction, Vector2Int> _dragInputted;
}
class MainGame
{
private void ChangeBoard(Vector2Int index, Vector2Int nextIndex)
{
// 교환 로직 ...
}
}
결과
상하좌우 교환이 가능하게 됨