forked from Unity-Technologies/EntityComponentSystemSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemoInputGatheringSystem.cs
More file actions
104 lines (86 loc) · 4.09 KB
/
Copy pathDemoInputGatheringSystem.cs
File metadata and controls
104 lines (86 loc) · 4.09 KB
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
using Unity.Entities;
using UnityEngine;
// in order to circumvent API breakages that do not affect physics, some packages are removed from the project on CI
// any code referencing APIs in com.unity.inputsystem must be guarded behind UNITY_INPUT_SYSTEM_EXISTS
#if UNITY_INPUT_SYSTEM_EXISTS
using UnityEngine.InputSystem;
#endif
[AlwaysUpdateSystem]
[UpdateInGroup(typeof(InitializationSystemGroup))]
class DemoInputGatheringSystem : SystemBase
#if UNITY_INPUT_SYSTEM_EXISTS
,
InputActions.ICharacterControllerActions,
InputActions.IVehicleActions
#endif
{
EntityQuery m_CharacterControllerInputQuery;
EntityQuery m_CharacterGunInputQuery;
EntityQuery m_VehicleInputQuery;
#pragma warning disable 649
Vector2 m_CharacterMovement;
Vector2 m_CharacterLooking;
float m_CharacterFiring;
bool m_CharacterJumped;
Vector2 m_VehicleLooking;
Vector2 m_VehicleSteering;
float m_VehicleThrottle;
int m_VehicleChanged;
#pragma warning restore 649
protected override void OnCreate()
{
#if UNITY_INPUT_SYSTEM_EXISTS
m_InputActions = new InputActions();
m_InputActions.CharacterController.SetCallbacks(this);
m_InputActions.Vehicle.SetCallbacks(this);
#endif
m_CharacterControllerInputQuery = GetEntityQuery(typeof(CharacterControllerInput));
m_CharacterGunInputQuery = GetEntityQuery(typeof(CharacterGunInput));
m_VehicleInputQuery = GetEntityQuery(typeof(VehicleInput));
}
#if UNITY_INPUT_SYSTEM_EXISTS
InputActions m_InputActions;
protected override void OnStartRunning() => m_InputActions.Enable();
protected override void OnStopRunning() => m_InputActions.Disable();
void InputActions.ICharacterControllerActions.OnMove(InputAction.CallbackContext context) => m_CharacterMovement = context.ReadValue<Vector2>();
void InputActions.ICharacterControllerActions.OnLook(InputAction.CallbackContext context) => m_CharacterLooking = context.ReadValue<Vector2>();
void InputActions.ICharacterControllerActions.OnFire(InputAction.CallbackContext context) => m_CharacterFiring = context.ReadValue<float>();
void InputActions.ICharacterControllerActions.OnJump(InputAction.CallbackContext context) { if (context.started) m_CharacterJumped = true; }
void InputActions.IVehicleActions.OnLook(InputAction.CallbackContext context) => m_VehicleLooking = context.ReadValue<Vector2>();
void InputActions.IVehicleActions.OnSteering(InputAction.CallbackContext context) => m_VehicleSteering = context.ReadValue<Vector2>();
void InputActions.IVehicleActions.OnThrottle(InputAction.CallbackContext context) => m_VehicleThrottle = context.ReadValue<float>();
void InputActions.IVehicleActions.OnPrevious(InputAction.CallbackContext context) { if (context.started) m_VehicleChanged = -1; }
void InputActions.IVehicleActions.OnNext(InputAction.CallbackContext context) { if (context.started) m_VehicleChanged = 1; }
#endif
protected override void OnUpdate()
{
// character controller
if (m_CharacterControllerInputQuery.CalculateEntityCount() == 0)
EntityManager.CreateEntity(typeof(CharacterControllerInput));
m_CharacterControllerInputQuery.SetSingleton(new CharacterControllerInput
{
Looking = m_CharacterLooking,
Movement = m_CharacterMovement,
Jumped = m_CharacterJumped ? 1 : 0
});
if (m_CharacterGunInputQuery.CalculateEntityCount() == 0)
EntityManager.CreateEntity(typeof(CharacterGunInput));
m_CharacterGunInputQuery.SetSingleton(new CharacterGunInput
{
Looking = m_CharacterLooking,
Firing = m_CharacterFiring,
});
m_CharacterJumped = false;
// vehicle
if (m_VehicleInputQuery.CalculateEntityCount() == 0)
EntityManager.CreateEntity(typeof(VehicleInput));
m_VehicleInputQuery.SetSingleton(new VehicleInput
{
Looking = m_VehicleLooking,
Steering = m_VehicleSteering,
Throttle = m_VehicleThrottle,
Change = m_VehicleChanged
});
m_VehicleChanged = 0;
}
}