Updated input system, added touch inputs

Now InputTranslator has its own interface to provide polymorphism in GameSession class
Touch input read only if target platform is android
This commit is contained in:
VladimirPirozhenko 2022-08-16 04:01:08 +03:00
parent e1c8070226
commit f2797a5355
16 changed files with 294 additions and 140 deletions

1
.gitignore vendored
View File

@ -56,6 +56,7 @@ ExportedObj/
sysinfo.txt
# Builds
/[Aa]ndroidBuild*
*.apk
*.aab
*.unitypackage

View File

@ -607,7 +607,6 @@ Transform:
m_Children:
- {fileID: 1858832248}
- {fileID: 1791311983}
- {fileID: 378990291}
m_Father: {fileID: 0}
m_RootOrder: 10
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -746,107 +745,6 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 377940177}
m_CullTransparentMesh: 1
--- !u!1 &378990290
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 378990291}
- component: {fileID: 378990294}
- component: {fileID: 378990293}
- component: {fileID: 378990292}
m_Layer: 5
m_Name: ScoreCanvas
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!224 &378990291
RectTransform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 378990290}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 0, y: 0, z: 0}
m_ConstrainProportionsScale: 0
m_Children:
- {fileID: 678372250}
m_Father: {fileID: 245285643}
m_RootOrder: 2
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0, y: 0}
m_AnchorMax: {x: 0, y: 0}
m_AnchoredPosition: {x: 0, y: 0}
m_SizeDelta: {x: 0, y: 0}
m_Pivot: {x: 0, y: 0}
--- !u!114 &378990292
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 378990290}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3}
m_Name:
m_EditorClassIdentifier:
m_IgnoreReversedGraphics: 1
m_BlockingObjects: 0
m_BlockingMask:
serializedVersion: 2
m_Bits: 4294967295
--- !u!114 &378990293
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 378990290}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3}
m_Name:
m_EditorClassIdentifier:
m_UiScaleMode: 1
m_ReferencePixelsPerUnit: 100
m_ScaleFactor: 1
m_ReferenceResolution: {x: 800, y: 600}
m_ScreenMatchMode: 0
m_MatchWidthOrHeight: 0
m_PhysicalUnit: 3
m_FallbackScreenDPI: 96
m_DefaultSpriteDPI: 96
m_DynamicPixelsPerUnit: 1
m_PresetInfoIsWorld: 0
--- !u!223 &378990294
Canvas:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 378990290}
m_Enabled: 1
serializedVersion: 3
m_RenderMode: 0
m_Camera: {fileID: 0}
m_PlaneDistance: 100
m_PixelPerfect: 0
m_ReceivesEvents: 1
m_OverrideSorting: 0
m_OverridePixelPerfect: 0
m_SortingBucketNormalizedSize: 0
m_AdditionalShaderChannelsFlag: 25
m_SortingLayerID: 0
m_SortingOrder: 0
m_TargetDisplay: 0
--- !u!1 &634835457
GameObject:
m_ObjectHideFlags: 0
@ -2172,6 +2070,7 @@ RectTransform:
m_Children:
- {fileID: 220002229}
- {fileID: 1754320530}
- {fileID: 678372250}
m_Father: {fileID: 245285643}
m_RootOrder: 0
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@ -2852,7 +2751,7 @@ PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
m_TransformParent: {fileID: 378990291}
m_TransformParent: {fileID: 1858832248}
m_Modifications:
- target: {fileID: 6634832997822410792, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_SizeDelta.x
@ -2892,7 +2791,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_RootOrder
value: 0
value: 2
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_AnchorMax.x
@ -2904,31 +2803,31 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_AnchorMin.x
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_AnchorMin.y
value: 1
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_SizeDelta.x
value: 161.66
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_SizeDelta.y
value: 36.7283
value: 0.14001465
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_LocalScale.x
value: 1.1175
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_LocalScale.y
value: 1.1175
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_LocalScale.z
value: 1.1175
value: 1
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_LocalPosition.x
@ -2960,11 +2859,11 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_AnchoredPosition.x
value: -90.33002
value: 0
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_AnchoredPosition.y
value: -18.084717
value: 0.21002197
objectReference: {fileID: 0}
- target: {fileID: 6634832998035864265, guid: 68d36a24cad029f4a91d5c8e7aa18281, type: 3}
propertyPath: m_LocalEulerAnglesHint.x

View File

@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 9055f72a604bc3f4ba01bc120f0643d2, type: 3}
m_Name: PlayerData
m_EditorClassIdentifier:
<InvincibilityTime>k__BackingField: 3
<InvincibilityTime>k__BackingField: 300
<JumpHeight>k__BackingField: 5.3
<LaneSwitchSpeed>k__BackingField: 25
<Speed>k__BackingField: 20

View File

@ -8,8 +8,7 @@ public class GameSession : MonoBehaviour,IResettable
public static GameSession Instance { get; private set; }
[SerializeField] private Player currentPlayer;
private InputTranslator<KeyBinding> InputTranslator;
private IInputTranslator inputTranslator;
private bool isSessionPaused = false;
private bool isInputAlreadyRestricted = false;
@ -21,32 +20,39 @@ public class GameSession : MonoBehaviour,IResettable
private void Update()
{
InputTranslator.Tick();
inputTranslator.Tick();
}
private void Init()
{
InputTranslator = new InputTranslator<KeyBinding>();
IBindingHolder<KeyBinding> holder = new KeyBindingHolder();
InputTranslator.Init(holder);
if (ApplicationUtil.platform == RuntimePlatform.Android || ApplicationUtil.platform == RuntimePlatform.IPhonePlayer)
{
IBindingHolder<TouchBinding> touchHolder = new TouchBindingHolder();
inputTranslator = new InputTranslator<TouchBinding>(touchHolder);
}
else
{
IBindingHolder<KeyBinding> keyHolder = new KeyBindingHolder();
inputTranslator = new InputTranslator<KeyBinding>(keyHolder);
}
}
public void AddCommandTranslator(ICommandTranslator translator)
{
InputTranslator.AddCommandTranslator(translator);
inputTranslator.AddCommandTranslator(translator);
}
public void PauseSession(bool isPaused)
{
Time.timeScale = isPaused ? 0 : 1;
if (!isSessionPaused && InputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
if (!isSessionPaused && inputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
{
isInputAlreadyRestricted = true;
isSessionPaused = isPaused;
return;
}
if (!InputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
if (!inputTranslator.IsTranslationResticted(InputConstants.InGameCommands))
{
isInputAlreadyRestricted = false;
}
@ -60,13 +66,15 @@ public class GameSession : MonoBehaviour,IResettable
public void RestrictInputs(List<ECommand> commands,bool isRestricted)
{
InputTranslator.RestictTranslation(commands, isRestricted);
inputTranslator.RestictTranslation(commands, isRestricted);
}
public void RestartSession()
{
SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
ResetToDefault();
}
public void GoToMainMenu()
{
SceneManager.LoadScene("MainMenu", LoadSceneMode.Single);

View File

@ -0,0 +1,24 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchBindingHolder : IBindingHolder<TouchBinding>
{
public Dictionary<ECommand, TouchBinding> InputBindings { get; private set; }
private readonly Dictionary<ECommand, TouchBinding> DefaultTouchBindings = new Dictionary<ECommand, TouchBinding>
{
{ECommand.NONE, new TouchBinding(ETouchGesture.NONE)},
{ECommand.DOWN, new TouchBinding(ETouchGesture.SWIPE_DOWN)},
{ECommand.UP, new TouchBinding(ETouchGesture.SWIPE_UP)},
{ECommand.LEFT, new TouchBinding(ETouchGesture.SWIPE_LEFT)},
{ECommand.RIGHT, new TouchBinding(ETouchGesture.SWIPE_RIGHT)},
};
public void Init()
{
if (InputBindings == null)
InputBindings = DefaultTouchBindings;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 23353de203a94c447af7be38973acc1d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -3,7 +3,7 @@ using UnityEngine;
public interface IBinding
{
public bool IsPressed { get; }
public bool IsReleased { get; }
public bool IsPressed();
public bool IsReleased();
public bool IsRestricted { get; set; }
}

View File

@ -4,8 +4,6 @@ public class KeyBinding : IBinding
{
private KeyCode keyBinding;
private KeyCode alternativeKeyBinding;
public bool IsPressed => Input.GetKeyDown(keyBinding) || Input.GetKeyDown(alternativeKeyBinding);
public bool IsReleased => Input.GetKeyUp(keyBinding) || Input.GetKeyUp(alternativeKeyBinding);
public bool IsRestricted { get; set; }
public KeyBinding(KeyCode key,KeyCode alternative = KeyCode.None)
@ -24,4 +22,14 @@ public class KeyBinding : IBinding
keyBinding = key;
}
public bool IsReleased()
{
return Input.GetKeyUp(keyBinding) || Input.GetKeyUp(alternativeKeyBinding);
}
public bool IsPressed()
{
return Input.GetKeyDown(keyBinding) || Input.GetKeyDown(alternativeKeyBinding);
}
}

View File

@ -0,0 +1,127 @@
using UnityEngine;
public enum ETouchGesture
{
NONE,
TAP,
SWIPE_LEFT,
SWIPE_RIGHT,
SWIPE_UP,
SWIPE_DOWN
}
public class TouchBinding : IBinding
{
private ETouchGesture wantedTouchGesture;
private ETouchGesture actualGesture;
private bool isDragging = false;
private int swipeThreshold = 100;
private Vector2 startTouch;
private Vector2 swipeDelta;
public bool IsPressed()
{
if (wantedTouchGesture == ETouchGesture.NONE)
return false;
ReadGesture();
if (actualGesture == wantedTouchGesture)
{
return true;
}
return false;
}
public bool IsReleased()
{
return false;
}
public bool IsRestricted { get; set; }
public TouchBinding(ETouchGesture gesture)
{
wantedTouchGesture = gesture;
IsRestricted = false;
}
private void ReadGesture()
{
actualGesture = ETouchGesture.NONE;
#region ForPC
#if UNITY_EDITOR
if (Input.GetMouseButtonDown(0))
{
actualGesture = ETouchGesture.TAP;
isDragging = true;
startTouch = Input.mousePosition;
}
else if (Input.GetMouseButtonUp(0))
{
isDragging = false;
actualGesture = ETouchGesture.NONE;
Reset();
}
#endif
#endregion
if (Input.touches.Length > 0)
{
if (Input.touches[0].phase == TouchPhase.Began)
{
actualGesture = ETouchGesture.TAP;
isDragging = true;
startTouch = Input.touches[0].position;
}
else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
{
isDragging = false;
Reset();
}
}
swipeDelta = Vector2.zero;
if (isDragging)
{
if (Input.touches.Length < 0)
swipeDelta = Input.touches[0].position - startTouch;
else if (Input.GetMouseButton(0))
swipeDelta = (Vector2)Input.mousePosition - startTouch;
}
if (swipeDelta.magnitude > swipeThreshold)
{
float x = swipeDelta.x;
float y = swipeDelta.y;
if (Mathf.Abs(x) > Mathf.Abs(y))
{
if (x < 0)
actualGesture = ETouchGesture.SWIPE_LEFT;
else
actualGesture = ETouchGesture.SWIPE_RIGHT;
}
else
{
if (y < 0)
actualGesture = ETouchGesture.SWIPE_DOWN;
else
actualGesture = ETouchGesture.SWIPE_UP;
}
Reset();
}
}
private void Reset()
{
startTouch = swipeDelta = Vector2.zero;
isDragging = false;
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 6aea6c10fce13c64f924ba743d8f421b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -1,16 +1,34 @@

using System.Collections.Generic;
public class InputTranslator<T> where T : IBinding
public interface IInputTranslator
{
//public void Init(IBindingHolder<T> holder);
public void AddCommandTranslator(ICommandTranslator translator);
public void RemoveCommandTranslator(ICommandTranslator translator);
public void RestictTranslation(List<ECommand> commands, bool isRestricted);
public bool IsTranslationResticted(List<ECommand> commands);
public void Tick();
}
public class InputTranslator<T> : IInputTranslator where T : IBinding
{
private List<ICommandTranslator> commandTranslators;
private IBindingHolder<T> bindingHolder;
public void Init(IBindingHolder<T> holder)
public InputTranslator(IBindingHolder<T> holder)
{
commandTranslators = new List<ICommandTranslator>();
bindingHolder = holder;
bindingHolder.Init();
}
//public void Init(IBindingHolder<T> holder)
//{
// commandTranslators = new List<ICommandTranslator>();
// bindingHolder = holder;
// bindingHolder.Init();
//}
public void AddCommandTranslator(ICommandTranslator translator)
{
@ -60,10 +78,10 @@ public class InputTranslator<T> where T : IBinding
{
if (keyBinding.Value.IsRestricted)
continue;
if (keyBinding.Value.IsPressed)
commands.Add(keyBinding.Key, new PressedState(keyBinding.Value.IsPressed, keyBinding.Value.IsReleased));
if (keyBinding.Value.IsReleased)
commands.Add(keyBinding.Key, new PressedState(keyBinding.Value.IsPressed, keyBinding.Value.IsReleased));
if (keyBinding.Value.IsPressed())
commands.Add(keyBinding.Key, new PressedState(true, false));
if (keyBinding.Value.IsReleased())
commands.Add(keyBinding.Key, new PressedState(false, true));
}
if (commands.Count == 0)

View File

@ -0,0 +1,21 @@
using System.Collections;
using UnityEngine;
public class ApplicationUtil
{
public static RuntimePlatform platform
{
get
{
#if UNITY_ANDROID
return RuntimePlatform.Android;
#elif UNITY_IOS
return RuntimePlatform.IPhonePlayer;
#elif UNITY_STANDALONE_OSX
return RuntimePlatform.OSXPlayer;
#elif UNITY_STANDALONE_WIN
return RuntimePlatform.WindowsPlayer;
#endif
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dc32e4a16f3dd1540b5208d59de19e2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,14 @@
{
"MonoBehaviour": {
"Version": 4,
"EnableBurstCompilation": true,
"EnableOptimisations": true,
"EnableSafetyChecks": false,
"EnableDebugInAllBuilds": false,
"CpuMinTargetX32": 0,
"CpuMaxTargetX32": 0,
"CpuMinTargetX64": 0,
"CpuMaxTargetX64": 0,
"OptimizeFor": 0
}
}

View File

@ -12,12 +12,12 @@ MonoBehaviour:
m_Script: {fileID: 13964, guid: 0000000000000000e000000000000000, type: 0}
m_Name:
m_EditorClassIdentifier:
m_EnablePreReleasePackages: 0
m_EnablePreReleasePackages: 1
m_EnablePackageDependencies: 0
m_AdvancedSettingsExpanded: 1
m_ScopedRegistriesSettingsExpanded: 1
m_SeeAllPackageVersions: 0
oneTimeWarningShown: 0
oneTimeWarningShown: 1
m_Registries:
- m_Id: main
m_Name:

View File

@ -68,7 +68,7 @@ PlayerSettings:
androidRenderOutsideSafeArea: 1
androidUseSwappy: 1
androidBlitType: 0
androidResizableWindow: 0
androidResizableWindow: 1
androidDefaultWindowWidth: 1920
androidDefaultWindowHeight: 1080
androidMinimumWindowWidth: 400
@ -154,7 +154,8 @@ PlayerSettings:
resetResolutionOnWindowResize: 0
androidSupportedAspectRatio: 1
androidMaxAspectRatio: 2.1
applicationIdentifier: {}
applicationIdentifier:
Android: com.DefaultCompany.AwesomeRunner
buildNumber:
Standalone: 0
iPhone: 0
@ -413,7 +414,7 @@ PlayerSettings:
m_BuildTargetGraphicsAPIs:
- m_BuildTarget: AndroidPlayer
m_APIs: 0b00000008000000
m_Automatic: 0
m_Automatic: 1
- m_BuildTarget: iOSSupport
m_APIs: 10000000
m_Automatic: 1