-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Update] Update GameController logic to use events
- Loading branch information
1 parent
27c40ab
commit 292f137
Showing
6 changed files
with
86 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,5 @@ | |
{ | ||
public interface IGameController | ||
{ | ||
bool FireButtonPressed(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |