feat: hook up player script to the game state manager

This commit is contained in:
cuqmbr 2022-03-19 19:07:47 +02:00
parent cb41bb6841
commit d0a38b3fe4
6 changed files with 42 additions and 16 deletions

View File

@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a0c6f5ae91978603eb6c7d5f18a02582
guid: 7edc139ff9324963d921e95b35cf4afa
folderAsset: yes
DefaultImporter:
externalObjects: {}

8
Assets/Sprites.meta generated
View File

@ -1,8 +0,0 @@
fileFormatVersion: 2
guid: c113a98e533b85e3ebb1ac64459822a0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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()

View File

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

View File

@ -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<Rigidbody2D>();
_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);
}
}

View File

@ -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();
}