Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [UpdateInGroup(typeof(SyncPointCauserSystemGroup))]
- public partial class BuildingSystem : SystemBase
- {
- private bool _onBuildPreview;
- private Camera _mainCamera;
- private Entity _staticResourcesContainer;
- private Entity _candidateEntity;
- private BuildingAuthoring _candidateBuilding;
- private BatchMaterialID _buildPreviewOkMaterial;
- private BatchMaterialID _buildPreviewErrorMaterial;
- private BatchMaterialID _buildRequestMaterial;
- private EndSimulationEntityCommandBufferSystem _ecbSystem;
- protected override void OnCreate()
- {
- _ecbSystem = World.GetOrCreateSystemManaged<EndSimulationEntityCommandBufferSystem>();
- RequireForUpdate<PrefabsContainerData>();
- }
- protected override void OnStartRunning()
- {
- EventCenter.Register<BuildingAuthoring>(EventType.OnBuildPreview, OnBuildPreview);
- EventCenter.Register<BuildingAuthoring, HexUnitAspect>(EventType.OnRequestBuild, OnBuildRequest);
- var inputAsset = InputManager.Instance.InputAsset;
- inputAsset.InGame.CancelAction.performed += OnPerformCancel;
- _mainCamera = Camera.main;
- _staticResourcesContainer = SystemAPI.GetSingletonEntity<StaticResourcesContainerAspect>();
- var container = SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer);
- _buildPreviewOkMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewOk.GetInstanceID());
- _buildPreviewErrorMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildPreviewError.GetInstanceID());
- _buildRequestMaterial = container.GetMaterialIndex(StaticResourcesContainer.Instance.buildRequest.GetInstanceID());
- }
- protected override void OnUpdate()
- {
- if (_onBuildPreview)
- PreviewBuilding();
- }
- private void OnBuildPreview(BuildingAuthoring building)
- {
- if (_onBuildPreview && building.buildingName == _candidateBuilding.buildingName)
- return;
- _candidateBuilding = building;
- SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer)
- .TryGetBuildingPrefab(building.buildingName, out var candidate);
- _candidateEntity = EntityManager.Instantiate(candidate);
- _onBuildPreview = true;
- }
- private void OnBuildRequest(BuildingAuthoring building, HexUnitAspect unit)
- {
- EntityCommandBuffer ecb = _ecbSystem.CreateCommandBuffer();
- ecb.RemoveComponent<PreviewBuildingTag>(_candidateEntity);
- ecb.AddComponent<WaitingForTaskRegTag>(_candidateEntity);
- ecb.AddComponent<BuildTaskData>(_candidateEntity);
- ecb.SetComponent(_candidateEntity, new BuildTaskData
- {
- priority = 1
- });
- ecb.SetComponent(_candidateEntity, new BuildingMaterialData
- {
- missing = new NativeParallelHashMap<int, int>(4, Allocator.Persistent),
- transmitted = new NativeParallelHashMap<int, int>(4, Allocator.Persistent),
- required = new NativeParallelHashMap<int, int>(4, Allocator.Persistent)
- });
- }
- private void PreviewBuilding()
- {
- if (!Raycast(out var targetUnit))
- return;
- PreviewBuildingAspect building = SystemAPI.GetAspect<PreviewBuildingAspect>(_candidateEntity);
- building.Position = targetUnit.SurfaceCenterNoZFighting;
- bool isBuildable = targetUnit.Data.occupant == Entity.Null && !targetUnit.Data.isUnderwater;
- if (Pointer.current.press.wasPressedThisFrame && isBuildable)
- {
- SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID = _buildRequestMaterial;
- targetUnit.Occupy(_candidateEntity);
- _onBuildPreview = false;
- EventCenter.BroadCast(EventType.OnRequestBuild, _candidateBuilding, targetUnit);
- }
- else
- {
- SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID =
- isBuildable ? _buildPreviewOkMaterial : _buildPreviewErrorMaterial;
- }
- }
- private bool Raycast(out HexUnitAspect result)
- {
- SystemAPI.GetSingleton<PhysicsWorldSingleton>().CollisionWorld
- .CastRay(RaycastInputFactory.CreateRaycastInput
- (
- _mainCamera.ScreenPointToRay(Pointer.current.position.value),
- CollisionLayer.PlayerRaycast
- ), out var hit);
- if (!SystemAPI.HasComponent<HexUnitData>(hit.Entity))
- {
- result = default;
- return false;
- }
- result = SystemAPI.GetAspect<HexUnitAspect>(hit.Entity);
- return true;
- }
- private void OnPerformCancel(InputAction.CallbackContext ctx)
- {
- if (!_onBuildPreview)
- return;
- EntityManager.DestroyEntity(_candidateEntity);
- _candidateEntity = Entity.Null;
- _onBuildPreview = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement