rabidus-test/Assets/Scripts/ScoreController.cs

87 lines
2.2 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class ScoreController : MonoBehaviour, ITextChangable
{
public event Action<float> OnScoreChange;
private float _currentScore = 0;
private UIPopupController _UIPopupController;
public UnityEvent<object> OnTextChange => _OnTextChange;
private UnityEvent<object> _OnTextChange = new UnityEvent<object>();
private BonusController _bonusController;
private void Awake()
{
_bonusController = FindObjectOfType<BonusController>();
_UIPopupController = FindObjectOfType<UIPopupController>();
}
[ContextMenu("Debug add score")]
private void DebugAddScore()
{
AddScore(100, true);
}
[ContextMenu("Debug add score in time")]
private void DebugAddScoreInTime()
{
AddScoreInTime(100, 2, false);
}
public void AddScoreInTime(float ammount, float time, bool useBonus)
{
StartCoroutine(ScoreInTime_Coroutine(ammount,time,useBonus));
}
private IEnumerator ScoreInTime_Coroutine(float ammount, float time, bool useBonus)
{
var timeOffset = time / ammount;
for (int i = 0; i < ammount; i++)
{
AddScore(1, useBonus, false);
yield return new WaitForSeconds(timeOffset);
}
}
public void AddScore(float ammount, bool useBonus = false, bool usePopup = true)
{
float resScore = ammount;
if (useBonus)
{
_bonusController.DecreaseReload();
resScore *= _bonusController.BonusValue;
}
_currentScore += resScore;
PlayerSetup.Instance.AddPlayerScore((int)resScore);
OnScoreChange?.Invoke(_currentScore);
if (usePopup)
{
_UIPopupController.SpawnPopup
(
resScore.ToString(),
UIMagnetType.Score,
() => {
_OnTextChange?.Invoke(_currentScore);
_bonusController.UseBonus();
}
);
}
else
{
_OnTextChange?.Invoke(_currentScore);
}
}
}