Turn all Singletons into GameObjects

This commit is contained in:
Denis-Cosmin Nutiu 2019-08-24 00:29:38 +03:00
parent 22830ee4be
commit c8059dcf24
24 changed files with 119 additions and 98 deletions

View file

@ -10,8 +10,6 @@ GameObject:
m_Component: m_Component:
- component: {fileID: 158905820630581587} - component: {fileID: 158905820630581587}
- component: {fileID: 158905820630581586} - component: {fileID: 158905820630581586}
- component: {fileID: 158905820630581596}
- component: {fileID: 6316647355651322741}
m_Layer: 0 m_Layer: 0
m_Name: GameManager m_Name: GameManager
m_TagString: Untagged m_TagString: Untagged
@ -47,31 +45,3 @@ MonoBehaviour:
m_EditorClassIdentifier: m_EditorClassIdentifier:
preStageUiPrefab: {fileID: 4994633962466276707, guid: 1f5ebc13237e070449ea8bc9e8b07ac8, preStageUiPrefab: {fileID: 4994633962466276707, guid: 1f5ebc13237e070449ea8bc9e8b07ac8,
type: 3} type: 3}
--- !u!114 &158905820630581596
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 158905820630581597}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 308dfc6b26c3b422faf1717231fc888b, type: 3}
m_Name:
m_EditorClassIdentifier:
boardHolder: {fileID: 0}
startPosition: {fileID: 0}
indestructibleWallPrefab: {fileID: 5996206011854593075, guid: 84b92ac1e96d940bda97d1b1125887d7,
type: 3}
--- !u!114 &6316647355651322741
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 158905820630581597}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: e64481d2e18e4b6ca4f22926a5061450, type: 3}
m_Name:
m_EditorClassIdentifier:

View file

@ -272,13 +272,11 @@ PrefabInstance:
propertyPath: m_LocalEulerAnglesHint.z propertyPath: m_LocalEulerAnglesHint.z
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 158905820630581596, guid: fdaa7451ab31b477b930076bbc888bab, - target: {fileID: 0}
type: 3}
propertyPath: boardHolder propertyPath: boardHolder
value: value:
objectReference: {fileID: 585332173} objectReference: {fileID: 585332173}
- target: {fileID: 158905820630581596, guid: fdaa7451ab31b477b930076bbc888bab, - target: {fileID: 0}
type: 3}
propertyPath: startPosition propertyPath: startPosition
value: value:
objectReference: {fileID: 185746015} objectReference: {fileID: 185746015}

View file

@ -8,7 +8,7 @@ namespace src.Ammo
{ {
public class BombCameraShake : GameplayComponent public class BombCameraShake : GameplayComponent
{ {
private readonly BombsUtilManager _bombsUtilManager = BombsUtilManager.Instance; private BombsUtilManager _bombsUtilManager;
public float amplitudeGain = 3f; public float amplitudeGain = 3f;
public float frequencyGain = 3f; public float frequencyGain = 3f;
@ -18,6 +18,7 @@ namespace src.Ammo
// Start is called before the first frame update // Start is called before the first frame update
private void Start() private void Start()
{ {
_bombsUtilManager = BombsUtilManager.instance;
_virtualCamera = GetComponent<CinemachineVirtualCamera>(); _virtualCamera = GetComponent<CinemachineVirtualCamera>();
_noiseMachine = _virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>(); _noiseMachine = _virtualCamera.GetCinemachineComponent<CinemachineBasicMultiChannelPerlin>();
} }

View file

@ -11,7 +11,7 @@ namespace src.Ammo
public GameObject explosionPrefab; public GameObject explosionPrefab;
private BombCameraShake _cameraShake; private BombCameraShake _cameraShake;
private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance; private readonly BombsUtilManager _bombsUtil = BombsUtilManager.instance;
private bool _exploded; private bool _exploded;
// Start is called before the first frame update // Start is called before the first frame update

View file

@ -1,4 +1,5 @@
using src.Base; using System;
using src.Base;
using src.Managers; using src.Managers;
using UnityEngine; using UnityEngine;
@ -8,12 +9,18 @@ namespace src.Ammo
{ {
public GameObject bombPrefab; public GameObject bombPrefab;
private readonly BombsUtilManager _bombsUtil = BombsUtilManager.Instance; private BombsUtilManager _bombsUtil;
public void PlaceBomb(Transform transform) public void Start()
{ {
var absX = Mathf.RoundToInt(transform.position.x); _bombsUtil = BombsUtilManager.instance;
var absY = Mathf.RoundToInt(transform.position.y); }
public void PlaceBomb(Transform location)
{
var position1 = location.position;
var absX = Mathf.RoundToInt(position1.x);
var absY = Mathf.RoundToInt(position1.y);
var position = new Vector2(absX, absY); var position = new Vector2(absX, absY);
if (_bombsUtil.CanPlaceBomb(position)) if (_bombsUtil.CanPlaceBomb(position))
{ {

View file

@ -5,7 +5,7 @@ namespace src.Ammo
{ {
public class Explosion : GameplayComponent public class Explosion : GameplayComponent
{ {
private readonly BombsUtilManager _bombUtil = BombsUtilManager.Instance; private readonly BombsUtilManager _bombUtil = BombsUtilManager.instance;
public void Start() public void Start()
{ {

View file

@ -6,10 +6,10 @@ using UnityEngine;
namespace src.Base namespace src.Base
{ {
public abstract class EnemyBase : MonoBehaviour, IExplosable public abstract class EnemyBase : GameplayComponent, IExplosable
{ {
private readonly Vector2[] _directions = {Vector3.up, Vector3.down, Vector3.left, Vector3.right}; private readonly Vector2[] _directions = {Vector3.up, Vector3.down, Vector3.left, Vector3.right};
private readonly GameStateManager _gameStateManager = GameStateManager.Instance; private readonly GameStateManager _gameStateManager = GameStateManager.instance;
protected Rigidbody2D Rigidbody2d { get; set; } protected Rigidbody2D Rigidbody2d { get; set; }
private Collider2D Collider2D { get; set; } private Collider2D Collider2D { get; set; }

View file

@ -6,13 +6,13 @@ namespace src.Base
{ {
public class UpgradeBase : GameplayComponent, IUpgrade public class UpgradeBase : GameplayComponent, IUpgrade
{ {
protected GameManager GameManager; protected GameManager gameManager;
private UpgradeManager _upgradeManager; private UpgradeManager _upgradeManager;
public void Start() public void Start()
{ {
GameManager = GameManager.Instance; gameManager = GameManager.instance;
_upgradeManager = UpgradeManager.Instance; _upgradeManager = UpgradeManager.instance;
} }
public virtual void PerformUpgrade() public virtual void PerformUpgrade()

View file

@ -5,7 +5,7 @@ namespace src.Helpers
{ {
public static class ApplicationActions public static class ApplicationActions
{ {
private static GameStateManager _gameStateManager = GameStateManager.Instance; private static GameStateManager _gameStateManager = GameStateManager.instance;
public static void QuitGame() public static void QuitGame()
{ {

View file

@ -4,6 +4,10 @@ namespace src.Helpers
{ {
public static class PrefabAtlas public static class PrefabAtlas
{ {
/* UI */
public static readonly GameObject PreStageUi =
Resources.Load<GameObject>("UI/PreStageUI");
/* Snow Walls */ /* Snow Walls */
public static readonly GameObject DestructibleHighSnow = public static readonly GameObject DestructibleHighSnow =
Resources.Load<GameObject>("Walls/destructible_high_snow"); Resources.Load<GameObject>("Walls/destructible_high_snow");

View file

@ -34,10 +34,10 @@ namespace src.Level
} }
/* Used to group spawned objects */ /* Used to group spawned objects */
public Transform boardHolder; private Transform _boardHolder;
/* Holds the starting position of the player */ /* Holds the starting position of the player */
public Transform startPosition; private Transform _startPosition;
/* Holds references to prefabs for the specified level. */ /* Holds references to prefabs for the specified level. */
private GameObject _indestructibleWallPrefab; private GameObject _indestructibleWallPrefab;
@ -61,7 +61,14 @@ namespace src.Level
private List<GameObject> _enemies; private List<GameObject> _enemies;
/* Singletons */ /* Singletons */
private GameStateManager _gameStateManager = GameStateManager.Instance; private GameStateManager _gameStateManager;
public void Awake()
{
_startPosition = GameObject.Find("RespawnPosition").GetComponent<Transform>();
_boardHolder = GameObject.Find("Grid").GetComponent<Transform>();
_gameStateManager = GameStateManager.instance;
}
public void SetLevelData(LevelData levelData) public void SetLevelData(LevelData levelData)
{ {
@ -115,9 +122,9 @@ namespace src.Level
} }
/* We want to iterate over the X axis taking into consideration the startPosition's offset */ /* We want to iterate over the X axis taking into consideration the startPosition's offset */
for (var x = startPosition.position.x; x < Columns; x++) for (var x = _startPosition.position.x; x < Columns; x++)
{ {
for (var y = startPosition.position.y; y > Rows * -1; y--) for (var y = _startPosition.position.y; y > Rows * -1; y--)
{ {
/* We want the following positions to be a safe zone. */ /* We want the following positions to be a safe zone. */
/* Don't place anything on starting position */ /* Don't place anything on starting position */
@ -182,7 +189,7 @@ namespace src.Level
var randomWall = _destructibleWallPrefabs.ChoseRandom(); var randomWall = _destructibleWallPrefabs.ChoseRandom();
var instance = Instantiate(randomWall, position, Quaternion.identity); var instance = Instantiate(randomWall, position, Quaternion.identity);
_destructibleWalls.Add(instance); _destructibleWalls.Add(instance);
instance.transform.SetParent(boardHolder); instance.transform.SetParent(_boardHolder);
} }
private bool PlaceIndestructibleTile(float x, float y) private bool PlaceIndestructibleTile(float x, float y)
@ -198,7 +205,7 @@ namespace src.Level
var instance = var instance =
Instantiate(_indestructibleWallPrefab, new Vector3(x, y, 0f), Quaternion.identity); Instantiate(_indestructibleWallPrefab, new Vector3(x, y, 0f), Quaternion.identity);
instance.transform.SetParent(boardHolder); instance.transform.SetParent(_boardHolder);
return true; return true;
} }
@ -223,7 +230,7 @@ namespace src.Level
DebugHelper.LogVerbose($"PlaceEnemy: x:{position.x} y:{position.y}"); DebugHelper.LogVerbose($"PlaceEnemy: x:{position.x} y:{position.y}");
var randomEnemy = _enemiesPrefab.ChoseRandom(); var randomEnemy = _enemiesPrefab.ChoseRandom();
var instance = Instantiate(randomEnemy, position, Quaternion.identity); var instance = Instantiate(randomEnemy, position, Quaternion.identity);
instance.transform.SetParent(boardHolder); instance.transform.SetParent(_boardHolder);
_enemies.Add(instance); _enemies.Add(instance);
} }

View file

@ -1,9 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using src.Base;
using UnityEngine; using UnityEngine;
namespace src.Managers namespace src.Managers
{ {
public sealed class BombsUtilManager public sealed class BombsUtilManager : GameplayComponent
{ {
private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>(); private readonly HashSet<Vector3> _usedPosition = new HashSet<Vector3>();
@ -20,11 +21,19 @@ namespace src.Managers
public float ExplosionDuration { get; } = 0.55f; public float ExplosionDuration { get; } = 0.55f;
private BombsUtilManager() public static BombsUtilManager instance;
{
}
public static BombsUtilManager Instance { get; } = new BombsUtilManager(); public void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != null)
{
Destroy(gameObject);
}
}
public void IncreasePower() public void IncreasePower()
{ {

View file

@ -1,5 +1,4 @@
using System.Collections; using System.Collections;
using src.Ammo;
using src.Helpers; using src.Helpers;
using src.Level; using src.Level;
using src.Level.src.Level; using src.Level.src.Level;
@ -10,23 +9,22 @@ namespace src.Managers
{ {
public class GameManager : MonoBehaviour public class GameManager : MonoBehaviour
{ {
public static GameManager Instance; public static GameManager instance;
// Inner Components
private PlayerController _playerController;
private GameStateManager _gameStateManager;
private LevelManager _levelManager; private LevelManager _levelManager;
private UpgradeManager _upgradeManager; private UpgradeManager _upgradeManager;
private BombsUtilManager _bombsUtilManager; private BombsUtilManager _bombsUtilManager;
// External Components
public GameObject preStageUiPrefab;
private PlayerController _playerController;
private readonly GameStateManager _gameStateManager = GameStateManager.Instance;
public void Awake() public void Awake()
{ {
if (Instance == null) if (instance == null)
{ {
Instance = this; instance = this;
} }
else if (Instance != null) else if (instance != null)
{ {
Destroy(gameObject); Destroy(gameObject);
} }
@ -34,10 +32,12 @@ namespace src.Managers
/* Don't destroy when reloading the scene */ /* Don't destroy when reloading the scene */
DontDestroyOnLoad(gameObject); DontDestroyOnLoad(gameObject);
// Load inner components // Load singletons
_levelManager = GetComponent<LevelManager>(); _bombsUtilManager = gameObject.AddComponent<BombsUtilManager>();
_upgradeManager = GetComponent<UpgradeManager>(); _gameStateManager = gameObject.AddComponent<GameStateManager>();
_bombsUtilManager = BombsUtilManager.Instance; _levelManager = gameObject.AddComponent<LevelManager>();
_upgradeManager = gameObject.AddComponent<UpgradeManager>();
gameObject.AddComponent<PlayerUpgrade>();
// Load external components // Load external components
_playerController = GameObject.Find("Player").GetComponent<PlayerController>(); _playerController = GameObject.Find("Player").GetComponent<PlayerController>();
@ -72,7 +72,7 @@ namespace src.Managers
private IEnumerator PreInitGame() private IEnumerator PreInitGame()
{ {
var preStageUi = Instantiate(preStageUiPrefab); // Will destroy itself. var preStageUi = Instantiate(PrefabAtlas.PreStageUi); // Will destroy itself.
preStageUi.SetActive(true); preStageUi.SetActive(true);
yield return new WaitForSeconds(1f); yield return new WaitForSeconds(1f);
Destroy(preStageUi); Destroy(preStageUi);

View file

@ -1,12 +1,26 @@
using src.Base;
using UnityEngine;
namespace src.Managers namespace src.Managers
{ {
public class GameStateManager public class GameStateManager : GameplayComponent
{ {
public static GameStateManager Instance { get; } = new GameStateManager(); public static GameStateManager instance;
public bool IsGamePaused { get; internal set; } public bool IsGamePaused { get; internal set; }
public bool IsPlayerMovementForbidden { get; internal set; } public bool IsPlayerMovementForbidden { get; internal set; }
public int Level { get; private set; } = 1; public int Level { get; private set; } = 1;
public void Awake()
{
if (instance == null)
{
instance = this;
}
else if (instance != null)
{
Destroy(gameObject);
}
}
public void IncreaseLevel() public void IncreaseLevel()
{ {

View file

@ -10,17 +10,17 @@ namespace src.Managers
{ {
public class UpgradeManager : GameplayComponent, IDynamicLevelData public class UpgradeManager : GameplayComponent, IDynamicLevelData
{ {
public static UpgradeManager Instance; public static UpgradeManager instance;
private List<GameObject> _unclaimedUpgrades = new List<GameObject>(); private List<GameObject> _unclaimedUpgrades = new List<GameObject>();
private GameObject[] _upgradePrefabs; private GameObject[] _upgradePrefabs;
private void Awake() private void Awake()
{ {
if (Instance == null) if (instance == null)
{ {
Instance = this; instance = this;
} }
else if (Instance != null) else if (instance != null)
{ {
Destroy(gameObject); Destroy(gameObject);
} }

View file

@ -8,7 +8,7 @@ namespace src.Player
{ {
public class PlayerController : GameplayComponent, IExplosable public class PlayerController : GameplayComponent, IExplosable
{ {
private readonly GameStateManager _gameStateManager = GameStateManager.Instance; private GameStateManager _gameStateManager;
private Rigidbody2D _rigidbody2d; private Rigidbody2D _rigidbody2d;
private Collider2D _collider2D; private Collider2D _collider2D;
private Transform _respawnPosition; private Transform _respawnPosition;
@ -26,13 +26,15 @@ namespace src.Player
protected void Start() protected void Start()
{ {
_gameStateManager = GameStateManager.instance;
_playerUpgrade = PlayerUpgrade.instance;
_rigidbody2d = GetComponent<Rigidbody2D>(); _rigidbody2d = GetComponent<Rigidbody2D>();
_collider2D = GetComponent<Collider2D>(); _collider2D = GetComponent<Collider2D>();
_animator = GetComponentInChildren<Animator>(); _animator = GetComponentInChildren<Animator>();
_respawnPosition = GameObject.Find("RespawnPosition").transform; _respawnPosition = GameObject.Find("RespawnPosition").transform;
_bombsSpawner = GameObject.Find("BombSpawner").GetComponent<BombsSpawner>(); _bombsSpawner = GameObject.Find("BombSpawner").GetComponent<BombsSpawner>();
_playerUpgrade = PlayerUpgrade.Instance;
movementSpeed = _playerUpgrade.GetMovementSpeed(); movementSpeed = _playerUpgrade.GetMovementSpeed();
_playerUpgrade.PlayerSpeed += OnSpeedUpgrade; _playerUpgrade.PlayerSpeed += OnSpeedUpgrade;

View file

@ -1,16 +1,25 @@
using src.Base;
namespace src.Player namespace src.Player
{ {
public class PlayerUpgrade public class PlayerUpgrade : GameplayComponent
{ {
public delegate void IncreaseSpeedDelegate(float speed); public delegate void IncreaseSpeedDelegate(float speed);
public static PlayerUpgrade Instance = new PlayerUpgrade(); public static PlayerUpgrade instance;
public event IncreaseSpeedDelegate PlayerSpeed; public event IncreaseSpeedDelegate PlayerSpeed;
public const float MaxPlayerSpeed = 8f; public const float MaxPlayerSpeed = 8f;
private float _movementSpeed = 4f; private float _movementSpeed = 4f;
private PlayerUpgrade() public void Awake()
{ {
if (instance == null)
{
instance = this;
}
else if (instance != null)
{
Destroy(gameObject);
}
} }
public float GetMovementSpeed() public float GetMovementSpeed()
@ -25,7 +34,7 @@ namespace src.Player
return; return;
} }
_movementSpeed += speed; _movementSpeed += speed;
PlayerSpeed.Invoke(speed); PlayerSpeed?.Invoke(speed);
} }
} }
} }

View file

@ -7,7 +7,7 @@ namespace src.UI
{ {
public class PreStageUiScript : GameplayComponent public class PreStageUiScript : GameplayComponent
{ {
private readonly GameStateManager _gameStateManager = GameStateManager.Instance; private readonly GameStateManager _gameStateManager = GameStateManager.instance;
private Text _stageText; private Text _stageText;
public void Start() public void Start()

View file

@ -6,7 +6,7 @@ namespace src.Upgrade
{ {
public override void PerformUpgrade() public override void PerformUpgrade()
{ {
var bombManager = GameManager.GetBombsUtilManager(); var bombManager = gameManager.GetBombsUtilManager();
bombManager.IncreasePower(); bombManager.IncreasePower();
} }
} }

View file

@ -6,7 +6,7 @@ namespace src.Upgrade
{ {
public override void PerformUpgrade() public override void PerformUpgrade()
{ {
var bombManager = GameManager.GetBombsUtilManager(); var bombManager = gameManager.GetBombsUtilManager();
bombManager.IncreaseAllowedBombs(); bombManager.IncreaseAllowedBombs();
} }
} }

View file

@ -7,7 +7,7 @@ namespace src.Upgrade
{ {
public override void PerformUpgrade() public override void PerformUpgrade()
{ {
var bombManager = GameManager.GetBombsUtilManager(); var bombManager = gameManager.GetBombsUtilManager();
bombManager.IncreaseAllowedBombs(); bombManager.IncreaseAllowedBombs();
bombManager.IncreasePower(); bombManager.IncreasePower();
} }

View file

@ -7,7 +7,7 @@ namespace src.Upgrade
{ {
public override void PerformUpgrade() public override void PerformUpgrade()
{ {
var player = PlayerUpgrade.Instance; var player = PlayerUpgrade.instance;
player.IncreaseSpeed(.5f); player.IncreaseSpeed(.5f);
} }
} }

View file

@ -18,7 +18,7 @@ namespace src.Wall
private void Start() private void Start()
{ {
_upgradeManager = GameManager.Instance.GetUpgradeManager(); _upgradeManager = GameManager.instance.GetUpgradeManager();
_animator = GetComponentInChildren<Animator>(); _animator = GetComponentInChildren<Animator>();
// _animator.speed = 0; // _animator.speed = 0;
} }

View file

@ -12,7 +12,7 @@ namespace src.Wall
private void Start() private void Start()
{ {
_gameManager = GameManager.Instance; _gameManager = GameManager.instance;
_collider2D = GetComponent<Collider2D>(); _collider2D = GetComponent<Collider2D>();
} }