Adaid's Workroom

[IC] Isometric 타일맵 에디터 제작기 (4) - 맵 그리드와 스냅 그리드 분리 본문

프로젝트/Infinite Cocktail

[IC] Isometric 타일맵 에디터 제작기 (4) - 맵 그리드와 스냅 그리드 분리

어데이드 2018. 1. 5. 15:08

18.01.05

맵 그리드와 스냅 그리드 분리하여 Draw

맵 그리드와 스냅 그리드

맵 그리드가 맵을 타일 단위로 보여준다면 스냅 그리드는 그와 별개로 수치를 입력받아 보여준다.

타일 크기 128, (스냅) 그리드 크기 64이면 아래과 같이 보여지는 식.

점선이 스냅 그리드이다.




수정 & 추가된 함수들.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void OnSceneGUI()
{
    SceneView.RepaintAll();
 
    //그리드 상수 업데이트
    mapGridWidth = mapManager.tileSize * gridConst * 0.5f;
    mapGridHeight = mapManager.tileSize * gridConst * 0.25f;
    snapGridWidth = mapManager.gridSize * gridConst * 0.5f;
    snapGridHeight = mapManager.gridSize * gridConst * 0.25f;
 
    initialPosition = mapManager.transform.position;
    initialPosition.y += mapGridHeight;
 
    //그리드 Draw
    DrawMapGrid();
    DrawSnapGrid();
 
    //마우스 입력 관련
   ...
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
private void DrawMapGrid()
    {
        //그리드 선 색 설정
        Handles.color = mapManager.mapGridColor;
 
        //왼쪽 아래 방향 그리드 Draw
        Vector2 startPos = initialPosition;
        Vector2 endPos = initialPosition;
         
        endPos.x -= mapManager.mapHeight * mapGridWidth;
        endPos.y -= mapManager.mapHeight * mapGridHeight;
 
        for (int i = 0; i < (mapManager.mapWidth + 1); i++)
        {
            Handles.DrawLine(startPos, endPos);
 
            startPos.x += mapGridWidth;
            startPos.y -= mapGridHeight;
            endPos.x += mapGridWidth;
            endPos.y -= mapGridHeight;
        }
 
        //오른쪽 아래 방향 그리드 Draw
        startPos = initialPosition;
        endPos = initialPosition;
 
        endPos.x += mapManager.mapWidth * mapGridWidth;
        endPos.y -= mapManager.mapWidth * mapGridHeight;
 
        for (int i = 0; i < (mapManager.mapHeight + 1); i++)
        {
            Handles.DrawLine(startPos, endPos);
 
            startPos.x -= mapGridWidth;
            startPos.y -= mapGridHeight;
            endPos.x -= mapGridWidth;
            endPos.y -= mapGridHeight;
        }
 
    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
private void DrawSnapGrid()
    {
        int snapWidthCount = Mathf.FloorToInt(mapManager.mapWidth * (mapGridWidth / snapGridWidth)) + 1;
        int snapHeightCount = Mathf.FloorToInt(mapManager.mapHeight * (mapGridHeight / snapGridHeight)) + 1;
 
        //그리드 선 색 설정
        Handles.color = mapManager.snapGridColor;
 
        //왼쪽 아래 방향 그리드 Draw
        Vector2 startPos = initialPosition;
        Vector2 endPos = initialPosition;
         
        endPos.x -= mapManager.mapHeight * mapGridWidth;
        endPos.y -= mapManager.mapHeight * mapGridHeight;
 
        for (int i = 0; i < snapWidthCount; i++)
        {
           Handles.DrawDottedLine(startPos, endPos, 2f);
 
            startPos.x += snapGridWidth;
            startPos.y -= snapGridHeight;
            endPos.x += snapGridWidth;
            endPos.y -= snapGridHeight;
        }
 
        //오른쪽 아래 방향 그리드 Draw
        startPos = initialPosition;
        endPos = initialPosition;
         
        endPos.x += mapManager.mapWidth * mapGridWidth;
        endPos.y -= mapManager.mapWidth * mapGridHeight;
 
        for (int i = 0; i < snapHeightCount; i++)
        {
            Handles.DrawDottedLine(startPos, endPos, 2f);
 
            startPos.x -= snapGridWidth;
            startPos.y -= snapGridHeight;
            endPos.x -= snapGridWidth;
            endPos.y -= snapGridHeight;
        }
    }

변경점중 하나는 약간의 최적화 및 코드 간결화를  initialPosition을 미리 계산하여 사용한것이다.

여튼 위와 같이 개선하여 맵과 스냅의 그리드를 각각 따로 보여주었다.

Comments