보드 구현
보드를 구현하기 위한 위치 저장소
- 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)
{
// 교환 로직 ...
}
}
결과
상하좌우 교환이 가능하게 됨
'클론코딩 > 애니팡' 카테고리의 다른 글
애니팡 클론코딩 (6) - 연속적인 타일이 3개 이상인지 확인하기 (0) | 2024.02.01 |
---|---|
애니팡 클론코딩 (5) - 파괴 시 생성과 정렬, 커스텀 에디터 (1) | 2024.01.30 |
애니팡 클론코딩 (4) - 실패 이동, 파괴 로직 구현 (0) | 2024.01.25 |
애니팡 클론코딩 (3) - 랜덤 타일, 파괴할 타일 추가 (0) | 2024.01.24 |
애니팡 클론코딩 (1) - Slider 구현 (0) | 2024.01.17 |