Skip to content

Instantly share code, notes, and snippets.

@Leopotam
Created October 10, 2017 20:24
Show Gist options
  • Save Leopotam/abc197dd31f233d7edbed39604513d3f to your computer and use it in GitHub Desktop.
Save Leopotam/abc197dd31f233d7edbed39604513d3f to your computer and use it in GitHub Desktop.
Desktop Input
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