Updated InputTranslator and View manager, it fixed few bugs with allowing controls
InputTranslator now can check whether is translation is restricted, added new method to ViewManager - IsActive - to check if view is active or not
This commit is contained in:
parent
9af89dd8b7
commit
0fb508126d
@ -16,10 +16,12 @@ public class GameSession : MonoBehaviour,IResettable
|
||||
Instance = this;
|
||||
Init();
|
||||
}
|
||||
|
||||
private void Update()
|
||||
{
|
||||
InputTranslator.Tick();
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
InputTranslator = new InputTranslator<KeyBinding>();
|
||||
@ -31,12 +33,12 @@ public class GameSession : MonoBehaviour,IResettable
|
||||
{
|
||||
InputTranslator.AddCommandTranslator(translator);
|
||||
}
|
||||
|
||||
public void PauseSession(bool isPaused)
|
||||
{
|
||||
Time.timeScale = isPaused ? 0 : 1;
|
||||
//List<ECommand> commands = new List<ECommand>();
|
||||
//ECommand[] commandRange = { ECommand.LEFT,ECommand.RIGHT,ECommand.UP,ECommand.DOWN,ECommand.SHOOT};
|
||||
//commands.AddRange(commandRange);
|
||||
if (InputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
|
||||
return;
|
||||
RestrictInputs(InputConstants.InGameCommands,isRestricted: isPaused);
|
||||
}
|
||||
|
||||
|
@ -4,7 +4,6 @@ public class InputTranslator<T> where T : IBinding
|
||||
{
|
||||
private List<ICommandTranslator> commandTranslators;
|
||||
private IBindingHolder<T> bindingHolder;
|
||||
private bool isTranslating = true;
|
||||
|
||||
public void Init(IBindingHolder<T> holder)
|
||||
{
|
||||
@ -13,6 +12,8 @@ public class InputTranslator<T> where T : IBinding
|
||||
bindingHolder.Init();
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void AddCommandTranslator(ICommandTranslator translator)
|
||||
{
|
||||
if (commandTranslators.Contains(translator))
|
||||
@ -37,10 +38,21 @@ public class InputTranslator<T> where T : IBinding
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsTranslationResticted(List<ECommand> commands)
|
||||
{
|
||||
foreach (ECommand command in commands)
|
||||
{
|
||||
if (bindingHolder.InputBindings.ContainsKey(command))
|
||||
{
|
||||
return bindingHolder.InputBindings[command].IsRestricted;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Tick()
|
||||
{
|
||||
if (!isTranslating)
|
||||
return;
|
||||
if (commandTranslators.Count == 0)
|
||||
return;
|
||||
|
||||
|
@ -4,7 +4,6 @@ using UnityEngine;
|
||||
|
||||
public class Pause : MonoBehaviour,ICommandTranslator
|
||||
{
|
||||
private bool isOpened = false;
|
||||
private void Start()
|
||||
{
|
||||
GameSession.Instance.AddCommandTranslator(this);
|
||||
@ -17,7 +16,8 @@ public class Pause : MonoBehaviour,ICommandTranslator
|
||||
switch (command)
|
||||
{
|
||||
case ECommand.OPEN_PAUSE_MENU:
|
||||
isOpened = isOpened ? false : true;
|
||||
bool isOpened = ViewManager.Instance.IsActive<PausedView>();
|
||||
isOpened = !isOpened;
|
||||
ViewManager.Instance.Show<PausedView>(isOpened);
|
||||
GameSession.Instance.PauseSession(isOpened);
|
||||
break;
|
||||
|
@ -16,4 +16,9 @@ public abstract class BaseView : MonoBehaviour
|
||||
{
|
||||
gameObject.SetActive(isActive);
|
||||
}
|
||||
|
||||
public bool IsActive()
|
||||
{
|
||||
return gameObject.activeInHierarchy;
|
||||
}
|
||||
}
|
||||
|
@ -53,6 +53,18 @@ public class ViewManager : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsActive<T>() where T : BaseView
|
||||
{
|
||||
foreach (var view in views)
|
||||
{
|
||||
if (view is T)
|
||||
{
|
||||
return view.IsActive();
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Add(BaseView view,bool isActiveByDefault = true)
|
||||
{
|
||||
view.Init();
|
||||
@ -65,18 +77,4 @@ public class ViewManager : MonoBehaviour
|
||||
views.Remove(view);
|
||||
view.Destroy();
|
||||
}
|
||||
|
||||
public bool TryGetView<T>(out T baseView) where T : BaseView
|
||||
{
|
||||
foreach (var view in views)
|
||||
{
|
||||
if (view is T)
|
||||
{
|
||||
baseView = (T)view;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
baseView = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user