Advertisement
443eb9

Untitled

Sep 30th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.93 KB | None | 0 0
  1. [UpdateInGroup(typeof(SyncPointCauserSystemGroup))]
  2. public partial class BuildingSystem : SystemBase
  3. {
  4.     private bool _onBuildPreview;
  5.     private Camera _mainCamera;
  6.     private Entity _staticResourcesContainer;
  7.     private Entity _candidateEntity;
  8.     private BuildingAuthoring _candidateBuilding;
  9.     private BatchMaterialID _buildPreviewOkMaterial;
  10.     private BatchMaterialID _buildPreviewErrorMaterial;
  11.     private BatchMaterialID _buildRequestMaterial;
  12.     private EndSimulationEntityCommandBufferSystem _ecbSystem;
  13.     protected override void OnCreate()
  14.     {
  15.         _ecbSystem = World.GetOrCreateSystemManaged<EndSimulationEntityCommandBufferSystem>();
  16.         RequireForUpdate<PrefabsContainerData>();
  17.     }
  18.     protected override void OnStartRunning()
  19.     {
  20.         EventCenter.Register<BuildingAuthoring>(EventType.OnBuildPreview, OnBuildPreview);
  21.         EventCenter.Register<BuildingAuthoring, HexUnitAspect>(EventType.OnRequestBuild, OnBuildRequest);
  22.         var inputAsset = InputManager.Instance.InputAsset;
  23.         inputAsset.InGame.CancelAction.performed += OnPerformCancel;
  24.         _mainCamera = Camera.main;
  25.         _staticResourcesContainer = SystemAPI.GetSingletonEntity<StaticResourcesContainerAspect>();
  26.         var container = SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer);
  27.         _buildPreviewOkMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewOk.GetInstanceID());
  28.         _buildPreviewErrorMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewError.GetInstanceID());
  29.         _buildRequestMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildRequest.GetInstanceID());
  30.     }
  31.     protected override void OnUpdate()
  32.     {
  33.         if (_onBuildPreview)
  34.             PreviewBuilding();
  35.     }
  36.     private void OnBuildPreview(BuildingAuthoring building)
  37.     {
  38.         if (_onBuildPreview && building.buildingName == _candidateBuilding.buildingName)
  39.             return;
  40.         _candidateBuilding = building;
  41.         SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer)
  42.             .TryGetBuildingPrefab(building.buildingName, out var candidate);
  43.         _candidateEntity = EntityManager.Instantiate(candidate);
  44.         _onBuildPreview = true;
  45.     }
  46.     private void OnBuildRequest(BuildingAuthoring building, HexUnitAspect unit)
  47.     {
  48.         EntityCommandBuffer ecb = _ecbSystem.CreateCommandBuffer();
  49.         ecb.RemoveComponent<PreviewBuildingTag>(_candidateEntity);
  50.         ecb.AddComponent<WaitingForTaskRegTag>(_candidateEntity);
  51.         ecb.AddComponent<BuildTaskData>(_candidateEntity);
  52.         ecb.SetComponent(_candidateEntity, new BuildTaskData
  53.         {
  54.             priority = 1
  55.         });
  56.         ecb.SetComponent(_candidateEntity, new BuildingMaterialData
  57.         {
  58.             missing = new NativeParallelHashMap<int, int>(4, Allocator.Persistent),
  59.             transmitted = new NativeParallelHashMap<int, int>(4, Allocator.Persistent),
  60.             required = new NativeParallelHashMap<int, int>(4, Allocator.Persistent)
  61.         });
  62.     }
  63.     private void PreviewBuilding()
  64.     {
  65.         if (!Raycast(out var targetUnit))
  66.             return;
  67.         PreviewBuildingAspect building = SystemAPI.GetAspect<PreviewBuildingAspect>(_candidateEntity);
  68.         building.Position = targetUnit.SurfaceCenterNoZFighting;
  69.         bool isBuildable = targetUnit.Data.occupant == Entity.Null && !targetUnit.Data.isUnderwater;
  70.         if (Pointer.current.press.wasPressedThisFrame && isBuildable)
  71.         {
  72.             SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID = _buildRequestMaterial;
  73.             targetUnit.Occupy(_candidateEntity);
  74.             _onBuildPreview = false;
  75.             EventCenter.BroadCast(EventType.OnRequestBuild, _candidateBuilding, targetUnit);
  76.         }
  77.         else
  78.         {
  79.             SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID =
  80.                 isBuildable ? _buildPreviewOkMaterial : _buildPreviewErrorMaterial;
  81.         }
  82.     }
  83.     private bool Raycast(out HexUnitAspect result)
  84.     {
  85.         SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld
  86.             .CastRay(RaycastInputFactory.CreateRaycastInput
  87.             (
  88.                 _mainCamera.ScreenPointToRay(Pointer.current.position.value),
  89.                 CollisionLayer.PlayerRaycast
  90.             ), out var hit);
  91.         if (!SystemAPI.HasComponent<HexUnitData>(hit.Entity))
  92.         {
  93.             result = default;
  94.             return false;
  95.         }
  96.         result = SystemAPI.GetAspect<HexUnitAspect>(hit.Entity);
  97.         return true;
  98.     }
  99.     private void OnPerformCancel(InputAction.CallbackContext ctx)
  100.     {
  101.         if (!_onBuildPreview)
  102.             return;
  103.         EntityManager.DestroyEntity(_candidateEntity);
  104.         _candidateEntity = Entity.Null;
  105.         _onBuildPreview = false;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement