using UnityEngine; using System; using System.Collections.Generic; public class RandomSpawner : MonoBehaviour { public GameObject player, fire; public GameObject prefabBois, prefabTree; public GameObject prefabTent; public GameObject prefabMobBear, prefabMobOgre; public UnityEngine.Tilemaps.Tilemap tileMap; public UnityEngine.Tilemaps.TileBase tileSprite; private List treePositionList; void Start () { this.treePositionList = new List(); GameObject prefabTentInstance = Instantiate(prefabTent, new Vector3(UnityEngine.Random.Range(-50, 50), UnityEngine.Random.Range(-50, 50), 0), Quaternion.identity); prefabTentInstance.GetComponent().player = player; GameObject prefabMobOgreInstance = Instantiate(prefabMobOgre, new Vector3(UnityEngine.Random.Range(-500, 500), UnityEngine.Random.Range(-500, 500), 0), Quaternion.identity); prefabMobOgreInstance.GetComponent().player = player; prefabMobOgreInstance.GetComponent().fire = fire; for (int i = 0; i < UnityEngine.Random.Range(4, 10); i++) { GameObject prefabMobBearInstance = Instantiate(prefabMobBear, new Vector3(UnityEngine.Random.Range(-500, 500), UnityEngine.Random.Range(-500, 500), 0), Quaternion.identity); prefabMobBearInstance.GetComponent().player = player; prefabMobBearInstance.GetComponent().tent = prefabTentInstance; } } void Update () { float playerX = player.gameObject.transform.position.x; float playerY = player.gameObject.transform.position.y; float ratio = 1.5f; for (int xIterator = -50; xIterator < 50; xIterator++) { for (int yIterator = -50; yIterator < 50; yIterator++) { float objectX = playerX + xIterator; float objectY = playerY + yIterator; if (tileMap.GetTile(new Vector3Int((int)objectX, (int)objectY, 0)) == null) { tileMap.SetTile(new Vector3Int((int)objectX, (int)objectY, 0), tileSprite); } Vector2 treePosition = new Vector2((int)objectX, (int)objectY); if (Mathf.PerlinNoise(objectX * ratio, objectY * ratio) > 0.95 && treePositionList.FindIndex(pos => ( pos.x == treePosition.x && pos.y == treePosition.y ) ) == -1 && Math.Abs(objectX) > 4 && Math.Abs(objectY) > 4) { treePositionList.Add(treePosition); float perlinNoise = Mathf.PerlinNoise(xIterator * ratio, yIterator * ratio); float p = UnityEngine.Random.value; GameObject g = Instantiate(p > 0.75 ? prefabTree : prefabBois, new Vector3(objectX, objectY, 0), p > 0.75 ? Quaternion.identity : Quaternion.Euler(0, 0, UnityEngine.Random.Range(0, 360))); g.GetComponent().flipX = p > 0.80; if (p > 0.75) g.GetComponent().player = player.GetComponent(); } } } } }