Beispiel #1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        int curEntityCnt = m_mobQuery.CalculateEntityCount();

        if (na_rayCommands.Length != curEntityCnt)
        {
            mobMovementJH.Complete();

            na_rayCommands.Dispose();
            na_rayCommands = new NativeArray <RaycastCommand>(curEntityCnt, Allocator.Persistent);

            na_rayHits.Dispose();
            na_rayHits = new NativeArray <RaycastHit>(curEntityCnt, Allocator.Persistent);
        }

        var setupRaycastJob = new SetupRaycastJob
        {
            layerMask       = 1 << LayerMask.NameToLayer("Wall"),
                rayCommands = na_rayCommands,
                wallPos     = GameManager.instance.Target.transform.position
        };

        var setupJH   = setupRaycastJob.Schedule(this, inputDeps);
        var raycastJH = RaycastCommand.ScheduleBatch(na_rayCommands, na_rayHits, 100, setupJH);

        var mobMovementJob = new MobMovementJob
        {
            deltaTime = Time.deltaTime,
            hits      = na_rayHits
        };

        mobMovementJH = mobMovementJob.Schedule(this, raycastJH);
        mobMovementJH.Complete();

        var entities = m_mobQuery.ToEntityArray(Allocator.TempJob);

        for (int i = 0; i < entities.Length; ++i)
        {
            if (EntityManager.GetComponentData <MobStateData>(entities[i]).Value == MobState.Throw)
            {
                Vector3       curPos = EntityManager.GetComponentData <Translation>(entities[i]).Value;
                SpearBehavior spear  = PoolManager.instance.SpearPool.SpawnObject(curPos + SettingsManager.ThrowingPoint,
                                                                                  Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehavior;

                spear.Throw();
                EntityManager.SetComponentData(entities[i], new MobStateData {
                    Value = MobState.FromTarget
                });
            }
        }
        entities.Dispose();

        return(mobMovementJH);
    }
Beispiel #2
0
        protected override JobHandle OnUpdate(JobHandle deps)
        {
            _position    = _vehicleGroup.GetComponentDataArray <Position>();
            _rotation    = _vehicleGroup.GetComponentDataArray <Rotation>();
            _vehicleData = _vehicleGroup.GetComponentDataArray <VehicleData>();
            _hitResult   = _vehicleGroup.GetComponentDataArray <HitResult>();

            //RoadGraph.Instance.VehicleData = _vehicleData;

            var commands = new NativeArray <RaycastCommand>(Capacity, Allocator.TempJob);
            var hits     = new NativeArray <RaycastHit>(Capacity, Allocator.TempJob);

            // 1: Setup Raycast for environment sensing
            var setupRaycastJob = new SetupRaycastJob
            {
                Commands    = commands,
                VehicleData = _vehicleData,
            };

            deps = setupRaycastJob.Schedule(Capacity, 64, deps);

            // 2: Raycast jobs
            deps = RaycastCommand.ScheduleBatch(commands, hits, 64, deps);

            // 3: Transfer raycast result to vehicledata
            var transferJob = new TransferRaycastResultJob
            {
                Commands    = commands,
                RaycastHits = hits,
                HitResult   = _hitResult
            };

            deps = transferJob.Schedule(Capacity, 64, deps);

            // 4: move vehicle
            var vehicleMoveJob = new VehicleMoveJob()
            {
                Positions    = _position,
                Rotations    = _rotation,
                VehicleData  = _vehicleData,
                RoadNodes    = _roadNodes,
                RoadSegments = _roadSegments,
                RandSeed     = _randSeed,
                HitResult    = _hitResult
            };

            vehicleMoveJob.FrameSeed = (uint)Time.frameCount;
            vehicleMoveJob.DeltaTime = Time.deltaTime;
            deps = vehicleMoveJob.Schedule(Capacity, 64, deps);

            return(deps);
        }
    private void Update()
    {
        if (!taa_mobs.isCreated || taa_mobs.length == 0 ||
            nl_startPos.Length != taa_mobs.length ||
            nl_targetPos.Length != taa_mobs.length ||
            na_mobStates.Length != taa_mobs.length)
        {
            return;
        }

        #region ARRAY_SETUP
        if (!na_rayCommands.IsCreated || na_rayCommands.Length != taa_mobs.length)
        {
            movementJH.Complete();

            if (na_rayCommands.IsCreated)
            {
                na_rayCommands.Dispose();
            }

            if (na_rayHits.IsCreated)
            {
                na_rayHits.Dispose();
            }

            na_rayCommands = new NativeArray <RaycastCommand>(taa_mobs.length, Allocator.Persistent);
            na_rayHits     = new NativeArray <RaycastHit>(taa_mobs.length, Allocator.Persistent);
        }

        if (!na_spearRayCommands.IsCreated || na_spearRayCommands.Length != taa_spears.length)
        {
            spearMovementJH.Complete();

            if (na_spearRayCommands.IsCreated)
            {
                na_spearRayCommands.Dispose();
            }

            if (na_spearRayHits.IsCreated)
            {
                na_spearRayHits.Dispose();
            }

            na_spearRayCommands = new NativeArray <RaycastCommand>(taa_spears.length, Allocator.Persistent);
            na_spearRayHits     = new NativeArray <RaycastHit>(taa_spears.length, Allocator.Persistent);
        }

        #endregion

        var setupRaycastJob = new SetupRaycastJob
        {
            layerMask       = 1 << LayerMask.NameToLayer("Wall"),
                rayCommands = na_rayCommands,
                targetPos   = nl_targetPos,
                wallPos     = GameManager.instance.Target.transform.position
        };

        var setupJH   = setupRaycastJob.Schedule(taa_mobs);
        var raycastJH = RaycastCommand.ScheduleBatch(na_rayCommands, na_rayHits, 100, setupJH);

        var mobMovementJob = new MobMovementJob
        {
            deltaTime = Time.deltaTime,
            hits      = na_rayHits,
            mobStates = na_mobStates,
            startPos  = nl_startPos,
            targetPos = nl_targetPos
        };

        movementJH = mobMovementJob.Schedule(taa_mobs, raycastJH);

        var spearGravityJob = new SpearGravityJob
        {
            deltaTime  = Time.deltaTime,
            Gravity    = Physics.gravity,
            State      = na_spearState,
            Velocities = na_spearVelocities
        };

        var spearGravityJH = spearGravityJob.Schedule(taa_spears, movementJH);

        var spearRaycastSetup = new SpearRaycastSetupJob
        {
            deltaTime      = Time.deltaTime,
            layer          = 1 << LayerMask.NameToLayer("Wall"),
                Raycasts   = na_spearRayCommands,
                Velocities = na_spearVelocities
        };

        var spearRaySetupJH = spearRaycastSetup.Schedule(taa_spears, spearGravityJH);

        var spearRaycastBatchJH = RaycastCommand.ScheduleBatch(na_spearRayCommands, na_spearRayHits, 100, spearRaySetupJH);

        var spearCollisionResponse = new SpearCalculateResponse
        {
            Hits       = na_spearRayHits,
            States     = na_spearState,
            Velocities = na_spearVelocities
        };

        var responseJH = spearCollisionResponse.Schedule(taa_spears.length, 100, spearRaycastBatchJH);

        var spearMovementJob = new SpearMovementJob
        {
            DeltaTime  = Time.deltaTime,
            Hits       = na_spearRayHits,
            State      = na_spearState,
            Velocities = na_spearVelocities
        };

        spearMovementJH = spearMovementJob.Schedule(taa_spears, responseJH);

        spearMovementJH.Complete();

        for (int i = 0; i < na_mobStates.Length; ++i)
        {
            if (na_mobStates[i] == MobState.Throw)
            {
                SpearBehaviorExt spear = PoolManager.instance.SpearPool.SpawnObject(taa_mobs[i].position + SettingsManager.ThrowingPoint,
                                                                                    Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehaviorExt;

                if (spear)
                {
                    int spearIndex = PoolManager.instance.SpearPool.IndexOf(spear);
                    na_spearState[spearIndex]       = SpearState.Starting;
                    taa_spears[spearIndex].position = taa_mobs[i].position + SettingsManager.ThrowingPoint;
                    taa_spears[spearIndex].rotation = Quaternion.Euler(SettingsManager.ThrowingRotation);
                    na_mobStates[i] = MobState.FromTarget;
                }
            }
        }

        for (int i = 0; i < na_spearState.Length; ++i)
        {
            if (na_spearState[i] == SpearState.Inactive)
            {
                var spear = PoolManager.Instance.SpearPool.GetAt(i);
                if (spear && spear.isActiveAndEnabled)
                {
                    PoolManager.Instance.SpearPool.ReturnToPool(spear);
                }
            }
        }
    }
    private void Update()
    {
        if (!taa_mobs.isCreated || taa_mobs.length == 0 ||
            nl_startPos.Length != taa_mobs.length ||
            nl_targetPos.Length != taa_mobs.length ||
            na_mobStates.Length != taa_mobs.length)
        {
            return;
        }

        if (!na_rayCommands.IsCreated || na_rayCommands.Length != taa_mobs.length)
        {
            movementJH.Complete();

            if (na_rayCommands.IsCreated)
            {
                na_rayCommands.Dispose();
            }

            if (na_rayHits.IsCreated)
            {
                na_rayHits.Dispose();
            }

            na_rayCommands = new NativeArray <RaycastCommand>(taa_mobs.length, Allocator.Persistent);
            na_rayHits     = new NativeArray <RaycastHit>(taa_mobs.length, Allocator.Persistent);
        }

        var setupRaycastJob = new SetupRaycastJob
        {
            layerMask       = 1 << LayerMask.NameToLayer("Wall"),
                rayCommands = na_rayCommands,
                targetPos   = nl_targetPos,
                wallPos     = GameManager.instance.Target.transform.position
        };

        var setupJH   = setupRaycastJob.Schedule(taa_mobs);
        var raycastJH = RaycastCommand.ScheduleBatch(na_rayCommands, na_rayHits, 100, setupJH);

        var mobMovementJob = new MobMovementJob
        {
            deltaTime = Time.deltaTime,
            hits      = na_rayHits,
            mobStates = na_mobStates,
            startPos  = nl_startPos,
            targetPos = nl_targetPos
        };

        movementJH = mobMovementJob.Schedule(taa_mobs, raycastJH);

        movementJH.Complete();

        for (int i = 0; i < na_mobStates.Length; ++i)
        {
            if (na_mobStates[i] == MobState.Throw)
            {
                SpearBehavior spear = PoolManager.instance.SpearPool.SpawnObject(taa_mobs[i].position + SettingsManager.ThrowingPoint,
                                                                                 Quaternion.Euler(SettingsManager.ThrowingRotation)) as SpearBehavior;

                spear.Throw();
                na_mobStates[i] = MobState.FromTarget;
            }
        }
    }