-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathPlayerVehicleInputSystem.cs
125 lines (109 loc) · 4.67 KB
/
PlayerVehicleInputSystem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
using Unity.Entities;
using Unity.Mathematics;
using Unity.MegacityMetro.CameraManagement;
using UnityEngine.UIElements;
using System.Runtime.InteropServices;
#if UNITY_ANDROID || UNITY_IPHONE || ENABLED_VIRTUAL_JOYSTICK
using UnityEngine;
using Unity.MegacityMetro.UI;
#else
using UnityEngine;
#endif
using Unity.NetCode;
namespace Unity.MegacityMetro.Gameplay
{
/// <summary>
/// System to collect the player input and send it to the player vehicle.
/// </summary>
[WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
[UpdateInGroup(typeof(GhostInputSystemGroup))]
public partial class PlayerVehicleInputSystemBase : SystemBase
{
private GameInput m_GameInput;
private bool IsTrackpadTouched => PluginLoader.TouchesCount() > 0;
protected override void OnCreate()
{
base.OnCreate();
RequireForUpdate<ControlSettings>();
PluginLoader.LoadPlugin();
}
protected override void OnStopRunning()
{
base.OnStopRunning();
m_GameInput?.Disable();
PluginLoader.UnloadPlugin();
}
protected override void OnUpdate()
{
const float ConstLookVelocityMultiplier = 100f;
if (HybridCameraManager.Instance == null)
return;
float deltaTime = SystemAPI.Time.DeltaTime;
if (m_GameInput == null)
{
m_GameInput = new GameInput();
m_GameInput.Enable();
m_GameInput.Gameplay.Enable();
}
var controlSettings = SystemAPI.GetSingleton<ControlSettings>();
var invertHorizontal = controlSettings.InverseLookHorizontal ? -1 : 1;
var invertVertical = controlSettings.InverseLookVertical ? -1 : 1;
var accelerationRange = controlSettings.AccelerationRange;
#if UNITY_ANDROID || UNITY_IPHONE || ENABLED_VIRTUAL_JOYSTICK
var input = new PlayerVehicleInput
{
Acceleration = math.clamp(MobileControls.Instance.Speed, accelerationRange.x, accelerationRange.y),
LookVelocity = new float2(
MobileControls.Instance.JoystickLeft.Delta.y * invertVertical,
MobileControls.Instance.JoystickLeft.Delta.x * invertHorizontal) * ConstLookVelocityMultiplier,
Shoot = MobileControls.Instance.Shoot,
AimAssistSensitivity = controlSettings.AimAssistanceSensitivity,
};
#else
var gameplayInputActions = m_GameInput.Gameplay;
float2 lookInput = default;
if (math.any(gameplayInputActions.LookDelta.ReadValue<Vector2>()))
{
if (deltaTime != 0f)
{
lookInput = new float2
{
x = -gameplayInputActions.LookDelta.ReadValue<Vector2>().y * invertVertical,
y = gameplayInputActions.LookDelta.ReadValue<Vector2>().x * invertHorizontal,
};
float sensitivity = IsTrackpadTouched ? controlSettings.TrackpadSensitivity : 1.0f;
lookInput *= controlSettings.MouseSensitivity * sensitivity;
lookInput /= deltaTime; // convert delta displacement this frame to a velocity
}
}
else
{
lookInput = new float2
{
x = -gameplayInputActions.LookConst.ReadValue<Vector2>().y * invertVertical,
y = gameplayInputActions.LookConst.ReadValue<Vector2>().x * invertHorizontal,
};
lookInput *= ConstLookVelocityMultiplier;
}
var input = new PlayerVehicleInput
{
Acceleration = math.clamp(gameplayInputActions.Move.ReadValue<float>(), accelerationRange.x, accelerationRange.y),
Roll = gameplayInputActions.Roll.ReadValue<float>(),
LookVelocity = lookInput,
Shoot = gameplayInputActions.Fire.IsPressed(),
AimAssistSensitivity = controlSettings.AimAssistanceSensitivity,
#if DEVELOPMENT_BUILD || UNITY_EDITOR
Cheat_1 = gameplayInputActions.Cheat_1.IsPressed()
#endif
};
#endif
var job = new PlayerVehicleInputJob { CollectedInput = input};
job.Schedule();
}
}
[WorldSystemFilter(WorldSystemFilterFlags.LocalSimulation)]
[UpdateInGroup(typeof(SimulationSystemGroup), OrderFirst = true)]
public partial class PlayerVehicleInputSystemSinglePlayer : PlayerVehicleInputSystemBase
{
}
}