| | 1 | | using DCL.ECS7; |
| | 2 | | using DCL.ECS7.ComponentWrapper.Generic; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSComponents; |
| | 5 | | using DCL.Models; |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | |
|
| | 9 | | namespace ECSSystems.InputSenderSystem |
| | 10 | | { |
| | 11 | | public class ECSInputSenderSystem |
| | 12 | | { |
| | 13 | | private readonly IInternalECSComponent<InternalInputEventResults> inputResultComponent; |
| | 14 | | private readonly IInternalECSComponent<InternalEngineInfo> engineInfoComponent; |
| | 15 | | private readonly IReadOnlyDictionary<int, ComponentWriter> componentsWriter; |
| | 16 | | private readonly WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>> componentPool; |
| | 17 | | private readonly Func<int> getCurrentSceneNumber; |
| | 18 | | private uint lastTimestamp = 0; |
| | 19 | |
|
| 4 | 20 | | public ECSInputSenderSystem( |
| | 21 | | IInternalECSComponent<InternalInputEventResults> inputResultComponent, |
| | 22 | | IInternalECSComponent<InternalEngineInfo> engineInfoComponent, |
| | 23 | | IReadOnlyDictionary<int, ComponentWriter> componentsWriter, |
| | 24 | | WrappedComponentPool<IWrappedComponent<PBPointerEventsResult>> componentPool, |
| | 25 | | Func<int> getCurrentSceneNumber) |
| | 26 | | { |
| 4 | 27 | | this.inputResultComponent = inputResultComponent; |
| 4 | 28 | | this.engineInfoComponent = engineInfoComponent; |
| 4 | 29 | | this.componentsWriter = componentsWriter; |
| 4 | 30 | | this.componentPool = componentPool; |
| 4 | 31 | | this.getCurrentSceneNumber = getCurrentSceneNumber; |
| 4 | 32 | | } |
| | 33 | |
|
| | 34 | | public void Update() |
| | 35 | | { |
| 3 | 36 | | var inputResults = inputResultComponent.GetForAll(); |
| 3 | 37 | | int currentSceneNumber = getCurrentSceneNumber(); |
| 3 | 38 | | bool restrictedActionEnabled = false; |
| | 39 | |
|
| 12 | 40 | | for (int i = 0; i < inputResults.Count; i++) |
| | 41 | | { |
| 3 | 42 | | var model = inputResults[i].value.model; |
| | 43 | |
|
| 3 | 44 | | if (!model.dirty) |
| | 45 | | continue; |
| | 46 | |
|
| 2 | 47 | | var scene = inputResults[i].value.scene; |
| 2 | 48 | | var entity = inputResults[i].value.entity; |
| 2 | 49 | | long entityId = entity.entityId; |
| 2 | 50 | | InternalEngineInfo engineInfoModel = engineInfoComponent.GetFor(scene, SpecialEntityId.SCENE_ROOT_ENTITY |
| 2 | 51 | | bool checkRestrictedAction = scene.isPersistent || scene.sceneData.sceneNumber == currentSceneNumber; |
| | 52 | |
|
| 2 | 53 | | if (!componentsWriter.TryGetValue(scene.sceneData.sceneNumber, out var writer)) |
| | 54 | | continue; |
| | 55 | |
|
| 2 | 56 | | int count = model.events.Count; |
| | 57 | |
|
| 16 | 58 | | for (int j = 0; j < count; j++) |
| | 59 | | { |
| 6 | 60 | | InternalInputEventResults.EventData inputEvent = model.events[j]; |
| | 61 | |
|
| 6 | 62 | | InputAction actionButton = inputEvent.button; |
| 6 | 63 | | PointerEventType evtType = inputEvent.type; |
| 6 | 64 | | RaycastHit hit = inputEvent.hit; |
| | 65 | |
|
| 6 | 66 | | if (checkRestrictedAction && !restrictedActionEnabled) |
| | 67 | | { |
| 6 | 68 | | if (IsValidInputForRestrictedActions(entityId, actionButton, evtType, hit)) |
| | 69 | | { |
| 0 | 70 | | restrictedActionEnabled = true; |
| 0 | 71 | | engineInfoComponent.PutFor(currentSceneNumber, SpecialEntityId.SCENE_ROOT_ENTITY, engineInfo |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| 6 | 75 | | var componentPooled = componentPool.Get(); |
| 6 | 76 | | var componentModel = componentPooled.WrappedComponent.Model; |
| 6 | 77 | | componentModel.Button = inputEvent.button; |
| 6 | 78 | | componentModel.Hit = inputEvent.hit; |
| 6 | 79 | | componentModel.State = inputEvent.type; |
| 6 | 80 | | componentModel.Timestamp = lastTimestamp++; |
| 6 | 81 | | componentModel.TickNumber = engineInfoModel.SceneTick; |
| | 82 | |
|
| 6 | 83 | | writer.Append(entityId, ComponentID.POINTER_EVENTS_RESULT, componentPooled); |
| | 84 | | } |
| | 85 | |
|
| 2 | 86 | | model.events.Clear(); |
| | 87 | | } |
| 3 | 88 | | } |
| | 89 | |
|
| | 90 | | private static bool IsValidInputForRestrictedActions(long entityId, InputAction actionButton, |
| | 91 | | PointerEventType evtType, RaycastHit hit) |
| | 92 | | { |
| 6 | 93 | | if (entityId == SpecialEntityId.SCENE_ROOT_ENTITY) |
| 6 | 94 | | return false; |
| | 95 | |
|
| 0 | 96 | | return hit != null |
| | 97 | | && (evtType == PointerEventType.PetDown || evtType == PointerEventType.PetUp) |
| | 98 | | && actionButton != InputAction.IaAny; |
| | 99 | | } |
| | 100 | | } |
| | 101 | | } |