Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using HexagonSurvive.Containers;
- using HexagonSurvive.Containers.ECS;
- using HexagonSurvive.Containers.ECS.Components;
- using HexagonSurvive.Events;
- using HexagonSurvive.HexPhysics;
- using HexagonSurvive.Map.Aspects;
- using HexagonSurvive.Map.Components;
- using Unity.Entities;
- using Unity.Physics;
- using Unity.Rendering;
- using UnityEngine;
- using UnityEngine.InputSystem;
- using UnityEngine.Rendering;
- using EventType = HexagonSurvive.Events.EventType;
- namespace HexagonSurvive.Game.BuildingSystem
- {
- [UpdateInGroup(typeof(LateSimulationSystemGroup))]
- public partial class BuildingSystem : SystemBase
- {
- private bool _onBuildPreview;
- private Camera _mainCamera;
- private Entity _staticResourcesContainer;
- private Entity _candidateEntity;
- private DefaultInput _input;
- private BuildingMono _candidateBuilding;
- private BatchMaterialID _buildPreviewOkMaterial;
- private BatchMaterialID _buildPreviewErrorMaterial;
- private BatchMaterialID _buildRequestMaterial;
- protected override void OnCreate()
- {
- RequireForUpdate<PrefabsContainerData>();
- _input = new DefaultInput();
- _input.InGame.CancelAction.performed += OnPerformCancel;
- _input.Enable();
- }
- protected override void OnStartRunning()
- {
- EventCenter.Register<BuildingMono>(EventType.OnPreBuild, OnBuildPreview);
- EventCenter.Register<BuildingMono>(EventType.OnRequestBuild, OnBuildRequest);
- _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(BuildingMono building)
- {
- if (_onBuildPreview && building.id == _candidateBuilding.id)
- return;
- _candidateBuilding = building;
- SystemAPI.GetAspect<StaticResourcesContainerAspect>(_staticResourcesContainer)
- .TryGetBuildingPrefab(building.id, out var candidate);
- _candidateEntity = EntityManager.Instantiate(candidate);
- _onBuildPreview = true;
- }
- private void OnBuildRequest(BuildingMono building)
- {
- }
- private void PreviewBuilding()
- {
- if (!Raycast(out var targetUnit))
- return;
- BuildingAspect building = SystemAPI.GetAspect<BuildingAspect>(_candidateEntity);
- building.Position = targetUnit.SurfaceCenterNoZFighting;
- if (Pointer.current.press.wasPressedThisFrame && targetUnit.Occupant == Entity.Null)
- {
- SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID = _buildRequestMaterial;
- targetUnit.Occupant = _candidateEntity;
- _onBuildPreview = false;
- EventCenter.BroadCast(EventType.OnRequestBuild, _candidateBuilding);
- }
- else
- {
- SystemAPI.GetComponentRW<MaterialMeshInfo>(_candidateEntity).ValueRW.MaterialID =
- targetUnit.Occupant == Entity.Null
- ? _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)
- || SystemAPI.GetComponent<HexUnitData>(hit.Entity).isUnderwater)
- {
- result = default;
- return false;
- }
- result = SystemAPI.GetAspect<HexUnitAspect>(hit.Entity);
- return true;
- }
- private void OnPerformCancel(InputAction.CallbackContext ctx)
- {
- if (!_onBuildPreview)
- return;
- EntityManager.DestroyEntity(_candidateEntity);
- _onBuildPreview = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement