Skip to content

Commit

Permalink
[Add/Update] Add TimeManager, Update GameManager to bind/unbind events
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Dec 2, 2019
1 parent 6078af5 commit 0bb68f5
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 40 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 3 additions & 34 deletions MissileCommander/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1776773259}
m_RootOrder: 5
m_RootOrder: 4
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &676322499
GameObject:
Expand Down Expand Up @@ -263,36 +263,6 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &972352209
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 972352210}
m_Layer: 0
m_Name: BuildingLocator
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!4 &972352210
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 972352209}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: -4, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1776773259}
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1082526252
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -321,7 +291,7 @@ Transform:
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 1776773259}
m_RootOrder: 4
m_RootOrder: 3
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1092131172
GameObject:
Expand Down Expand Up @@ -390,7 +360,7 @@ MonoBehaviour:
buildingLocators:
- {fileID: 676322500}
- {fileID: 2063700999}
- {fileID: 972352210}
- {fileID: 0}
- {fileID: 1082526253}
- {fileID: 598110664}
--- !u!4 &1776773259
Expand All @@ -407,7 +377,6 @@ Transform:
- {fileID: 1092131173}
- {fileID: 676322500}
- {fileID: 2063700999}
- {fileID: 972352210}
- {fileID: 1082526253}
- {fileID: 598110664}
m_Father: {fileID: 0}
Expand Down
7 changes: 5 additions & 2 deletions MissileCommander/Assets/_Scripts/BuildingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ public BuildingManager(Building buildingPrefab, Transform[] buildingLocators)

Debug.Assert(this._buildingPrefab != null, "BuildingManager : Prefab is null!");
Debug.Assert(this._buildingLocators != null, "BuildingManager : Locators are null!");

SpawnBuildings();
}

public void SpawnBuildings()
Expand All @@ -36,5 +34,10 @@ public void SpawnBuildings()
_buildings.Add(building);
}
}

public void OnGameStart()
{
SpawnBuildings();
}
}
}
9 changes: 9 additions & 0 deletions MissileCommander/Assets/_Scripts/BulletLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class BulletLauncher : MonoBehaviour
[SerializeField] private float fireDelay = 0.5f;
private float _elapsedFireTime;
private bool _canShoot = true;
private bool _isGameStarted = false;

private Factory _bulletFactory;
private Factory _explosionFactory;
Expand All @@ -23,6 +24,8 @@ private void Awake()

private void Update()
{
if (!_isGameStarted) { return; }

if (!_canShoot)
{
_elapsedFireTime += Time.deltaTime;
Expand All @@ -34,8 +37,14 @@ private void Update()
}
}

public void OnGameStart()
{
_isGameStarted = true;
}

public void OnFireButtonPressed(Vector3 mousePosition)
{
if (!_isGameStarted) { return; }
if (!_canShoot) { return; }

// Instantiate Bullet
Expand Down
34 changes: 30 additions & 4 deletions MissileCommander/Assets/_Scripts/GameManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2. Create instance of any GameController Class and assign it to BulletLauncher.
* ===================================================================================*/

using System;
using UnityEngine;

namespace MissileCommander
Expand All @@ -15,19 +16,44 @@ public class GameManager : MonoBehaviour
[SerializeField] private Transform launcherLocator;
[SerializeField] private Building buildingPrefab;
[SerializeField] private Transform[] buildingLocators;


private MouseGameController _mouseGameController;
private BulletLauncher _launcher;
private BuildingManager _buildingMgr;
private TimeManager _timeMgr;

private void Awake()
private void Start()
{
_timeMgr = gameObject.AddComponent<TimeManager>();

_launcher = Instantiate(launcherPrefab);
_launcher.transform.position = launcherLocator.position;

_buildingMgr = new BuildingManager(buildingPrefab, buildingLocators);

_mouseGameController = gameObject.AddComponent<MouseGameController>();

BindEvents();
_timeMgr.StartGame();
}

MouseGameController mouseController = gameObject.AddComponent<MouseGameController>();
mouseController.OnFireButtonPressed += _launcher.OnFireButtonPressed;
private void OnDestroy()
{
UnbindEvents();
}

private void BindEvents()
{
_mouseGameController.OnFireButtonPressed += _launcher.OnFireButtonPressed;
_timeMgr.onGameStart += _buildingMgr.OnGameStart;
_timeMgr.onGameStart += _launcher.OnGameStart;
}

private void UnbindEvents()
{
_mouseGameController.OnFireButtonPressed -= _launcher.OnFireButtonPressed;
_timeMgr.onGameStart -= _buildingMgr.OnGameStart;
_timeMgr.onGameStart -= _launcher.OnGameStart;
}
}
}
28 changes: 28 additions & 0 deletions MissileCommander/Assets/_Scripts/TimeManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections;
using UnityEngine;

namespace MissileCommander
{
public class TimeManager : MonoBehaviour
{
public event Action onGameStart;
private bool _isGameStarted = false;

public void StartGame(float startDelay = 1f)
{
if (_isGameStarted) { return; }

_isGameStarted = true;
StartCoroutine(DelayedGameStart(startDelay));
}

private IEnumerator DelayedGameStart(float delayTime)
{
WaitForSeconds delay = new WaitForSeconds(delayTime);
yield return delay;

onGameStart?.Invoke();
}
}
}
3 changes: 3 additions & 0 deletions MissileCommander/Assets/_Scripts/TimeManager.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0bb68f5

Please sign in to comment.