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;
|
Instance = this;
|
||||||
Init();
|
Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Update()
|
private void Update()
|
||||||
{
|
{
|
||||||
InputTranslator.Tick();
|
InputTranslator.Tick();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Init()
|
private void Init()
|
||||||
{
|
{
|
||||||
InputTranslator = new InputTranslator<KeyBinding>();
|
InputTranslator = new InputTranslator<KeyBinding>();
|
||||||
@ -31,12 +33,12 @@ public class GameSession : MonoBehaviour,IResettable
|
|||||||
{
|
{
|
||||||
InputTranslator.AddCommandTranslator(translator);
|
InputTranslator.AddCommandTranslator(translator);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PauseSession(bool isPaused)
|
public void PauseSession(bool isPaused)
|
||||||
{
|
{
|
||||||
Time.timeScale = isPaused ? 0 : 1;
|
Time.timeScale = isPaused ? 0 : 1;
|
||||||
//List<ECommand> commands = new List<ECommand>();
|
if (InputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
|
||||||
//ECommand[] commandRange = { ECommand.LEFT,ECommand.RIGHT,ECommand.UP,ECommand.DOWN,ECommand.SHOOT};
|
return;
|
||||||
//commands.AddRange(commandRange);
|
|
||||||
RestrictInputs(InputConstants.InGameCommands,isRestricted: isPaused);
|
RestrictInputs(InputConstants.InGameCommands,isRestricted: isPaused);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ public class InputTranslator<T> where T : IBinding
|
|||||||
{
|
{
|
||||||
private List<ICommandTranslator> commandTranslators;
|
private List<ICommandTranslator> commandTranslators;
|
||||||
private IBindingHolder<T> bindingHolder;
|
private IBindingHolder<T> bindingHolder;
|
||||||
private bool isTranslating = true;
|
|
||||||
|
|
||||||
public void Init(IBindingHolder<T> holder)
|
public void Init(IBindingHolder<T> holder)
|
||||||
{
|
{
|
||||||
@ -13,6 +12,8 @@ public class InputTranslator<T> where T : IBinding
|
|||||||
bindingHolder.Init();
|
bindingHolder.Init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void AddCommandTranslator(ICommandTranslator translator)
|
public void AddCommandTranslator(ICommandTranslator translator)
|
||||||
{
|
{
|
||||||
if (commandTranslators.Contains(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()
|
public void Tick()
|
||||||
{
|
{
|
||||||
if (!isTranslating)
|
|
||||||
return;
|
|
||||||
if (commandTranslators.Count == 0)
|
if (commandTranslators.Count == 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class Pause : MonoBehaviour,ICommandTranslator
|
public class Pause : MonoBehaviour,ICommandTranslator
|
||||||
{
|
{
|
||||||
private bool isOpened = false;
|
|
||||||
private void Start()
|
private void Start()
|
||||||
{
|
{
|
||||||
GameSession.Instance.AddCommandTranslator(this);
|
GameSession.Instance.AddCommandTranslator(this);
|
||||||
@ -17,7 +16,8 @@ public class Pause : MonoBehaviour,ICommandTranslator
|
|||||||
switch (command)
|
switch (command)
|
||||||
{
|
{
|
||||||
case ECommand.OPEN_PAUSE_MENU:
|
case ECommand.OPEN_PAUSE_MENU:
|
||||||
isOpened = isOpened ? false : true;
|
bool isOpened = ViewManager.Instance.IsActive<PausedView>();
|
||||||
|
isOpened = !isOpened;
|
||||||
ViewManager.Instance.Show<PausedView>(isOpened);
|
ViewManager.Instance.Show<PausedView>(isOpened);
|
||||||
GameSession.Instance.PauseSession(isOpened);
|
GameSession.Instance.PauseSession(isOpened);
|
||||||
break;
|
break;
|
||||||
|
@ -16,4 +16,9 @@ public abstract class BaseView : MonoBehaviour
|
|||||||
{
|
{
|
||||||
gameObject.SetActive(isActive);
|
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)
|
public void Add(BaseView view,bool isActiveByDefault = true)
|
||||||
{
|
{
|
||||||
view.Init();
|
view.Init();
|
||||||
@ -65,18 +77,4 @@ public class ViewManager : MonoBehaviour
|
|||||||
views.Remove(view);
|
views.Remove(view);
|
||||||
view.Destroy();
|
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