From e24371bf8b42c2ec293bc6101569284c085f5573 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Nu=C8=9Biu?= Date: Tue, 25 Jun 2019 22:04:36 +0300 Subject: [PATCH] Implmenent PlayerUpgrade singleton --- Assets/DevMocks/FlameUpgrade.prefab | 13 ++++++++ Assets/DevMocks/SpeedUpgrade.prefab | 13 ++++++++ Assets/Scripts/src/Player/PlayerController.cs | 7 +++++ Assets/Scripts/src/Player/PlayerUpgrade.cs | 31 +++++++++++++++++++ .../Scripts/src/Player/PlayerUpgrade.cs.meta | 3 ++ .../Scripts/src/Upgrade/PlayerSpeedUpgrade.cs | 4 +-- 6 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 Assets/Scripts/src/Player/PlayerUpgrade.cs create mode 100644 Assets/Scripts/src/Player/PlayerUpgrade.cs.meta diff --git a/Assets/DevMocks/FlameUpgrade.prefab b/Assets/DevMocks/FlameUpgrade.prefab index 3f00305..cebe9d9 100644 --- a/Assets/DevMocks/FlameUpgrade.prefab +++ b/Assets/DevMocks/FlameUpgrade.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 685382037628668327} - component: {fileID: 7037551310800415105} - component: {fileID: 7958292204112110464} + - component: {fileID: 5334633105760309190} m_Layer: 0 m_Name: FlameUpgrade m_TagString: Untagged @@ -106,3 +107,15 @@ BoxCollider2D: serializedVersion: 2 m_Size: {x: 1, y: 1} m_EdgeRadius: 0 +--- !u!114 &5334633105760309190 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 7385886653159515755} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 94153367f2364ab38597c8f4a6521909, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/DevMocks/SpeedUpgrade.prefab b/Assets/DevMocks/SpeedUpgrade.prefab index 5c56ba5..97f31ac 100644 --- a/Assets/DevMocks/SpeedUpgrade.prefab +++ b/Assets/DevMocks/SpeedUpgrade.prefab @@ -11,6 +11,7 @@ GameObject: - component: {fileID: 5958400204756161002} - component: {fileID: 7382262205660184478} - component: {fileID: 2747432428292150867} + - component: {fileID: 6291791717356696744} m_Layer: 0 m_Name: SpeedUpgrade m_TagString: Untagged @@ -106,3 +107,15 @@ BoxCollider2D: serializedVersion: 2 m_Size: {x: 1, y: 1} m_EdgeRadius: 0 +--- !u!114 &6291791717356696744 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 4242909486338491383} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0b8c723aa8c7476789cbee67fcadfbef, type: 3} + m_Name: + m_EditorClassIdentifier: diff --git a/Assets/Scripts/src/Player/PlayerController.cs b/Assets/Scripts/src/Player/PlayerController.cs index f34270d..07cddba 100644 --- a/Assets/Scripts/src/Player/PlayerController.cs +++ b/Assets/Scripts/src/Player/PlayerController.cs @@ -11,6 +11,7 @@ namespace src.Player private Transform _respawnPosition; private BombsSpawner _bombsSpawner; private Animator _animator; + private PlayerUpgrade _playerUpgrade; private static readonly int AnimHorizontal = Animator.StringToHash("AnimHorizontal"); private static readonly int AnimVertical = Animator.StringToHash("AnimVertical"); @@ -21,6 +22,12 @@ namespace src.Player _respawnPosition = GameObject.Find("RespawnPosition").transform; _bombsSpawner = GameObject.Find("BombSpawner").GetComponent(); _animator = GetComponentInChildren(); + _playerUpgrade = PlayerUpgrade.Instance; + + movementSpeed = _playerUpgrade.GetMovementSpeed(); + _playerUpgrade.PlayerSpeed += IncreaseSpeed; + + /* Always start at the starting point. */ Respawn(); } diff --git a/Assets/Scripts/src/Player/PlayerUpgrade.cs b/Assets/Scripts/src/Player/PlayerUpgrade.cs new file mode 100644 index 0000000..8a3d369 --- /dev/null +++ b/Assets/Scripts/src/Player/PlayerUpgrade.cs @@ -0,0 +1,31 @@ +namespace src.Player +{ + public class PlayerUpgrade + { + public delegate void IncreaseSpeedDelegate(float speed); + public static PlayerUpgrade Instance = new PlayerUpgrade(); + public event IncreaseSpeedDelegate PlayerSpeed; + public const float MaxPlayerSpeed = 8f; + private float _movementSpeed = 4f; + + private PlayerUpgrade() + { + + } + + public float GetMovementSpeed() + { + return _movementSpeed; + } + + public void IncreaseSpeed(float speed) + { + if (_movementSpeed >= MaxPlayerSpeed) + { + return; + } + _movementSpeed += speed; + PlayerSpeed.Invoke(speed); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/src/Player/PlayerUpgrade.cs.meta b/Assets/Scripts/src/Player/PlayerUpgrade.cs.meta new file mode 100644 index 0000000..6ae36f7 --- /dev/null +++ b/Assets/Scripts/src/Player/PlayerUpgrade.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f43c74f0122b4324bb89907975813ddb +timeCreated: 1561488128 \ No newline at end of file diff --git a/Assets/Scripts/src/Upgrade/PlayerSpeedUpgrade.cs b/Assets/Scripts/src/Upgrade/PlayerSpeedUpgrade.cs index 7ec3ab4..4f1f5b3 100644 --- a/Assets/Scripts/src/Upgrade/PlayerSpeedUpgrade.cs +++ b/Assets/Scripts/src/Upgrade/PlayerSpeedUpgrade.cs @@ -1,5 +1,4 @@ using src.Player; -using UnityEngine; namespace src.Upgrade { @@ -7,8 +6,7 @@ namespace src.Upgrade { public override void PerformUpgrade() { - /* TODO: Refactor to use a player manager. */ - var player = GameObject.Find("Player").GetComponent(); + var player = PlayerUpgrade.Instance; player.IncreaseSpeed(.5f); } }