refactor: decouple UI and Score managers logic

Single Responsibility Principle?
This commit is contained in:
cuqmbr 2022-07-22 13:50:49 +03:00
parent bfc0b89703
commit 9fba37c78a
16 changed files with 7027 additions and 354 deletions

View File

@ -1,72 +0,0 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1102 &-2884916739564254968
AnimatorState:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Blinking
m_Speed: 0.5
m_CycleOffset: 0
m_Transitions: []
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
m_WriteDefaultValues: 1
m_Mirror: 0
m_SpeedParameterActive: 0
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: 9f7236835fcd2ced9b7dffbfb717e85a, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:
--- !u!91 &9100000
AnimatorController:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Main Menu Tooltip Text
serializedVersion: 5
m_AnimatorParameters: []
m_AnimatorLayers:
- serializedVersion: 5
m_Name: New Layer
m_StateMachine: {fileID: 3611635739952748551}
m_Mask: {fileID: 0}
m_Motions: []
m_Behaviours: []
m_BlendingMode: 0
m_SyncedLayerIndex: -1
m_DefaultWeight: 0
m_IKPass: 0
m_SyncedLayerAffectsTiming: 0
m_Controller: {fileID: 9100000}
--- !u!1107 &3611635739952748551
AnimatorStateMachine:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: New Layer
m_ChildStates:
- serializedVersion: 1
m_State: {fileID: -2884916739564254968}
m_Position: {x: 300, y: 100, z: 0}
m_ChildStateMachines: []
m_AnyStateTransitions: []
m_EntryTransitions: []
m_StateMachineTransitions: {}
m_StateMachineBehaviours: []
m_AnyStatePosition: {x: 50, y: 20, z: 0}
m_EntryPosition: {x: 50, y: 120, z: 0}
m_ExitPosition: {x: 800, y: 120, z: 0}
m_ParentStateMachinePosition: {x: 800, y: 20, z: 0}
m_DefaultState: {fileID: -2884916739564254968}

View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: 702524d517a0b63fab2f760eec1d3e9e
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 9100000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -10,7 +10,8 @@ AnimatorState:
m_Name: FadeIn
m_Speed: -2
m_CycleOffset: 0
m_Transitions: []
m_Transitions:
- {fileID: -4889874045983499319}
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
@ -44,6 +45,9 @@ AnimatorStateMachine:
- serializedVersion: 1
m_State: {fileID: -5849089519810212424}
m_Position: {x: 300, y: 100, z: 0}
- serializedVersion: 1
m_State: {fileID: 6851006133677717036}
m_Position: {x: 600, y: -100, z: 0}
m_ChildStateMachines: []
m_AnyStateTransitions: []
m_EntryTransitions: []
@ -80,6 +84,28 @@ AnimatorState:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:
--- !u!1101 &-4889874045983499319
AnimatorStateTransition:
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name:
m_Conditions: []
m_DstStateMachine: {fileID: 0}
m_DstState: {fileID: 6851006133677717036}
m_Solo: 0
m_Mute: 0
m_IsExit: 0
serializedVersion: 3
m_TransitionDuration: 0
m_TransitionOffset: 0
m_ExitTime: 0
m_HasExitTime: 1
m_HasFixedDuration: 1
m_InterruptionSource: 0
m_OrderedInterruption: 1
m_CanTransitionToSelf: 1
--- !u!91 &9100000
AnimatorController:
m_ObjectHideFlags: 0
@ -128,3 +154,29 @@ AnimatorState:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:
--- !u!1102 &6851006133677717036
AnimatorState:
serializedVersion: 6
m_ObjectHideFlags: 1
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: MainMenuTooltipBlinking
m_Speed: 1
m_CycleOffset: 0
m_Transitions: []
m_StateMachineBehaviours: []
m_Position: {x: 50, y: 50, z: 0}
m_IKOnFeet: 0
m_WriteDefaultValues: 1
m_Mirror: 0
m_SpeedParameterActive: 0
m_MirrorParameterActive: 0
m_CycleOffsetParameterActive: 0
m_TimeParameterActive: 0
m_Motion: {fileID: 7400000, guid: 9aa1240c7f6743ab6afe1e0cc3cc0189, type: 2}
m_Tag:
m_SpeedParameter:
m_MirrorParameter:
m_CycleOffsetParameter:
m_TimeParameter:

View File

@ -6,7 +6,7 @@ AnimationClip:
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_Name: Text Blinking
m_Name: MainMenuTooltipBlinking
serializedVersion: 6
m_Legacy: 0
m_Compressed: 0
@ -51,9 +51,47 @@ AnimationClip:
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_fontColor.a
path:
path: Tooltip Text (TMP)
classID: 114
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
- curve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 136
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_fontColor.a
path: Title Text (TMP)
classID: 114
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
- curve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: Infinity
outSlope: Infinity
tangentMode: 103
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_IsActive
path: Start Button
classID: 1
script: {fileID: 0}
m_PPtrCurves: []
m_SampleRate: 60
m_WrapMode: 0
@ -63,7 +101,21 @@ AnimationClip:
m_ClipBindingConstant:
genericBindings:
- serializedVersion: 2
path: 0
path: 3595111152
attribute: 4185109675
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
typeID: 114
customType: 0
isPPtrCurve: 0
- serializedVersion: 2
path: 4118037921
attribute: 2086281974
script: {fileID: 0}
typeID: 1
customType: 0
isPPtrCurve: 0
- serializedVersion: 2
path: 1725491303
attribute: 4185109675
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
typeID: 114
@ -125,9 +177,47 @@ AnimationClip:
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_fontColor.a
path:
path: Tooltip Text (TMP)
classID: 114
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
- curve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: 0
outSlope: 0
tangentMode: 136
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_fontColor.a
path: Title Text (TMP)
classID: 114
script: {fileID: 11500000, guid: f4688fdb7df04437aeb418b961361dc5, type: 3}
- curve:
serializedVersion: 2
m_Curve:
- serializedVersion: 3
time: 0
value: 1
inSlope: Infinity
outSlope: Infinity
tangentMode: 103
weightedMode: 0
inWeight: 0
outWeight: 0
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 4
attribute: m_IsActive
path: Start Button
classID: 1
script: {fileID: 0}
m_EulerEditorCurves: []
m_HasGenericRootTransform: 0
m_HasMotionFloatCurves: 0

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 9f7236835fcd2ced9b7dffbfb717e85a
guid: 9aa1240c7f6743ab6afe1e0cc3cc0189
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 7400000

File diff suppressed because it is too large Load Diff

BIN
Assets/Sprites/Progress Bar Fill Fade.png (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -0,0 +1,120 @@
fileFormatVersion: 2
guid: df60252f31fd49ad3ad6c61d2186138f
TextureImporter:
internalIDToNameTable: []
externalObjects: {}
serializedVersion: 11
mipmaps:
mipMapMode: 0
enableMipMap: 0
sRGBTexture: 1
linearTexture: 0
fadeOut: 0
borderMipMap: 0
mipMapsPreserveCoverage: 0
alphaTestReferenceValue: 0.5
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 0
streamingMipmaps: 0
streamingMipmapsPriority: 0
vTOnly: 0
grayScaleToAlpha: 0
generateCubemap: 6
cubemapConvolution: 0
seamlessCubemap: 0
textureFormat: 1
maxTextureSize: 2048
textureSettings:
serializedVersion: 2
filterMode: 1
aniso: 1
mipBias: 0
wrapU: 1
wrapV: 1
wrapW: 1
nPOTScale: 0
lightmap: 0
compressionQuality: 50
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spritePixelsToUnits: 100
spriteBorder: {x: 35, y: 35, z: 35, w: 35}
spriteGenerateFallbackPhysicsShape: 1
alphaUsage: 1
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 8
textureShape: 1
singleChannelComponent: 0
flipbookRows: 1
flipbookColumns: 1
maxTextureSizeSet: 0
compressionQualitySet: 0
textureFormatSet: 0
ignorePngGamma: 0
applyGammaDecoding: 0
platformSettings:
- serializedVersion: 3
buildTarget: DefaultTexturePlatform
maxTextureSize: 128
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Standalone
maxTextureSize: 128
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
- serializedVersion: 3
buildTarget: Android
maxTextureSize: 128
resizeAlgorithm: 0
textureFormat: -1
textureCompression: 1
compressionQuality: 50
crunchedCompression: 0
allowsAlphaSplitting: 0
overridden: 0
androidETC2FallbackOverride: 0
forceMaximumCompressionQuality_BC6H_BC7: 0
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
physicsShape: []
bones: []
spriteID: 5e97eb03825dee720800000000000000
internalID: 0
vertices: []
indices:
edges: []
weights: []
secondaryTextures: []
spritePackingTag:
pSDRemoveMatte: 0
pSDShowRemoveMatteOption: 0
userData:
assetBundleName:
assetBundleVariant:

BIN
Assets/Sprites/icons collection 1.png (Stored with Git LFS) Normal file

Binary file not shown.

4296
Assets/Sprites/icons collection 1.png.meta generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,10 @@
using System;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public int CurrentScore { get; protected set; }
private int _currentScore;
private int _highScore;
[SerializeField] private int _initialRewardMultiplier;
[SerializeField] private int _maxRewardMultiplier;
@ -16,14 +15,9 @@ public class ScoreManager : MonoBehaviour
private int _previousMaxExperience;
private int _currentExperience;
[Header("UI")]
[SerializeField] private TextMeshProUGUI _currentScoreText;
[SerializeField] private TextMeshProUGUI _rewardMultiplierText;
[SerializeField] private Slider _experienceSlider;
[SerializeField] private float _sliderSmoothTime;
[SerializeField] private float _sliderMaxSpeed;
private float _sliderVelocity;
[Header("Dependencies")]
[SerializeField] private UIManager _uiManager;
private void Awake()
{
PlayerEvents.OnBallTouched += AddScore;
@ -32,27 +26,35 @@ public class ScoreManager : MonoBehaviour
GameStateManager.Instance.OnGameStateChange += OnGameStateChange;
}
private void Start()
{
_highScore = SessionStore.HighScore;
}
private void Update()
{
_experienceSlider.value = Mathf.SmoothDamp(_experienceSlider.value, _currentExperience - _previousMaxExperience, ref _sliderVelocity, _sliderSmoothTime, _sliderMaxSpeed);
if (GameStateManager.Instance.CurrentGameState != GameState.Game)
{
return;
}
_uiManager.UpdateExperienceSlider(_currentExperience - _previousMaxExperience, out bool isFullExperienceSlider);
if (_currentRewardMultiplier >= _maxRewardMultiplier)
{
return;
}
if (Math.Abs(_experienceSlider.value - _experienceSlider.maxValue) < 0.1f)
if (isFullExperienceSlider)
{
LevelUp();
_experienceSlider.maxValue = _currentMaxExperience;
_experienceSlider.value = 0;
}
}
private void AddScore()
{
CurrentScore += _currentRewardMultiplier;
_currentScoreText.text = CurrentScore.ToString();
_currentScore += _currentRewardMultiplier;
_uiManager.SetScoreText(_currentScore);
}
private void AddExperience()
@ -65,7 +67,10 @@ public class ScoreManager : MonoBehaviour
_previousMaxExperience = _currentExperience;
_currentMaxExperience = (int) Math.Ceiling(_currentMaxExperience * 1.75f);
_currentRewardMultiplier *= 2;
_rewardMultiplierText.text = $"×{_currentRewardMultiplier}";
_uiManager.SetRewardMultiplierText(_currentRewardMultiplier);
_uiManager.SetExperienceSliderValue(0);
_uiManager.SetExperienceSliderMaxValue(_currentMaxExperience);
}
private void ResetExperienceAndRewardMultiplier()
@ -76,14 +81,15 @@ public class ScoreManager : MonoBehaviour
_currentMaxExperience = _initialMaxExperience;
_previousMaxExperience = 0;
_rewardMultiplierText.text = "×1";
_experienceSlider.maxValue = _currentMaxExperience;
_uiManager.SetRewardMultiplierText(1);
_uiManager.SetExperienceSliderValue(0);
_uiManager.SetExperienceSliderMaxValue(_currentMaxExperience);
}
private void ResetAllValues()
{
CurrentScore = 0;
_currentScoreText.text = "0";
_currentScore = 0;
_uiManager.SetScoreText(0);
ResetExperienceAndRewardMultiplier();
}

View File

@ -1,6 +1,8 @@
using System;
using System.Threading.Tasks;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class UIManager : MonoBehaviour
{
@ -8,6 +10,13 @@ public class UIManager : MonoBehaviour
[SerializeField] private Animator _mainMenuAnimator;
[SerializeField] private Animator _gameMenuAnimator;
[SerializeField] private Animator _gameOverMenuAnimator;
[Header("Game Menu -> Score")]
[SerializeField] private TextMeshProUGUI _currentScoreText;
[SerializeField] private TextMeshProUGUI _rewardMultiplierText;
[SerializeField] private Slider _experienceSlider;
[SerializeField] private float _sliderSmoothTime;
private float _sliderVelocity;
private void Awake()
{
@ -19,6 +28,33 @@ public class UIManager : MonoBehaviour
GameStateManager.Instance.OnGameStateChange -= OnGameStateChange;
}
public void SetScoreText(int score)
{
_currentScoreText.text = $"{score}";
}
public void SetRewardMultiplierText(int multiplier)
{
_rewardMultiplierText.text = $"×{multiplier}";
}
public void SetExperienceSliderValue(int value)
{
_experienceSlider.value = value;
}
public void SetExperienceSliderMaxValue(int value)
{
_experienceSlider.maxValue = value;
}
public void UpdateExperienceSlider(int targetValue, out bool isFull)
{
_experienceSlider.value = Mathf.SmoothDamp(_experienceSlider.value, targetValue, ref _sliderVelocity, _sliderSmoothTime);
isFull = Math.Abs(_experienceSlider.value - _experienceSlider.maxValue) < 0.1f;
}
private async void OnGameStateChange(GameState newGameState)
{
switch (newGameState)
@ -36,9 +72,21 @@ public class UIManager : MonoBehaviour
_mainMenuAnimator.CrossFade("FadeIn", 0);
break;
case GameState.PreGame:
_mainMenuAnimator.CrossFade("FadeOut", 0);
await Task.Delay((int) (Math.Abs(_mainMenuAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.length / _mainMenuAnimator.GetCurrentAnimatorStateInfo(0).speed) * 1000));
if (_gameOverMenuAnimator.gameObject.activeSelf)
{
_gameOverMenuAnimator.CrossFade("FadeOut", 0);
await Task.Delay((int) (Math.Abs(_gameOverMenuAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.length / _gameOverMenuAnimator.GetCurrentAnimatorStateInfo(0).speed) * 1000));
_gameOverMenuAnimator.gameObject.SetActive(false);
}
if (_mainMenuAnimator.gameObject.activeSelf)
{
_mainMenuAnimator.CrossFade("FadeOut", 0);
await Task.Delay(10);
await Task.Delay((int) (Math.Abs(_mainMenuAnimator.GetCurrentAnimatorClipInfo(0)[0].clip.length / _mainMenuAnimator.GetCurrentAnimatorStateInfo(0).speed) * 1000));
_mainMenuAnimator.gameObject.SetActive(false);
}
_mainMenuAnimator.gameObject.SetActive(false);
_gameOverMenuAnimator.gameObject.SetActive(false);
_gameMenuAnimator.gameObject.SetActive(true);
_gameMenuAnimator.CrossFade("FadeIn", 0);
break;

View File

@ -12,6 +12,7 @@ public class PlayerCollisions : MonoBehaviour
PlayerEvents.SendWallTouched();
break;
case "Floor":
PlayerEvents.SendDeath();
GameStateManager.Instance.ChangeState(GameState.GameOver);
break;
}

View File

@ -6,7 +6,11 @@ public class PlayerEvents
public static event Action OnWallTouched;
public static event Action OnDeath;
public static void SendBallTouched() => OnBallTouched?.Invoke();
public static void SendWallTouched() => OnWallTouched?.Invoke();
public static void SendDeath() => OnDeath?.Invoke();
}

View File

@ -1,8 +0,0 @@
[System.Serializable]
public class PlayerData
{
public string Username { get; set; }
public string Password { get; set; }
public int HighScore { get; set; }
}

View File

@ -1,11 +0,0 @@
fileFormatVersion: 2
guid: 89eb7b496b46c2935a7ffba56bc751dd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: