You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
using Ai; |
|
using DG.Tweening; |
|
using Meta; |
|
using UnityEngine; |
|
using Random = UnityEngine.Random; |
|
|
|
namespace Equip |
|
{ |
|
public class LootManager : MonoBehaviour |
|
{ |
|
[SerializeField] private GameObject _loot; |
|
[SerializeField] private float _lootChance; |
|
[SerializeField] private GameObject _coin; |
|
[SerializeField] private int _min = 2; |
|
[SerializeField] private int _max = 5; |
|
|
|
private void OnEnable() |
|
{ |
|
AiController.Instance.EnemyDiedEvent += OnEnemyDied; |
|
} |
|
|
|
private void OnDisable() |
|
{ |
|
AiController.Instance.EnemyDiedEvent -= OnEnemyDied; |
|
} |
|
|
|
private void OnEnemyDied(Vector3 position) |
|
{ |
|
DropLoot(position); |
|
} |
|
|
|
private void DropLoot(Vector3 position) |
|
{ |
|
SoundsManager.Instance.PlaySound("drop_gold"); |
|
var coinCount = Random.Range(_min, _max); |
|
for (var i = 0; i < coinCount; i++) |
|
{ |
|
var obj = Instantiate(_coin); |
|
position.y = obj.transform.position.y; |
|
obj.transform.position = position; |
|
var random = (Vector3) Random.insideUnitCircle * 2; |
|
random.z = random.y; |
|
random.y = 0; |
|
obj.transform.DOMove(position + random, 0.5f); |
|
} |
|
|
|
var randomChance = Random.value; |
|
if (randomChance < _lootChance + PlayerResources.Instance.EquipChance) |
|
{ |
|
var item = EquipSettings.UnlockRandomItem(); |
|
if (item != null) |
|
{ |
|
var obj = Instantiate(_loot); |
|
obj.transform.position = position; |
|
var random = (Vector3)Random.insideUnitCircle * 2; |
|
random.z = random.y; |
|
random.y = 0; |
|
obj.transform.DOMove(position + random, 0.5f); |
|
PlayerResources.Instance.AddItem(item); |
|
} |
|
} |
|
else |
|
return; |
|
} |
|
} |
|
}
|
|
|