rabidus-test/Assets/Scripts/ScoreController.cs

93 lines
2.4 KiB
C#
Raw 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 LeaderboardController _leaderboardController;
private float _currentScore = 0;
private bool _decreaseBonus;
private Coroutine _decreaseReloadCoroutine;
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>();
_leaderboardController = FindObjectOfType<LeaderboardController>();
}
[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.SetPlayerScore(_currentScore);
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
}