포탈이 글로벌 타이머 사이클에 맞춰 웨이브를 소환하도록 함
시작 코스트 4, 웨이브당 증가량 10%
This commit is contained in:
@@ -2,43 +2,120 @@ using Northbound;
|
||||
using System.Collections.Generic;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using static Northbound.ObstacleSpawner;
|
||||
using static UnityEditor.FilePathAttribute;
|
||||
|
||||
public class EnemyPortal : MonoBehaviour
|
||||
{
|
||||
[Header("Spawn Settings")]
|
||||
[Tooltip("소환할 몬스터 목록")]
|
||||
[SerializeField] private List<GameObject> Enemies = new();
|
||||
|
||||
// Start is called once before the first execution of Update after the MonoBehaviour is created
|
||||
void Start()
|
||||
[System.Serializable]
|
||||
public class MonsterEntry
|
||||
{
|
||||
GlobalTimer.Instance.OnCycleComplete += SpawnEnemy;
|
||||
public Northbound.Data.MonsterData monsterData;
|
||||
public GameObject prefab;
|
||||
}
|
||||
|
||||
private void SpawnEnemy()
|
||||
{
|
||||
foreach (GameObject obj in Enemies)
|
||||
{
|
||||
GameObject enemy = Instantiate(obj, transform);
|
||||
[Header("Spawn Settings")]
|
||||
[Tooltip("몬스터 데이터 목록 (Editor에서 자동 로드 가능)")]
|
||||
[SerializeField] private List<MonsterEntry> monsterEntries = new();
|
||||
|
||||
// Add FogOfWarVisibility component to hide enemies in unexplored areas
|
||||
if (enemy.GetComponent<FogOfWarVisibility>() == null)
|
||||
[Header("Cost Settings")]
|
||||
[Tooltip("시간당 코스트 시작 값")]
|
||||
[SerializeField] private float initialCost = 4f;
|
||||
[Tooltip("사이클마다 코스트 증가율 (%)")]
|
||||
[SerializeField] private float costIncreaseRate = 10f;
|
||||
|
||||
private float currentCost;
|
||||
|
||||
void Start()
|
||||
{
|
||||
currentCost = initialCost;
|
||||
GlobalTimer.Instance.OnCycleStart += OnCycleStart;
|
||||
}
|
||||
|
||||
private void OnCycleStart(int cycleNumber)
|
||||
{
|
||||
SpawnMonsters();
|
||||
IncreaseCost();
|
||||
}
|
||||
|
||||
private void SpawnMonsters()
|
||||
{
|
||||
float remainingCost = currentCost;
|
||||
int spawnedCount = 0;
|
||||
|
||||
while (remainingCost > 0 && monsterEntries.Count > 0)
|
||||
{
|
||||
MonsterEntry selectedEntry = SelectMonsterByWeight();
|
||||
|
||||
if (selectedEntry.monsterData.cost > remainingCost)
|
||||
{
|
||||
var visibility = enemy.AddComponent<FogOfWarVisibility>();
|
||||
visibility.showInExploredAreas = false; // Enemies hidden when not visible
|
||||
visibility.updateInterval = 0.2f;
|
||||
if (!CanSpawnAnyMonster(remainingCost))
|
||||
{
|
||||
break;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
enemy.GetComponent<NetworkObject>().Spawn();
|
||||
Debug.Log(enemy);
|
||||
SpawnEnemy(selectedEntry.prefab);
|
||||
remainingCost -= selectedEntry.monsterData.cost;
|
||||
spawnedCount++;
|
||||
}
|
||||
|
||||
if (spawnedCount > 0)
|
||||
{
|
||||
Debug.Log($"[EnemyPortal] Spawned {spawnedCount} monsters (Cost used: {currentCost - remainingCost:F2})");
|
||||
}
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
private bool CanSpawnAnyMonster(float remainingCost)
|
||||
{
|
||||
|
||||
foreach (var entry in monsterEntries)
|
||||
{
|
||||
if (entry.monsterData.cost <= remainingCost)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private MonsterEntry SelectMonsterByWeight()
|
||||
{
|
||||
float totalWeight = 0f;
|
||||
foreach (var entry in monsterEntries)
|
||||
{
|
||||
totalWeight += entry.monsterData.weight;
|
||||
}
|
||||
|
||||
float randomValue = Random.Range(0f, totalWeight);
|
||||
float cumulativeWeight = 0f;
|
||||
|
||||
foreach (var entry in monsterEntries)
|
||||
{
|
||||
cumulativeWeight += entry.monsterData.weight;
|
||||
if (randomValue <= cumulativeWeight)
|
||||
{
|
||||
return entry;
|
||||
}
|
||||
}
|
||||
|
||||
return monsterEntries[0];
|
||||
}
|
||||
|
||||
private void SpawnEnemy(GameObject prefab)
|
||||
{
|
||||
GameObject enemy = Instantiate(prefab, transform);
|
||||
|
||||
if (enemy.GetComponent<FogOfWarVisibility>() == null)
|
||||
{
|
||||
var visibility = enemy.AddComponent<FogOfWarVisibility>();
|
||||
visibility.showInExploredAreas = false;
|
||||
visibility.updateInterval = 0.2f;
|
||||
}
|
||||
|
||||
enemy.GetComponent<NetworkObject>().Spawn();
|
||||
}
|
||||
|
||||
private void IncreaseCost()
|
||||
{
|
||||
currentCost *= (1f + costIncreaseRate / 100f);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user