생성
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class AbstractDungeonGenerator : MonoBehaviour
{
[SerializeField]
protected TilemapVisualizer tilemapVisualizer = null;
[SerializeField]
protected Vector2Int startPosition = Vector2Int.zero;
public void GenerateDungeon()
{
tilemapVisualizer.Clear();
RunProceduralGeneration();
}
protected abstract void RunProceduralGeneration();
}
수정
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;
public class SimpleRandomWalkDungeonGenerator : AbstractDungeonGenerator
{
[SerializeField]
private int iterations = 10;
[SerializeField]
public int walkLenght = 10;
[SerializeField]
public bool startRandomlyEachIteraction = true;
protected override void RunProceduralGeneration()
{
HashSet<Vector2Int> floorPositions = RunRandomWalk();
tilemapVisualizer.Clear();
tilemapVisualizer.PaintFloorTiles(floorPositions);
}
protected HashSet<Vector2Int> RunRandomWalk()
{
var currentPosition = startPosition;
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
for (int i = 0; i < iterations; i++)
{
var path = ProceduralGenerationAlgorithms.SimpleRandomWalk(currentPosition, walkLenght);
floorPositions.UnionWith(path);
if (startRandomlyEachIteraction)
currentPosition = floorPositions.ElementAt(Random.Range(0, floorPositions.Count));
}
return floorPositions;
}
}
코드
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(AbstractDungeonGenerator), true)]
public class RandomDungeonGeneratorEditor : Editor
{
AbstractDungeonGenerator generator;
private void Awake()
{
generator = (AbstractDungeonGenerator)target;
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if(GUILayout.Button("Create Dungeon"))
{
generator.GenerateDungeon();
}
}
}
코드
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName ="SimpleRandomWalkParameters_",menuName = "PCG/SimpleRandomWalkData")]
public class SimpleRandomWalkSO : ScriptableObject
{
public int iterations = 10, walkLenght = 10;
public bool startRandomlyEachIteration = true;
}
수정
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using Random = UnityEngine.Random;
public class SimpleRandomWalkDungeonGenerator : AbstractDungeonGenerator
{
[SerializeField]
private SimpleRandomWalkSO randomWalkParameters;
protected override void RunProceduralGeneration()
{
HashSet<Vector2Int> floorPositions = RunRandomWalk();
tilemapVisualizer.Clear();
tilemapVisualizer.PaintFloorTiles(floorPositions);
}
protected HashSet<Vector2Int> RunRandomWalk()
{
var currentPosition = startPosition;
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
for (int i = 0; i < randomWalkParameters.iterations; i++)
{
var path = ProceduralGenerationAlgorithms.SimpleRandomWalk(currentPosition, randomWalkParameters.walkLenght);
floorPositions.UnionWith(path);
if (randomWalkParameters.startRandomlyEachIteration)
currentPosition = floorPositions.ElementAt(Random.Range(0, floorPositions.Count));
}
return floorPositions;
}
}
코드
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CorridorFirstDungeonGenerator : SimpleRandomWalkDungeonGenerator
{
[SerializeField]
private int corridorLength = 14, corridorCount = 5;
[SerializeField]
private float roomPercent = 0.8f;
[SerializeField]
public SimpleRandomWalkSO roomGenerationParameters;
protected override void RunProceduralGeneration()
{
CorriderFirstGeneration();
}
private void CorriderFirstGeneration()
{
HashSet<Vector2Int> floorPositions = new HashSet<Vector2Int>();
CreateCorriders(floorPositions);
tilemapVisualizer.PaintFloorTiles(floorPositions);
WallGenerator.CreateWalls(floorPositions, tilemapVisualizer);
}
private void CreateCorriders(HashSet<Vector2Int> floorPositions)
{
var currentPosition = startPosition;
for (int i = 0; i < corridorCount; i++)
{
var corridor = ProceduralGenerationAlgorithms.RandomWalkCorridor(currentPosition, corridorLength);
currentPosition = corridor[corridor.Count - 1];
floorPositions.UnionWith(corridor);
}
}
}
'Project T > 진행' 카테고리의 다른 글
[팀 프로젝트] - 랜덤 워크와 이진 공간 분할 알고리즘(맵 생성6) (0) | 2021.12.03 |
---|---|
[팀 프로젝트] - 랜덤 워크와 이진 공간 분할 알고리즘(맵 생성5) (0) | 2021.12.02 |
[팀 프로젝트] - 랜덤 워크와 이진 공간 분할 알고리즘(맵 생성3) (0) | 2021.11.30 |
[팀 프로젝트] - 랜덤 워크와 이진 공간 분할 알고리즘(맵 생성2) (0) | 2021.11.29 |
[팀 프로젝트] - 랜덤 워크와 이진 공간 분할 알고리즘(맵 생성) (0) | 2021.11.26 |