From 292f13765a77480aa03dcbac84bcd71d948e51ba Mon Sep 17 00:00:00 2001 From: SEUNGHU LEE Date: Thu, 28 Nov 2019 13:58:42 +0900 Subject: [PATCH] [Update] Update GameController logic to use events --- .../Assets/Scenes/SampleScene.unity | 45 +++++++++++++++++++ .../Assets/_Scripts/BulletLauncher.cs | 26 ++--------- .../Assets/_Scripts/GameManager.cs | 5 ++- .../Assets/_Scripts/IGameController.cs | 1 - .../Assets/_Scripts/KeyGameController.cs | 18 +++++--- .../Assets/_Scripts/MouseGameController.cs | 27 +++++++++-- 6 files changed, 86 insertions(+), 36 deletions(-) diff --git a/MissileCommander/Assets/Scenes/SampleScene.unity b/MissileCommander/Assets/Scenes/SampleScene.unity index 51e9726..cce91ab 100644 --- a/MissileCommander/Assets/Scenes/SampleScene.unity +++ b/MissileCommander/Assets/Scenes/SampleScene.unity @@ -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} diff --git a/MissileCommander/Assets/_Scripts/BulletLauncher.cs b/MissileCommander/Assets/_Scripts/BulletLauncher.cs index 6f8617c..53c3895 100644 --- a/MissileCommander/Assets/_Scripts/BulletLauncher.cs +++ b/MissileCommander/Assets/_Scripts/BulletLauncher.cs @@ -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!"); - } - } - } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/GameManager.cs b/MissileCommander/Assets/_Scripts/GameManager.cs index ee24cab..a4eb905 100644 --- a/MissileCommander/Assets/_Scripts/GameManager.cs +++ b/MissileCommander/Assets/_Scripts/GameManager.cs @@ -5,7 +5,6 @@ * 2. Create instance of any GameController Class and assign it to BulletLauncher. * ===================================================================================*/ -using System; using UnityEngine; namespace MissileCommander @@ -18,7 +17,9 @@ public class GameManager : MonoBehaviour private void Awake() { _launcher = Instantiate(launcherPrefab); - _launcher.SetGameController(new MouseGameController()); + + MouseGameController mouseController = gameObject.AddComponent(); + mouseController.FireButtonPressed += _launcher.OnFireButtonPressed; } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/IGameController.cs b/MissileCommander/Assets/_Scripts/IGameController.cs index 3955710..3a237cf 100644 --- a/MissileCommander/Assets/_Scripts/IGameController.cs +++ b/MissileCommander/Assets/_Scripts/IGameController.cs @@ -2,6 +2,5 @@ { public interface IGameController { - bool FireButtonPressed(); } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/KeyGameController.cs b/MissileCommander/Assets/_Scripts/KeyGameController.cs index c233946..86626ed 100644 --- a/MissileCommander/Assets/_Scripts/KeyGameController.cs +++ b/MissileCommander/Assets/_Scripts/KeyGameController.cs @@ -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(); + } } } } \ No newline at end of file diff --git a/MissileCommander/Assets/_Scripts/MouseGameController.cs b/MissileCommander/Assets/_Scripts/MouseGameController.cs index 6d86203..af43136 100644 --- a/MissileCommander/Assets/_Scripts/MouseGameController.cs +++ b/MissileCommander/Assets/_Scripts/MouseGameController.cs @@ -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 FireButtonPressed = delegate { }; + + private void Awake() + { + _mainCamera = Camera.main != null ? Camera.main : FindObjectOfType(); + } + + 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; } } } \ No newline at end of file