Pathfinding
Remade prefabs, added scripts for pathfinding through navmesh, rewrote enemy spawn, roomGen and parts of handler
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Unity.AI.Navigation;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
public class MapGenManager : MonoBehaviour
|
||||
{
|
||||
@@ -20,6 +23,10 @@ public class MapGenManager : MonoBehaviour
|
||||
[Header("Layout")]
|
||||
[SerializeField] private MapLayout layout;
|
||||
|
||||
[Header("NavMesh Settings")]
|
||||
[SerializeField] private bool useGlobalNavMesh = true;
|
||||
private NavMeshSurface globalNavMeshSurface;
|
||||
|
||||
[Header("Generation Settings")]
|
||||
[SerializeField] private int RoomDistance = 3;
|
||||
|
||||
@@ -27,7 +34,13 @@ public class MapGenManager : MonoBehaviour
|
||||
public event Action<MapGenManager> OnGenerationComplete;
|
||||
public IReadOnlyDictionary<Vector2Int, GameObject> GridToRoom => gridToRoom;
|
||||
|
||||
void Start() => GenerateFromLayout();
|
||||
void Start() => StartCoroutine(GenerateWithDelay());
|
||||
|
||||
private IEnumerator GenerateWithDelay()
|
||||
{
|
||||
yield return null; // Počkej jeden frame
|
||||
GenerateFromLayout();
|
||||
}
|
||||
|
||||
private void GenerateFromLayout()
|
||||
{
|
||||
@@ -54,7 +67,7 @@ public class MapGenManager : MonoBehaviour
|
||||
GameObject spawnRoom = Instantiate(spawnPrefab, spawnPos, Quaternion.identity, transform);
|
||||
gridToRoom[new Vector2Int(spawnX, 0)] = spawnRoom;
|
||||
|
||||
|
||||
|
||||
/* ----------- Spawn the player ----------- */
|
||||
if (Player)
|
||||
{
|
||||
@@ -66,30 +79,8 @@ public class MapGenManager : MonoBehaviour
|
||||
/* ----------- Build rooms ----------- */
|
||||
BuildRooms(lines, bottomRow, cellSize);
|
||||
|
||||
/* ----------- Set entrances ----------- */
|
||||
foreach (var kv in gridToRoom)
|
||||
{
|
||||
Vector2Int g = kv.Key;
|
||||
RoomHandler rh = kv.Value.GetComponent<RoomHandler>();
|
||||
bool north = gridToRoom.ContainsKey(g + Vector2Int.left);
|
||||
bool south = gridToRoom.ContainsKey(g + Vector2Int.right);
|
||||
bool east = gridToRoom.ContainsKey(g + Vector2Int.up);
|
||||
bool west = gridToRoom.ContainsKey(g + Vector2Int.down);
|
||||
rh.SetEntrances(north, south, east, west);
|
||||
}
|
||||
|
||||
/* ----------- Build corridors ----------- */
|
||||
BuildCorridors();
|
||||
|
||||
|
||||
/* ----------- Toggle all doors ----------- */
|
||||
foreach (var keyValuePair in gridToRoom)
|
||||
{
|
||||
RoomHandler rh = keyValuePair.Value.GetComponent<RoomHandler>();
|
||||
rh.ToggleAllDoors();
|
||||
}
|
||||
|
||||
OnGenerationComplete?.Invoke(this);
|
||||
// Použij coroutine pro dokončení generování
|
||||
StartCoroutine(FinishGenerationAfterDelay());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -230,6 +221,64 @@ public class MapGenManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
private IEnumerator FinishGenerationAfterDelay()
|
||||
{
|
||||
yield return new WaitForSeconds(0.5f);
|
||||
|
||||
/* ----------- Set entrances ----------- */
|
||||
foreach (var kv in gridToRoom)
|
||||
{
|
||||
Vector2Int g = kv.Key;
|
||||
RoomHandler rh = kv.Value.GetComponent<RoomHandler>();
|
||||
bool north = gridToRoom.ContainsKey(g + Vector2Int.left);
|
||||
bool south = gridToRoom.ContainsKey(g + Vector2Int.right);
|
||||
bool east = gridToRoom.ContainsKey(g + Vector2Int.up);
|
||||
bool west = gridToRoom.ContainsKey(g + Vector2Int.down);
|
||||
rh.SetEntrances(north, south, east, west);
|
||||
}
|
||||
|
||||
/* ----------- Build corridors ----------- */
|
||||
BuildCorridors();
|
||||
|
||||
/* ----------- Toggle all doors ----------- */
|
||||
foreach (var keyValuePair in gridToRoom)
|
||||
{
|
||||
RoomHandler rh = keyValuePair.Value.GetComponent<RoomHandler>();
|
||||
rh.ToggleAllDoors();
|
||||
}
|
||||
|
||||
// Vytvoř globální NavMesh
|
||||
if (useGlobalNavMesh)
|
||||
{
|
||||
yield return new WaitForSeconds(0.2f);
|
||||
CreateGlobalNavMesh();
|
||||
}
|
||||
|
||||
OnGenerationComplete?.Invoke(this);
|
||||
}
|
||||
|
||||
private void CreateGlobalNavMesh()
|
||||
{
|
||||
// Odstraň všechny existující NavMeshSurface z místností
|
||||
foreach (var room in gridToRoom.Values)
|
||||
{
|
||||
NavMeshSurface roomSurface = room.GetComponent<NavMeshSurface>();
|
||||
if (roomSurface != null)
|
||||
{
|
||||
Destroy(roomSurface);
|
||||
}
|
||||
}
|
||||
|
||||
// Vytvoř globální NavMeshSurface
|
||||
globalNavMeshSurface = gameObject.AddComponent<NavMeshSurface>();
|
||||
globalNavMeshSurface.collectObjects = CollectObjects.Children;
|
||||
globalNavMeshSurface.useGeometry = NavMeshCollectGeometry.PhysicsColliders;
|
||||
|
||||
// Bake globální NavMesh
|
||||
globalNavMeshSurface.BuildNavMesh();
|
||||
Debug.Log("Global NavMesh baked");
|
||||
}
|
||||
|
||||
/* ============================================================ */
|
||||
/* HELPERS */
|
||||
/* ============================================================ */
|
||||
|
||||
Reference in New Issue
Block a user