Created
October 10, 2017 20:24
-
-
Save Leopotam/abc197dd31f233d7edbed39604513d3f to your computer and use it in GitHub Desktop.
Desktop Input
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
using Client.Common; | |
using Client.Zone.Units; | |
using Client.Zone.View; | |
using LeopotamGroup.Common; | |
using LeopotamGroup.Events; | |
using UnityEngine; | |
namespace Client.Zone.Input { | |
sealed class DesktopInput : MonoBehaviourService<DesktopInput> { | |
bool _inputEnabled; | |
protected override void OnCreateService () { | |
var ueb = Service<UnityEventBus>.Get (); | |
ueb.Subscribe<InputEnableState> (OnEnableState); | |
_inputEnabled = true; | |
} | |
protected override void OnDestroyService () { | |
if (Service<UnityEventBus>.IsRegistered) { | |
var ueb = Service<UnityEventBus>.Get (); | |
ueb.Unsubscribe<InputEnableState> (OnEnableState); | |
} | |
} | |
void Update () { | |
if (!_inputEnabled) { | |
return; | |
} | |
var unit = Service<UnitSystem>.Get ().GetActive (); | |
if (unit == null) { | |
return; | |
} | |
if (UnityEngine.Input.GetKeyDown (KeyCode.Alpha1)) { | |
unit.Crouch = false; | |
} | |
if (UnityEngine.Input.GetKeyDown (KeyCode.Alpha2)) { | |
unit.Crouch = true; | |
} | |
Vector2 aim; | |
if (UnityEngine.Input.GetMouseButton (0)) { | |
var pos = (Vector2) UnityEngine.Input.mousePosition; | |
aim = (pos - new Vector2 (Screen.width, Screen.height) * 0.5f).normalized; | |
} else { | |
aim = Vector2.zero; | |
} | |
var move = new Vector2 ( | |
UnityEngine.Input.GetAxis (UnityIdents.AxisHorizontal), | |
UnityEngine.Input.GetAxis (UnityIdents.AxisVertical)).normalized; | |
var ueb = Service<UnityEventBus>.Get (); | |
ueb.Publish<ViewSetInput> (new ViewSetInput (move, aim)); | |
} | |
void OnEnableState (InputEnableState eventData) { | |
_inputEnabled = eventData.State; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment