MIF_E31221357/Assets/Scripts/ItemQuest.cs

32 lines
1.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class ItemQuest : MonoBehaviour
{
public GameObject itemSpawn;
public void SpawnItems(List<GameObject> validNodeObjects, int jumlahItem, GameObject startNode)
{
if (validNodeObjects == null || validNodeObjects.Count == 0) return;
List<GameObject> spawnCandidates = new List<GameObject>(validNodeObjects);
spawnCandidates.RemoveAll(n => Vector3.Distance(n.transform.position, startNode.transform.position) < 0.01f);
GameObject lastNode = validNodeObjects[validNodeObjects.Count - 1];
spawnCandidates.Remove(lastNode);
for (int i = 0; i < jumlahItem && spawnCandidates.Count > 0; i++)
{
int index = Random.Range(0, spawnCandidates.Count);
GameObject chosenNode = spawnCandidates[index];
GameObject item = Instantiate(itemSpawn, chosenNode.transform);
item.transform.localPosition = Vector3.zero;
spawnCandidates.RemoveAt(index);
}
}
}