Advertisement
443eb9

Untitled

Sep 26th, 2023
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.91 KB | None | 0 0
  1. using HexagonSurvive.Containers;
  2. using HexagonSurvive.Containers.ECS;
  3. using HexagonSurvive.Containers.ECS.Components;
  4. using HexagonSurvive.Events;
  5. using HexagonSurvive.HexPhysics;
  6. using HexagonSurvive.Map.Aspects;
  7. using HexagonSurvive.Map.Components;
  8. using Unity.Entities;
  9. using Unity.Physics;
  10. using Unity.Rendering;
  11. using UnityEngine;
  12. using UnityEngine.InputSystem;
  13. using UnityEngine.Rendering;
  14. using EventType = HexagonSurvive.Events.EventType;
  15.  
  16. namespace HexagonSurvive.Game.BuildingSystem
  17. {
  18.     [UpdateInGroup(typeof(LateSimulationSystemGroup))]
  19.     public partial class BuildingSystem : SystemBase
  20.     {
  21.         private bool _onBuildPreview;
  22.         private Camera _mainCamera;
  23.         private Entity _staticResourcesContainer;
  24.         private Entity _candidateEntity;
  25.         private DefaultInput _input;
  26.         private BuildingMono _candidateBuilding;
  27.         private BatchMaterialID _buildPreviewOkMaterial;
  28.         private BatchMaterialID _buildPreviewErrorMaterial;
  29.         private BatchMaterialID _buildRequestMaterial;
  30.  
  31.         protected override void OnCreate()
  32.         {
  33.             RequireForUpdate<PrefabsContainerData>();
  34.  
  35.             _input = new DefaultInput();
  36.             _input.InGame.CancelAction.performed += OnPerformCancel;
  37.             _input.Enable();
  38.         }
  39.  
  40.         protected override void OnStartRunning()
  41.         {
  42.             EventCenter.Register<BuildingMono>(EventType.OnPreBuild, OnBuildPreview);
  43.             EventCenter.Register<BuildingMono>(EventType.OnRequestBuild, OnBuildRequest);
  44.  
  45.             _mainCamera = Camera.main;
  46.             _staticResourcesContainer = SystemAPI.GetSingletonEntity<StaticResourcesContainerAspect>();
  47.             var container = SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer);
  48.  
  49.             _buildPreviewOkMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewOk.GetInstanceID());
  50.             _buildPreviewErrorMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewError.GetInstanceID());
  51.             _buildRequestMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildRequest.GetInstanceID());
  52.         }
  53.  
  54.         protected override void OnUpdate()
  55.         {
  56.             if (_onBuildPreview)
  57.                 PreviewBuilding();
  58.         }
  59.  
  60.         private void OnBuildPreview(BuildingMono building)
  61.         {
  62.             if (_onBuildPreview && building.id == _candidateBuilding.id)
  63.                 return;
  64.  
  65.             _candidateBuilding = building;
  66.             SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer)
  67.                 .TryGetBuildingPrefab(building.id, out var candidate);
  68.             _candidateEntity = EntityManager.Instantiate(candidate);
  69.             _onBuildPreview = true;
  70.         }
  71.  
  72.         private void OnBuildRequest(BuildingMono building)
  73.         {
  74.         }
  75.  
  76.         private void PreviewBuilding()
  77.         {
  78.             if (!Raycast(out var targetUnit))
  79.                 return;
  80.  
  81.             BuildingAspect building = SystemAPI.GetAspect<BuildingAspect>(_candidateEntity);
  82.             building.Position = targetUnit.SurfaceCenterNoZFighting;
  83.  
  84.             if (Pointer.current.press.wasPressedThisFrame && targetUnit.Occupant == Entity.Null)
  85.             {
  86.                 SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID = _buildRequestMaterial;
  87.                 targetUnit.Occupant = _candidateEntity;
  88.                 _onBuildPreview = false;
  89.                 EventCenter.BroadCast(EventType.OnRequestBuild, _candidateBuilding);
  90.             }
  91.             else
  92.             {
  93.                 SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID =
  94.                     targetUnit.Occupant == Entity.Null
  95.                         ? _buildPreviewOkMaterial
  96.                         : _buildPreviewErrorMaterial;
  97.             }
  98.         }
  99.  
  100.         private bool Raycast(out HexUnitAspect result)
  101.         {
  102.             SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld
  103.                 .CastRay(RaycastInputFactory.CreateRaycastInput
  104.                 (
  105.                     _mainCamera.ScreenPointToRay(Pointer.current.position.value),
  106.                     CollisionLayer.PlayerRaycast
  107.                 ), out var hit);
  108.  
  109.             if (!SystemAPI.HasComponent<HexUnitData>(hit.Entity)
  110.                 || SystemAPI.GetComponent<HexUnitData>(hit.Entity).isUnderwater)
  111.             {
  112.                 result = default;
  113.                 return false;
  114.             }
  115.  
  116.             result = SystemAPI.GetAspect<HexUnitAspect>(hit.Entity);
  117.             return true;
  118.         }
  119.  
  120.         private void OnPerformCancel(InputAction.CallbackContext ctx)
  121.         {
  122.             if (!_onBuildPreview)
  123.                 return;
  124.  
  125.             EntityManager.DestroyEntity(_candidateEntity);
  126.             _onBuildPreview = false;
  127.         }
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement