본문 바로가기
게임프로젝트/쿠키런 모작

타일맵 랜덤 생성값 저장하기(2)

by pudding81 2024. 3. 30.

난수 제너레이터 새버전

시드를 다양하게 생성해서 타일맵을 다양한 순서로 만들 수 있다.

 

- 게임을 다시 시작할때마다 맵이 동일한 순서로 나오도록 함

- 타일맵의 배열의 인덱스 최소값과 최대값을 랜덤으로 생성하고

시드에 저장함으로서 맵의 순서를 동일하게 할 수 있음.

 

==>  다양한 시드의 생성이 가능하지만

인덱스의 순서는 컴퓨터가 뽑은 랜덤 숫자에 의한 것으로

개발자가 원하는 대로 순서를 지정 할 수는 없음. => 좀 더 연구해 볼 예정

 

using Redcode.Pools;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditor.EditorTools;
using UnityEngine;

public class TileManager : MonoBehaviour
{
    PoolManager poolManager;
   


    // 난수생성기
    public long seed = 6; // 0 ~ 9223372036854775807 (long 타입의 양수 범위)
    private int minNumber = 0; //타일맵 인덱스 최소범위
    public int maxNumber = 6; //타일맵 인덱스 최대범위
    //필요한 임의의 숫자들의 총 개수// 타일맵의 길이
    private int iterations = 99;
    //이미 생성된 임의의 숫자들의 리스트
    private List<int> results = new List<int>();

    // LCG Parameters
    // seed, a, c, m은 난수 생성 알고리즘에 사용되는 변수들로,
    // 각각 초기값, 곱셈 상수, 덧셈 상수, 모듈로 상수를 나타냅니다. 
    private const long a = 1103515245;
    private const long c = 12345;
    private const long m = 2147483647;


    private int count = 0;

    private void Awake()
    {
        poolManager = GetComponent<PoolManager>();
        
    }



    private void Start()
    {

        GenerateRandomNumbers();


        // 타일맵 생성 코루틴 
        StartCoroutine(CreateTileMap());



    }


  

    IEnumerator CreateTileMap() // 딜레이 시간마다 무한 생성 
    {

        while (true)
        {

            

            //int randomIndex = UnityEngine.Random.Range(0, 4); // 랜덤 인덱스 생성

            // 게임을 시작 할때마다 타일맵을 순서대로 활성화함
            int randomIndex = results[count];

            TileMapController tileMap = poolManager.GetFromPool<TileMapController>(randomIndex);
            // 여기서 randomIndex는 풀의 인덱스입니다.

            //  Debug.LogFormat("맵 번호 {0}", randomIndex);

           // 타일맵 인덱스를 순서대로 반복한다.
            count++;
            yield return new WaitForSeconds(4.5f);

        }


    }

    public void ReturnPool(TileMapController tileMap)
    {

       
        // Debug.Log("풀에 반환");
        // 오브젝트를 풀에 반환합니다.
        poolManager.TakeToPool<TileMapController>(tileMap.idname, tileMap);
       


    }





    //타일맵 랜덤 번호를 생성하고 저장
    void GenerateRandomNumbers()
    {
        long originalSeed = seed; // 초기 seed 값을 임시 변수에 저장
        results.Clear();
        for (int i = 0; i < iterations; i++)
        {
            seed = (a * seed + c) % m; // 현재 seed를 사용하여 난수 생성
            int randomNumber = minNumber + (int)(seed % (maxNumber - minNumber + 1));
            results.Add(randomNumber);
        }
        seed = originalSeed; // 난수 생성 후 원래 seed 값으로 복원
    }

}

 

https://github.com/kastro723/RandomSeedGenerator

 

GitHub - kastro723/RandomSeedGenerator

Contribute to kastro723/RandomSeedGenerator development by creating an account on GitHub.

github.com