Skip to content
김광수 edited this page Oct 11, 2023 · 1 revision

< Readme

UIMap

MapManager에서 Map의 정보를 받아와서 UIMap 프리펩에 Map을 그려줍니다.

Code Summary

Field

  • floorNumber

    • 만들고자 하는 맵의 층수정보
  • root

  • 생성할 지역, 경로들의 부모 오브젝트

  • route

    • 맵의 이동경로를 표시해줄 GameObject 프리펩
  • Battle

    • Battle 이벤트의 GameObject 프리펩
  • Chest

    • Chest 이벤트의 GameObject 프리펩
  • Potion

    • Potion 이벤트의 GameObject 프리펩
  • Elite

    • Elite 이벤트의 GameObject 프리펩
  • floor

    • 현재 층수 정보를 저장
  • roundTxt

    • 현재 층수 정보를 그려줄 TextMeshProGUI

Method

private

  • Start()

    • UIMap 오브젝트가 생성됬을때 실행
    • InitMap() 메서드를 실행하여 UI를 초기화하고 맵을 그린다.
  • Update()

    • GameManager의 현재 층수 정보를 확인하여 UIMap의 floor와 다를시 floor와 roundTxt를 업데이트한다.
  • InitMap()

    • 현재 층 수를 0으로 초기화한다
    • MapManager에게 새로운 맵의 생성을 요청한다
    • MapManager에서 가진 Map을 참조해서 UI를 그리는 DrawMap(), DrawRoute() 메서드를 호춣한다.
  • DrawMap()

    • UI의 크기에 맞게 위치를 조정해서 DrawIcon() 메서드를 통해 한 Floor씩 UI를 그린다.
  • DrawIcon()

    • UI의 크기에 맞게 위치를 조정해서 한 층에 해당되는 location들의 아이콘을 그린다.
    • 각각의 Icon들에게 location의 정보를 전달한다.
  • SelectEvent()

    • LocationInfo에 저장된 EventType에 따라서 어떤 Icon을 그릴지 정한다.
  • DrawRoute()

    • Map을 순회하면서 아래층에서 위층으로가는 경로를 UI에 그려준다.

Code

using System.Collections.Generic;
using TMPro;
using UnityEngine;

public class UIMap : UIBase
{
    public int floorNumber;
    public GameObject root;
    public GameObject route;

    public GameObject Battle;
    public GameObject Chest;
    public GameObject Potion;
    public GameObject Elite;

    public int floor;
    public TextMeshProUGUI roundTxt;

    private void Start()
    {
        InitMap();
        
    }

    private void Update()
    {
        LocationInfo current = GameManager.Instance.CurrentLocationInfo;
        if (current != null && floor != current.Floor + 1)
        {
            floor = current.Floor + 1;
            roundTxt.text = (floor).ToString();
        }
    }

    private void InitMap()
    {
        floor = 0;
        roundTxt.text = floor.ToString();
        MapManager.Instance.GenerateMapStructure(floorNumber);
        DrawMap();
        DrawRoute();
    }

    private void DrawMap()
    {
        for (int i = 0; i < floorNumber; i++)
        {
            DrawIcon(MapManager.Instance.Map[i], -400 + 20 + 800 / floorNumber * i);
            // 맵 root의 길이 760 880
            // 880 / 11 한층의 높이 80
        }

    }

    private void DrawIcon(List<LocationInfo> floor, float y)
    {
        foreach (LocationInfo location in floor)
        {
            GameObject select = SelectEvent(location.EventType);
            GameObject go = Instantiate(select, root.transform);
            location.rectTransform = go.GetComponent<RectTransform>();
            go.transform.localPosition = new Vector3(-380 + 20 + 720 / 9 * location.Index, y, 0);
            go.GetComponentInChildren<EventIcon>().info = location;

        }
        
        // -380 == floor의길이 / 2 * -1
        // +20 == icon의 width / 2
        // 720 == floor의 길이 - icon의 width
        // 최대 인덱스 0 ~ 9
        // info.Index 현재 인덱스
        // 놓아도 되는 범위는? -360 ~ 360 여기에 10개까지 들어간다.
    }

    private GameObject SelectEvent(EventType type)
    {
        switch (type)
        {
            case EventType.Battle:
                return Battle;
            case EventType.Chest:
                return Chest;
            case EventType.Potion:
                return Potion;
            case EventType.Elite:
                return Elite;
            default:
                break;
        }
        return null;
    }

    private void DrawRoute()
    {
        for(int i =0; i < floorNumber-1; i++)
        {
            foreach(LocationInfo location in MapManager.Instance.Map[i])
            {
                int nextIndex = location.RouteIndexs[0];
                Vector3 start = location.rectTransform.position;
                Vector3 end = MapManager.Instance.Map[i + 1][nextIndex].rectTransform.position;
                Vector3 direction = (end - start).normalized;
                float angle = Mathf.Atan2(direction.y, direction.x) *Mathf.Rad2Deg;

                Instantiate(route, (start+end) / 2f, Quaternion.Euler(0,0,angle), root.transform);

            }
        }
        
    }
}

Clone this wiki locally