Esempio n. 1
0
        public void SetMovementValues()
        {
            _flowFieldQuery      = GetEntityQuery(typeof(FlowFieldData));
            _flowFieldEntity     = _flowFieldQuery.GetSingletonEntity();
            _flowFieldData       = EntityManager.GetComponentData <FlowFieldData>(_flowFieldEntity);
            _destinationCellData = EntityManager.GetComponentData <DestinationCellData>(_flowFieldEntity);
            _entityBuffer        = EntityManager.GetBuffer <EntityBufferElement>(_flowFieldEntity);
            _gridEntities        = _entityBuffer.Reinterpret <Entity>();

            if (_cellDataContainer.IsCreated)
            {
                _cellDataContainer.Dispose();
            }
            _cellDataContainer = new NativeArray <CellData>(_gridEntities.Length, Allocator.Persistent);

            for (int i = 0; i < _entityBuffer.Length; i++)
            {
                _cellDataContainer[i] = GetComponent <CellData>(_entityBuffer[i]);
            }

            Entities.ForEach((ref EntityMovementData entityMovementData) =>
            {
                entityMovementData.destinationReached = false;
            }).Run();
        }
Esempio n. 2
0
        protected override void OnUpdate()
        {
            if (_flowFieldEntity.Equals(Entity.Null))
            {
                return;
            }
            float         deltaTime       = Time.DeltaTime;
            FlowFieldData flowFieldData   = _flowFieldData;
            int2          destinationCell = _destinationCellData.destinationIndex;
            JobHandle     jobHandle       = new JobHandle();

            jobHandle = Entities.ForEach((ref PhysicsVelocity physVelocity, ref EntityMovementData entityMovementData,
                                          ref Translation translation) =>
            {
                int2 curCellIndex = FlowFieldHelper.GetCellIndexFromWorldPos(translation.Value, flowFieldData.gridSize,
                                                                             flowFieldData.cellRadius * 2);

                if (curCellIndex.Equals(destinationCell))
                {
                    entityMovementData.destinationReached = true;
                }

                int flatCurCellIndex = FlowFieldHelper.ToFlatIndex(curCellIndex, flowFieldData.gridSize.y);
                float2 moveDirection = _cellDataContainer[flatCurCellIndex].bestDirection;
                float finalMoveSpeed = (entityMovementData.destinationReached ? entityMovementData.destinationMoveSpeed : entityMovementData.moveSpeed) * deltaTime;

                physVelocity.Linear.xz = moveDirection * finalMoveSpeed;
                translation.Value.y    = 0f;
            }).ScheduleParallel(jobHandle);
            jobHandle.Complete();
        }
        protected override void OnUpdate()
        {
            if (Input.GetMouseButtonDown(0))
            {
                Vector3 mousePos      = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 10);
                Vector3 worldMousePos = _mainCamera.ScreenToWorldPoint(mousePos);

                _flowFieldControllerEntity = _flowFieldControllerQuery.GetSingletonEntity();

                FlowFieldControllerData flowFieldControllerData = EntityManager.GetComponentData <FlowFieldControllerData>(_flowFieldControllerEntity);
                GridDebug.instance.FlowFieldControllerData = flowFieldControllerData;

                FlowFieldData flowFieldData = new FlowFieldData
                {
                    gridSize   = flowFieldControllerData.gridSize,
                    cellRadius = flowFieldControllerData.cellRadius,
                    clickedPos = worldMousePos
                };

                NewFlowFieldData newFlowFieldData = new NewFlowFieldData {
                    isExistingFlowField = true
                };

                if (_flowFieldEntity.Equals(Entity.Null))
                {
                    _flowFieldEntity = EntityManager.CreateEntity();
                    EntityManager.AddComponent <FlowFieldData>(_flowFieldEntity);
                    newFlowFieldData.isExistingFlowField = false;
                }

                EntityManager.AddComponent <NewFlowFieldData>(_flowFieldEntity);
                EntityManager.SetComponentData(_flowFieldEntity, flowFieldData);
                EntityManager.SetComponentData(_flowFieldEntity, newFlowFieldData);
            }
        }