hellbound/Assets/Scripts/Game/Loot/LootSpawner.cs

100 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using game;
using UnityEngine;
public class LootSpawner : MonoBehaviour
{
[SerializeField, MetagenConfig("@loot")] private string _confPath = "@loot/poor_spawn_coins";
[SerializeField] private LootCoin.MoveTarget _coinsTarget = LootCoin.MoveTarget.COUNTER;
[SerializeField] private bool _autoCollect = true;
[SerializeField] private float _lootBuffBonus = 1;
private ConfLootSpawn _conf;
protected virtual Vector3 SpawnOffset => Vector3.zero;
private const int COINS_COUNT_IN_PART = 1;
private const float DELAY_AROUND_SPAWN = 0.07f;
protected virtual void Awake()
{
_conf = G.Instance.Configs.Get<ConfLootSpawn>(_confPath);
}
public void Init(ConfLootSpawn conf, LootCoin.MoveTarget coinsTarget)
{
_conf = conf;
_coinsTarget = coinsTarget;
_lootBuffBonus = 1f;
}
[CustomButton("DO SPAWN")]
public void Spawn()
{
StartCoroutine(SpawnCRT());
}
public void Spawn(ConfLootSpawn conf, Transform owner)
{
StartCoroutine(SpawnCRT(conf, owner));
}
public IEnumerator SpawnCRT()
{
yield return SpawnCRT(_conf, transform);
}
public void SpawnImmediate()
{
SpawnImmediate(_conf, transform);
}
private void SpawnImmediate(ConfLootSpawn conf, Transform owner)
{
if(conf.items.Count == 0)
return;
int coinsCount = Mathf.RoundToInt(conf.count * _lootBuffBonus);
for (var i = 0; i < coinsCount; i++)
{
var confItem = G.Instance.Configs.Get<ConfLootItem>(conf.items[Random.Range(0, conf.items.Count)]);
SpawnCoin(owner, confItem);
}
}
private IEnumerator SpawnCRT(ConfLootSpawn conf, Transform owner)
{
if(conf.items.Count == 0)
yield break;
int coinsCount = Mathf.RoundToInt(conf.count * _lootBuffBonus);
yield return SpawnLootRoutine(conf, owner, coinsCount);
}
private IEnumerator SpawnLootRoutine(ConfLootSpawn conf, Transform owner, int coinsCount)
{
var spawnedCoins = 0;
while(spawnedCoins < coinsCount)
{
for (var j = 0; j < COINS_COUNT_IN_PART && spawnedCoins < conf.count; j++)
{
var confItem = G.Instance.Configs.Get<ConfLootItem>(conf.items[Random.Range(0, conf.items.Count)]);
SpawnCoin(owner, confItem);
spawnedCoins++;
}
yield return new WaitForSeconds(DELAY_AROUND_SPAWN);
}
}
private void SpawnCoin(Transform owner, ConfLootItem confItem)
{
var coin = Assets.Create<LootCoin>(confItem.prefab);
coin.transform.SetParent(G.Instance.Level.transform);
coin.transform.position = owner.position + SpawnOffset;
coin.Init(_coinsTarget, SyncCoinsTextType.SESSION, true, false, autoMove: _autoCollect);
}
}