From b573baf37dfacd8cd5882285167d48c7b425d683 Mon Sep 17 00:00:00 2001 From: Michail Date: Mon, 1 Aug 2022 19:40:06 +0300 Subject: [PATCH] No.2 SOLID GameState Fix References Solution (bad-way) - Solution 1 Bad Way - Each get, set its own findObject, GetComp instead one Field Global for the class otherwise this was throwing Null and it was half-working --- Assets/Prefabs/_managers/Level Starter.prefab | 159 +++-- Assets/Scenes/Main Menu.unity | 14 - Assets/Scripts/Abilities/NPC/BowserFire.cs | 19 +- Assets/Scripts/Abilities/Pickups/BlockCoin.cs | 5 +- .../Abilities/Pickups/CollectibleBlock.cs | 67 +- .../Pickups/PlayerPickUpAbilities.cs | 127 ++++ .../Pickups/PlayerPickUpAbilities.cs.meta | 3 + .../Scripts/Abilities/Player/MarioFireball.cs | 35 +- .../Scripts/Abilities/Player/MarioStompBox.cs | 6 +- .../Abilities/Player/PlayerAbilities.cs | 245 +++++++ .../Abilities/Player/PlayerAbilities.cs.meta | 3 + .../Scripts/Core/Managers/GameStateManager.cs | 201 +++++- Assets/Scripts/Core/Managers/LevelManager.cs | 649 +++--------------- .../Core/Managers/LevelManager.cs.meta | 18 +- .../Core/Managers/SaveGameStateOnMemory.cs | 12 +- Assets/Scripts/Core/Managers/SoundManager.cs | 89 +-- Assets/Scripts/Core/NPC/Bowser.cs | 97 ++- Assets/Scripts/Core/NPC/KoopaShell.cs | 88 +-- Assets/Scripts/Core/NPC/Piranha.cs | 51 +- Assets/Scripts/Core/Player/PlayerAnimator.cs | 14 + .../Core/Player/PlayerAnimator.cs.meta | 3 + .../Scripts/Core/Player/PlayerController.cs | 18 +- .../Core/Player/PlayerController_Misc.cs | 9 +- .../Core/Player/PlayerController_Movement.cs | 4 +- .../Scripts/Interfaces/Abilities/PickUps.meta | 8 + .../PickUps/IPlayerPickUpAbilities.cs | 17 + .../PickUps/IPlayerPickUpAbilities.cs.meta | 3 + .../Scripts/Interfaces/Abilities/Player.meta | 3 + .../Abilities/Player/IPlayerAbilities.cs | 26 + .../Abilities/Player/IPlayerAbilities.cs.meta | 3 + .../Abilities/Player/PlayerAbilitiesBase.cs | 18 + .../Player/PlayerAbilitiesBase.cs.meta | 3 + .../Core/Managers/GameStateManagerBase.cs | 70 +- .../Core/Managers/IGameStateManager.cs | 4 + .../Managers/IGameStateManagerEssentials.cs | 15 +- .../Interfaces/Core/Managers/ILevelManager.cs | 24 +- .../Interfaces/Core/Managers/ISoundManager.cs | 4 +- .../Core/Managers/ISoundManagerExtras.cs | 1 - .../Core/Managers/LevelManagerBase.cs | 50 +- .../Interfaces/Level/ILevelServices.cs | 14 + .../Interfaces/Level/ILevelServices.cs.meta | 3 + Assets/Scripts/Interfaces/Level/ILoadLevel.cs | 9 + .../Interfaces/Level/ISoundLevelHandle.cs | 11 + .../Level/ISoundLevelHandle.cs.meta | 3 + Assets/Scripts/Interfaces/UI/IHUD.cs | 21 + Assets/Scripts/Interfaces/UI/IHUD.cs.meta | 3 + Assets/Scripts/Level/BridgeAxe.cs | 6 +- Assets/Scripts/Level/Castle.cs | 9 +- Assets/Scripts/Level/Firebar.cs | 2 +- Assets/Scripts/Level/FlagPole.cs | 2 +- Assets/Scripts/Level/KillPlane.cs | 37 +- Assets/Scripts/Level/LevelHandleMusic.cs | 90 +++ Assets/Scripts/Level/LevelHandleMusic.cs.meta | 3 + Assets/Scripts/Level/LevelSceneHandle.cs | 102 +++ Assets/Scripts/Level/LevelSceneHandle.cs.meta | 3 + Assets/Scripts/Level/LevelServices.cs | 71 ++ Assets/Scripts/Level/LevelServices.cs.meta | 3 + Assets/Scripts/Level/PipeWarpDown.cs | 8 +- Assets/Scripts/Level/PipeWarpSide.cs | 85 ++- Assets/Scripts/Level/PipeWarpUp.cs | 68 +- Assets/Scripts/Level/RegularBrickBlock.cs | 14 +- Assets/Scripts/Level/SetLevelHUD.cs | 82 +++ Assets/Scripts/Level/SetLevelHUD.cs.meta | 3 + .../Scripts/UI/MainCameraFollowNoBackwards.cs | 2 +- .../UI/MainCameraFollowWithBackwards.cs | 2 +- 65 files changed, 1773 insertions(+), 1068 deletions(-) create mode 100644 Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs create mode 100644 Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs.meta create mode 100644 Assets/Scripts/Abilities/Player/PlayerAbilities.cs create mode 100644 Assets/Scripts/Abilities/Player/PlayerAbilities.cs.meta create mode 100644 Assets/Scripts/Core/Player/PlayerAnimator.cs create mode 100644 Assets/Scripts/Core/Player/PlayerAnimator.cs.meta create mode 100644 Assets/Scripts/Interfaces/Abilities/PickUps.meta create mode 100644 Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs create mode 100644 Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs.meta create mode 100644 Assets/Scripts/Interfaces/Abilities/Player.meta create mode 100644 Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs create mode 100644 Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs.meta create mode 100644 Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs create mode 100644 Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs.meta create mode 100644 Assets/Scripts/Interfaces/Level/ILevelServices.cs create mode 100644 Assets/Scripts/Interfaces/Level/ILevelServices.cs.meta create mode 100644 Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs create mode 100644 Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs.meta create mode 100644 Assets/Scripts/Interfaces/UI/IHUD.cs create mode 100644 Assets/Scripts/Interfaces/UI/IHUD.cs.meta create mode 100644 Assets/Scripts/Level/LevelHandleMusic.cs create mode 100644 Assets/Scripts/Level/LevelHandleMusic.cs.meta create mode 100644 Assets/Scripts/Level/LevelSceneHandle.cs create mode 100644 Assets/Scripts/Level/LevelSceneHandle.cs.meta create mode 100644 Assets/Scripts/Level/LevelServices.cs create mode 100644 Assets/Scripts/Level/LevelServices.cs.meta create mode 100644 Assets/Scripts/Level/SetLevelHUD.cs create mode 100644 Assets/Scripts/Level/SetLevelHUD.cs.meta diff --git a/Assets/Prefabs/_managers/Level Starter.prefab b/Assets/Prefabs/_managers/Level Starter.prefab index 910842c..bf38325 100644 --- a/Assets/Prefabs/_managers/Level Starter.prefab +++ b/Assets/Prefabs/_managers/Level Starter.prefab @@ -41,9 +41,13 @@ GameObject: m_Component: - component: {fileID: 4200383187230866} - component: {fileID: 114750208195470174} - - component: {fileID: 9213773268289219142} + - component: {fileID: 8664226275268435725} + - component: {fileID: 6261290678525385038} + - component: {fileID: 2612591450176041130} + - component: {fileID: 4672400234462731894} + - component: {fileID: 6533670772227153969} m_Layer: 0 - m_Name: Level Manager + m_Name: LevelManager m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 @@ -77,28 +81,63 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 73fb07ac7d5f744a48fe6c56ff43ae1b, type: 3} m_Name: m_EditorClassIdentifier: - hurryUp: 0 - marioSize: 0 - lives: 0 - coins: 0 - scores: 0 - timeLeft: 0 - isInvinciblePowerdown: 0 - isInvincibleStarman: 0 +--- !u!114 &8664226275268435725 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1004064948157000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a4c34187099f4992aa4100a92a21daa4, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &6261290678525385038 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1004064948157000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 6ade286070354de3b9d0016e59d3514c, type: 3} + m_Name: + m_EditorClassIdentifier: scoreText: {fileID: 114097602158662608} coinText: {fileID: 114108165712623676} timeText: {fileID: 114793167711461314} floatingTextEffect: {fileID: 1196479104050882, guid: 1ad1e9f06ef1b47b0ba83ef5484e63cf, type: 3} - coinBonus: 200 - powerupBonus: 1000 - starmanBonus: 1000 - oneupBonus: 0 - breakBlockBonus: 50 +--- !u!114 &2612591450176041130 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1004064948157000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a9f7b0b9f0004af8ac05a3f1a1615857, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!114 &4672400234462731894 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1004064948157000} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4aec0917187d4d6da4e3b666d4c9f5b0, type: 3} + m_Name: + m_EditorClassIdentifier: stompBounceVelocity: {x: 0, y: 15} - gamePaused: 0 - timerPaused: 0 - musicPaused: 0 ---- !u!114 &9213773268289219142 + isPoweringDown: 0 + isInvinciblePowerdown: 0 + isInvincibleStarman: 0 +--- !u!114 &6533670772227153969 MonoBehaviour: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -107,36 +146,9 @@ MonoBehaviour: m_GameObject: {fileID: 1004064948157000} m_Enabled: 1 m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 3f47bb3869c463547a55f8a109a25243, type: 3} + m_Script: {fileID: 11500000, guid: ec26d0a421514f7494efe4820aef7735, type: 3} m_Name: m_EditorClassIdentifier: - musicSource: {fileID: 82793086007151078} - soundSource: {fileID: 82989831986403836} - effectsSource: {fileID: 807602078252860291} - pauseSoundSource: {fileID: 82006143512131390} - levelMusic: {fileID: 8300000, guid: a396fb5db437f453d89c54963e7b9719, type: 3} - levelMusicHurry: {fileID: 8300000, guid: 980db8ff7fdfd4de9b676bef749ea0a7, type: 3} - starmanMusic: {fileID: 8300000, guid: fe80c79237cc34e99babbc6d54016d93, type: 3} - starmanMusicHurry: {fileID: 8300000, guid: f1d4bda1392ad4f6cb3bb8e494af8eb5, type: 3} - levelCompleteMusic: {fileID: 8300000, guid: 632e293310a2b4c088dd51fdeaa9d8b8, type: 3} - castleCompleteMusic: {fileID: 8300000, guid: 6dbea1eceaa2e48b6aa7c92fac4fbafe, type: 3} - oneUpSound: {fileID: 8300000, guid: cdc43de50f4574c75afcc7e74975b29a, type: 3} - bowserFallSound: {fileID: 8300000, guid: 04c05e6a635a94c57ba4f3a3f3aaa89f, type: 3} - bowserFireSound: {fileID: 8300000, guid: de02ec0c771a0476cb3609ebf94653a8, type: 3} - breakBlockSound: {fileID: 8300000, guid: c738f1c61166b40e19d4d970e7b9c9df, type: 3} - bumpSound: {fileID: 8300000, guid: 6f487163c53194893952b1f457060b86, type: 3} - coinSound: {fileID: 8300000, guid: d885ffc842af744d89f7d595ffcc176a, type: 3} - deadSound: {fileID: 8300000, guid: 90ceef20da9a84d9891da3179ede4177, type: 3} - fireballSound: {fileID: 8300000, guid: 7016dd03d29e64aa18bc6f1ae5bd8bec, type: 3} - flagpoleSound: {fileID: 8300000, guid: 45ea427b60eb04afe8cc4c33f225da7a, type: 3} - jumpSmallSound: {fileID: 8300000, guid: bc5ca5e0703c54692a186434e1149ccd, type: 3} - jumpSuperSound: {fileID: 8300000, guid: 7988561719a0c40049ae5a3c2870d1e7, type: 3} - kickSound: {fileID: 8300000, guid: 96f7a44168f114e6ba76d5c1bef36f86, type: 3} - pipePowerdownSound: {fileID: 8300000, guid: b520e6b32619c490a934225b9b162dc2, type: 3} - powerupSound: {fileID: 8300000, guid: 26301d2e86e99420599e315297a825df, type: 3} - powerupAppearSound: {fileID: 8300000, guid: 97a450c65284a4a3094ba5c2221a492b, type: 3} - stompSound: {fileID: 8300000, guid: d84415726495b4d46b5a557ed2a97cfa, type: 3} - warningSound: {fileID: 8300000, guid: c0ea6c25686884e1a9547efdebf43492, type: 3} --- !u!1 &1006136692691096 GameObject: m_ObjectHideFlags: 0 @@ -2462,6 +2474,8 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 1667618895872265512} + - component: {fileID: 1336133381744637994} + - component: {fileID: 6470472101489442007} m_Layer: 0 m_Name: SoundManager m_TagString: SoundManager @@ -2488,6 +2502,57 @@ Transform: m_Father: {fileID: 4200383187230866} m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!114 &1336133381744637994 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 350106262282576655} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 3f47bb3869c463547a55f8a109a25243, type: 3} + m_Name: + m_EditorClassIdentifier: + musicSource: {fileID: 82793086007151078} + soundSource: {fileID: 82989831986403836} + effectsSource: {fileID: 807602078252860291} + pauseSoundSource: {fileID: 82006143512131390} + levelMusic: {fileID: 8300000, guid: a396fb5db437f453d89c54963e7b9719, type: 3} + levelMusicHurry: {fileID: 8300000, guid: 980db8ff7fdfd4de9b676bef749ea0a7, type: 3} + starmanMusic: {fileID: 8300000, guid: fe80c79237cc34e99babbc6d54016d93, type: 3} + starmanMusicHurry: {fileID: 8300000, guid: f1d4bda1392ad4f6cb3bb8e494af8eb5, type: 3} + levelCompleteMusic: {fileID: 8300000, guid: 632e293310a2b4c088dd51fdeaa9d8b8, type: 3} + castleCompleteMusic: {fileID: 8300000, guid: 6dbea1eceaa2e48b6aa7c92fac4fbafe, type: 3} + oneUpSound: {fileID: 8300000, guid: cdc43de50f4574c75afcc7e74975b29a, type: 3} + bowserFallSound: {fileID: 8300000, guid: 04c05e6a635a94c57ba4f3a3f3aaa89f, type: 3} + bowserFireSound: {fileID: 8300000, guid: de02ec0c771a0476cb3609ebf94653a8, type: 3} + breakBlockSound: {fileID: 8300000, guid: c738f1c61166b40e19d4d970e7b9c9df, type: 3} + bumpSound: {fileID: 8300000, guid: 6f487163c53194893952b1f457060b86, type: 3} + coinSound: {fileID: 8300000, guid: d885ffc842af744d89f7d595ffcc176a, type: 3} + deadSound: {fileID: 8300000, guid: 90ceef20da9a84d9891da3179ede4177, type: 3} + fireballSound: {fileID: 8300000, guid: 7016dd03d29e64aa18bc6f1ae5bd8bec, type: 3} + flagpoleSound: {fileID: 8300000, guid: 45ea427b60eb04afe8cc4c33f225da7a, type: 3} + jumpSmallSound: {fileID: 8300000, guid: bc5ca5e0703c54692a186434e1149ccd, type: 3} + jumpSuperSound: {fileID: 8300000, guid: 7988561719a0c40049ae5a3c2870d1e7, type: 3} + kickSound: {fileID: 8300000, guid: 96f7a44168f114e6ba76d5c1bef36f86, type: 3} + pipePowerdownSound: {fileID: 8300000, guid: b520e6b32619c490a934225b9b162dc2, type: 3} + powerupSound: {fileID: 8300000, guid: 26301d2e86e99420599e315297a825df, type: 3} + powerupAppearSound: {fileID: 8300000, guid: 97a450c65284a4a3094ba5c2221a492b, type: 3} + stompSound: {fileID: 8300000, guid: d84415726495b4d46b5a557ed2a97cfa, type: 3} + warningSound: {fileID: 8300000, guid: c0ea6c25686884e1a9547efdebf43492, type: 3} +--- !u!114 &6470472101489442007 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 350106262282576655} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: b08dd87c3e2f4e6085d0ed5d145c09cf, type: 3} + m_Name: + m_EditorClassIdentifier: --- !u!1 &2281765797751069528 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scenes/Main Menu.unity b/Assets/Scenes/Main Menu.unity index 6f24e94..668e3dc 100644 --- a/Assets/Scenes/Main Menu.unity +++ b/Assets/Scenes/Main Menu.unity @@ -1443,7 +1443,6 @@ GameObject: - component: {fileID: 693547088} - component: {fileID: 693547089} - component: {fileID: 693547090} - - component: {fileID: 693547091} m_Layer: 0 m_Name: SoundManager m_TagString: SoundManager @@ -1518,19 +1517,6 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: 16df295c1140448a9440ab3d732d3e2a, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!114 &693547091 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 693547087} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 411fe83d26391384bbc5244f7de427bc, type: 3} - m_Name: - m_EditorClassIdentifier: - soundTest: {fileID: 8300000, guid: a396fb5db437f453d89c54963e7b9719, type: 3} --- !u!1 &819667513 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/Abilities/NPC/BowserFire.cs b/Assets/Scripts/Abilities/NPC/BowserFire.cs index 523117d..2b02ab4 100644 --- a/Assets/Scripts/Abilities/NPC/BowserFire.cs +++ b/Assets/Scripts/Abilities/NPC/BowserFire.cs @@ -5,16 +5,15 @@ namespace Abilities.NPC { public class BowserFire : Enemy { - private LevelManager t_LevelManager; - private Rigidbody2D m_Rigidbody2D; + private LevelManager _levelManager; + private Rigidbody2D _rigidbody2D; private float absSpeedX = 18; public float directionX = -1; // 1 for right, -1 for left - // Use this for initialization void Start () { - t_LevelManager = FindObjectOfType (); - m_Rigidbody2D = FindObjectOfType (); + _levelManager = FindObjectOfType (); + _rigidbody2D = FindObjectOfType (); transform.localScale = new Vector3 (directionX, 1, 1); // orient sprite starmanBonus = 0; @@ -24,8 +23,8 @@ void Start () { stompBonus = 0; } - void Update() { - m_Rigidbody2D.velocity = new Vector2 (absSpeedX * directionX, m_Rigidbody2D.velocity.y); + private void Update() { + _rigidbody2D.velocity = new Vector2 (absSpeedX * directionX, _rigidbody2D.velocity.y); } public override void TouchedByStarmanMario() { @@ -43,9 +42,9 @@ public override void HitByMarioFireball() { public override void StompedByMario() { } - void OnTriggerEnter2D(Collider2D other) { - if (other.tag == "Player") { - t_LevelManager.MarioPowerDown (); + private void OnTriggerEnter2D(Collider2D other) { + if (other.CompareTag("Player")) { + _levelManager.GetPlayerAbilities.MarioPowerDown (); } } } diff --git a/Assets/Scripts/Abilities/Pickups/BlockCoin.cs b/Assets/Scripts/Abilities/Pickups/BlockCoin.cs index 85cb482..bb0dda4 100644 --- a/Assets/Scripts/Abilities/Pickups/BlockCoin.cs +++ b/Assets/Scripts/Abilities/Pickups/BlockCoin.cs @@ -1,16 +1,17 @@ using Core.Managers; +using Interfaces.Core.Managers; using UnityEngine; namespace Abilities.Pickups { public class BlockCoin : MonoBehaviour { - private LevelManager _levelManager; + private ILevelManager _levelManager; private void Start() { _levelManager = FindObjectOfType(); - _levelManager.AddCoin(transform.position + Vector3.down); + _levelManager.GetPlayerPickUpAbilities.AddCoin(transform.position + Vector3.down); } } } \ No newline at end of file diff --git a/Assets/Scripts/Abilities/Pickups/CollectibleBlock.cs b/Assets/Scripts/Abilities/Pickups/CollectibleBlock.cs index 7f10dfa..9f35b4d 100644 --- a/Assets/Scripts/Abilities/Pickups/CollectibleBlock.cs +++ b/Assets/Scripts/Abilities/Pickups/CollectibleBlock.cs @@ -10,8 +10,8 @@ namespace Abilities.Pickups { public class CollectibleBlock : MonoBehaviour { - private Animator m_Animator; - private LevelManager t_LevelManager; + private Animator _animator; + private LevelManager _levelManager; public bool isPowerupBlock; public GameObject objectToSpawn; @@ -21,52 +21,51 @@ public class CollectibleBlock : MonoBehaviour { public Vector3 spawnPositionOffset; private float WaitBetweenBounce = .25f; - private bool isActive; - private float time1, time2; + private bool _isActive; + private float _time1, _time2; public List enemiesOnTop = new List (); // Use this for initialization - void Start () { - m_Animator = GetComponent (); - t_LevelManager = FindObjectOfType (); - time1 = Time.time; - isActive = true; + private void Start () { + _animator = GetComponent (); + _levelManager = FindObjectOfType (); + _time1 = Time.time; + _isActive = true; } void OnTriggerEnter2D(Collider2D other) { - time2 = Time.time; - if (other.tag == "Player" && time2 - time1 >= WaitBetweenBounce) { - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BumpSound); + _time2 = Time.time; + if (!other.CompareTag("Player") || !(_time2 - _time1 >= WaitBetweenBounce)) return; + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BumpSound); - if (isActive) { - m_Animator.SetTrigger ("bounce"); + if (_isActive) { + _animator.SetTrigger ("bounce"); - // Hit any enemy on top - foreach (GameObject enemyObj in enemiesOnTop) { - t_LevelManager.BlockHitEnemy (enemyObj.GetComponent ()); - } + // Hit any enemy on top + foreach (GameObject enemyObj in enemiesOnTop) { + _levelManager.GetPlayerAbilities.BlockHitEnemy (enemyObj.GetComponent ()); + } - if (timesToSpawn > 0) { - if (isPowerupBlock) { // spawn mushroom or fireflower depending on Mario's size - if (t_LevelManager.marioSize == 0) { - objectToSpawn = bigMushroom; - } else { - objectToSpawn = fireFlower; - } + if (timesToSpawn > 0) { + if (isPowerupBlock) { // spawn mushroom or fireflower depending on Mario's size + if (_levelManager.GetGameStateManager.PlayerSize == 0) { + objectToSpawn = bigMushroom; + } else { + objectToSpawn = fireFlower; } - Instantiate (objectToSpawn, transform.position + spawnPositionOffset, Quaternion.identity); - timesToSpawn--; - - if (timesToSpawn == 0) { - m_Animator.SetTrigger ("deactivated"); - isActive = false; - } } - } + Instantiate (objectToSpawn, transform.position + spawnPositionOffset, Quaternion.identity); + timesToSpawn--; - time1 = Time.time; + if (timesToSpawn == 0) { + _animator.SetTrigger ("deactivated"); + _isActive = false; + } + } } + + _time1 = Time.time; } diff --git a/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs b/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs new file mode 100644 index 0000000..0d3f8ee --- /dev/null +++ b/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs @@ -0,0 +1,127 @@ +using Interfaces.Abilities.Pickups; +using Interfaces.Core.Managers; +using UnityEngine; + +namespace Abilities.Pickups +{ + public class PlayerPickUpAbilities : MonoBehaviour, IPlayerPickUpAbilities + { + private ILevelManager _levelManager; + + private void Awake() + { + _levelManager = GetComponent(); + } + + public void AddLife() + { + _levelManager.GetGameStateManager.Lives++; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.OneUpSound); + } + + public void AddLife(Vector3 spawnPos) + { + _levelManager.GetGameStateManager.Lives++; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.OneUpSound); + _levelManager.GetHUD.CreateFloatingText("1UP", spawnPos); + } + + public void AddCoin() + { + _levelManager.GetHUD.Coins++; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.CoinSound); + if (_levelManager.GetHUD.Coins == 100) { + AddLife(); + _levelManager.GetHUD.Coins = 0; + } + + _levelManager.GetHUD.SetHudCoin(); + AddScore(_levelManager.GetGameStateManager.CoinBonus); + } + + public void AddCoin(Vector3 spawnPos) + { + _levelManager.GetHUD.Coins++; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.CoinSound); + if (_levelManager.GetHUD.Coins == 100) { + AddLife(); + _levelManager.GetHUD.Coins = 0; + } + + _levelManager.GetHUD.SetHudCoin(); + AddScore(_levelManager.GetGameStateManager.CoinBonus, spawnPos); + } + + public void AddScore(int bonus) + { + _levelManager.GetHUD.Scores += bonus; + _levelManager.GetHUD.SetHudScore(); + } + + public void AddScore(int bonus, Vector3 spawnPos) + { + _levelManager.GetHUD.Scores += bonus; + _levelManager.GetHUD.SetHudScore(); + if (bonus > 0) { + _levelManager.GetHUD.CreateFloatingText(bonus.ToString(), spawnPos); + } + } + } + // public class PlayerPickUpAbilities : MonoBehaviour, IPlayerPickUpAbilities + // { + // public int coinBonus = 200; + // + // private ILevelManager _levelManager; + // + // public void AddLife(Vector3 spawnPos = default) + // { + // if (spawnPos == default) { + // _levelManager.GetGameStateManager.Lives++; + // _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.OneUpSound); + // } else { + // _levelManager.GetGameStateManager.Lives++; + // _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.OneUpSound); + // _levelManager.GetHUD.CreateFloatingText("1UP", spawnPos); + // } + // } + // + // public void AddCoin(Vector3 spawnPos = default) + // { + // if (spawnPos == default) { + // _levelManager.GetHUD.Coins++; + // _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.CoinSound); + // if (_levelManager.GetHUD.Coins == 100) { + // AddLife(); + // _levelManager.GetHUD.Coins = 0; + // } + // + // _levelManager.GetHUD.SetHudCoin(); + // AddScore(coinBonus); + // } else { + // _levelManager.GetHUD.Coins++; + // _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.CoinSound); + // if (_levelManager.GetHUD.Coins == 100) { + // AddLife(); + // _levelManager.GetHUD.Coins = 0; + // } + // + // _levelManager.GetHUD.SetHudCoin(); + // AddScore(coinBonus, spawnPos); + // } + // } + // + // public void AddScore(int? bonus = null, Vector3 spawnPos = default) + // { + // if (bonus == null) { + // _levelManager.GetHUD.Scores += bonus ?? default(int); + // _levelManager.GetHUD.SetHudScore(); + // } else { + // _levelManager.GetHUD.Scores += bonus ?? default(int); + // _levelManager.GetHUD.SetHudScore(); + // if (bonus > 0) { + // _levelManager.GetHUD.CreateFloatingText(bonus.ToString(), spawnPos); + // } + // } + // } + // } +} \ No newline at end of file diff --git a/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs.meta b/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs.meta new file mode 100644 index 0000000..726992a --- /dev/null +++ b/Assets/Scripts/Abilities/Pickups/PlayerPickUpAbilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a9f7b0b9f0004af8ac05a3f1a1615857 +timeCreated: 1659179156 \ No newline at end of file diff --git a/Assets/Scripts/Abilities/Player/MarioFireball.cs b/Assets/Scripts/Abilities/Player/MarioFireball.cs index 35f6f1e..1514586 100644 --- a/Assets/Scripts/Abilities/Player/MarioFireball.cs +++ b/Assets/Scripts/Abilities/Player/MarioFireball.cs @@ -8,42 +8,39 @@ namespace Abilities.Player public class MarioFireball : MonoBehaviour { public float directionX; // > 0 for right, < 0 for left private const float ExplosionDuration = .25f; - private readonly Vector2 absVelocity = new Vector2(20, 11); + private readonly Vector2 _absVelocity = new Vector2(20, 11); - private LevelManager tLevelManager; - private Rigidbody2D mRigidbody2D; - private Animator mAnimator; + private LevelManager _levelManager; + private Rigidbody2D _rigidbody2D; + private Animator _animator; private static readonly int Exploded = Animator.StringToHash("exploded"); - // Use this for initialization private void Awake() { - // old Start() - tLevelManager = FindObjectOfType(); - mRigidbody2D = GetComponent(); - mAnimator = GetComponent(); + _levelManager = FindObjectOfType(); + _rigidbody2D = GetComponent(); + _animator = GetComponent(); } private void Start() { // initial velocity - mRigidbody2D.velocity = new Vector2(directionX * absVelocity.x, -absVelocity.y); + _rigidbody2D.velocity = new Vector2(directionX * _absVelocity.x, -_absVelocity.y); } - // Update is called once per frame private void Update() { - mRigidbody2D.velocity = new Vector2(directionX * absVelocity.x, mRigidbody2D.velocity.y); + _rigidbody2D.velocity = new Vector2(directionX * _absVelocity.x, _rigidbody2D.velocity.y); } private void Explode() { - mRigidbody2D.constraints = RigidbodyConstraints2D.FreezeAll; - mAnimator.SetTrigger(Exploded); - tLevelManager.GetSoundManager.SoundSource.PlayOneShot(tLevelManager.GetSoundManager.BumpSound); + _rigidbody2D.constraints = RigidbodyConstraints2D.FreezeAll; + _animator.SetTrigger(Exploded); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.BumpSound); Destroy(gameObject, ExplosionDuration); } private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag.Contains("Enemy")) { Enemy enemy = other.gameObject.GetComponent(); - tLevelManager.FireballTouchEnemy(enemy); + _levelManager.GetPlayerAbilities.FireballTouchEnemy(enemy); Explode(); } else { // bounce off grounds @@ -58,9 +55,9 @@ private void OnCollisionEnter2D(Collision2D other) { Explode(); } else if (normal == bottomSide) { // bounce off - mRigidbody2D.velocity = new Vector2(mRigidbody2D.velocity.x, absVelocity.y); + _rigidbody2D.velocity = new Vector2(_rigidbody2D.velocity.x, _absVelocity.y); } else { - mRigidbody2D.velocity = new Vector2(mRigidbody2D.velocity.x, -absVelocity.y); + _rigidbody2D.velocity = new Vector2(_rigidbody2D.velocity.x, -_absVelocity.y); } } } @@ -73,7 +70,7 @@ private IEnumerator WaitFireBallSpawn() { private void OnTriggerEnter2D(Collider2D other) { if (!other.tag.Contains("Enemy")) return; Enemy enemy = other.gameObject.GetComponent(); - tLevelManager.FireballTouchEnemy(enemy); + _levelManager.GetPlayerAbilities.FireballTouchEnemy(enemy); Explode(); } } diff --git a/Assets/Scripts/Abilities/Player/MarioStompBox.cs b/Assets/Scripts/Abilities/Player/MarioStompBox.cs index a6b00b9..ba91f26 100644 --- a/Assets/Scripts/Abilities/Player/MarioStompBox.cs +++ b/Assets/Scripts/Abilities/Player/MarioStompBox.cs @@ -5,11 +5,11 @@ namespace Abilities.Player { public class MarioStompBox : MonoBehaviour { - private LevelManager tLevelManager; + private LevelManager _levelManager; // Use this for initialization private void Start () { - tLevelManager = FindObjectOfType (); + _levelManager = FindObjectOfType (); } private void OnTriggerEnter2D(Collider2D other) { @@ -17,7 +17,7 @@ private void OnTriggerEnter2D(Collider2D other) { other.gameObject.CompareTag("Enemy/Bowser")) return; Debug.Log (this.name + " OnTriggerEnter2D: recognizes " + other.gameObject.name); Enemy enemy = other.gameObject.GetComponent (); - tLevelManager.MarioStompEnemy (enemy); + _levelManager.GetPlayerAbilities.MarioStompEnemy (enemy); Debug.Log (this.name + " OnTriggerEnter2D: finishes calling stomp method on " + other.gameObject.name); } } diff --git a/Assets/Scripts/Abilities/Player/PlayerAbilities.cs b/Assets/Scripts/Abilities/Player/PlayerAbilities.cs new file mode 100644 index 0000000..99a2d21 --- /dev/null +++ b/Assets/Scripts/Abilities/Player/PlayerAbilities.cs @@ -0,0 +1,245 @@ +using System.Collections; +using Abilities.Pickups; +using Core.NPC; +using Core.Player; +using Interfaces.Abilities.Player; +using Interfaces.Core.Managers; +using UnityEngine; + +namespace Abilities.Player +{ + public class PlayerAbilities : PlayerAbilitiesBase, IPlayerAbilities + { + private ILevelManager _levelManager; + + public Rigidbody2D PlayerRigidbody2D + { + get => _marioRigidbody2D; + set => _marioRigidbody2D = value; + } + + public bool IsRespawning + { + get => _isRespawning; + set => _isRespawning = value; + } + + public Vector2 StompBounceVelocity + { + get => stompBounceVelocity; + set => stompBounceVelocity = value; + } + + public bool IsPoweringDown + { + get => isPoweringDown; + set => isPoweringDown = value; + } + + public bool IsInvinciblePowerdown + { + get => isInvinciblePowerdown; + set => isInvinciblePowerdown = value; + } + + public bool IsInvincibleStarman + { + get => isInvincibleStarman; + set => isInvincibleStarman = value; + } + + private void Awake() + { + _levelManager = GetComponent(); + } + + public bool IsInvincible() + { + return IsInvinciblePowerdown || IsInvincibleStarman; + } + + public void MarioInvincibleStarman() + { + StartCoroutine(MarioInvincibleStarmanCo()); + _levelManager.GetPlayerPickUpAbilities.AddScore(_levelManager.GetGameStateManager.StarmanBonus, + _levelManager.GetPlayerController.transform.position); + } + + private IEnumerator MarioInvincibleStarmanCo() + { + IsInvincibleStarman = true; + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsInvincibleStarmanAnim, true); + _levelManager.GetPlayerController.gameObject.layer = LayerMask.NameToLayer("Mario After Starman"); + _levelManager.GetSoundManager.GetSoundLevelHandle.ChangeMusic(_levelManager.GetGameStateManager.HurryUp + ? _levelManager.GetSoundManager.StarmanMusicHurry + : _levelManager.GetSoundManager.StarmanMusic); + + yield return new WaitForSeconds(MarioInvincibleStarmanDuration); + IsInvincibleStarman = false; + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsInvincibleStarmanAnim, false); + _levelManager.GetPlayerController.gameObject.layer = LayerMask.NameToLayer("Mario"); + _levelManager.GetSoundManager.GetSoundLevelHandle.ChangeMusic(_levelManager.GetGameStateManager.HurryUp + ? _levelManager.GetSoundManager.LevelMusicHurry + : _levelManager.GetSoundManager.LevelMusic); + } + + public void MarioInvinciblePowerdown() + { + StartCoroutine(MarioInvinciblePowerdownCo()); + } + + private IEnumerator MarioInvinciblePowerdownCo() + { + IsInvinciblePowerdown = true; + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsInvinciblePowerdownAnim, true); + _levelManager.GetPlayerController.gameObject.layer = LayerMask.NameToLayer("Mario After Powerdown"); + yield return new WaitForSeconds(MarioInvinciblePowerdownDuration); + IsInvinciblePowerdown = false; + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsInvinciblePowerdownAnim, false); + _levelManager.GetPlayerController.gameObject.layer = LayerMask.NameToLayer("Mario"); + } + + public void MarioPowerUp() + { + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager + .PowerupSound); // should play sound regardless of size + if (_levelManager.GetGameStateManager.PlayerSize < 2) { + StartCoroutine(MarioPowerUpCo()); + } + + _levelManager.GetPlayerPickUpAbilities.AddScore(_levelManager.GetGameStateManager.PowerupBonus, + _levelManager.GetPlayerController.transform.position); + } + + private IEnumerator MarioPowerUpCo() + { + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsPoweringUpAnim, true); + Time.timeScale = 0f; + PlayerAnimator.PlayerAnimatorComponent.updateMode = AnimatorUpdateMode.UnscaledTime; + + yield return new WaitForSecondsRealtime(TransformDuration); + yield return new WaitWhile(() => _levelManager.GetGameStateManager.GamePaused); + + Time.timeScale = 1; + PlayerAnimator.PlayerAnimatorComponent.updateMode = AnimatorUpdateMode.Normal; + + _levelManager.GetGameStateManager.PlayerSize++; + _levelManager.GetPlayerController.UpdateSize(); + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsPoweringUpAnim, false); + } + + public void MarioPowerDown() + { + if (!IsPoweringDown) { + Debug.Log(this.name + " MarioPowerDown: called and executed"); + IsPoweringDown = true; + + if (_levelManager.GetGameStateManager.PlayerSize > 0) { + StartCoroutine(MarioPowerDownCo()); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager + .PipePowerdownSound); + } else { + MarioRespawn(); + } + + Debug.Log(this.name + " MarioPowerDown: done executing"); + } else { + Debug.Log(this.name + " MarioPowerDown: called but not executed"); + } + } + + private IEnumerator MarioPowerDownCo() + { + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsPoweringDownAnim, true); + Time.timeScale = 0f; + PlayerAnimator.PlayerAnimatorComponent.updateMode = AnimatorUpdateMode.UnscaledTime; + + yield return new WaitForSecondsRealtime(TransformDuration); + yield return new WaitWhile(() => _levelManager.GetGameStateManager.GamePaused); + + Time.timeScale = 1; + PlayerAnimator.PlayerAnimatorComponent.updateMode = AnimatorUpdateMode.Normal; + MarioInvinciblePowerdown(); + + _levelManager.GetGameStateManager.PlayerSize = 0; + _levelManager.GetPlayerController.UpdateSize(); + PlayerAnimator.PlayerAnimatorComponent.SetBool(PlayerAnimator.IsPoweringDownAnim, false); + IsPoweringDown = false; + } + + public void MarioRespawn(bool timeUp = false) + { + if (_levelManager.GetPlayerAbilities.IsRespawning) return; + _levelManager.GetPlayerAbilities.IsRespawning = true; + + _levelManager.GetGameStateManager.PlayerSize = 0; + _levelManager.GetGameStateManager.Lives--; + + _levelManager.GetSoundManager.SoundSource.Stop(); + _levelManager.GetSoundManager.MusicSource.Stop(); + _levelManager.GetGameStateManager.MusicPaused = true; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.DeadSound); + + Time.timeScale = 0f; + _levelManager.GetPlayerController.FreezeAndDie(); + + if (timeUp) { + Debug.Log(this.name + " MarioRespawn: called due to timeup"); + } + + Debug.Log(this.name + " MarioRespawn: lives left=" + _levelManager.GetGameStateManager.Lives.ToString()); + + if (_levelManager.GetGameStateManager.Lives > 0) { + _levelManager.GetLoadLevelSceneHandler.ReloadCurrentLevel( + _levelManager.GetSoundManager.DeadSound.length, timeUp); + } else { + _levelManager.GetLoadLevelSceneHandler.LoadGameOver(_levelManager.GetSoundManager.DeadSound.length, + timeUp); + Debug.Log(this.name + " MarioRespawn: all dead"); + } + } + + /****************** Kill enemy */ + public void MarioStompEnemy(Enemy enemy) + { + _marioRigidbody2D.velocity = + new Vector2(_marioRigidbody2D.velocity.x + stompBounceVelocity.x, stompBounceVelocity.y); + enemy.StompedByMario(); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.StompSound); + _levelManager.GetPlayerPickUpAbilities.AddScore(enemy.stompBonus, enemy.gameObject.transform.position); + Debug.Log(this.name + " MarioStompEnemy called on " + enemy.gameObject.name); + } + + public void MarioStarmanTouchEnemy(Enemy enemy) + { + enemy.TouchedByStarmanMario(); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.KickSound); + _levelManager.GetPlayerPickUpAbilities.AddScore(enemy.starmanBonus, enemy.gameObject.transform.position); + Debug.Log(this.name + " MarioStarmanTouchEnemy called on " + enemy.gameObject.name); + } + + public void RollingShellTouchEnemy(Enemy enemy) + { + enemy.TouchedByRollingShell(); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.KickSound); + _levelManager.GetPlayerPickUpAbilities.AddScore(enemy.rollingShellBonus, + enemy.gameObject.transform.position); + Debug.Log(this.name + " RollingShellTouchEnemy called on " + enemy.gameObject.name); + } + + public void BlockHitEnemy(Enemy enemy) + { + enemy.HitBelowByBlock(); + _levelManager.GetPlayerPickUpAbilities.AddScore(enemy.hitByBlockBonus, enemy.gameObject.transform.position); + Debug.Log(this.name + " BlockHitEnemy called on " + enemy.gameObject.name); + } + + public void FireballTouchEnemy(Enemy enemy) + { + enemy.HitByMarioFireball(); + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.KickSound); + _levelManager.GetPlayerPickUpAbilities.AddScore(enemy.fireballBonus, enemy.gameObject.transform.position); + Debug.Log(this.name + " FireballTouchEnemy called on " + enemy.gameObject.name); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Abilities/Player/PlayerAbilities.cs.meta b/Assets/Scripts/Abilities/Player/PlayerAbilities.cs.meta new file mode 100644 index 0000000..785afa7 --- /dev/null +++ b/Assets/Scripts/Abilities/Player/PlayerAbilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4aec0917187d4d6da4e3b666d4c9f5b0 +timeCreated: 1659206392 \ No newline at end of file diff --git a/Assets/Scripts/Core/Managers/GameStateManager.cs b/Assets/Scripts/Core/Managers/GameStateManager.cs index 090a01f..5cb6681 100644 --- a/Assets/Scripts/Core/Managers/GameStateManager.cs +++ b/Assets/Scripts/Core/Managers/GameStateManager.cs @@ -1,4 +1,6 @@ -using Interfaces.Core.Managers; +using System.Collections; +using Interfaces.Core.Managers; +using JetBrains.Annotations; using UnityEngine; namespace Core.Managers @@ -44,6 +46,8 @@ public int Coins set => coins = value; } + public int BreakBlockBonus { get; set; } + public int Scores { get => scores; @@ -56,6 +60,12 @@ public float TimeLeft set => timeLeft = value; } + public int TimeLeftInt + { + get => timeLeftInt; + set => timeLeftInt = value; + } + public bool HurryUp { get => hurryUp; @@ -74,10 +84,112 @@ public bool TimeUp set => timeUp = value; } + // public bool GamePaused + // { + // get => gamePaused; + // set => gamePaused = value; + // } + + public bool GamePaused + { + get => _levelManager is { GamePaused: true }; + // get + // { + // _levelManager ??= FindObjectOfType(); + // if (_levelManager != null) { + // return _levelManager.GamePaused; + // } + // return false; + // } + set + { + LevelManager _levelManager = FindObjectOfType(); + //_levelManager ??= FindObjectOfType(); + if (_levelManager == null) return; + _levelManager.GamePaused = value; + } + } + + public bool MusicPaused + { + get => _levelManager is { MusicPaused: false }; + // get + // { + // _levelManager ??= FindObjectOfType(); + // if (_levelManager != null) { + // return _levelManager.MusicPaused; + // } + // return false; + // } + set + { + LevelManager _levelManager = FindObjectOfType(); + //_levelManager ??= FindObjectOfType(); + if (_levelManager==null) return; + _levelManager.MusicPaused = value; + } + } + + public bool TimerPaused + { + //get => _levelManager is { TimerPaused: false }; + get + { + LevelManager _levelManager = FindObjectOfType(); + //_levelManager ??= FindObjectOfType(); + return _levelManager != null && _levelManager.TimerPaused; + } + set + { + LevelManager _levelManager = FindObjectOfType(); + //_levelManager ??= FindObjectOfType(); + if (_levelManager==null) return; + _levelManager.TimerPaused = value; + } + } + // public bool TimerPaused + // { + // get => timerPaused; + // set => timerPaused = value; + // } + + // public bool MusicPaused + // { + // get => _levelManager.MusicPaused; + // set => musicPaused = value; + // } + + public int CoinBonus + { + get => coinBonus; + set => coinBonus = value; + } + + public int PowerupBonus + { + get => powerupBonus; + set => powerupBonus = value; + } + + public int StarmanBonus + { + get => starmanBonus; + set => starmanBonus = value; + } + + public int OneupBonus + { + get => oneupBonus; + set => oneupBonus = value; + } + #endregion private ISaveGameState _saveGameStateOnMemory; + [CanBeNull] private ILevelManager _levelManager; + //[CanBeNull] private IPlayerPickUpAbilities _playerPickUpAbilities; + private void Awake() { RetainGameStateManagerPerLoad(); @@ -128,6 +240,7 @@ public void ConfigNewLevel() TimeLeft = 400.5f; HurryUp = false; ResetSpawnPosition(); + //TimerPaused = false; } public void ConfigReplayedLevel() @@ -141,5 +254,91 @@ public void GetSaveGameState() { _saveGameStateOnMemory.SaveGameState(this); } + + public void PauseUnPauseState() + { + _levelManager = FindObjectOfType(); + StartCoroutine(!GamePaused ? PauseGameCo() : UnpauseGameCo()); + } + + private IEnumerator PauseGameCo() + { + if (_levelManager == null) { + yield break; + } + + Debug.Log("PauseGameCo!!!!!!!!!!"); + GamePaused = true; + PauseGamePrevTimeScale = Time.timeScale; + + Time.timeScale = 0; + PausePrevMusicPaused = MusicPaused; + _levelManager.GetSoundManager.MusicSource.Pause(); + MusicPaused = true; + _levelManager.GetSoundManager.SoundSource.Pause(); + + // Set any active animators that use unscaled time mode to normal + UnScaledAnimators.Clear(); + foreach (Animator animator in FindObjectsOfType()) { + if (animator.updateMode != AnimatorUpdateMode.UnscaledTime) continue; + UnScaledAnimators.Add(animator); + animator.updateMode = AnimatorUpdateMode.Normal; + } + + _levelManager.GetSoundManager.PauseSoundSource.Play(); + yield return new WaitForSecondsRealtime(_levelManager.GetSoundManager.PauseSoundSource.clip.length); + Debug.Log(this.name + " PauseGameCo stops: records prevTimeScale=" + PauseGamePrevTimeScale.ToString()); + } + + private IEnumerator UnpauseGameCo() + { + if (_levelManager == null) { + yield break; + } + + _levelManager.GetSoundManager.PauseSoundSource.Play(); + yield return new WaitForSecondsRealtime(_levelManager.GetSoundManager.PauseSoundSource.clip.length); + + MusicPaused = PausePrevMusicPaused; + if (!MusicPaused) { + _levelManager.GetSoundManager.MusicSource.UnPause(); + } + + _levelManager.GetSoundManager.SoundSource.UnPause(); + + // Reset animators + foreach (Animator animator in UnScaledAnimators) { + animator.updateMode = AnimatorUpdateMode.UnscaledTime; + } + + UnScaledAnimators.Clear(); + + Time.timeScale = PauseGamePrevTimeScale; + GamePaused = false; + Debug.Log(this.name + " UnpauseGameCo stops: resume prevTimeScale=" + PauseGamePrevTimeScale.ToString()); + } + + public void TimerHUD() + { + // _levelManager = FindObjectOfType(); + // if (_levelManager.GetGameStateManager.TimerPaused) return; + // _levelManager.GetHUD.TimeLeft -= Time.deltaTime; // / .4f; // 1 game sec ~ 0.4 real time sec + // _levelManager.GetHUD.SetHudTime(); + } + + public void GamePauseCheck() + { + // _levelManager = FindObjectOfType(); + // if (!Input.GetButtonDown("Pause")) return; + // _levelManager.GetGameStateManager.PauseUnPauseState(); + } + + public void TimeUpCounter() + { + // _levelManager = FindObjectOfType(); + // if (_levelManager.GetHUD.TimeLeftInt <= 0) { + // _levelManager.GetPlayerAbilities.MarioRespawn(true); + // } + } } } \ No newline at end of file diff --git a/Assets/Scripts/Core/Managers/LevelManager.cs b/Assets/Scripts/Core/Managers/LevelManager.cs index 825f8b0..4a629d6 100644 --- a/Assets/Scripts/Core/Managers/LevelManager.cs +++ b/Assets/Scripts/Core/Managers/LevelManager.cs @@ -1,596 +1,175 @@ -using System.Collections; -using System.Text.RegularExpressions; -using Abilities.Pickups; -using Core.NPC; +using Abilities.Pickups; using Core.Player; +using Interfaces.Abilities.Pickups; +using Interfaces.Abilities.Player; using Interfaces.Core.Managers; +using Interfaces.Level; using UnityEngine; using UnityEngine.SceneManagement; +using Interfaces.UI; namespace Core.Managers { - [RequireComponent(typeof(ISoundManagerExtras))] + //[RequireComponent(typeof(ISoundManagerExtras))] + //[RequireComponent(typeof(ISoundLevelHandle))] + [RequireComponent(typeof(IGameStateManager))] + [RequireComponent(typeof(ILoadLevelSceneHandle))] + [RequireComponent(typeof(IHUD))] + [RequireComponent(typeof(IPlayerPickUpAbilities))] + [RequireComponent(typeof(IPlayerAbilities))] public class LevelManager : LevelManagerBase, ILevelManager { - private IGameStateManager _gameStateManager; - private ISoundManagerExtras _soundManager; - private PlayerController _playerController; //TODO IPlayerController - public ISoundManagerExtras GetSoundManager => _soundManager; - - private void Awake() - { - _soundManager = GetComponent(); - Time.timeScale = 1; - } - - private void Start() - { - _gameStateManager = FindObjectOfType(); - RetrieveGameState(); - - _playerController = FindObjectOfType(); - MarioAnimator = _playerController.gameObject.GetComponent(); - MarioRigidbody2D = _playerController.gameObject.GetComponent(); - _playerController.UpdateSize(); - - GetSoundManager.GetSoundVolume(); - - // HUD - SetHudCoin(); - SetHudScore(); - SetHudTime(); - ChangeMusic(hurryUp ? GetSoundManager.LevelMusicHurry : GetSoundManager.LevelMusic); - - Debug.Log(this.name + " Start: current scene is " + SceneManager.GetActiveScene().name); - } - - private void OnEnable() - { - Coin.OnCoinCollected += AddCoin; - Starman.OnStarmanCollected += MarioInvincibleStarman; - OneUpMushroom.OnOneUpCollected += AddLife; - PowerupObject.OnPowerUpCollected += MarioPowerUp; - } - - private void OnDisable() - { - Coin.OnCoinCollected -= AddCoin; - Starman.OnStarmanCollected -= MarioInvincibleStarman; - OneUpMushroom.OnOneUpCollected -= AddLife; - PowerupObject.OnPowerUpCollected -= MarioPowerUp; - } - - public void RetrieveGameState() - { - marioSize = _gameStateManager.PlayerSize; - lives = _gameStateManager.Lives; - coins = _gameStateManager.Coins; - scores = _gameStateManager.Scores; - timeLeft = _gameStateManager.TimeLeft; - hurryUp = _gameStateManager.HurryUp; - } - - private void Update() - { - if (!timerPaused) { - timeLeft -= Time.deltaTime; // / .4f; // 1 game sec ~ 0.4 real time sec - SetHudTime(); - } - - if (TimeLeftInt < 100 && !hurryUp) { - hurryUp = true; - PauseMusicPlaySound(GetSoundManager.WarningSound, true); - ChangeMusic(isInvincibleStarman ? GetSoundManager.StarmanMusicHurry : GetSoundManager.LevelMusicHurry, - GetSoundManager.WarningSound.length); - } - - if (TimeLeftInt <= 0) { - MarioRespawn(true); - } - - if (!Input.GetButtonDown("Pause")) return; - StartCoroutine(!gamePaused ? PauseGameCo() : UnpauseGameCo()); - } - - - /****************** Game pause */ - - private IEnumerator PauseGameCo() - { - gamePaused = true; - PauseGamePrevTimeScale = Time.timeScale; - - Time.timeScale = 0; - PausePrevMusicPaused = musicPaused; - GetSoundManager.MusicSource.Pause(); - musicPaused = true; - GetSoundManager.SoundSource.Pause(); - - // Set any active animators that use unscaled time mode to normal - UnScaledAnimators.Clear(); - foreach (Animator animator in FindObjectsOfType()) { - if (animator.updateMode != AnimatorUpdateMode.UnscaledTime) continue; - UnScaledAnimators.Add(animator); - animator.updateMode = AnimatorUpdateMode.Normal; - } + #region GettersAndSetters - GetSoundManager.PauseSoundSource.Play(); - yield return new WaitForSecondsRealtime(GetSoundManager.PauseSoundSource.clip.length); - Debug.Log(this.name + " PauseGameCo stops: records prevTimeScale=" + PauseGamePrevTimeScale.ToString()); - } - - private IEnumerator UnpauseGameCo() - { - GetSoundManager.PauseSoundSource.Play(); - yield return new WaitForSecondsRealtime(GetSoundManager.PauseSoundSource.clip.length); - - musicPaused = PausePrevMusicPaused; - if (!musicPaused) { - GetSoundManager.MusicSource.UnPause(); - } - - GetSoundManager.SoundSource.UnPause(); - - // Reset animators - foreach (Animator animator in UnScaledAnimators) { - animator.updateMode = AnimatorUpdateMode.UnscaledTime; - } - - UnScaledAnimators.Clear(); - - Time.timeScale = PauseGamePrevTimeScale; - gamePaused = false; - Debug.Log(this.name + " UnpauseGameCo stops: resume prevTimeScale=" + PauseGamePrevTimeScale.ToString()); - } - - - /****************** Invincibility */ - public bool IsInvincible() - { - return isInvinciblePowerdown || isInvincibleStarman; - } - - private void MarioInvincibleStarman() - { - StartCoroutine(MarioInvincibleStarmanCo()); - AddScore(starmanBonus, _playerController.transform.position); - } + // Exposed API's Calls External + public ISoundManagerExtras GetSoundManager => _soundManager; + public ILoadLevelSceneHandle GetLoadLevelSceneHandler => _loadLevelSceneHandler; + public IGameStateManager GetGameStateManager => _gameStateManager; + public IHUD GetHUD => _hud; + public IPlayerPickUpAbilities GetPlayerPickUpAbilities => _playerPickUpAbilities; + public IPlayerAbilities GetPlayerAbilities => _playerAbilities; - IEnumerator MarioInvincibleStarmanCo() - { - isInvincibleStarman = true; - MarioAnimator.SetBool(IsInvincibleStarmanAnim, true); - _playerController.gameObject.layer = LayerMask.NameToLayer("Mario After Starman"); - ChangeMusic(hurryUp ? GetSoundManager.StarmanMusicHurry : GetSoundManager.StarmanMusic); + public PlayerController GetPlayerController => _playerController; - yield return new WaitForSeconds(MarioInvincibleStarmanDuration); - isInvincibleStarman = false; - MarioAnimator.SetBool(IsInvincibleStarmanAnim, false); - _playerController.gameObject.layer = LayerMask.NameToLayer("Mario"); - ChangeMusic(hurryUp ? GetSoundManager.LevelMusicHurry : GetSoundManager.LevelMusic); - } + //public ISoundLevelHandle GetSoundLevelHandle => _soundLevelHandler; + public ILevelServices GetLevelServices => _levelServices; - void MarioInvinciblePowerdown() + public bool TimerPaused { - StartCoroutine(MarioInvinciblePowerdownCo()); + get => timerPaused; + set => timerPaused = value; } - - IEnumerator MarioInvinciblePowerdownCo() + + public bool GamePaused { - isInvinciblePowerdown = true; - MarioAnimator.SetBool(IsInvinciblePowerdownAnim, true); - _playerController.gameObject.layer = LayerMask.NameToLayer("Mario After Powerdown"); - yield return new WaitForSeconds(MarioInvinciblePowerdownDuration); - isInvinciblePowerdown = false; - MarioAnimator.SetBool(IsInvinciblePowerdownAnim, false); - _playerController.gameObject.layer = LayerMask.NameToLayer("Mario"); + get => gamePaused; + set => gamePaused = value; } - - - /****************** Powerup / Powerdown / Die */ - private void MarioPowerUp() + + public bool MusicPaused { - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager - .PowerupSound); // should play sound regardless of size - if (marioSize < 2) { - StartCoroutine(MarioPowerUpCo()); - } - - AddScore(powerupBonus, _playerController.transform.position); + get => musicPaused; + set => musicPaused = value; } - private IEnumerator MarioPowerUpCo() - { - MarioAnimator.SetBool(IsPoweringUpAnim, true); - Time.timeScale = 0f; - MarioAnimator.updateMode = AnimatorUpdateMode.UnscaledTime; + #endregion - yield return new WaitForSecondsRealtime(TransformDuration); - yield return new WaitWhile(() => gamePaused); - - Time.timeScale = 1; - MarioAnimator.updateMode = AnimatorUpdateMode.Normal; - - marioSize++; - _playerController.UpdateSize(); - MarioAnimator.SetBool(IsPoweringUpAnim, false); - } - - public void MarioPowerDown() - { - if (!IsPoweringDown) { - Debug.Log(this.name + " MarioPowerDown: called and executed"); - IsPoweringDown = true; + private IGameStateManager _gameStateManager; - if (marioSize > 0) { - StartCoroutine(MarioPowerDownCo()); - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.PipePowerdownSound); - } else { - MarioRespawn(); - } + private ISoundManagerExtras _soundManager; - Debug.Log(this.name + " MarioPowerDown: done executing"); - } else { - Debug.Log(this.name + " MarioPowerDown: called but not executed"); - } - } + //private ISoundLevelHandle _soundLevelHandler; + private PlayerController _playerController; //TODO IPlayerController + private ILoadLevelSceneHandle _loadLevelSceneHandler; + private IHUD _hud; + private IPlayerPickUpAbilities _playerPickUpAbilities; + private IPlayerAbilities _playerAbilities; + private ILevelServices _levelServices; - private IEnumerator MarioPowerDownCo() + private void Awake() { - MarioAnimator.SetBool(IsPoweringDownAnim, true); - Time.timeScale = 0f; - AnimatorUpdateMode updateMode = MarioAnimator.updateMode; - updateMode = AnimatorUpdateMode.UnscaledTime; - - yield return new WaitForSecondsRealtime(TransformDuration); - yield return new WaitWhile(() => gamePaused); + _soundManager = FindObjectOfType(); + //_soundManager = GetComponent(); + //_soundLevelHandler = GetComponent(); + _loadLevelSceneHandler = GetComponent(); + _gameStateManager = FindObjectOfType(); + _hud = GetComponent(); + _playerPickUpAbilities = GetComponent(); + _playerAbilities = GetComponent(); + _levelServices = GetComponent(); Time.timeScale = 1; - updateMode = AnimatorUpdateMode.Normal; - MarioAnimator.updateMode = updateMode; - MarioInvinciblePowerdown(); - - marioSize = 0; - _playerController.UpdateSize(); - MarioAnimator.SetBool(IsPoweringDownAnim, false); - IsPoweringDown = false; - } - - public void MarioRespawn(bool timeUp = false) - { //TODO make every one class - if (IsRespawning) return; - IsRespawning = true; - - marioSize = 0; - lives--; - - GetSoundManager.SoundSource.Stop(); - GetSoundManager.MusicSource.Stop(); - musicPaused = true; - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.DeadSound); - - Time.timeScale = 0f; - _playerController.FreezeAndDie(); - - if (timeUp) { - Debug.Log(this.name + " MarioRespawn: called due to timeup"); - } - - Debug.Log(this.name + " MarioRespawn: lives left=" + lives.ToString()); - - if (lives > 0) { - ReloadCurrentLevel(GetSoundManager.DeadSound.length, timeUp); - } else { - LoadGameOver(GetSoundManager.DeadSound.length, timeUp); - Debug.Log(this.name + " MarioRespawn: all dead"); - } - } - - - /****************** Kill enemy */ - public void MarioStompEnemy(Enemy enemy) - { - MarioRigidbody2D.velocity = - new Vector2(MarioRigidbody2D.velocity.x + stompBounceVelocity.x, stompBounceVelocity.y); - enemy.StompedByMario(); - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.StompSound); - AddScore(enemy.stompBonus, enemy.gameObject.transform.position); - Debug.Log(this.name + " MarioStompEnemy called on " + enemy.gameObject.name); - } - - public void MarioStarmanTouchEnemy(Enemy enemy) - { - enemy.TouchedByStarmanMario(); - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.KickSound); - AddScore(enemy.starmanBonus, enemy.gameObject.transform.position); - Debug.Log(this.name + " MarioStarmanTouchEnemy called on " + enemy.gameObject.name); - } - - public void RollingShellTouchEnemy(Enemy enemy) - { - enemy.TouchedByRollingShell(); - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.KickSound); - AddScore(enemy.rollingShellBonus, enemy.gameObject.transform.position); - Debug.Log(this.name + " RollingShellTouchEnemy called on " + enemy.gameObject.name); - } - - public void BlockHitEnemy(Enemy enemy) - { - enemy.HitBelowByBlock(); - AddScore(enemy.hitByBlockBonus, enemy.gameObject.transform.position); - Debug.Log(this.name + " BlockHitEnemy called on " + enemy.gameObject.name); - } - - public void FireballTouchEnemy(Enemy enemy) - { - enemy.HitByMarioFireball(); - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.KickSound); - AddScore(enemy.fireballBonus, enemy.gameObject.transform.position); - Debug.Log(this.name + " FireballTouchEnemy called on " + enemy.gameObject.name); - } - - /****************** Scene loading */ - private void LoadSceneDelay(string sceneName, float delay = LoadSceneDelayTime) - { - timerPaused = true; - StartCoroutine(LoadSceneDelayCo(sceneName, delay)); - } - - private IEnumerator LoadSceneDelayCo(string sceneName, float delay) - { - Debug.Log(this.name + " LoadSceneDelayCo: starts loading " + sceneName); - - float waited = 0; - while (waited < delay) { - if (!gamePaused) { - // should not count delay while game paused - waited += Time.unscaledDeltaTime; - } - - yield return null; - } - - yield return new WaitWhile(() => gamePaused); - - Debug.Log(this.name + " LoadSceneDelayCo: done loading " + sceneName); - - IsRespawning = false; - IsPoweringDown = false; - SceneManager.LoadScene(sceneName); - } - - public void LoadNewLevel(string sceneName, float delay = LoadSceneDelayTime) - { - _gameStateManager.GetSaveGameState(); - _gameStateManager.ConfigNewLevel(); - _gameStateManager.SceneToLoad = sceneName; - LoadSceneDelay("Level Start Screen", delay); - } - - public void LoadSceneCurrentLevel(string sceneName, float delay = LoadSceneDelayTime) - { - _gameStateManager.GetSaveGameState(); - _gameStateManager.ResetSpawnPosition(); // TODO - LoadSceneDelay(sceneName, delay); - } - - public void LoadSceneCurrentLevelSetSpawnPipe(string sceneName, int spawnPipeIdx, - float delay = LoadSceneDelayTime) - { - _gameStateManager.GetSaveGameState(); - _gameStateManager.SetSpawnPipe(spawnPipeIdx); - LoadSceneDelay(sceneName, delay); - Debug.Log(this.name + " LoadSceneCurrentLevelSetSpawnPipe: supposed to load " + sceneName - + ", spawnPipeIdx=" + spawnPipeIdx.ToString() + "; actual GSM spawnFromPoint=" - + _gameStateManager.SpawnFromPoint.ToString() + ", spawnPipeIdx=" - + _gameStateManager.SpawnPipeIdx.ToString()); - } - - private void ReloadCurrentLevel(float delay = LoadSceneDelayTime, bool timeUp = false) - { - _gameStateManager.GetSaveGameState(); - _gameStateManager.ConfigReplayedLevel(); - _gameStateManager.SceneToLoad = SceneManager.GetActiveScene().name; - LoadSceneDelay(timeUp ? "Time Up Screen" : "Level Start Screen", delay); - } - - private void LoadGameOver(float delay = LoadSceneDelayTime, bool timeup = false) - { - int currentHighScore = PlayerPrefs.GetInt("highScore", 0); - if (scores > currentHighScore) { - PlayerPrefs.SetInt("highScore", scores); - } - - _gameStateManager.TimeUp = timeup; - LoadSceneDelay("Game Over Screen", delay); - } - - - /****************** HUD and sound effects */ - private void SetHudCoin() - { - coinText.text = "x" + coins.ToString("D2"); - } - - private void SetHudScore() - { - scoreText.text = scores.ToString("D6"); } - private void SetHudTime() + private void Start() { - TimeLeftInt = Mathf.RoundToInt(timeLeft); - timeText.text = TimeLeftInt.ToString("D3"); - } + RetrieveGameState(); - private void CreateFloatingText(string text, Vector3 spawnPos) - { - GameObject textEffect = Instantiate(floatingTextEffect, spawnPos, Quaternion.identity); - textEffect.GetComponentInChildren().text = text.ToUpper(); + Debug.Log(this.name + " Start: current scene is " + SceneManager.GetActiveScene().name); } - - private void ChangeMusic(AudioClip clip, float delay = 0) + private void OnEnable() { - StartCoroutine(ChangeMusicCo(clip, delay)); + Coin.OnCoinCollected += _playerPickUpAbilities.AddCoin; + Starman.OnStarmanCollected += _playerAbilities.MarioInvincibleStarman; + OneUpMushroom.OnOneUpCollected += _playerPickUpAbilities.AddLife; + PowerupObject.OnPowerUpCollected += _playerAbilities.MarioPowerUp; } - private IEnumerator ChangeMusicCo(AudioClip clip, float delay) + private void OnDisable() { - Debug.Log(this.name + " ChangeMusicCo: starts changing music to " + clip.name); - GetSoundManager.MusicSource.clip = clip; - yield return new WaitWhile(() => gamePaused); - yield return new WaitForSecondsRealtime(delay); - yield return new WaitWhile(() => gamePaused || musicPaused); - if (!IsRespawning) { - GetSoundManager.MusicSource.Play(); - } - - Debug.Log(this.name + " ChangeMusicCo: done changing music to " + clip.name); + Coin.OnCoinCollected -= _playerPickUpAbilities.AddCoin; + Starman.OnStarmanCollected -= _playerAbilities.MarioInvincibleStarman; + OneUpMushroom.OnOneUpCollected -= _playerPickUpAbilities.AddLife; + PowerupObject.OnPowerUpCollected -= _playerAbilities.MarioPowerUp; } - private void PauseMusicPlaySound(AudioClip clip, bool resumeMusic) + public void RetrieveGameState() { - StartCoroutine(PauseMusicPlaySoundCo(clip, resumeMusic)); - } + // Lives = _gameStateManager.Lives; + _hud.Coins = _gameStateManager.Coins; + _hud.Scores = _gameStateManager.Scores; + _hud.TimeLeft = _gameStateManager.TimeLeft; + // marioSize = _gameStateManager.PlayerSize; + // HurryUp = _gameStateManager.HurryUp; - private IEnumerator PauseMusicPlaySoundCo(AudioClip clip, bool resumeMusic) - { - string musicClipName = ""; - if (GetSoundManager.MusicSource.clip) { - musicClipName = GetSoundManager.MusicSource.clip.name; - } - - Debug.Log(this.name + " Pause musicPlaySoundCo: starts pausing music " + musicClipName + " to play sound " + - clip.name); - - musicPaused = true; - GetSoundManager.MusicSource.Pause(); - GetSoundManager.SoundSource.PlayOneShot(clip); - yield return new WaitForSeconds(clip.length); - if (resumeMusic) { - GetSoundManager.MusicSource.UnPause(); - - musicClipName = ""; - if (GetSoundManager.MusicSource.clip) { - musicClipName = GetSoundManager.MusicSource.clip.name; - } - - Debug.Log(this.name + " PausemusicPlaySoundCo: resume playing music " + musicClipName); - } - - musicPaused = false; + _playerController = FindObjectOfType(); + PlayerAnimator.PlayerAnimatorComponent = _playerController.gameObject.GetComponent(); + _playerAbilities.PlayerRigidbody2D = _playerController.gameObject.GetComponent(); + _playerController.UpdateSize(); - Debug.Log(this.name + " PausemusicPlaySoundCo: done pausing music to play sound " + clip.name); - } + GetSoundManager.GetSoundVolume(); - /****************** Game state */ - public void AddLife() - { - lives++; - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.OneUpSound); + _hud.SetHUD(); + _soundManager.GetSoundLevelHandle.ChangeMusic(_gameStateManager.HurryUp + ? GetSoundManager.LevelMusicHurry + : GetSoundManager.LevelMusic); } - public void AddLife(Vector3 spawnPos) - { - lives++; - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.OneUpSound); - CreateFloatingText("1UP", spawnPos); - } - - public void AddCoin() + private void Update() { - coins++; - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.CoinSound); - if (coins == 100) { - AddLife(); - coins = 0; - } + // _gameStateManager.TimerHUD(); + // + // _soundManager.GetSoundLevelHandle.TimerHUDMusic(); + // + // _gameStateManager.TimeUpCounter(); + // + // _gameStateManager.GamePauseCheck(); + TimerHUD(); - SetHudCoin(); - AddScore(coinBonus); - } + TimerHUDMusic(); - public void AddCoin(Vector3 spawnPos) - { - coins++; - GetSoundManager.SoundSource.PlayOneShot(GetSoundManager.CoinSound); - if (coins == 100) { - AddLife(); - coins = 0; - } + TimeUpCounter(); - SetHudCoin(); - AddScore(coinBonus, spawnPos); + GamePauseCheck(); } - - public void AddScore(int bonus) + public void TimerHUD() { - scores += bonus; - SetHudScore(); + if (_gameStateManager.TimerPaused) return; + _hud.TimeLeft -= Time.deltaTime; // / .4f; // 1 game sec ~ 0.4 real time sec + _hud.SetHudTime(); } - - public void AddScore(int bonus, Vector3 spawnPos) + public void GamePauseCheck() { - scores += bonus; - SetHudScore(); - if (bonus > 0) { - CreateFloatingText(bonus.ToString(), spawnPos); - } + if (!Input.GetButtonDown("Pause")) return; + _gameStateManager.PauseUnPauseState(); } - - - /****************** Misc */ - public Vector3 FindSpawnPosition() + public void TimeUpCounter() { - Vector3 spawnPosition; - GameStateManager gameStateManager = FindObjectOfType(); - Debug.Log(this.name + " FindSpawnPosition: GSM spawnFromPoint=" + - gameStateManager.SpawnFromPoint.ToString() - + " spawnPipeIdx= " + gameStateManager.SpawnPipeIdx.ToString() - + " spawnPointIdx=" + gameStateManager.SpawnPointIdx.ToString()); - if (gameStateManager.SpawnFromPoint) { - spawnPosition = GameObject.Find("Spawn Points").transform.GetChild(gameStateManager.SpawnPointIdx) - .transform.position; - } else { - spawnPosition = GameObject.Find("Spawn Pipes").transform.GetChild(gameStateManager.SpawnPipeIdx) - .transform.Find("Spawn Pos").transform.position; + if (_hud.TimeLeftInt <= 0) { + _playerAbilities.MarioRespawn(true); } - - return spawnPosition; } - - public string GetWorldName(string sceneName) - { - string[] sceneNameParts = Regex.Split(sceneName, " - "); - return sceneNameParts[0]; - } - - public bool IsSceneInCurrentWorld(string sceneName) - { - return GetWorldName(sceneName) == GetWorldName(SceneManager.GetActiveScene().name); - } - - public void MarioCompleteCastle() - { - timerPaused = true; - ChangeMusic(GetSoundManager.CastleCompleteMusic); - GetSoundManager.MusicSource.loop = false; - _playerController.AutomaticWalk(_playerController.CastleWalkSpeedX); - } - - public void MarioCompleteLevel() - { - timerPaused = true; - ChangeMusic(GetSoundManager.LevelCompleteMusic); - GetSoundManager.MusicSource.loop = false; - } - - public void MarioReachFlagPole() + public void TimerHUDMusic() { - timerPaused = true; - PauseMusicPlaySound(GetSoundManager.FlagpoleSound, false); - _playerController.ClimbFlagPole(); + if (_hud.TimeLeftInt >= 100 || _gameStateManager.HurryUp) return; + _gameStateManager.HurryUp = true; + _soundManager.GetSoundLevelHandle.PauseMusicPlaySound(_soundManager.WarningSound, true); + _soundManager.GetSoundLevelHandle.ChangeMusic( + _playerAbilities.IsInvincibleStarman + ? _soundManager.StarmanMusicHurry + : _soundManager.LevelMusicHurry, + _soundManager.WarningSound.length); } } } \ No newline at end of file diff --git a/Assets/Scripts/Core/Managers/LevelManager.cs.meta b/Assets/Scripts/Core/Managers/LevelManager.cs.meta index 1e8f4b2..4fff80e 100644 --- a/Assets/Scripts/Core/Managers/LevelManager.cs.meta +++ b/Assets/Scripts/Core/Managers/LevelManager.cs.meta @@ -1,25 +1,21 @@ fileFormatVersion: 2 guid: 73fb07ac7d5f744a48fe6c56ff43ae1b -timeCreated: 1498235484 -licenseType: Free MonoImporter: + externalObjects: {} serializedVersion: 2 defaultReferences: - scoreText: {instanceID: 0} - coinText: {instanceID: 0} - timeText: {instanceID: 0} - - FloatingTextEffect: {fileID: 1196479104050882, guid: 1ad1e9f06ef1b47b0ba83ef5484e63cf, - type: 2} + - FloatingTextEffect: {fileID: 1196479104050882, guid: 1ad1e9f06ef1b47b0ba83ef5484e63cf, type: 3} - musicSource: {instanceID: 0} - soundSource: {instanceID: 0} - levelMusic: {instanceID: 0} - levelMusicHurry: {instanceID: 0} - starmanMusic: {fileID: 8300000, guid: fe80c79237cc34e99babbc6d54016d93, type: 3} - starmanMusicHurry: {fileID: 8300000, guid: f1d4bda1392ad4f6cb3bb8e494af8eb5, type: 3} - - levelCompleteMusic: {fileID: 8300000, guid: 632e293310a2b4c088dd51fdeaa9d8b8, - type: 3} - - castleCompleteMusic: {fileID: 8300000, guid: 6dbea1eceaa2e48b6aa7c92fac4fbafe, - type: 3} + - levelCompleteMusic: {fileID: 8300000, guid: 632e293310a2b4c088dd51fdeaa9d8b8, type: 3} + - castleCompleteMusic: {fileID: 8300000, guid: 6dbea1eceaa2e48b6aa7c92fac4fbafe, type: 3} - oneUpSound: {fileID: 8300000, guid: cdc43de50f4574c75afcc7e74975b29a, type: 3} - bowserFallSound: {fileID: 8300000, guid: 04c05e6a635a94c57ba4f3a3f3aaa89f, type: 3} - bowserFireSound: {fileID: 8300000, guid: de02ec0c771a0476cb3609ebf94653a8, type: 3} @@ -33,11 +29,9 @@ MonoImporter: - jumpSuperSound: {fileID: 8300000, guid: 7988561719a0c40049ae5a3c2870d1e7, type: 3} - kickSound: {fileID: 8300000, guid: 96f7a44168f114e6ba76d5c1bef36f86, type: 3} - pauseSound: {fileID: 8300000, guid: 5aec150c938c24103aaf3606c480427c, type: 3} - - pipePowerdownSound: {fileID: 8300000, guid: b520e6b32619c490a934225b9b162dc2, - type: 3} + - pipePowerdownSound: {fileID: 8300000, guid: b520e6b32619c490a934225b9b162dc2, type: 3} - powerupSound: {fileID: 8300000, guid: 26301d2e86e99420599e315297a825df, type: 3} - - powerupAppearSound: {fileID: 8300000, guid: 97a450c65284a4a3094ba5c2221a492b, - type: 3} + - powerupAppearSound: {fileID: 8300000, guid: 97a450c65284a4a3094ba5c2221a492b, type: 3} - stompSound: {fileID: 8300000, guid: d84415726495b4d46b5a557ed2a97cfa, type: 3} - warningSound: {fileID: 8300000, guid: c0ea6c25686884e1a9547efdebf43492, type: 3} executionOrder: 0 diff --git a/Assets/Scripts/Core/Managers/SaveGameStateOnMemory.cs b/Assets/Scripts/Core/Managers/SaveGameStateOnMemory.cs index 7745bcb..c288554 100644 --- a/Assets/Scripts/Core/Managers/SaveGameStateOnMemory.cs +++ b/Assets/Scripts/Core/Managers/SaveGameStateOnMemory.cs @@ -11,12 +11,12 @@ public void SaveGameState(IGameStateManager gameStateManager) { //if (gameStateManager == null) throw new NullReferenceException(); LevelManager levelManager = Object.FindObjectOfType(); - gameStateManager.PlayerSize = levelManager.marioSize; - gameStateManager.Lives = levelManager.lives; - gameStateManager.Coins = levelManager.coins; - gameStateManager.Scores = levelManager.scores; - gameStateManager.TimeLeft = levelManager.timeLeft; - gameStateManager.HurryUp = levelManager.hurryUp; + gameStateManager.PlayerSize = levelManager.GetGameStateManager.PlayerSize; + gameStateManager.Lives = levelManager.GetGameStateManager.Lives; + gameStateManager.Coins = levelManager.GetHUD.Coins; + gameStateManager.Scores = levelManager.GetHUD.Scores; + gameStateManager.TimeLeft = levelManager.GetHUD.TimeLeft; + gameStateManager.HurryUp = levelManager.GetGameStateManager.HurryUp; } } } \ No newline at end of file diff --git a/Assets/Scripts/Core/Managers/SoundManager.cs b/Assets/Scripts/Core/Managers/SoundManager.cs index b3d9954..3ba9ee2 100644 --- a/Assets/Scripts/Core/Managers/SoundManager.cs +++ b/Assets/Scripts/Core/Managers/SoundManager.cs @@ -1,6 +1,5 @@ -using System; -using System.Collections; using Interfaces.Core.Managers; +using Interfaces.Level; using JetBrains.Annotations; using UnityEngine; @@ -31,13 +30,12 @@ namespace Core.Managers #endregion + // [RequireComponent(typeof(ISoundManagerExtras))] + // [RequireComponent(typeof(ISoundLevelHandle))] public class SoundManager : SoundManagerBase, ISoundManagerExtras { // public static ISoundManager Instance { get; private set; } - [CanBeNull] private IMasterVolume _masterVolume; - [CanBeNull] private ISoundHandle _soundHandle; - // private void Awake() // { // if (Instance == null) @@ -51,6 +49,16 @@ public class SoundManager : SoundManagerBase, ISoundManagerExtras // } // } + [CanBeNull] private IMasterVolume _masterVolume; + private ISoundLevelHandle _soundLevelHandler; + + public ISoundLevelHandle GetSoundLevelHandle => _soundLevelHandler; + + private void Awake() + { + _soundLevelHandler = GetComponent(); + } + public AudioSource MusicSource { get => musicSource; @@ -212,7 +220,7 @@ public AudioClip WarningSound get => warningSound; set => warningSound = value; } - + public void GetSoundVolume() { MusicSource.volume = PlayerPrefs.GetFloat("musicVolume"); @@ -227,73 +235,4 @@ public void PlaySound(AudioClip clip) effectsSource.PlayOneShot(clip); } } - - internal interface ISoundHandle - { - public void ChangeMusic(AudioClip clip, float delay = 0); - public void PauseMusicPlaySound(AudioClip clip, bool resumeMusic); - } - - public class LevelHandleMusic : MonoBehaviour, ISoundHandle - { - public bool gamePaused; - public bool timerPaused; - public bool musicPaused; - - private ISoundManagerExtras _soundManager; - - public void ChangeMusic(AudioClip clip, float delay = 0) - { - StartCoroutine(ChangeMusicCo(clip, delay)); - } - - private IEnumerator ChangeMusicCo(AudioClip clip, float delay) - { - Debug.Log(this.name + " ChangeMusicCo: starts changing music to " + clip.name); - _soundManager.MusicSource.clip = clip; - yield return new WaitWhile(() => gamePaused); - yield return new WaitForSecondsRealtime(delay); - yield return new WaitWhile(() => gamePaused || musicPaused); - // if (!IsRespawning) { - // _soundManager.MusicSource.Play(); - // } - - Debug.Log(this.name + " ChangeMusicCo: done changing music to " + clip.name); - } - - public void PauseMusicPlaySound(AudioClip clip, bool resumeMusic) - { - StartCoroutine(PauseMusicPlaySoundCo(clip, resumeMusic)); - } - - private IEnumerator PauseMusicPlaySoundCo(AudioClip clip, bool resumeMusic) - { - string musicClipName = ""; - if (_soundManager.MusicSource.clip) { - musicClipName = _soundManager.MusicSource.clip.name; - } - - Debug.Log(this.name + " Pause musicPlaySoundCo: starts pausing music " + musicClipName + " to play sound " + - clip.name); - - musicPaused = true; - _soundManager.MusicSource.Pause(); - _soundManager.SoundSource.PlayOneShot(clip); - yield return new WaitForSeconds(clip.length); - if (resumeMusic) { - _soundManager.MusicSource.UnPause(); - - musicClipName = ""; - if (_soundManager.MusicSource.clip) { - musicClipName = _soundManager.MusicSource.clip.name; - } - - Debug.Log(this.name + " PausemusicPlaySoundCo: resume playing music " + musicClipName); - } - - musicPaused = false; - - Debug.Log(this.name + " PausemusicPlaySoundCo: done pausing music to play sound " + clip.name); - } - } } \ No newline at end of file diff --git a/Assets/Scripts/Core/NPC/Bowser.cs b/Assets/Scripts/Core/NPC/Bowser.cs index 92cda62..998e685 100644 --- a/Assets/Scripts/Core/NPC/Bowser.cs +++ b/Assets/Scripts/Core/NPC/Bowser.cs @@ -7,40 +7,40 @@ namespace Core.NPC { public class Bowser : Enemy { - private LevelManager t_LevelManager; - private GameObject mario; - private Rigidbody2D m_Rigidbody2D; + private LevelManager _levelManager; + private GameObject _mario; + private Rigidbody2D _rigidbody2D; - public Transform FirePos; - public GameObject BowserImpostor; - public GameObject BowserFire; + public Transform firePos; + public GameObject bowserImpostor; + public GameObject bowserFire; public bool canMove; public bool active; private Vector2 impostorInitialVelocity = new Vector2 (3, 3); private float minDistanceToMove = 55; // start moving if mario is within this distance - private int fireResistance = 5; + private int _fireResistance = 5; private float waitBetweenJump = 3; private float shootFireDelay = .1f; // how long after jump should Bowser release fireball private float absSpeedX = 1.5f; - private float directionX = 1; + private float _directionX = 1; private float minJumpSpeedY = 3; private float maxJumpSpeedY = 7; - private float timer; - private float jumpSpeedY; + private float _timer; + private float _jumpSpeedY; - private int defeatBonus; - private bool isFalling; + private int _defeatBonus; + private bool _isFalling; // Use this for initialization - void Start () { - t_LevelManager = FindObjectOfType (); - mario = FindObjectOfType ().gameObject; - m_Rigidbody2D = GetComponent (); - timer = 0; + private void Start () { + _levelManager = FindObjectOfType (); + _mario = FindObjectOfType ().gameObject; + _rigidbody2D = GetComponent (); + _timer = 0; canMove = false; active = true; @@ -49,53 +49,53 @@ void Start () { hitByBlockBonus = 0; fireballBonus = 0; stompBonus = 0; - defeatBonus = 5000; + _defeatBonus = 5000; } // Update is called once per frame - void Update () { + private void Update () { if (active) { - if (!canMove && Mathf.Abs (mario.gameObject.transform.position.x - transform.position.x) <= minDistanceToMove) { + if (!canMove && Mathf.Abs (_mario.gameObject.transform.position.x - transform.position.x) <= minDistanceToMove) { canMove = true; } if (canMove) { - m_Rigidbody2D.velocity = new Vector2 (directionX * absSpeedX, m_Rigidbody2D.velocity.y); - timer -= Time.deltaTime; + _rigidbody2D.velocity = new Vector2 (_directionX * absSpeedX, _rigidbody2D.velocity.y); + _timer -= Time.deltaTime; - if (timer <= 0) { + if (_timer <= 0) { // Turn to face Mario - if (mario.transform.position.x < transform.position.x) { // mario to the left + if (_mario.transform.position.x < transform.position.x) { // mario to the left transform.localScale = new Vector3 (-1, 1, 1); - } else if (mario.transform.position.x > transform.position.x) { + } else if (_mario.transform.position.x > transform.position.x) { transform.localScale = new Vector3 (1, 1, 1); } // Switch walk direction - directionX = -directionX; + _directionX = -_directionX; // Jump a random height - jumpSpeedY = Random.Range (minJumpSpeedY, maxJumpSpeedY); - m_Rigidbody2D.velocity = new Vector2 (m_Rigidbody2D.velocity.x, jumpSpeedY); + _jumpSpeedY = Random.Range (minJumpSpeedY, maxJumpSpeedY); + _rigidbody2D.velocity = new Vector2 (_rigidbody2D.velocity.x, _jumpSpeedY); // Shoot fireball after some delay StartCoroutine (ShootFireCo (shootFireDelay)); - timer = waitBetweenJump; + _timer = waitBetweenJump; } } - } else if (m_Rigidbody2D.velocity.y < 0 && !isFalling) { // fall as bridge collapses - isFalling = true; - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BowserFallSound); + } else if (_rigidbody2D.velocity.y < 0 && !_isFalling) { // fall as bridge collapses + _isFalling = true; + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BowserFallSound); } } - IEnumerator ShootFireCo(float delay) { + private IEnumerator ShootFireCo(float delay) { yield return new WaitForSeconds (delay); - GameObject fire = Instantiate(BowserFire, FirePos.position, Quaternion.identity); + GameObject fire = Instantiate(bowserFire, firePos.position, Quaternion.identity); fire.GetComponent ().directionX = transform.localScale.x; - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BowserFireSound); + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BowserFireSound); } public override void TouchedByStarmanMario() { @@ -108,31 +108,30 @@ public override void HitBelowByBlock() { } public override void HitByMarioFireball() { - fireResistance--; - if (fireResistance <= 0) { - GameObject impostor = Instantiate (BowserImpostor, transform.position, Quaternion.identity); - impostor.GetComponent ().velocity = - new Vector2 (impostorInitialVelocity.x * directionX, impostorInitialVelocity.y); - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BowserFallSound); - - t_LevelManager.AddScore (defeatBonus); - Destroy (gameObject); - } + _fireResistance--; + if (_fireResistance > 0) return; + GameObject impostor = Instantiate (bowserImpostor, transform.position, Quaternion.identity); + impostor.GetComponent ().velocity = + new Vector2 (impostorInitialVelocity.x * _directionX, impostorInitialVelocity.y); + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BowserFallSound); + + _levelManager.GetPlayerPickUpAbilities.AddScore (_defeatBonus); + Destroy (gameObject); } public override void StompedByMario() { } - void OnCollisionEnter2D(Collision2D other) { + private void OnCollisionEnter2D(Collision2D other) { Vector2 normal = other.contacts[0].normal; Vector2 leftSide = new Vector2 (-1f, 0f); Vector2 rightSide = new Vector2 (1f, 0f); bool sideHit = normal == leftSide || normal == rightSide; if (other.gameObject.tag == "Player") { - t_LevelManager.MarioPowerDown (); - } else if (sideHit && other.gameObject.tag != "Mario Fireball") { // switch walk direction - directionX = -directionX; + _levelManager.GetPlayerAbilities.MarioPowerDown (); + } else if (sideHit && !other.gameObject.CompareTag("Mario Fireball")) { // switch walk direction + _directionX = -_directionX; } } } diff --git a/Assets/Scripts/Core/NPC/KoopaShell.cs b/Assets/Scripts/Core/NPC/KoopaShell.cs index 9ee7745..93b1f4d 100644 --- a/Assets/Scripts/Core/NPC/KoopaShell.cs +++ b/Assets/Scripts/Core/NPC/KoopaShell.cs @@ -1,31 +1,31 @@ using Core.Managers; using Core.Player; +using Interfaces.Core.Managers; using UnityEngine; namespace Core.NPC { public class KoopaShell : Enemy { - private Animator m_Animator; - private Rigidbody2D m_Rigidbody2D; - private LevelManager t_LevelManager; - private PlayerController playerController; + private Animator _animator; + private Rigidbody2D _rigidbody2D; + private ILevelManager _levelManager; + private PlayerController _playerController; public GameObject Koopa; public float rollSpeedX = 7; - private float waitTillRevive = 5; - private float waitTillRespawn = 1.5f; + private float _waitTillRevive = 5; + private float _waitTillRespawn = 1.5f; - private float currentRollVelocityX; - private bool isReviving; + private float _currentRollVelocityX; + private bool _isReviving; public bool isRolling; - // Use this for initialization - void Start () { - t_LevelManager = FindObjectOfType (); - playerController = FindObjectOfType (); - m_Animator = GetComponent (); - m_Rigidbody2D = GetComponent (); - isReviving = false; + private void Start () { + _levelManager = FindObjectOfType (); + _playerController = FindObjectOfType (); + _animator = GetComponent (); + _rigidbody2D = GetComponent (); + _isReviving = false; isRolling = false; starmanBonus = 200; // ??? @@ -35,24 +35,24 @@ void Start () { stompBonus = 500; } - void Update() { - if (!isReviving && !isRolling) { - waitTillRevive -= Time.deltaTime; - if (waitTillRevive <= 0) { - m_Animator.SetTrigger ("revived"); - isReviving = true; + private void Update() { + if (!_isReviving && !isRolling) { + _waitTillRevive -= Time.deltaTime; + if (_waitTillRevive <= 0) { + _animator.SetTrigger ("revived"); + _isReviving = true; } - } else if (isReviving && !isRolling) { - waitTillRespawn -= Time.deltaTime; - if (waitTillRespawn <= 0) { + } else if (_isReviving && !isRolling) { + _waitTillRespawn -= Time.deltaTime; + if (_waitTillRespawn <= 0) { Instantiate (Koopa, transform.position, Quaternion.identity); Destroy (gameObject); } } else if (isRolling) { - m_Rigidbody2D.velocity = new Vector2 (currentRollVelocityX, m_Rigidbody2D.velocity.y); + _rigidbody2D.velocity = new Vector2 (_currentRollVelocityX, _rigidbody2D.velocity.y); } - if (hasBeenStomped) { + if (_hasBeenStomped) { stompBonus = 0; } } @@ -61,39 +61,41 @@ public override void TouchedByRollingShell() { if (!isRolling) { FlipAndDie (); } else { // change direction if touched by another rolling shell - currentRollVelocityX = -currentRollVelocityX; + _currentRollVelocityX = -_currentRollVelocityX; rollingShellBonus = 0; // ??? } } - bool hasBeenStomped = false; + private bool _hasBeenStomped; + private static readonly int Rolled = Animator.StringToHash("rolled"); + public override void StompedByMario() { isBeingStomped = true; if (!isRolling) { // start rolling left/right depending on Mario's direction - if (playerController.transform.localScale.x == 1) { - currentRollVelocityX = rollSpeedX; - } else if (playerController.transform.localScale.x == -1) { - currentRollVelocityX = -rollSpeedX; + if (_playerController.transform.localScale.x == 1) { + _currentRollVelocityX = rollSpeedX; + } else if (_playerController.transform.localScale.x == -1) { + _currentRollVelocityX = -rollSpeedX; } isRolling = true; - m_Animator.SetTrigger ("rolled"); + _animator.SetTrigger (Rolled); } else { isRolling = false; } - hasBeenStomped = true; + _hasBeenStomped = true; isBeingStomped = false; } - - void OnCollisionEnter2D(Collision2D other) { - if (isRolling) { - if (other.gameObject.tag.Contains("Enemy")) { // kill off other enemies - Enemy enemy = other.gameObject.GetComponent(); - t_LevelManager.RollingShellTouchEnemy (enemy); - } else { - currentRollVelocityX = -currentRollVelocityX; - } + + private void OnCollisionEnter2D(Collision2D other) + { + if (!isRolling) return; + if (other.gameObject.tag.Contains("Enemy")) { // kill off other enemies + Enemy enemy = other.gameObject.GetComponent(); + _levelManager.GetPlayerAbilities.RollingShellTouchEnemy (enemy); + } else { + _currentRollVelocityX = -_currentRollVelocityX; } } } diff --git a/Assets/Scripts/Core/NPC/Piranha.cs b/Assets/Scripts/Core/NPC/Piranha.cs index f9fe056..0f97fb6 100644 --- a/Assets/Scripts/Core/NPC/Piranha.cs +++ b/Assets/Scripts/Core/NPC/Piranha.cs @@ -6,23 +6,22 @@ namespace Core.NPC { public class Piranha : Enemy { - private LevelManager t_LevelManager; - private GameObject mario; - private CircleCollider2D m_CircleCollider2D; - private PatrolVertical patrolScript; + private LevelManager _levelManager; + [SerializeField] private GameObject mario; + private CircleCollider2D _circleCollider2D; + private PatrolVertical _patrolScript; - private bool visible; + private bool _visible; private float maxDistanceToMove = 2; // should not emerge if Mario is within this distance of pipe - // Use this for initialization - void Start () { - t_LevelManager = FindObjectOfType (); + private void Start () { + _levelManager = FindObjectOfType (); mario = FindObjectOfType ().gameObject; - m_CircleCollider2D = GetComponent (); - patrolScript = GetComponent (); - visible = false; - patrolScript.canMove = false; - m_CircleCollider2D.enabled = false; + _circleCollider2D = GetComponent (); + _patrolScript = GetComponent (); + _visible = false; + _patrolScript.canMove = false; + _circleCollider2D.enabled = false; starmanBonus = 100; // ??? rollingShellBonus = 500; // ??? @@ -31,23 +30,23 @@ void Start () { stompBonus = 0; } - void OnBecameVisible() { - visible = true; + private void OnBecameVisible() { + _visible = true; } - void Update() { - if (visible) { - if (Mathf.Abs (mario.transform.position.x - transform.position.x) > maxDistanceToMove) { - m_CircleCollider2D.enabled = true; - patrolScript.canMove = true; - } else if (patrolScript.isAtDownStop) { // do not emerge - m_CircleCollider2D.enabled = false; - patrolScript.canMove = false; - } + private void Update() + { + if (!_visible) return; + if (Mathf.Abs (mario.transform.position.x - transform.position.x) > maxDistanceToMove) { + _circleCollider2D.enabled = true; + _patrolScript.canMove = true; + } else if (_patrolScript.isAtDownStop) { // do not emerge + _circleCollider2D.enabled = false; + _patrolScript.canMove = false; } } - void DestroyPiranhaStruct() { + private void DestroyPiranhaStruct() { Destroy (gameObject.transform.parent.gameObject); } @@ -71,7 +70,7 @@ public override void StompedByMario() { void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") { - t_LevelManager.MarioPowerDown (); + _levelManager.GetPlayerAbilities.MarioPowerDown (); } } } diff --git a/Assets/Scripts/Core/Player/PlayerAnimator.cs b/Assets/Scripts/Core/Player/PlayerAnimator.cs new file mode 100644 index 0000000..064c816 --- /dev/null +++ b/Assets/Scripts/Core/Player/PlayerAnimator.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +namespace Core.Player +{ + public static class PlayerAnimator + { + public static Animator PlayerAnimatorComponent; + + public static readonly int IsInvincibleStarmanAnim = Animator.StringToHash("isInvincibleStarman"); + public static readonly int IsInvinciblePowerdownAnim = Animator.StringToHash("isInvinciblePowerdown"); + public static readonly int IsPoweringUpAnim = Animator.StringToHash("isPoweringUp"); + public static readonly int IsPoweringDownAnim = Animator.StringToHash("isPoweringDown"); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Core/Player/PlayerAnimator.cs.meta b/Assets/Scripts/Core/Player/PlayerAnimator.cs.meta new file mode 100644 index 0000000..d14ec5a --- /dev/null +++ b/Assets/Scripts/Core/Player/PlayerAnimator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f9e6686fca55435ea68e5bd26e880307 +timeCreated: 1659208111 \ No newline at end of file diff --git a/Assets/Scripts/Core/Player/PlayerController.cs b/Assets/Scripts/Core/Player/PlayerController.cs index a271d00..c72b57e 100644 --- a/Assets/Scripts/Core/Player/PlayerController.cs +++ b/Assets/Scripts/Core/Player/PlayerController.cs @@ -43,7 +43,7 @@ private void Update() IsFalling = MRigidbody2D.velocity.y < 0 && !base.IsGrounded; IsChangingDirection = CurrentSpeedX > 0 && FaceDirectionX * MoveDirectionX < 0; - if (!InputFreezed || LevelManager.gamePaused) return; + if (!InputFreezed || LevelManager.GetGameStateManager.GamePaused) return; if (_isDying) { _deadUpTimer -= Time.unscaledDeltaTime; if (_deadUpTimer > 0) { @@ -81,15 +81,15 @@ private void OnCollisionEnter2D(Collision2D other) // TODO: koopa shell static does no damage Enemy enemy = other.gameObject.GetComponent(); - if (!LevelManager.IsInvincible()) { + if (!LevelManager.GetPlayerAbilities.IsInvincible()) { if (!other.gameObject.GetComponent() || other.gameObject.GetComponent() .isRolling || // non-rolling shell should do no damage !bottomHit || (!enemy.isBeingStomped)) { - LevelManager.MarioPowerDown(); + LevelManager.GetPlayerAbilities.MarioPowerDown(); } - } else if (LevelManager.isInvincibleStarman) { - LevelManager.MarioStarmanTouchEnemy(enemy); + } else if (LevelManager.GetPlayerAbilities.IsInvincibleStarman) { + LevelManager.GetPlayerAbilities.MarioStarmanTouchEnemy(enemy); } } else if (other.gameObject.CompareTag("Goal") && IsClimbingFlagPole && bottomHit) { IsClimbingFlagPole = false; @@ -106,15 +106,15 @@ private void OnCollisionStay2D(Collision2D collision) // TODO: koopa shell static does no damage Enemy enemy = collision.gameObject.GetComponent(); - if (!LevelManager.IsInvincible()) { + if (!LevelManager.GetPlayerAbilities.IsInvincible()) { if (!collision.gameObject.GetComponent() || collision.gameObject.GetComponent() .isRolling || // non-rolling shell should do no damage !bottomHit || (!enemy.isBeingStomped)) { - LevelManager.MarioPowerDown(); + LevelManager.GetPlayerAbilities.MarioPowerDown(); } - } else if (LevelManager.isInvincibleStarman) { - LevelManager.MarioStarmanTouchEnemy(enemy); + } else if (LevelManager.GetPlayerAbilities.IsInvincibleStarman) { + LevelManager.GetPlayerAbilities.MarioStarmanTouchEnemy(enemy); } } } diff --git a/Assets/Scripts/Core/Player/PlayerController_Misc.cs b/Assets/Scripts/Core/Player/PlayerController_Misc.cs index 7789703..31c6b55 100644 --- a/Assets/Scripts/Core/Player/PlayerController_Misc.cs +++ b/Assets/Scripts/Core/Player/PlayerController_Misc.cs @@ -19,7 +19,8 @@ private void DeSubscribeToEvents() protected override void PerLevelInitialization() { // Drop Mario at spawn position - transform.position = FindObjectOfType().FindSpawnPosition(); + transform.position = FindObjectOfType() + .GetLevelServices.FindSpawnPosition(); // Set correct size UpdateSize(); @@ -114,7 +115,7 @@ private void SetMidairParams() MidairDecelerationX = .21f; } } - + [SerializeField] private bool _isDying; private float _deadUpTimer = .25f; @@ -227,7 +228,9 @@ public void AutomaticCrouch() /// public override void UpdateSize() { - GetComponent().SetInteger(PlayerSizeAnimator, FindObjectOfType().marioSize); + GetComponent().SetInteger(PlayerSizeAnimator, + FindObjectOfType() + .GetGameStateManager.PlayerSize); } /// diff --git a/Assets/Scripts/Core/Player/PlayerController_Movement.cs b/Assets/Scripts/Core/Player/PlayerController_Movement.cs index dda2e45..a186246 100644 --- a/Assets/Scripts/Core/Player/PlayerController_Movement.cs +++ b/Assets/Scripts/Core/Player/PlayerController_Movement.cs @@ -27,7 +27,7 @@ protected override void PlayerControlsSubscribe() { } private void Shooting(InputAction.CallbackContext obj) { - if (!IsShooting || LevelManager.marioSize != 2) return; + if (!IsShooting || LevelManager.GetGameStateManager.PlayerSize != 2) return; FireTime2 = Time.time; if (!(FireTime2 - FireTime1 >= WaitBetweenFire)) return; @@ -81,7 +81,7 @@ private void Jump_performed(InputAction.CallbackContext context) { IsJumping = true; SpeedXBeforeJump = CurrentSpeedX; WasDashingBeforeJump = IsDashing; - LevelManager.GetSoundManager.SoundSource.PlayOneShot(LevelManager.marioSize == 0 + LevelManager.GetSoundManager.SoundSource.PlayOneShot(LevelManager.GetGameStateManager.PlayerSize == 0 ? LevelManager.GetSoundManager.JumpSmallSound : LevelManager.GetSoundManager.JumpSuperSound); } diff --git a/Assets/Scripts/Interfaces/Abilities/PickUps.meta b/Assets/Scripts/Interfaces/Abilities/PickUps.meta new file mode 100644 index 0000000..d47ebfb --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/PickUps.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: e349988e5170459a8dc7af0b5682bfaf +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs b/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs new file mode 100644 index 0000000..3212f80 --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs @@ -0,0 +1,17 @@ +using UnityEngine; + +namespace Interfaces.Abilities.Pickups +{ + public interface IPlayerPickUpAbilities + { + // public void AddLife(Vector3 spawnPos = default); + // public void AddCoin(Vector3 spawnPos = default); + // public void AddScore(int? bonus = null, Vector3 spawnPos = default); + public void AddLife(); + public void AddLife(Vector3 spawnPos); + public void AddCoin(); + public void AddCoin(Vector3 spawnPos); + public void AddScore(int bonus); + public void AddScore(int bonus, Vector3 spawnPos); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs.meta b/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs.meta new file mode 100644 index 0000000..9391b3e --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/PickUps/IPlayerPickUpAbilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 7bd012af71db46c691d409296f4ff4cd +timeCreated: 1659197217 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/Player.meta b/Assets/Scripts/Interfaces/Abilities/Player.meta new file mode 100644 index 0000000..2f1c793 --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/Player.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1f1711f8b0d243da925848278136ff25 +timeCreated: 1659197811 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs b/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs new file mode 100644 index 0000000..a9f7d57 --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs @@ -0,0 +1,26 @@ +using Core.NPC; +using UnityEngine; + +namespace Interfaces.Abilities.Player +{ + public interface IPlayerAbilities + { + public bool IsInvincible(); + public void MarioInvincibleStarman(); + public void MarioInvinciblePowerdown(); + public void MarioPowerUp(); + public void MarioPowerDown(); + public void MarioRespawn(bool timeUp = false); + public void MarioStompEnemy(Enemy enemy); + public void MarioStarmanTouchEnemy(Enemy enemy); + public void RollingShellTouchEnemy(Enemy enemy); + public void BlockHitEnemy(Enemy enemy); + public void FireballTouchEnemy(Enemy enemy); + public Rigidbody2D PlayerRigidbody2D { get; set; } + public bool IsRespawning { get; set; } + public Vector2 StompBounceVelocity { get; set; } + public bool IsInvinciblePowerdown { get; set; } + public bool IsInvincibleStarman { get; set; } + public bool IsPoweringDown { get; set; } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs.meta b/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs.meta new file mode 100644 index 0000000..6d50603 --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/Player/IPlayerAbilities.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 2cdae81cf5b14af195f6966eb8bade7d +timeCreated: 1659198318 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs b/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs new file mode 100644 index 0000000..4d9e6fd --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs @@ -0,0 +1,18 @@ +using UnityEngine; + +namespace Interfaces.Abilities.Player +{ + public abstract class PlayerAbilitiesBase : MonoBehaviour + { + protected Rigidbody2D _marioRigidbody2D; + protected bool _isRespawning; + [SerializeField] protected Vector2 stompBounceVelocity = new(0, 15); + [SerializeField] protected bool isPoweringDown; + [SerializeField] protected bool isInvinciblePowerdown; + [SerializeField] protected bool isInvincibleStarman; + + protected const float MarioInvinciblePowerdownDuration = 2f; + protected const float MarioInvincibleStarmanDuration = 12f; + protected const float TransformDuration = 1f; + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs.meta b/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs.meta new file mode 100644 index 0000000..13d15f3 --- /dev/null +++ b/Assets/Scripts/Interfaces/Abilities/Player/PlayerAbilitiesBase.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: aeacc70cbaf2434c87785891d3c44856 +timeCreated: 1659224897 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/GameStateManagerBase.cs b/Assets/Scripts/Interfaces/Core/Managers/GameStateManagerBase.cs index 4e083f2..78a087d 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/GameStateManagerBase.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/GameStateManagerBase.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using System.Collections.Generic; +using UnityEngine; namespace Interfaces.Core.Managers { @@ -13,9 +14,76 @@ public abstract class GameStateManagerBase : MonoBehaviour [SerializeField] protected int coins; [SerializeField] protected int scores; [SerializeField] protected float timeLeft; + [SerializeField] protected bool hurryUp; [SerializeField] protected string sceneToLoad; // what scene to load after level start screen finishes? [SerializeField] protected bool timeUp; + + [SerializeField] protected int coinBonus = 200; + [SerializeField] protected int powerupBonus = 1000; + [SerializeField] protected int starmanBonus = 1000; + [SerializeField] protected int oneupBonus; + [SerializeField] protected int breakBlockBonus = 50; + [SerializeField] protected int timeLeftInt; + + [SerializeField] protected bool gamePaused; + [SerializeField] protected bool timerPaused; + [SerializeField] protected bool musicPaused; + + + protected readonly List UnScaledAnimators = new List(); + protected float PauseGamePrevTimeScale; + protected bool PausePrevMusicPaused; } + + // public class GameStateDataBase : MonoBehaviour + // { + // [SerializeField] protected int coinBonus = 200; + // [SerializeField] protected int powerupBonus = 1000; + // [SerializeField] protected int starmanBonus = 1000; + // [SerializeField] protected int oneupBonus; + // [SerializeField] protected int breakBlockBonus = 50; + // [SerializeField] protected int timeLeftInt; + // + // + // [SerializeField] protected bool gamePaused; + // [SerializeField] protected bool timerPaused; + // [SerializeField] protected bool musicPaused; + // + // + // protected readonly List UnScaledAnimators = new List(); + // protected float PauseGamePrevTimeScale; + // protected bool PausePrevMusicPaused; + // } + // + // public class GameStateData: GameStateDataBase, IGameStateData + // { + // public bool CoinBonus { get; set; } + // public int PowerupBonus { get; set; } + // public int StarmanBonus { get; set; } + // public int OneupBonus { get; set; } + // public int BreakBlockBonus { get; set; } + // public int TimeLeftInt { get; set; } + // public int GamePaused { get; set; } + // public float TimerPaused { get; set; } + // public bool MusicPaused { get; set; } + // public string UnScaledAnimators { get; set; } + // public bool TimeUp { get; set; } + // } + // + // public interface IGameStateData + // { + // public bool CoinBonus { get; set; } + // public int PowerupBonus { get; set; } + // public int StarmanBonus { get; set; } + // public int OneupBonus { get; set; } + // public int BreakBlockBonus { get; set; } + // public int TimeLeftInt { get; set; } + // public int GamePaused { get; set; } + // public float TimerPaused { get; set; } + // public bool MusicPaused { get; set; } + // public string UnScaledAnimators { get; set; } + // public bool TimeUp { get; set; } + // } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/IGameStateManager.cs b/Assets/Scripts/Interfaces/Core/Managers/IGameStateManager.cs index 4bb3215..c403dd6 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/IGameStateManager.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/IGameStateManager.cs @@ -10,5 +10,9 @@ public interface IGameStateManager : IGameStateManagerEssentials public void ConfigNewLevel(); public void ConfigReplayedLevel(); public void GetSaveGameState(); + public void PauseUnPauseState(); + public void TimerHUD(); + public void GamePauseCheck(); + public void TimeUpCounter(); } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/IGameStateManagerEssentials.cs b/Assets/Scripts/Interfaces/Core/Managers/IGameStateManagerEssentials.cs index 8277f5f..61f1bbc 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/IGameStateManagerEssentials.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/IGameStateManagerEssentials.cs @@ -5,15 +5,22 @@ public interface IGameStateManagerEssentials public bool SpawnFromPoint { get; set; } public int SpawnPointIdx { get; set; } public int SpawnPipeIdx { get; set; } - - public int PlayerSize { get; set; } + public int PlayerSize { get; set; } // 0..2 public int Lives { get; set; } public int Coins { get; set; } + public int CoinBonus { get; set; } + public int PowerupBonus { get; set; } + public int StarmanBonus { get; set; } + public int OneupBonus { get; set; } + public int BreakBlockBonus { get; set; } public int Scores { get; set; } public float TimeLeft { get; set; } - public bool HurryUp { get; set; } - + public int TimeLeftInt { get; set; } + public bool HurryUp { get; set; } // within last 100 secs? public string SceneToLoad { get; set; } public bool TimeUp { get; set; } + public bool GamePaused { get; set; } + public bool TimerPaused { get; set; } + public bool MusicPaused { get; set; } } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/ILevelManager.cs b/Assets/Scripts/Interfaces/Core/Managers/ILevelManager.cs index 29ab62b..ebcfbfb 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/ILevelManager.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/ILevelManager.cs @@ -1,4 +1,10 @@ -using Interfaces.UI; +using Abilities.Pickups; +using Core.Player; +using Interfaces.Abilities; +using Interfaces.Abilities.Pickups; +using Interfaces.Abilities.Player; +using Interfaces.Level; +using Interfaces.UI; namespace Interfaces.Core.Managers { @@ -6,10 +12,20 @@ public interface ILevelManager : ILevelManagerEssentials { public void RetrieveGameState(); } - + public interface ILevelManagerEssentials { - + public ISoundManagerExtras GetSoundManager { get; } + public ILoadLevelSceneHandle GetLoadLevelSceneHandler { get; } + public IGameStateManager GetGameStateManager { get; } + public IHUD GetHUD { get; } + public IPlayerPickUpAbilities GetPlayerPickUpAbilities { get; } + public IPlayerAbilities GetPlayerAbilities { get; } + public PlayerController GetPlayerController { get; } + // public ISoundLevelHandle GetSoundLevelHandle { get; } + public ILevelServices GetLevelServices { get; } + public bool TimerPaused { get; set; } + public bool GamePaused { get; set; } + public bool MusicPaused { get; set; } } - } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/ISoundManager.cs b/Assets/Scripts/Interfaces/Core/Managers/ISoundManager.cs index 15c4556..725de05 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/ISoundManager.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/ISoundManager.cs @@ -1,4 +1,5 @@ -using UnityEngine; +using Interfaces.Level; +using UnityEngine; namespace Interfaces.Core.Managers { @@ -21,5 +22,6 @@ public interface ISoundManager public AudioClip PowerupSound { get; set; } public AudioClip PowerupAppearSound { get; set; } public AudioClip WarningSound { get; set; } + public ISoundLevelHandle GetSoundLevelHandle { get; } } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/ISoundManagerExtras.cs b/Assets/Scripts/Interfaces/Core/Managers/ISoundManagerExtras.cs index 3a41d33..0dfd166 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/ISoundManagerExtras.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/ISoundManagerExtras.cs @@ -13,7 +13,6 @@ public interface ISoundManagerExtras : ISoundManager public AudioClip FlagpoleSound { get; set; } public AudioClip PipePowerdownSound { get; set; } public AudioClip StompSound { get; set; } - public void GetSoundVolume(); } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Core/Managers/LevelManagerBase.cs b/Assets/Scripts/Interfaces/Core/Managers/LevelManagerBase.cs index 3b8559d..20313b0 100644 --- a/Assets/Scripts/Interfaces/Core/Managers/LevelManagerBase.cs +++ b/Assets/Scripts/Interfaces/Core/Managers/LevelManagerBase.cs @@ -6,52 +6,8 @@ namespace Interfaces.Core.Managers { public abstract class LevelManagerBase : MonoBehaviour { - protected const float LoadSceneDelayTime = 1f; - - public bool hurryUp; // within last 100 secs? - public int marioSize; // 0..2 - public int lives; - public int coins; - public int scores; - public float timeLeft; - protected int TimeLeftInt; - - protected bool IsRespawning; - protected bool IsPoweringDown; - - public bool isInvinciblePowerdown; - public bool isInvincibleStarman; - protected const float MarioInvinciblePowerdownDuration = 2; - protected const float MarioInvincibleStarmanDuration = 12; - protected const float TransformDuration = 1; - - protected Animator MarioAnimator; - protected Rigidbody2D MarioRigidbody2D; - - public Text scoreText; - public Text coinText; - public Text timeText; - public GameObject floatingTextEffect; - protected const float FloatingTextOffsetY = 2f; - - public int coinBonus = 200; - public int powerupBonus = 1000; - public int starmanBonus = 1000; - public int oneupBonus; - public int breakBlockBonus = 50; - - public Vector2 stompBounceVelocity = new(0, 15); - - public bool gamePaused; - public bool timerPaused; - public bool musicPaused; - - protected readonly List UnScaledAnimators = new List(); - protected float PauseGamePrevTimeScale; - protected bool PausePrevMusicPaused; - protected static readonly int IsInvincibleStarmanAnim = Animator.StringToHash("isInvincibleStarman"); - protected static readonly int IsInvinciblePowerdownAnim = Animator.StringToHash("isInvinciblePowerdown"); - protected static readonly int IsPoweringUpAnim = Animator.StringToHash("isPoweringUp"); - protected static readonly int IsPoweringDownAnim = Animator.StringToHash("isPoweringDown"); + [SerializeField] protected bool gamePaused; + [SerializeField] protected bool timerPaused; + [SerializeField] protected bool musicPaused; } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Level/ILevelServices.cs b/Assets/Scripts/Interfaces/Level/ILevelServices.cs new file mode 100644 index 0000000..7584b60 --- /dev/null +++ b/Assets/Scripts/Interfaces/Level/ILevelServices.cs @@ -0,0 +1,14 @@ +using UnityEngine; + +namespace Interfaces.Level +{ + public interface ILevelServices + { + public Vector3 FindSpawnPosition(); + public string GetWorldName(string sceneName); + public bool IsSceneInCurrentWorld(string sceneName); + public void MarioCompleteCastle(); + public void MarioCompleteLevel(); + public void MarioReachFlagPole(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Level/ILevelServices.cs.meta b/Assets/Scripts/Interfaces/Level/ILevelServices.cs.meta new file mode 100644 index 0000000..2ad5f54 --- /dev/null +++ b/Assets/Scripts/Interfaces/Level/ILevelServices.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cfa5733a9e2045bdbfc8d14133e40b1b +timeCreated: 1659248381 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Level/ILoadLevel.cs b/Assets/Scripts/Interfaces/Level/ILoadLevel.cs index e69bda2..393a2f1 100644 --- a/Assets/Scripts/Interfaces/Level/ILoadLevel.cs +++ b/Assets/Scripts/Interfaces/Level/ILoadLevel.cs @@ -4,4 +4,13 @@ public interface ILoadLevel { void LoadLevel(string loadLevelName = "Main Menu", float delay = 0); } + + public interface ILoadLevelSceneHandle : ILoadLevel + { + public void LoadSceneDelay(string loadLevelName, float delay = 0); + public void LoadSceneCurrentLevel(string loadLevelName, float delay = 0); + public void LoadSceneCurrentLevelSetSpawnPipe(string sceneName, int spawnPipeIdx, float delay = 0); + public void ReloadCurrentLevel(float delay = 0, bool timeUp = false); + public void LoadGameOver(float delay = 0, bool timeUp = false); + } } \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs b/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs new file mode 100644 index 0000000..71a9da4 --- /dev/null +++ b/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs @@ -0,0 +1,11 @@ +using UnityEngine; + +namespace Interfaces.Level +{ + public interface ISoundLevelHandle + { + public void ChangeMusic(AudioClip clip, float delay = 0); + public void PauseMusicPlaySound(AudioClip clip, bool resumeMusic); + public void TimerHUDMusic(); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs.meta b/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs.meta new file mode 100644 index 0000000..2cfdec0 --- /dev/null +++ b/Assets/Scripts/Interfaces/Level/ISoundLevelHandle.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f042cfa33a0b46fcaf453483ffeb6490 +timeCreated: 1659017998 \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/UI/IHUD.cs b/Assets/Scripts/Interfaces/UI/IHUD.cs new file mode 100644 index 0000000..c8bfb8c --- /dev/null +++ b/Assets/Scripts/Interfaces/UI/IHUD.cs @@ -0,0 +1,21 @@ +using UnityEngine; +using UnityEngine.UI; + +namespace Interfaces.UI +{ + public interface IHUD + { + public Text ScoreText { get; } + public Text CoinText { get; } + public Text TimeText { get; } + public int Coins { get; set; } + public int Scores { get; set; } + public float TimeLeft { get; set; } + public int TimeLeftInt { get; set; } + public void SetHUD(); + public void SetHudCoin(); + public void SetHudScore(); + public void SetHudTime(); + public void CreateFloatingText(string text, Vector3 spawnPosition); + } +} \ No newline at end of file diff --git a/Assets/Scripts/Interfaces/UI/IHUD.cs.meta b/Assets/Scripts/Interfaces/UI/IHUD.cs.meta new file mode 100644 index 0000000..871a99e --- /dev/null +++ b/Assets/Scripts/Interfaces/UI/IHUD.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: f38746fbfbb4400aaa59f65e9ce501f9 +timeCreated: 1659098751 \ No newline at end of file diff --git a/Assets/Scripts/Level/BridgeAxe.cs b/Assets/Scripts/Level/BridgeAxe.cs index ab5317e..99c0950 100644 --- a/Assets/Scripts/Level/BridgeAxe.cs +++ b/Assets/Scripts/Level/BridgeAxe.cs @@ -33,13 +33,13 @@ void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player" && !gotAxe) { gotAxe = true; playerController.FreezeUserInput (); - t_LevelManager.timerPaused = true; + t_LevelManager.GetGameStateManager.TimerPaused = true; if (bowser) { // bowser not yet defeated bowser.active = false; StartCoroutine (CollapseBridgeCo ()); } else { - t_LevelManager.MarioCompleteCastle (); + t_LevelManager.GetLevelServices.MarioCompleteCastle (); } gameObject.GetComponent ().enabled = false; } @@ -54,7 +54,7 @@ IEnumerator CollapseBridgeCo() { t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BreakBlockSound); yield return new WaitForSeconds (waitBetweenCollapse); } - t_LevelManager.MarioCompleteCastle (); + t_LevelManager.GetLevelServices.MarioCompleteCastle (); Destroy (gameObject); } } diff --git a/Assets/Scripts/Level/Castle.cs b/Assets/Scripts/Level/Castle.cs index 72c4d44..4941b05 100644 --- a/Assets/Scripts/Level/Castle.cs +++ b/Assets/Scripts/Level/Castle.cs @@ -1,4 +1,6 @@ -using Core.Managers; +using System; +using Core.Managers; +using Interfaces.Level; using UnityEngine; namespace Level @@ -12,7 +14,6 @@ public class Castle : MonoBehaviour { private const float FlagVelocityY = 0.025f; public string sceneName; - // Use this for initialization private void Start () { _levelManager = FindObjectOfType (); _flag = transform.Find ("Flag"); @@ -27,7 +28,7 @@ private void FixedUpdate() position = new Vector2 (position.x, position.y + FlagVelocityY); _flag.position = position; } else { - _levelManager.LoadNewLevel (sceneName, _levelManager.GetSoundManager.LevelCompleteMusic.length); + _levelManager.GetLoadLevelSceneHandler.LoadLevel(sceneName, _levelManager.GetSoundManager.LevelCompleteMusic.length); } } @@ -35,7 +36,7 @@ private void OnCollisionEnter2D(Collision2D other) { if (!other.gameObject.CompareTag("Player")) return; _moveFlag = true; - _levelManager.MarioCompleteLevel (); + _levelManager.GetLevelServices.MarioCompleteLevel (); } } } diff --git a/Assets/Scripts/Level/Firebar.cs b/Assets/Scripts/Level/Firebar.cs index e41232c..4f50c9b 100644 --- a/Assets/Scripts/Level/Firebar.cs +++ b/Assets/Scripts/Level/Firebar.cs @@ -52,7 +52,7 @@ public override void StompedByMario() { void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") { - t_LevelManager.MarioPowerDown (); + t_LevelManager.GetPlayerAbilities.MarioPowerDown (); } } diff --git a/Assets/Scripts/Level/FlagPole.cs b/Assets/Scripts/Level/FlagPole.cs index 80648d7..a4e454f 100644 --- a/Assets/Scripts/Level/FlagPole.cs +++ b/Assets/Scripts/Level/FlagPole.cs @@ -28,7 +28,7 @@ void FixedUpdate() { void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.tag == "Player" && !moveFlag) { moveFlag = true; - t_LevelManager.MarioReachFlagPole (); + t_LevelManager.GetLevelServices.MarioReachFlagPole (); } } } diff --git a/Assets/Scripts/Level/KillPlane.cs b/Assets/Scripts/Level/KillPlane.cs index 88fb270..cf11a20 100644 --- a/Assets/Scripts/Level/KillPlane.cs +++ b/Assets/Scripts/Level/KillPlane.cs @@ -3,25 +3,22 @@ namespace Level { - public class KillPlane : MonoBehaviour { - private LevelManager t_LevelManager; + public class KillPlane : MonoBehaviour + { + private LevelManager _levelManager; - // Use this for initialization - void Start () { - t_LevelManager = FindObjectOfType (); - } - - // Update is called once per frame - void Update () { - - } + private void Start() + { + _levelManager = FindObjectOfType(); + } - void OnTriggerEnter2D(Collider2D other) { - if (other.gameObject.tag == "Player") { - t_LevelManager.MarioRespawn (); - } else { - Destroy (other.gameObject); - } - } - } -} + private void OnTriggerEnter2D(Collider2D other) + { + if (other.gameObject.CompareTag("Player")) { + _levelManager.GetPlayerAbilities.MarioRespawn(); + } else { + Destroy(other.gameObject); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelHandleMusic.cs b/Assets/Scripts/Level/LevelHandleMusic.cs new file mode 100644 index 0000000..7433494 --- /dev/null +++ b/Assets/Scripts/Level/LevelHandleMusic.cs @@ -0,0 +1,90 @@ +using System.Collections; +using Core.Managers; +using Interfaces.Core.Managers; +using Interfaces.Level; +using UnityEngine; + +namespace Level +{ + public class LevelHandleMusic : MonoBehaviour, ISoundLevelHandle + { + private ILevelManager _levelManager; + private ISoundManagerExtras _soundManager; + + private void Awake() + { + // _levelManager = GetComponent(); + // _soundManager = GetComponent(); + _levelManager = FindObjectOfType(); + _soundManager = GetComponent(); + } + + public void ChangeMusic(AudioClip clip, float delay = 0) + { + StartCoroutine(ChangeMusicCo(clip, delay)); + } + + private IEnumerator ChangeMusicCo(AudioClip clip, float delay) + { + Debug.Log(this.name + " ChangeMusicCo: starts changing music to " + clip.name); + _soundManager.MusicSource.clip = clip; + yield return new WaitWhile(() => _levelManager.GetGameStateManager.GamePaused); + yield return new WaitForSecondsRealtime(delay); + // yield return new WaitWhile(() => false || false); + yield return new WaitWhile(() => + _levelManager.GetGameStateManager.GamePaused || _levelManager.GetGameStateManager.MusicPaused); + if (!_levelManager.GetPlayerAbilities.IsRespawning) { + _soundManager.MusicSource.Play(); + } + + Debug.Log(this.name + " ChangeMusicCo: done changing music to " + clip.name); + } + + public void PauseMusicPlaySound(AudioClip clip, bool resumeMusic) + { + StartCoroutine(PauseMusicPlaySoundCo(clip, resumeMusic)); + } + + private IEnumerator PauseMusicPlaySoundCo(AudioClip clip, bool resumeMusic) + { + string musicClipName = ""; + if (_soundManager.MusicSource.clip) { + musicClipName = _soundManager.MusicSource.clip.name; + } + + Debug.Log(this.name + " Pause musicPlaySoundCo: starts pausing music " + musicClipName + " to play sound " + + clip.name); + + _levelManager.GetGameStateManager.MusicPaused = true; + _soundManager.MusicSource.Pause(); + _soundManager.SoundSource.PlayOneShot(clip); + yield return new WaitForSeconds(clip.length); + if (resumeMusic) { + _soundManager.MusicSource.UnPause(); + + musicClipName = ""; + if (_soundManager.MusicSource.clip) { + musicClipName = _soundManager.MusicSource.clip.name; + } + + Debug.Log(this.name + " PausemusicPlaySoundCo: resume playing music " + musicClipName); + } + + _levelManager.GetGameStateManager.MusicPaused = false; + + Debug.Log(this.name + " PausemusicPlaySoundCo: done pausing music to play sound " + clip.name); + } + + public void TimerHUDMusic() + { + if (_levelManager.GetHUD.TimeLeftInt >= 100 || _levelManager.GetGameStateManager.HurryUp) return; + _levelManager.GetGameStateManager.HurryUp = true; + _soundManager.GetSoundLevelHandle.PauseMusicPlaySound(_levelManager.GetSoundManager.WarningSound, true); + _soundManager.GetSoundLevelHandle.ChangeMusic( + _levelManager.GetPlayerAbilities.IsInvincibleStarman + ? _levelManager.GetSoundManager.StarmanMusicHurry + : _levelManager.GetSoundManager.LevelMusicHurry, + _levelManager.GetSoundManager.WarningSound.length); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelHandleMusic.cs.meta b/Assets/Scripts/Level/LevelHandleMusic.cs.meta new file mode 100644 index 0000000..dfa32df --- /dev/null +++ b/Assets/Scripts/Level/LevelHandleMusic.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: b08dd87c3e2f4e6085d0ed5d145c09cf +timeCreated: 1659017975 \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelSceneHandle.cs b/Assets/Scripts/Level/LevelSceneHandle.cs new file mode 100644 index 0000000..1b07f78 --- /dev/null +++ b/Assets/Scripts/Level/LevelSceneHandle.cs @@ -0,0 +1,102 @@ +using System.Collections; +using Interfaces.Core.Managers; +using Interfaces.Level; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Level +{ + public class LevelSceneHandle : MonoBehaviour, ILoadLevelSceneHandle + { + private const float LoadSceneDelayTime = 1f; + + private ILevelManager _levelManager; + // private IGameStateManager _gameStateManager; + + private void Awake() + { + _levelManager = GetComponent(); + // _gameStateManager = GetComponent(); + // _gameStateManager = FindObjectOfType(); + } + + public void LoadSceneDelay(string sceneName, float delay = LoadSceneDelayTime) + { + _levelManager.GetGameStateManager.TimerPaused = true; + StartCoroutine(LoadSceneDelayCo(sceneName, delay)); + } + + private IEnumerator LoadSceneDelayCo(string sceneName, float delay) + { + Debug.Log(this.name + " LoadSceneDelayCo: starts loading " + sceneName); + + float waited = 0; + while (waited < delay) { + if (!_levelManager.GetGameStateManager.GamePaused) { + // should not count delay while game paused + waited += Time.unscaledDeltaTime; + } + + yield return null; + } + + yield return new WaitWhile(() => _levelManager.GetGameStateManager.GamePaused); + + Debug.Log(this.name + " LoadSceneDelayCo: done loading " + sceneName); + + _levelManager.GetPlayerAbilities.IsRespawning = false; + _levelManager.GetPlayerAbilities.IsPoweringDown = false; + SceneManager.LoadScene(sceneName); + } + + public void LoadLevel(string sceneName, float delay = LoadSceneDelayTime) + { + IGameStateManager levelManagerGetGameStateManager = _levelManager.GetGameStateManager; + levelManagerGetGameStateManager.GetSaveGameState(); + levelManagerGetGameStateManager.ConfigNewLevel(); + levelManagerGetGameStateManager.SceneToLoad = sceneName; + LoadSceneDelay("Level Start Screen", delay); + } + + public void LoadSceneCurrentLevel(string sceneName, float delay = LoadSceneDelayTime) + { + IGameStateManager levelManagerGetGameStateManager = _levelManager.GetGameStateManager; + levelManagerGetGameStateManager.GetSaveGameState(); + levelManagerGetGameStateManager.ResetSpawnPosition(); // TODO + LoadSceneDelay(sceneName, delay); + } + + public void LoadSceneCurrentLevelSetSpawnPipe(string sceneName, int spawnPipeIdx, + float delay = LoadSceneDelayTime) + { + IGameStateManager levelManagerGetGameStateManager = _levelManager.GetGameStateManager; + levelManagerGetGameStateManager.GetSaveGameState(); + levelManagerGetGameStateManager.SetSpawnPipe(spawnPipeIdx); + LoadSceneDelay(sceneName, delay); + Debug.Log(this.name + " LoadSceneCurrentLevelSetSpawnPipe: supposed to load " + sceneName + + ", spawnPipeIdx=" + spawnPipeIdx.ToString() + "; actual GSM spawnFromPoint=" + + levelManagerGetGameStateManager.SpawnFromPoint.ToString() + ", spawnPipeIdx=" + + levelManagerGetGameStateManager.SpawnPipeIdx.ToString()); + } + + public void ReloadCurrentLevel(float delay = LoadSceneDelayTime, bool timeUp = false) + { + IGameStateManager levelManagerGetGameStateManager = _levelManager.GetGameStateManager; + levelManagerGetGameStateManager.GetSaveGameState(); + levelManagerGetGameStateManager.ConfigReplayedLevel(); + levelManagerGetGameStateManager.SceneToLoad = SceneManager.GetActiveScene().name; + LoadSceneDelay(timeUp ? "Time Up Screen" : "Level Start Screen", delay); + } + + public void LoadGameOver(float delay = LoadSceneDelayTime, bool timeUp = false) + { + int currentHighScore = PlayerPrefs.GetInt("highScore", 0); + if (_levelManager.GetHUD.Scores > currentHighScore) { + PlayerPrefs.SetInt("highScore", _levelManager.GetHUD.Scores); + } + + _levelManager.GetGameStateManager.TimeUp = timeUp; + LoadSceneDelay("Game Over Screen", delay); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelSceneHandle.cs.meta b/Assets/Scripts/Level/LevelSceneHandle.cs.meta new file mode 100644 index 0000000..9982027 --- /dev/null +++ b/Assets/Scripts/Level/LevelSceneHandle.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: a4c34187099f4992aa4100a92a21daa4 +timeCreated: 1659034004 \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelServices.cs b/Assets/Scripts/Level/LevelServices.cs new file mode 100644 index 0000000..15f5f3a --- /dev/null +++ b/Assets/Scripts/Level/LevelServices.cs @@ -0,0 +1,71 @@ +using System.Text.RegularExpressions; +using Core.Managers; +using Interfaces.Core.Managers; +using Interfaces.Level; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace Level +{ + public class LevelServices : MonoBehaviour, ILevelServices + { + private ILevelManager _levelManager; + + private void Awake() + { + _levelManager = GetComponent(); + } + + public Vector3 FindSpawnPosition() + { + Vector3 spawnPosition; + GameStateManager gameStateManager = FindObjectOfType(); + Debug.Log(this.name + " FindSpawnPosition: GSM spawnFromPoint=" + + gameStateManager.SpawnFromPoint.ToString() + + " spawnPipeIdx= " + gameStateManager.SpawnPipeIdx.ToString() + + " spawnPointIdx=" + gameStateManager.SpawnPointIdx.ToString()); + if (gameStateManager.SpawnFromPoint) { + spawnPosition = GameObject.Find("Spawn Points").transform.GetChild(gameStateManager.SpawnPointIdx) + .transform.position; + } else { + spawnPosition = GameObject.Find("Spawn Pipes").transform.GetChild(gameStateManager.SpawnPipeIdx) + .transform.Find("Spawn Pos").transform.position; + } + + return spawnPosition; + } + + public string GetWorldName(string sceneName) + { + string[] sceneNameParts = Regex.Split(sceneName, " - "); + return sceneNameParts[0]; + } + + public bool IsSceneInCurrentWorld(string sceneName) + { + return GetWorldName(sceneName) == GetWorldName(SceneManager.GetActiveScene().name); + } + + public void MarioCompleteCastle() + { + _levelManager.GetGameStateManager.TimerPaused = true; + _levelManager.GetSoundManager.GetSoundLevelHandle.ChangeMusic(_levelManager.GetSoundManager.CastleCompleteMusic); + _levelManager.GetSoundManager.MusicSource.loop = false; + _levelManager.GetPlayerController.AutomaticWalk(_levelManager.GetPlayerController.CastleWalkSpeedX); + } + + public void MarioCompleteLevel() + { + _levelManager.GetGameStateManager.TimerPaused = true; + _levelManager.GetSoundManager.GetSoundLevelHandle.ChangeMusic(_levelManager.GetSoundManager.LevelCompleteMusic); + _levelManager.GetSoundManager.MusicSource.loop = false; + } + + public void MarioReachFlagPole() + { + _levelManager.GetGameStateManager.TimerPaused = true; + _levelManager.GetSoundManager.GetSoundLevelHandle.PauseMusicPlaySound(_levelManager.GetSoundManager.FlagpoleSound, false); + _levelManager.GetPlayerController.ClimbFlagPole(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/LevelServices.cs.meta b/Assets/Scripts/Level/LevelServices.cs.meta new file mode 100644 index 0000000..ebe634d --- /dev/null +++ b/Assets/Scripts/Level/LevelServices.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: ec26d0a421514f7494efe4820aef7735 +timeCreated: 1659248364 \ No newline at end of file diff --git a/Assets/Scripts/Level/PipeWarpDown.cs b/Assets/Scripts/Level/PipeWarpDown.cs index 8214f2d..1a949a6 100644 --- a/Assets/Scripts/Level/PipeWarpDown.cs +++ b/Assets/Scripts/Level/PipeWarpDown.cs @@ -25,8 +25,8 @@ private void FixedUpdate() { if (!_isMoving) return; if (transform.position.y > _stop.position.y) { - if (!_levelManager.timerPaused) { - _levelManager.timerPaused = true; + if (!_levelManager.GetGameStateManager.TimerPaused) { + _levelManager.GetGameStateManager.TimerPaused = true; } Transform transformCached = transform; @@ -35,9 +35,9 @@ private void FixedUpdate() transformCached.position = position; } else { if (leadToSameLevel) { - _levelManager.LoadSceneCurrentLevel (sceneName); + _levelManager.GetLoadLevelSceneHandler.LoadSceneCurrentLevel (sceneName); } else { - _levelManager.LoadNewLevel (sceneName); + _levelManager.GetLoadLevelSceneHandler.LoadLevel(sceneName); } } } diff --git a/Assets/Scripts/Level/PipeWarpSide.cs b/Assets/Scripts/Level/PipeWarpSide.cs index bdb15f8..c51ad96 100644 --- a/Assets/Scripts/Level/PipeWarpSide.cs +++ b/Assets/Scripts/Level/PipeWarpSide.cs @@ -4,53 +4,48 @@ namespace Level { - public class PipeWarpSide : MonoBehaviour { - private LevelManager t_LevelManager; - private PlayerController playerController; - private bool reachedPortal; + public class PipeWarpSide : MonoBehaviour + { + private LevelManager _levelManager; + private PlayerController _playerController; + private bool _reachedPortal; - public string sceneName; - public int spawnPipeIdx; - public bool leadToSameLevel = true; + public string sceneName; + public int spawnPipeIdx; + public bool leadToSameLevel = true; + private void Start() + { + _levelManager = FindObjectOfType(); + _playerController = FindObjectOfType(); + } - // Use this for initialization - void Start () { - t_LevelManager = FindObjectOfType (); - playerController = FindObjectOfType (); - } - - // Update is called once per frame - void Update () { - - } + private void OnTriggerEnter2D(Collider2D other) + { + if (!other.CompareTag("Player")) return; + _playerController.AutomaticWalk(_playerController.LevelEntryWalkSpeedX); + _reachedPortal = true; + _levelManager.GetGameStateManager.TimerPaused = true; + Debug.Log(this.name + " OnTriggerEnter2D: " + transform.parent.gameObject.name + + " recognizes player, should automatic walk"); + } - void OnTriggerEnter2D(Collider2D other) { - if (other.tag == "Player") { - playerController.AutomaticWalk (playerController.LevelEntryWalkSpeedX); - reachedPortal = true; - t_LevelManager.timerPaused = true; - Debug.Log (this.name + " OnTriggerEnter2D: " + transform.parent.gameObject.name - + " recognizes player, should automatic walk"); - } - } + private void OnCollisionEnter2D(Collision2D other) + { + if (!other.gameObject.CompareTag("Player") || !_reachedPortal) return; + _levelManager.GetSoundManager.SoundSource.PlayOneShot(_levelManager.GetSoundManager.PipePowerdownSound); - void OnCollisionEnter2D(Collision2D other) { - if (other.gameObject.tag == "Player" && reachedPortal) { - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.PipePowerdownSound); - - if (leadToSameLevel) { - Debug.Log (this.name + " OnCollisionEnter2D: " + transform.parent.gameObject.name - + " teleports player to different scene same level " + sceneName - + ", pipe idx " + spawnPipeIdx); - t_LevelManager.LoadSceneCurrentLevelSetSpawnPipe (sceneName, spawnPipeIdx); - } else { - Debug.Log (this.name + " OnCollisionEnter2D: " + transform.parent.gameObject.name - + " teleports player to new level " + sceneName - + ", pipe idx " + spawnPipeIdx); - t_LevelManager.LoadNewLevel (sceneName); - } - } - } - } -} + if (leadToSameLevel) { + Debug.Log(this.name + " OnCollisionEnter2D: " + transform.parent.gameObject.name + + " teleports player to different scene same level " + sceneName + + ", pipe idx " + spawnPipeIdx); + _levelManager.GetLoadLevelSceneHandler.LoadSceneCurrentLevelSetSpawnPipe(sceneName, spawnPipeIdx); + } else { + Debug.Log(this.name + " OnCollisionEnter2D: " + transform.parent.gameObject.name + + " teleports player to new level " + sceneName + + ", pipe idx " + spawnPipeIdx); + _levelManager.GetLoadLevelSceneHandler.LoadLevel(sceneName); + } + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/PipeWarpUp.cs b/Assets/Scripts/Level/PipeWarpUp.cs index 98e3d90..a03dfae 100644 --- a/Assets/Scripts/Level/PipeWarpUp.cs +++ b/Assets/Scripts/Level/PipeWarpUp.cs @@ -1,58 +1,60 @@ -using Core.Managers; +using System; +using Core.Managers; using Core.Player; +using Interfaces.Core.Managers; using UnityEngine; namespace Level { - public class PipeWarpUp : MonoBehaviour { - private PlayerController mario; - private Transform stop; + public class PipeWarpUp : MonoBehaviour + { + private PlayerController _mario; + private Transform _stop; - private float platformVelocityY = .05f; + private const float PlatformVelocityY = .05f; public bool isTakingMarioUp; - private LevelManager t_LevelManager; + private ILevelManager _levelManager; - public bool resetSpawnPoint = false; + public bool resetSpawnPoint; - void Start() { - mario = FindObjectOfType(); - stop = transform.parent.transform.Find("Platform Stop"); - GameStateManager _gameStateManager = FindObjectOfType(); - t_LevelManager = FindObjectOfType(); + private void Start() + { + _mario = FindObjectOfType(); + _stop = transform.parent.transform.Find("Platform Stop"); + _levelManager = FindObjectOfType(); Debug.Log(this.name + " Start: " + transform.parent.gameObject.name - + " spawnFromPoint=" + _gameStateManager.SpawnFromPoint.ToString() - + " with idx=" + _gameStateManager.SpawnPipeIdx.ToString()); + + " spawnFromPoint=" + _levelManager.GetGameStateManager.SpawnFromPoint.ToString() + + " with idx=" + _levelManager.GetGameStateManager.SpawnPipeIdx.ToString()); - if (!_gameStateManager.SpawnFromPoint && - _gameStateManager.SpawnPipeIdx == transform.parent.GetSiblingIndex()) { + if (!_levelManager.GetGameStateManager.SpawnFromPoint && + _levelManager.GetGameStateManager.SpawnPipeIdx == transform.parent.GetSiblingIndex()) { isTakingMarioUp = true; - mario.FreezeUserInput(); - t_LevelManager.timerPaused = true; + _mario.FreezeUserInput(); + _levelManager.GetGameStateManager.TimerPaused = true; Debug.Log(this.name + " Start: " + transform.parent.gameObject.name + " taking Mario up"); } else { isTakingMarioUp = false; - transform.position = stop.position; + transform.position = _stop.position; Debug.Log(this.name + " Start: " + transform.parent.gameObject.name + " not taking Mario up"); } } - void FixedUpdate() { - if (isTakingMarioUp) { - if (transform.position.y < stop.position.y) { - transform.position = new Vector2(transform.position.x, transform.position.y + platformVelocityY); - } else if (t_LevelManager.timerPaused) { - GameStateManager _gameStateManager = FindObjectOfType(); - _gameStateManager.SpawnFromPoint = true; - if (resetSpawnPoint) { - _gameStateManager.ResetSpawnPosition(); - } - - mario.UnfreezeUserInput(); - t_LevelManager.timerPaused = false; - isTakingMarioUp = false; + private void FixedUpdate() + { + if (!isTakingMarioUp) return; + if (transform.position.y < _stop.position.y) { + transform.position = new Vector2(transform.position.x, transform.position.y + PlatformVelocityY); + } else if (_levelManager.GetGameStateManager.TimerPaused) { + _levelManager.GetGameStateManager.SpawnFromPoint = true; + if (resetSpawnPoint) { + _levelManager.GetGameStateManager.ResetSpawnPosition(); } + + _mario.UnfreezeUserInput(); + _levelManager.GetGameStateManager.TimerPaused = false; + isTakingMarioUp = false; } } } diff --git a/Assets/Scripts/Level/RegularBrickBlock.cs b/Assets/Scripts/Level/RegularBrickBlock.cs index 1044c3d..1fc8b54 100644 --- a/Assets/Scripts/Level/RegularBrickBlock.cs +++ b/Assets/Scripts/Level/RegularBrickBlock.cs @@ -11,7 +11,7 @@ public class RegularBrickBlock : MonoBehaviour { public GameObject BlockCoin; private float WaitBetweenBounce = .25f; - private LevelManager t_LevelManager; + private LevelManager _levelManager; private Animator m_Animator; private RegularBrickBlockCoinDetector m_CoinDetector; @@ -21,7 +21,7 @@ public class RegularBrickBlock : MonoBehaviour { // Use this for initialization void Start () { - t_LevelManager = FindObjectOfType (); + _levelManager = FindObjectOfType (); m_Animator = GetComponent (); m_CoinDetector = transform.parent.Find ("Coin Detector").GetComponent (); time1 = 0; @@ -33,7 +33,7 @@ void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player" && time2-time1 >= WaitBetweenBounce) { // Hit any enemy on top foreach (GameObject enemyObj in enemiesOnTop) { - t_LevelManager.BlockHitEnemy (enemyObj.GetComponent ()); + _levelManager.GetPlayerAbilities.BlockHitEnemy (enemyObj.GetComponent ()); } // check and collect coins on top @@ -43,13 +43,13 @@ void OnTriggerEnter2D(Collider2D other) { } // Bounce or break depending on Mario's size - if (t_LevelManager.marioSize == 0) { + if (_levelManager.GetGameStateManager.PlayerSize == 0) { m_Animator.SetTrigger ("bounce"); - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BumpSound); + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BumpSound); } else { BreakIntoPieces (); - t_LevelManager.AddScore(t_LevelManager.breakBlockBonus); - t_LevelManager.GetSoundManager.SoundSource.PlayOneShot (t_LevelManager.GetSoundManager.BreakBlockSound); + _levelManager.GetPlayerPickUpAbilities.AddScore(_levelManager.GetGameStateManager.BreakBlockBonus); + _levelManager.GetSoundManager.SoundSource.PlayOneShot (_levelManager.GetSoundManager.BreakBlockSound); } time1 = Time.time; } diff --git a/Assets/Scripts/Level/SetLevelHUD.cs b/Assets/Scripts/Level/SetLevelHUD.cs new file mode 100644 index 0000000..2bf839d --- /dev/null +++ b/Assets/Scripts/Level/SetLevelHUD.cs @@ -0,0 +1,82 @@ +using Interfaces.Core.Managers; +using Interfaces.UI; +using UnityEngine; +using UnityEngine.UI; + +namespace Level +{ + public class SetLevelHUD : MonoBehaviour, IHUD + { + [SerializeField] private Text scoreText; + [SerializeField] private Text coinText; + [SerializeField] private Text timeText; + [SerializeField] private GameObject floatingTextEffect; + private const float FloatingTextOffsetY = 2f; + + + public Text ScoreText => scoreText; + public Text CoinText => coinText; + public Text TimeText => timeText; + + private ILevelManager _levelManager; + + private void Awake() + { + _levelManager = GetComponent(); + } + + public int Coins + { + get => _levelManager.GetGameStateManager.Coins; + set => _levelManager.GetGameStateManager.Coins = value; + } + + public int Scores + { + get => _levelManager.GetGameStateManager.Scores; + set => _levelManager.GetGameStateManager.Scores = value; + } + + public float TimeLeft + { + get => _levelManager.GetGameStateManager.TimeLeft; + set => _levelManager.GetGameStateManager.TimeLeft = value; + } + + public int TimeLeftInt + { + get => _levelManager.GetGameStateManager.TimeLeftInt; + set => _levelManager.GetGameStateManager.TimeLeftInt = value; + } + + public void SetHUD() + { + SetHudCoin(); + SetHudScore(); + SetHudTime(); + } + + public void SetHudCoin() + { + coinText.text = "x" + _levelManager.GetGameStateManager.Coins.ToString("D2"); + } + + public void SetHudScore() + { + scoreText.text = _levelManager.GetGameStateManager.Scores.ToString("D6"); + } + + public void SetHudTime() + { + _levelManager.GetGameStateManager.TimeLeftInt = + Mathf.RoundToInt(_levelManager.GetGameStateManager.TimeLeft); + timeText.text = _levelManager.GetGameStateManager.TimeLeftInt.ToString("D3"); + } + + public void CreateFloatingText(string text, Vector3 spawnPos) + { + GameObject textEffect = Instantiate(floatingTextEffect, spawnPos, Quaternion.identity); + textEffect.GetComponentInChildren().text = text.ToUpper(); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Level/SetLevelHUD.cs.meta b/Assets/Scripts/Level/SetLevelHUD.cs.meta new file mode 100644 index 0000000..9d33043 --- /dev/null +++ b/Assets/Scripts/Level/SetLevelHUD.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 6ade286070354de3b9d0016e59d3514c +timeCreated: 1659098774 \ No newline at end of file diff --git a/Assets/Scripts/UI/MainCameraFollowNoBackwards.cs b/Assets/Scripts/UI/MainCameraFollowNoBackwards.cs index e49fdd5..f9f109c 100644 --- a/Assets/Scripts/UI/MainCameraFollowNoBackwards.cs +++ b/Assets/Scripts/UI/MainCameraFollowNoBackwards.cs @@ -36,7 +36,7 @@ public void GetCameraPrerequisites(MainCamera mainCamera) public void InitializeCameraPosition(MainCamera mainCamera) { - Vector3 spawnPosition = FindObjectOfType().FindSpawnPosition(); + Vector3 spawnPosition = FindObjectOfType().GetLevelServices.FindSpawnPosition(); _targetPosition = new Vector3(spawnPosition.x, transform.position.y, transform.position.z); bool passedLeftEdge = _targetPosition.x < _leftEdge.position.x + _cameraWidth; diff --git a/Assets/Scripts/UI/MainCameraFollowWithBackwards.cs b/Assets/Scripts/UI/MainCameraFollowWithBackwards.cs index 144a22c..cc15ca4 100644 --- a/Assets/Scripts/UI/MainCameraFollowWithBackwards.cs +++ b/Assets/Scripts/UI/MainCameraFollowWithBackwards.cs @@ -35,7 +35,7 @@ public void GetCameraPrerequisites(MainCamera mainCamera) public void InitializeCameraPosition(MainCamera mainCamera) { - Vector3 spawnPosition = FindObjectOfType().FindSpawnPosition(); + Vector3 spawnPosition = FindObjectOfType().GetLevelServices.FindSpawnPosition(); _targetPosition = new Vector3(spawnPosition.x, transform.position.y, transform.position.z); bool passedLeftEdge = _targetPosition.x < _leftEdge.position.x + _cameraWidth;