rabidus-test/Assets/Scripts/ScoreController.cs

87 lines
2.2 KiB
C#
Raw Permalink Normal View History

2023-08-22 15:41:12 +03:00
using System;
2023-08-15 17:38:54 +03:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
2023-08-22 15:41:12 +03:00
using UnityEngine.Events;
2023-08-15 17:38:54 +03:00
2023-08-22 15:41:12 +03:00
public class ScoreController : MonoBehaviour, ITextChangable
2023-08-15 17:38:54 +03:00
{
2023-08-22 15:41:12 +03:00
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;
2023-10-05 17:56:59 +03:00
PlayerSetup.Instance.AddPlayerScore((int)resScore);
2023-08-22 15:41:12 +03:00
OnScoreChange?.Invoke(_currentScore);
if (usePopup)
{
_UIPopupController.SpawnPopup
(
resScore.ToString(),
UIMagnetType.Score,
() => {
_OnTextChange?.Invoke(_currentScore);
_bonusController.UseBonus();
}
);
}
else
{
_OnTextChange?.Invoke(_currentScore);
}
}
2023-08-15 17:38:54 +03:00
}