Implement a way to stop player and enemies from moving

This commit is contained in:
Denis-Cosmin Nutiu 2019-07-16 20:07:10 +03:00
parent 929045a7b9
commit a8320b2b3f
5 changed files with 20 additions and 3 deletions

View file

@ -1,10 +1,12 @@
using UnityEngine;
using src.Helpers;
using src.Managers;
public abstract class EnemyBase : MonoBehaviour, IExplosable
{
protected Vector2[] _directions = { Vector3.up, Vector3.down, Vector3.left, Vector3.right };
protected readonly GameStateManager gameStateManager = GameStateManager.Instance;
protected Rigidbody2D Rigidbody2d { get; set; }
protected float Speed { get; set; }
@ -20,6 +22,7 @@ public abstract class EnemyBase : MonoBehaviour, IExplosable
// Update is called once per frame
protected void FixedUpdate()
{
if (gameStateManager.IsGamePaused || gameStateManager.IsPlayerMovementForbidden) {return;}
Rigidbody2d.MovePosition(Rigidbody2d.position + Direction * Speed * Time.deltaTime);
}

View file

@ -11,6 +11,7 @@ public class DumbEnemy : EnemyBase
protected new void FixedUpdate()
{
if (gameStateManager.IsGamePaused || gameStateManager.IsPlayerMovementForbidden) {return;}
if (transform.position.x == Mathf.Floor(transform.position.x) &&
transform.position.y == Mathf.Floor(transform.position.y))
{

View file

@ -35,5 +35,15 @@ namespace src.Helpers
PauseGame();
}
}
public static void ForbidPlayerMovement()
{
_gameStateManager.IsPlayerMovementForbidden = true;
}
public static void AllowPlayerMovement()
{
_gameStateManager.IsPlayerMovementForbidden = false;
}
}
}

View file

@ -4,6 +4,7 @@ namespace src.Managers
{
public static GameStateManager Instance { get; } = new GameStateManager();
public bool IsGamePaused { get; internal set; }
public bool IsPlayerMovementForbidden { get; internal set; }
public int Level { get; private set; } = 1;
@ -11,5 +12,7 @@ namespace src.Managers
{
Level += 1;
}
}
}

View file

@ -35,13 +35,13 @@ namespace src.Player
private void FixedUpdate()
{
if (_gameStateManager.IsGamePaused) {return;}
if (_gameStateManager.IsGamePaused || _gameStateManager.IsPlayerMovementForbidden) {return;}
HandleMovement();
}
private void Update()
{
if (_gameStateManager.IsGamePaused) {return;}
if (_gameStateManager.IsGamePaused || _gameStateManager.IsPlayerMovementForbidden) {return;}
HandleBomb();
}