C#/문제해결

0818 맵만들기

버스창문 2022. 8. 18. 00:13

  1 2 3 4 5 6 7
0        ㅇ
1     ㄴㄴㄴ
2
3
4
5
6
7


맵을 dictionary로
맵 : {int x , int y , id -> (저 ㅇ 기준 400(x *100 + y)) , List<int> nextMapId}
 다음 맵을 301, 401, 501(ㄴ 부분)(1~3개 랜덤으로 생성) 이라는 id로 넣고, dictionary에 그 id가 이미 있는지 체크, 없을시 새 맵 생성(재귀함수) 
현재 id가 x07 일시 다음맵은 보스맵으로 아예 새로운 id
현재 id가 0xx 혹은 6xx 일시 왼쪽 혹은 오른쪽으로 가지않음(-1 혹은 1이 들어가지않음)

 

 

https://gall.dcinside.com/mgallery/board/view/?id=slay&no=54917

 

슬더스 알쓸신잡) 맵 생성 규칙에 대해서 - 슬레이 더 스파이어 마이너 갤러리

이번 글에선 맵 생성 규칙이랑 각종 제반확률들에 대해서 좀 적어보려고 함.뭔가 적다보니까 이것저것 잡다하게 적을 사항이 많아서 번호를 붙여서 적겠음.1. 맵이 만들어지는 과정각 막에 진입

gall.dcinside.com

 

 

 

 

0818

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Map
{
    public int x;
    public int y;
    public int id;
    public List<int> nextMapIds;
    public UnityAction<int,int> next;

    public Map(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.id = x * 100 + y;
        if (this.y < 7)
        {
            this.next(this.x + Random.Range(-1, 2), this.y + 1);
        }
    }
    /*public void Init(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.id = x * 100 + y;
    }*/
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapManager : MonoBehaviour
{
    public Dictionary<int, Map> dicMap;
    private void Awake()
    {
        this.dicMap = new Dictionary<int, Map>();
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

 

 

0819

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Map
{
    public int x;
    public int y;
    public int id;
    public List<int> nextMapIds;
    public UnityAction<int, int , int> next;

    public Map(int x, int y)
    {
        this.x = x;
        this.y = y;
        this.id =10000 + x * 100 + y;
    }
    public void Init()
    {
        if (this.y < 7)
        {
            this.next(this.x, this.y, Random.Range(1, 4));
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapManager : MonoBehaviour
{
    public Dictionary<int, Map> dicMap;
    public Map startMap;
    private void Awake()
    {
        this.dicMap = new Dictionary<int, Map>();
        this.startMap = new Map(3,0);
    }
    // Start is called before the first frame update
    void Start()
    {
        this.mapInit(this.startMap);
        this.startMap.Init();
        this.dicMap.Add(this.startMap.id,this.startMap);
        Debug.Log(this.dicMap.Count);
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void mapInit(Map map)
    {
        Debug.Log(map.id);
        map.next = (x, y, count) => {
            switch (count)
            {
                case 1:
                    {
                        var ran = Random.Range(-1, 2);
                        if (!this.dicMap.ContainsKey(10000 + (100 * (x + ran)) + y))
                        {
                            if (!((x + ran) < 0 || (x + ran) > 600))
                            {
                                var newMap = new Map(x + ran, y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }

                        }
                        break;
                    }
                case 2:
                    {
                        List<int> nextXs = new List<int>();
                        var ran = Random.Range(-1, 2);
                        switch (ran)
                        {
                            case -1:
                                {
                                    nextXs.Add(0);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 0:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 1:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(0);
                                    break;
                                }
                        }
                        foreach (int a in nextXs)
                        {

                            if (!this.dicMap.ContainsKey(10000 + (100 * (x + a)) + y))
                            {
                                if (!((x + a) < 0 || (x + a) > 600))
                                {
                                    var newMap = new Map(x + a, y + 1);
                                    this.mapInit(newMap);
                                    newMap.Init();
                                    dicMap.Add(newMap.id, newMap);
                                }
                            }
                        }
                        /*if (!this.dicMap.ContainsKey(10000 + (100 * (x + nextXs[0])) + y))
                        {
                            if (!((x + nextXs[0]) < 0 || (x + nextXs[0]) > 600))
                            {
                                var newMap = new Map(x + nextXs[0], y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }
                        }
                        if (!this.dicMap.ContainsKey(10000 + (100 * (x + nextXs[1])) + y))
                        {
                            if (!((x + nextXs[1]) < 0 || (x + nextXs[1]) > 600))
                            {
                                var newMap = new Map(x + nextXs[1], y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }
                        }*/

                        break;
                    }
                case 3:
                    {
                        for (int i = -1; i < 2; i++)
                        {
                            if (!this.dicMap.ContainsKey(10000 + (100 * (x + i)) + y))
                            {
                                if (!((x + i) < 0 || (x + i) > 600))
                                {
                                    var newMap = new Map(x + i, y + 1);
                                    this.mapInit(newMap);
                                    newMap.Init();
                                    dicMap.Add(newMap.id, newMap);
                                }
                            }
                        }
                        /*if (!this.dicMap.ContainsKey(10000 + (100 * (x -1)) + y))
                        {
                            if (!((x -1) < 0 || (x -1) > 600))
                            {
                                var newMap = new Map(x -1, y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }
                        }
                        if (!this.dicMap.ContainsKey(10000 + (100 * (x)) + y))
                        {
                            if (!((x) < 0 || (x) > 600))
                            {
                                var newMap = new Map(x, y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }
                        }
                        if (!this.dicMap.ContainsKey(10000 + (100 * (x + 1)) + y))
                        {
                            if (!((x + 1) < 0 || (x + 1) > 600))
                            {
                                var newMap = new Map(x + 1, y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }
                        }*/
                        break;
                    }
            }
        };
    }
}

왜안되지 진짜

 

 

0819

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

public class MapManager : MonoBehaviour
{
    public Dictionary<int, Map> dicMap;
    public Map startMap;
    private void Awake()
    {
        this.dicMap = new Dictionary<int, Map>();
        this.startMap = new Map(3,0);
    }
    // Start is called before the first frame update
    void Start()
    {
        this.mapInit(this.startMap);
        this.startMap.Init();
        this.dicMap.Add(this.startMap.id,this.startMap);
        Debug.Log(this.dicMap.Count);
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void mapInit(Map map)
    {
        Debug.Log(map.id);
        map.next = (x, y, count) => {
            switch (count)
            {
                case 1:
                    {
                        var ran = Random.Range(-1, 2);
                        if (!this.dicMap.ContainsKey(10000 + (100 * (x + ran)) + y+1))
                        {
                            if (!((x + ran) < 0 || (x + ran) > 600))
                            {
                                var newMap = new Map(x + ran, y + 1);
                                this.mapInit(newMap);
                                newMap.Init();
                                dicMap.Add(newMap.id, newMap);
                            }

                        }
                        break;
                    }
                case 2:
                    {
                        List<int> nextXs = new List<int>();
                        var ran = Random.Range(-1, 2);
                        switch (ran)
                        {
                            case -1:
                                {
                                    nextXs.Add(0);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 0:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 1:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(0);
                                    break;
                                }
                        }
                        foreach (int a in nextXs)
                        {

                            if (!this.dicMap.ContainsKey(10000 + (100 * (x + a)) + y+1))
                            {
                                if (!((x + a) < 0 || (x + a) > 600))
                                {
                                    var newMap = new Map(x + a, y + 1);
                                    this.mapInit(newMap);
                                    newMap.Init();
                                    dicMap.Add(newMap.id, newMap);
                                }
                            }
                        }

                        break;
                    }
                case 3:
                    {
                        for (int i = -1; i < 2; i++)
                        {
                            if (!this.dicMap.ContainsKey(10000 + (100 * (x + i)) + y+1))
                            {
                                if (!((x + i) < 0 || (x + i) > 600))
                                {
                                    var newMap = new Map(x + i, y + 1);
                                    this.mapInit(newMap);
                                    newMap.Init();
                                    dicMap.Add(newMap.id, newMap);
                                }
                            }
                        }
                        break;
                    }
            }
        };
    }
}

아오 이전거 있는지 체크하고 집어넣고있었네

성공

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MapManager : MonoBehaviour
{
    public Dictionary<int, Map> dicMap;
    public Map startMap;
    public GameObject btnPrefab;
    private void Awake()
    {
        this.dicMap = new Dictionary<int, Map>();
        this.startMap = new Map(3, 0);
    }
    // Start is called before the first frame update
    void Start()
    {
        this.mapInit(this.startMap);
        this.startMap.Init();
        this.dicMap.Add(this.startMap.id, this.startMap);
        Debug.Log(this.dicMap.Count);
        foreach(var a in this.dicMap) 
        {
            var pos = GameObject.Find("map_" + a.Value.id.ToString());
            var btn = Instantiate(this.btnPrefab, pos.transform);
            btn.GetComponent<Button>().onClick.AddListener(() => {
                Debug.LogFormat("id : {0} , next : {1}",a.Value.id,a.Value.nextMapIds.Count);
                foreach(var id in a.Value.nextMapIds)
                {
                    Debug.Log(id);
                }
            });
        }
        
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void mapInit(Map map)
    {
        Debug.Log(map.id);
        map.next = (x, y, count) => {
            switch (count)
            {
                case 1:
                    {
                        var ran = Random.Range(-1, 2);
                        this.MapCreate(x, y, ran, map);
                        
                        break;
                    }
                case 2:
                    {
                        List<int> nextXs = new List<int>();
                        var ran = Random.Range(-1, 2);
                        switch (ran)
                        {
                            case -1:
                                {
                                    nextXs.Add(0);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 0:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 1:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(0);
                                    break;
                                }
                        }
                        foreach (int a in nextXs)
                        {
                            this.MapCreate(x, y, a, map);
                        }

                        break;
                    }
                case 3:
                    {
                        for (int i = -1; i < 2; i++)
                        {
                            this.MapCreate(x, y, i, map);

                        }
                        break;
                    }
            }
        };
    }
    public void MapCreate(int x, int y, int a , Map map)
    {
        if (!this.dicMap.ContainsKey(10000 + (100 * (x + a)) + y + 1))
        {
            if (!((x + a) < 0 || (x + a) > 6))
            {
                var newMap = new Map(x + a, y + 1);
                this.mapInit(newMap);
                newMap.Init();
                dicMap.Add(newMap.id, newMap);
            }

        }
        if (!((x + a) < 0 || (x + a) > 6))
            map.nextMapIds.Add(10000 + (100 * (x + a)) + y + 1);
    }
}

 

미리 비어있는 맵을 만들고 맵 생성하면 생성 된곳에만 버튼을 만드는 기능추가,

그 버튼을 누를시 해당 맵의 id와 다음 맵 id를 출력하는 기능 추가

 

 

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public enum eMapKind
{
    battle, elite, shop, question, boss
}
public class Map
{
    
    public int x;
    public int y;
    public int id;
    public eMapKind mapKind;
    public List<int> nextMapIds;
    public UnityAction<int, int, int> next;

    public Map(int x, int y)
    {
        this.nextMapIds = new List<int>();
        this.x = x;
        this.y = y;
        this.id = 10000 + x * 100 + y;
    }
    public void Init(int kind)
    {
        this.mapKind = (eMapKind)kind;
        if (this.y < 7)
        {
            this.next(this.x, this.y, Random.Range(1, 4));
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class MapManager : MonoBehaviour
{
    public Dictionary<int, Map> dicMap;
    public Map startMap;
    public GameObject btnPrefab;
    private void Awake()
    {
        this.dicMap = new Dictionary<int, Map>();
        this.startMap = new Map(3, 0);
    }
    // Start is called before the first frame update
    void Start()
    {
        this.mapInit(this.startMap);
        this.startMap.Init(this.RandomKind());
        this.dicMap.Add(this.startMap.id, this.startMap);
        Debug.Log(this.dicMap.Count);
        foreach(var a in this.dicMap) 
        {
            var pos = GameObject.Find("map_" + a.Value.id.ToString());
            var btn = Instantiate(this.btnPrefab, pos.transform);
            btn.GetComponent<Button>().onClick.AddListener(() => {
                Debug.LogFormat("id : {0} , kind : {1}",a.Value.id,a.Value.mapKind);
                foreach(var id in a.Value.nextMapIds)
                {
                    Debug.Log(id);
                }
            });
        }
        
    }

    // Update is called once per frame
    void Update()
    {

    }
    public void mapInit(Map map)
    {
        Debug.Log(map.id);
        map.next = (x, y, count) => {
            switch (count)
            {
                case 1:
                    {
                        var ran = Random.Range(-1, 2);
                        this.MapCreate(x, y, ran, map);
                        
                        break;
                    }
                case 2:
                    {
                        List<int> nextXs = new List<int>();
                        var ran = Random.Range(-1, 2);
                        switch (ran)
                        {
                            case -1:
                                {
                                    nextXs.Add(0);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 0:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(1);
                                    break;
                                }
                            case 1:
                                {
                                    nextXs.Add(-1);
                                    nextXs.Add(0);
                                    break;
                                }
                        }
                        foreach (int a in nextXs)
                        {
                            this.MapCreate(x, y, a, map);
                        }

                        break;
                    }
                case 3:
                    {
                        for (int i = -1; i < 2; i++)
                        {
                            this.MapCreate(x, y, i, map);

                        }
                        break;
                    }
            }
        };
    }
    public void MapCreate(int x, int y, int a , Map map)
    {
        if (!this.dicMap.ContainsKey(10000 + (100 * (x + a)) + y + 1))
        {
            if (!((x + a) < 0 || (x + a) > 6))
            {
                var newMap = new Map(x + a, y + 1);
                this.mapInit(newMap);
                newMap.Init(this.RandomKind());
                dicMap.Add(newMap.id, newMap);
            }

        }
        if (!((x + a) < 0 || (x + a) > 6))
            map.nextMapIds.Add(10000 + (100 * (x + a)) + y + 1);
    }
    public int RandomKind()
    {
        var ran = Random.Range(1, 100);
        int kind = 0;
        if (ran >= 1 && ran < 65)
        {
            kind = 0;
        }
        else if (ran >= 65 && ran < 80)
        {
            kind = 1;
        }
        else if (ran >= 80 && ran < 90)
        {
            kind = 2;
        }
        else if (ran >= 90 && ran < 100)
        {
            kind = 3;
        }
        return kind;
    }
}

 

맵에 종류를 만들고, 랜덤으로 종류를 부여하는 기능 추가
그리고 버튼누르면 본인의 id 와 종류를 출력

 

이제 선찍어야함
https://dhshin94.tistory.com/161

 

[유니티] 롤 골드그래프를 만들어보자(2) - 선 찍기

UI를 이용하여 롤 골드그래프를 만들어보자 코드만 필요하신분은 아래로 내리시면 있습니다. 이전에 점까지 찍었으니 이제 선을 찍을 차례입니다. 각 부분 코드만 정리하고 전체코드는 마지막

dhshin94.tistory.com

 

 

 

 

이렇게 라인이 있으면 일단 얘가 버튼 뒤로 가있어야하고,

이 Height부분이 동적으로 길이가 변해야함

'C# > 문제해결' 카테고리의 다른 글

0830 머신러닝 Ray  (0) 2022.08.30
0821 등급관리 프로그램  (1) 2022.08.21
0817 polybrush안됨  (0) 2022.08.17
0816 유니티에 pmx 가져오기  (0) 2022.08.16
0729 별 순서대로 나오게하기  (0) 2022.07.29