From d0a38b3fe478b538415fc7c82477a8a645977d51 Mon Sep 17 00:00:00 2001 From: cuqmbr Date: Sat, 19 Mar 2022 19:07:47 +0200 Subject: [PATCH] feat: hook up player script to the game state manager --- Assets/{Prefabs.meta => Resources.meta} | 2 +- Assets/Sprites.meta | 8 ---- .../GameStateManager/GameStateController.cs | 2 + .../PlayerScripts/PlayerCollisions.cs | 2 +- .../PlayerScripts/PlayerController.cs | 40 ++++++++++++++++++- Assets/_Scripts/PlayerScripts/PlayerEvents.cs | 4 -- 6 files changed, 42 insertions(+), 16 deletions(-) rename Assets/{Prefabs.meta => Resources.meta} (77%) delete mode 100644 Assets/Sprites.meta diff --git a/Assets/Prefabs.meta b/Assets/Resources.meta similarity index 77% rename from Assets/Prefabs.meta rename to Assets/Resources.meta index ed99b12..17f2f49 100644 --- a/Assets/Prefabs.meta +++ b/Assets/Resources.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: a0c6f5ae91978603eb6c7d5f18a02582 +guid: 7edc139ff9324963d921e95b35cf4afa folderAsset: yes DefaultImporter: externalObjects: {} diff --git a/Assets/Sprites.meta b/Assets/Sprites.meta deleted file mode 100644 index 4575f64..0000000 --- a/Assets/Sprites.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: c113a98e533b85e3ebb1ac64459822a0 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/_Scripts/Managers/GameStateManager/GameStateController.cs b/Assets/_Scripts/Managers/GameStateManager/GameStateController.cs index 93dd11c..c95dbe8 100644 --- a/Assets/_Scripts/Managers/GameStateManager/GameStateController.cs +++ b/Assets/_Scripts/Managers/GameStateManager/GameStateController.cs @@ -13,6 +13,8 @@ public class GameStateController : MonoBehaviour { //Change game state to selected in inspector state when the game starts GameStateManager.Instance.ChangeState(ChangeToState); + + PlayerEvents.OnBallTouched += () => GameStateManager.Instance.ChangeState(GameState.Game); } private void OnApplicationQuit() diff --git a/Assets/_Scripts/PlayerScripts/PlayerCollisions.cs b/Assets/_Scripts/PlayerScripts/PlayerCollisions.cs index 15e9847..ac3c952 100644 --- a/Assets/_Scripts/PlayerScripts/PlayerCollisions.cs +++ b/Assets/_Scripts/PlayerScripts/PlayerCollisions.cs @@ -12,7 +12,7 @@ public class PlayerCollisions : MonoBehaviour PlayerEvents.SendWallTouched(); break; case "Floor": - PlayerEvents.SendFloorTouched(); + GameStateManager.Instance.ChangeState(GameState.GameOver); break; } } diff --git a/Assets/_Scripts/PlayerScripts/PlayerController.cs b/Assets/_Scripts/PlayerScripts/PlayerController.cs index 9d12dd4..66fea33 100644 --- a/Assets/_Scripts/PlayerScripts/PlayerController.cs +++ b/Assets/_Scripts/PlayerScripts/PlayerController.cs @@ -15,6 +15,10 @@ public class PlayerController : MonoBehaviour private float _initialPositionY; private float _initialGravityScale; + [Header("Start position randomization")] + [SerializeField] private Vector2 _startPosition; + [Range(0f, 0.5f)] [SerializeField] private float _horisontalRadius; + [Header("Miscellaneous")] [SerializeField] private GameObject _touchTrigger; [SerializeField] private ScoreManager _scoreManager; @@ -23,8 +27,14 @@ public class PlayerController : MonoBehaviour { _rigidbody = GetComponent(); - _initialPositionY = transform.position.y; + _initialPositionY = _startPosition.y; _initialGravityScale = _rigidbody.gravityScale; + + //Calculate start position (target position of ball spawning animation) + _startPosition = new Vector2(_startPosition.x + Random.Range(-_horisontalRadius, _horisontalRadius), _startPosition.y); + transform.position = new Vector2(_startPosition.x, transform.position.y); + + GameStateManager.Instance.OnGameStateChange += OnGameStateChange; } public void OnTouch(Vector2 pointerPos) @@ -49,9 +59,35 @@ public class PlayerController : MonoBehaviour PlayerEvents.SendBallTouched(); } + private void OnGameStateChange(GameState newGameState) + { + switch (newGameState) + { + case GameState.Menu: + _rigidbody.simulated = false; + _rigidbody.velocity = Vector2.zero; + _rigidbody.angularVelocity = 0; + break; + case GameState.Game: + _rigidbody.simulated = true; + break; + case GameState.GameOver: + _rigidbody.simulated = false; + break; + } + } + private void Update() { //Change gravitational force over height - _rigidbody.gravityScale = _initialGravityScale + ((transform.position.y - _initialPositionY) * _gravityMultiplier); + if (GameStateManager.Instance.CurrentGameState == GameState.Game && transform.position.y > _startPosition.y) + _rigidbody.gravityScale = _initialGravityScale + ((transform.position.y - _initialPositionY) * _gravityMultiplier); + } + + private void FixedUpdate() + { + //Ball spawn animation + if (GameStateManager.Instance.CurrentGameState == GameState.Menu) + transform.position = Vector3.Lerp(transform.position, _startPosition, 0.15f); } } diff --git a/Assets/_Scripts/PlayerScripts/PlayerEvents.cs b/Assets/_Scripts/PlayerScripts/PlayerEvents.cs index f86311d..7925e23 100644 --- a/Assets/_Scripts/PlayerScripts/PlayerEvents.cs +++ b/Assets/_Scripts/PlayerScripts/PlayerEvents.cs @@ -6,11 +6,7 @@ public class PlayerEvents public static event Action OnWallTouched; - public static event Action OnFloorTouched; - public static void SendBallTouched() => OnBallTouched?.Invoke(); public static void SendWallTouched() => OnWallTouched?.Invoke(); - - public static void SendFloorTouched() => OnFloorTouched?.Invoke(); }