I’m trying to make a rouglike but I don’t know how to spawn an enemy on a random position on a procedurally generated tile map I tried using GetTileblock but it didn’t work
using System;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using UnityEngine.Serialization;
using UnityEngine.Tilemaps;
using Random = System.Random;
public class WalkerGenerator : MonoBehaviour
{
public enum Grid
{
Floor,
Water,
Empty
}
private Grid[,] GridHanler;
private List<WalkerObject> Walkers;
public Tilemap tilemap;
public Tilemap tilemap2;
public RuleTile floor;
public Tile water;
public int mapWidth;
public int mapHeight;
public int maximumWalkers;
public int tileCount;
public float fillPercentage;
public float waitTime;
public GameObject player;
public Vector3Int center;
public float time;
public bool done = false;
public GameObject enemy;
private void Start()
{
InitializeGrid();
}
private void Update()
{
spawn_enemys();
}
void InitializeGrid()
{
GridHanler = new Grid[mapWidth, mapHeight];
for (int x = 0; x < GridHanler.GetLength(0); x++)
{
for (int y = 0; y < GridHanler.GetLength(1); y++)
{
GridHanler[x, y] = Grid.Empty;
}
}
Walkers = new List<WalkerObject>();
Vector3Int tileCenter = new Vector3Int(GridHanler.GetLength(0) / 2, GridHanler.GetLength(1) / 2, 0);
center = tileCenter;
WalkerObject curWalker = new WalkerObject(new Vector2(tileCenter.x, tileCenter.y), GetDirection(), 0.5f);
GridHanler[tileCenter.x, tileCenter.y] = Grid.Floor;
tilemap.SetTile(tileCenter, floor);
Walkers.Add(curWalker);
tileCount++;
StartCoroutine(CreateFloors());
}
Vector2 GetDirection()
{
int choice = Mathf.FloorToInt(UnityEngine.Random.value * 3.99f);
switch (choice)
{
case 0:
return Vector2.down;
case 1:
return Vector2.left;
case 2:
return Vector2.up;
case 3:
return Vector2.right;
default:
return Vector2.zero;
}
}
IEnumerator CreateFloors()
{
while ((float)tileCount / (float)GridHanler.Length <= fillPercentage)
{
bool hasCreatedFloor = false;
foreach (WalkerObject curWalker in Walkers)
{
Vector3Int curPos = new Vector3Int((int)curWalker.Position.x, (int)curWalker.Position.y, 0);
if (GridHanler[curPos.x, curPos.y] != Grid.Floor)
{
tilemap.SetTile(curPos, floor);
tileCount++;
GridHanler[curPos.x, curPos.y] = Grid.Floor;
hasCreatedFloor = true;
}
}
//Walker Methods
ChanceToRemove();
ChanceToRedirect();
ChanceToCreate();
UpdatePosition();
if (hasCreatedFloor)
{
yield return new WaitForSeconds(waitTime);
}
}
StartCoroutine(CreateWalls());
}
void ChanceToRemove()
{
int updatedCount = Walkers.Count;
for (int i = 0; i < updatedCount; i++)
{
if (UnityEngine.Random.value < Walkers[i].ChanceToChange && Walkers.Count > 1)
{
Walkers.RemoveAt(i);
break;
}
}
}
void ChanceToRedirect()
{
for (int i = 0; i < Walkers.Count; i++)
{
if (UnityEngine.Random.value < Walkers[i].ChanceToChange)
{
WalkerObject curWalker = Walkers[i];
curWalker.Direction = GetDirection();
Walkers[i] = curWalker;
}
}
}
void ChanceToCreate()
{
int updatedCount = Walkers.Count;
for (int i = 0; i < updatedCount; i++)
{
if (UnityEngine.Random.value < Walkers[i].ChanceToChange && Walkers.Count < maximumWalkers)
{
Vector2 newDirection = GetDirection();
Vector2 newPosition = Walkers[i].Position;
WalkerObject newWalker = new WalkerObject(newPosition, newDirection, 0.5f);
Walkers.Add(newWalker);
}
}
}
void UpdatePosition()
{
for (int i = 0; i < Walkers.Count; i++)
{
WalkerObject foundWalker = Walkers[i];
foundWalker.Position += foundWalker.Direction;
foundWalker.Position.x = Mathf.Clamp(foundWalker.Position.x, 2, GridHanler.GetLength(0) - 10);
foundWalker.Position.y = Mathf.Clamp(foundWalker.Position.y, 5, GridHanler.GetLength(0) - 7);
Walkers[i] = foundWalker;
}
}
IEnumerator CreateWalls()
{
for (int x = 0; x < GridHanler.GetLength(0) - 1; x++)
{
for (int y = 0; y < GridHanler.GetLength(1) - 1; y++)
{
if (GridHanler[x, y] == Grid.Empty)
{
if (time < 10.5)
{
tilemap2.SetTile(new Vector3Int(x, y, 0), water);
GridHanler[x, y] = Grid.Water;
time += 0.0025f;
}
else
{
Instantiate(player, center, Quaternion.identity);
time = 0;
done = true;
yield break;
}
}
}
}
}
private void spawn_enemys()
{
if (done)
{
for (int x = 0; x < GridHanler.GetLength(0) - 1; x++)
{
for (int y = 0; y < GridHanler.GetLength(1) - 1; y++)
{
if (GridHanler[x, y] == Grid.Floor)
{
//starting here idk what to do
}
}
}
}
}
}
````public class WalkerObject
{
public Vector2 Position;
public Vector2 Direction;
public float ChanceToChange;
public WalkerObject(Vector2 pos, Vector2 dir, float chanceToChange)
{
Position = pos;
Direction = dir;
ChanceToChange = chanceToChange;
}
}
`