I have a generated dungeon that has been working fine up until recently. For some reason, when I generate the dungeon it looks fine in the scene view, but in the game view, it stops showing after going down some. It does not seem to be a consistent point in the game
Suprisingly I have not noticed the issue occurring when going up, just when going down. Additionally, it seems like it may only happen when the player prefab is spawned. Unless im mistaken, when i have moved the camera around the map without the player it shows fine
**Credit to SharpAccent for the dungeon generation algorithm
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
public class FloorGrid : MonoBehaviour
{
public GameObject virtualCamera;
public ResourcesManager resources;
public DungeonGenerator geno;
public EnemyManager enemies;
public List<Enemy> enemiesInDungeon;
public GameObject player;
Node selectedNode;
public GameObject nodePrefab;
public Sprite ground;
public Sprite wall;
Node[,] gridGraph;
int maxX;
int maxY;
public float scale = .32f;
private void Awake()
{
//resources = Resources.Load("ResourcesManager") as ResourcesManager;
resources.Init();
}
//Unity Functions
private void Start()
{
geno.OnLevelComplete = LevelGenerated;
geno.GenerateLevel();
}
//Custom Functions
private void LevelGenerated()
{
InitializeGrid();
AddItemInRooms();
//AddEnemiesInRooms();
HandlePlayer();
//Add player, enemies, etc
}
private void InitializeGrid()
{
Vector2 maxXY = geno.GetMaxValues();
maxX = Mathf.CeilToInt(maxXY.x);
maxY = Mathf.CeilToInt(maxXY.y);
Vector2 minXY = geno.GetMinValues();
int minX = Mathf.CeilToInt(Mathf.Abs(minXY.x));
int minY = Mathf.CeilToInt(Mathf.Abs(minXY.y));
maxX += minX;
maxY += minY;
gridGraph = new Node[maxX + 1, maxY + 1];
//Render Cells
foreach(GeneratorCell cell in geno.cells)
{
for(int x = cell.xPos; x <= cell.xPos + cell.width; x++)
{
for(int y = cell.yPos; y <= cell.yPos + cell.height; y++)
{
Node n = gridGraph[x,y];
if(n == null)
{
n = CreateAt(x, y, false);
gridGraph[x,y] = n;
}
bool isWall = false;
if(x == cell.xPos || x == cell.xPos + cell.width || y == cell.yPos || y == cell.yPos + cell.height)
{
isWall = true;
}
n.isWall = isWall;
if(isWall)
{
n.nodeReference.render.sprite = wall;
}
else
{
n.nodeReference.render.sprite = ground;
}
}
}
}
//Render Paths
foreach(Path p in geno.paths)
{
foreach(BlockPath bp in p.path)
{
int startX = Mathf.FloorToInt(bp.start.x);
int startY = Mathf.FloorToInt(bp.start.y);
int endX = Mathf.CeilToInt(bp.end.x);
int endY = Mathf.CeilToInt(bp.end.y);
int temp = startX;
startX = Mathf.Min(startX, endX);
endX = Mathf.Max(endX, temp);
temp = startY;
startY = Mathf.Min(startY, endY);
endY = Mathf.Max(endY, temp);
for(int x = startX; x <= endX; x++)
{
for(int y = startY; y <= endY; y++)
{
Node n = gridGraph[x,y];
if(n == null)
{
CreateAt(x, y, false);
}
else
{
if(n.isWall)
{
n.isWall = false;
n.nodeReference.render.sprite = ground;
}
}
AddPathWalls(x,y);
if(startY == endY)
{
int targetY = y + 1;
if(y == maxY)
{
targetY = y - 1;
}
Node n2 = gridGraph[x, targetY];
if(n2 == null)
{
CreateAt(x, targetY, false);
}
else
{
if(n2.isWall)
{
n2.isWall = false;
n2.nodeReference.render.sprite = ground;
}
}
AddPathWalls(x, targetY);
}
}
}
}
}
}
private void HandlePlayer()
{
int rando = Random.Range(0, geno.cells.Count);
Debug.Log($"Room {rando} chosen");
GeneratorCell cell = geno.cells[rando];
float x = cell.xPos + cell.width / 2;
float y = cell.yPos + cell.height/2;
Debug.Log($"{x} , {y}");
selectedNode = GetNode(Mathf.RoundToInt(x), Mathf.RoundToInt(y), true);
Vector3 targetPos = selectedNode.worldPosition;
targetPos.z -= .1f;
GameObject Julia = Instantiate(player) as GameObject;
Julia.transform.position = targetPos;
var camera = virtualCamera.GetComponent<CinemachineVirtualCamera>();
camera.Follow = Julia.transform;
camera.LookAt = Julia.transform;
}
private void AddItemInRooms()
{
foreach(GeneratorCell cell in geno.cells)
{
float x = cell.xPos + cell.width/2;
float y = cell.yPos + cell.height/2;
Node node = GetNode(Mathf.RoundToInt(x), Mathf.RoundToInt(y));
Vector3 targetPos = node.worldPosition;
targetPos.z -= .05f;
Item ranItem = resources.GetRandomItem();
GameObject obj = Instantiate(resources.pickupPrefab) as GameObject;
Pickup pickup = obj.GetComponent<Pickup>();
pickup.sprite.sprite = ranItem.itemSprite;
pickup.itemInstance = ranItem;
obj.transform.parent = resources.instancesParent;
obj.transform.position = targetPos;
}
}
private void AddEnemiesInRooms()
{
foreach(GeneratorCell cell in geno.cells)
{
float x = cell.xPos + cell.width/2;
float y = cell.yPos + cell.height/2;
Node node = GetNode(Mathf.RoundToInt(x), Mathf.RoundToInt(y));
Vector3 targetPos = node.worldPosition;
targetPos.z -= .05f;
Enemy ranEnemy = enemies.SpawnRandomEnemy();
GameObject obj = Instantiate(enemies.enemySpawnpointPrefab) as GameObject;
obj.transform.parent = resources.instancesParent;
obj.transform.position = targetPos;
}
}
private Node GetNode(int x, int y, bool clamp = false)
{
if(x < 0 || x > maxX || y < 0 || y > maxY)
{
if(!clamp)
{
return null;
}
else
{
if(x < 0)
x = 0;
if(x > maxX)
x = maxX;
if(y < 0)
y = 0;
if(y > maxY)
y = maxY;
}
}
return gridGraph[x, y];
}
private Node CreateAt(int x, int y, bool isWall)
{
Node n = gridGraph[x,y];
if(n == null)
{
n = new Node();
n.x = x;
n.y = y;
GameObject temp = Instantiate(nodePrefab);
Vector3 targetPos = Vector3.zero;
NodeReferences nodeRef = temp.GetComponent<NodeReferences>();
n.nodeReference = nodeRef;
targetPos.x = x * scale;
targetPos.y = y * scale;
targetPos.z = y;
n.worldPosition = targetPos;
temp.transform.position = targetPos;
temp.transform.parent = this.transform;
gridGraph[x,y] = n;
}
n.isWall = isWall;
if(isWall)
{
n.nodeReference.render.sprite = wall;
}
else
{
n.nodeReference.render.sprite = ground;
}
return n;
}
private void AddPathWalls(int x, int y)
{
//top node
if(y < maxY)
{
Node n = gridGraph[x, y + 1];
if(n == null)
{
CreateAt(x, y + 1, true);
}
}
//right node
if(x < maxX)
{
Node n = gridGraph[x + 1, y];
if(n == null)
{
CreateAt(x + 1, y, true);
}
}
//bottom
if(y > 0)
{
Node n = gridGraph[x, y - 1];
if(n == null)
{
CreateAt(x, y - 1, true);
}
}
//left node
if(x > 0)
{
Node n = gridGraph[x - 1, y];
if(n == null)
{
CreateAt(x - 1, y, true);
}
}
}
}
public class Node
{
public int x;
public int y;
public bool isWall;
public Vector3 worldPosition;
public NodeReferences nodeReference;
}