Skip to content

Commit

Permalink
[Update] Update GameController logic to use events
Browse files Browse the repository at this point in the history
  • Loading branch information
SeungHuLee committed Nov 28, 2019
1 parent 27c40ab commit 292f137
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 36 deletions.
45 changes: 45 additions & 0 deletions MissileCommander/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,48 @@ Transform:
m_Father: {fileID: 0}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1776773257
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1776773259}
- component: {fileID: 1776773258}
m_Layer: 0
m_Name: GameManager
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1776773258
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1776773257}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 6568d2037bb74621bb31463e9d57c466, type: 3}
m_Name:
m_EditorClassIdentifier:
launcherPrefab: {fileID: 7580796078773750041, guid: 8fc4a7e1b9214fe40bcc73660a06f8f5,
type: 3}
--- !u!4 &1776773259
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1776773257}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_Children: []
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
26 changes: 3 additions & 23 deletions MissileCommander/Assets/_Scripts/BulletLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
using System;
using UnityEngine;
using UnityEngine;

namespace MissileCommander
{
public class BulletLauncher : MonoBehaviour
{
private IGameController _controller;


public void SetGameController(IGameController controller)
public void OnFireButtonPressed(Vector3 position)
{
_controller = controller;
Debug.Log(position);
}

private void Update()
{
if (_controller != null)
{
if (_controller.FireButtonPressed())
{
Debug.Log("Bullet Fired!");
}
}
else
{
Debug.Log("GameController is null!");
}
}

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

using System;
using UnityEngine;

namespace MissileCommander
Expand All @@ -18,7 +17,9 @@ public class GameManager : MonoBehaviour
private void Awake()
{
_launcher = Instantiate(launcherPrefab);
_launcher.SetGameController(new MouseGameController());

MouseGameController mouseController = gameObject.AddComponent<MouseGameController>();
mouseController.FireButtonPressed += _launcher.OnFireButtonPressed;
}
}
}
1 change: 0 additions & 1 deletion MissileCommander/Assets/_Scripts/IGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@
{
public interface IGameController
{
bool FireButtonPressed();
}
}
18 changes: 12 additions & 6 deletions MissileCommander/Assets/_Scripts/KeyGameController.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
using UnityEngine;
using System;
using UnityEngine;

namespace MissileCommander
{
public class KeyGameController : IGameController
[Obsolete]
public class KeyGameController : MonoBehaviour, IGameController
{
private KeyCode _fireButton = KeyCode.Space;

public bool FireButtonPressed()
[SerializeField] private KeyCode fireButton = KeyCode.Space;
public event Action FireButtonPressed = delegate { };

private void Update()
{
return Input.GetKeyDown(_fireButton);
if (Input.GetKeyDown(fireButton))
{
FireButtonPressed();
}
}
}
}
27 changes: 23 additions & 4 deletions MissileCommander/Assets/_Scripts/MouseGameController.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
using UnityEngine;
using System;
using UnityEngine;

namespace MissileCommander
{
public class MouseGameController : IGameController
public class MouseGameController : MonoBehaviour, IGameController
{
public bool FireButtonPressed()
private Camera _mainCamera;
public event Action<Vector3> FireButtonPressed = delegate { };

private void Awake()
{
_mainCamera = Camera.main != null ? Camera.main : FindObjectOfType<Camera>();
}

private void Update()
{
if (Input.GetMouseButtonDown(0))
{
FireButtonPressed(GetClickPosition(Input.mousePosition));
}
}

private Vector3 GetClickPosition(Vector3 mousePosition)
{
return Input.GetMouseButtonDown(0);
Vector3 worldPos = _mainCamera.ScreenToWorldPoint(mousePosition);
worldPos.z = 0f;
return worldPos;
}
}
}

0 comments on commit 292f137

Please sign in to comment.