Exemplo n.º 1
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        var projectileTranslations = m_projectilesQuery.ToComponentDataArray <Translation>(Allocator.TempJob);
        var projectileCollisions   = m_projectilesQuery.ToComponentDataArray <CollisionSize>(Allocator.TempJob);
        var projectiles            = m_projectilesQuery.ToEntityArray(Allocator.TempJob);

        poolLocations.Clear();
        EntityManager.GetAllUniqueSharedComponentData <ProjectilePoolLocation>(poolLocations);

        if (projectileCollisions.Length > 0 && poolLocations.Count > 1)
        {
            var job = new ProjectileCollisionJob
            {
                ProjectileTranslations = projectileTranslations,
                ProjectileCollisions   = projectileCollisions,
                Projectiles            = projectiles,
                ECB          = m_beginSimEcbSystem.CreateCommandBuffer().ToConcurrent(),
                PoolLocation = poolLocations[1].Value
            };

            inputDeps = job.Schedule(this, inputDeps);
            m_projectilesQuery.AddDependency(inputDeps);
            m_beginSimEcbSystem.AddJobHandleForProducer(inputDeps);
        }
        else
        {
            projectileTranslations.Dispose();
            projectileCollisions.Dispose();
            projectiles.Dispose();
        }

        return(inputDeps);
    }
    protected override void OnUpdate()
    {
        var          ecb              = es_ecb.CreateCommandBuffer();
        var          parallelECB      = ecb.ToConcurrent();
        PhysicsWorld pw               = bpw.PhysicsWorld;
        float        deltaTime        = Time.DeltaTime;
        float3       directionToShoot = float3.zero;

        unsafe
        {
            Entities.ForEach((ref ShooterComponentData scd, ref Translation trans, ref Rotation rot, in LocalToWorld ltw) =>
            {
                scd.elapsedTime += deltaTime;
                ColliderDistanceInput colliderDistanceInput = new ColliderDistanceInput
                {
                    Collider    = (Unity.Physics.Collider *)(scd.colliderCast.GetUnsafePtr()),
                    Transform   = new RigidTransform(rot.Value, trans.Value),
                    MaxDistance = 0.25f
                };
                if (pw.CalculateDistance(colliderDistanceInput, out DistanceHit hit))
                {
                    directionToShoot            = math.normalize(hit.Position - trans.Value);
                    rot.Value                   = math.slerp(rot.Value, quaternion.LookRotation(directionToShoot, math.up()), deltaTime * 25);
                    scd.projectileSpawnPosition = math.transform(ltw.Value, new float3(0, 0.3f, 0.7f));
                    if (scd.elapsedTime >= scd.firingInterval)
                    {
                        scd.elapsedTime   = 0;
                        Entity projectile = ecb.Instantiate(scd.projectile);
                        ecb.SetComponent(projectile, new Translation {
                            Value = scd.projectileSpawnPosition
                        });
                        ecb.SetComponent(projectile, new Rotation {
                            Value = quaternion.LookRotation(ltw.Forward, math.up())
                        });
                        ecb.AddComponent <ProjectileFired>(projectile);
                        ecb.SetComponent(projectile, new ProjectileFired
                        {
                            elapsedTime        = 0,
                            projectileSpeed    = scd.projectileSpeed,
                            projectileLifeTime = scd.projectileLifeTime
                        });
                    }
                }
            }).Run();
        }

        Entities.ForEach((Entity e, int entityInQueryIndex, ref ProjectileFired pf, ref Translation trans, in LocalToWorld ltw) =>
        {
            pf.elapsedTime += deltaTime;
            trans.Value    += ltw.Forward * pf.projectileSpeed * deltaTime;
            if (pf.elapsedTime > pf.projectileLifeTime)
            {
                parallelECB.DestroyEntity(entityInQueryIndex, e);
            }
        }).ScheduleParallel();

        es_ecb.AddJobHandleForProducer(Dependency);

        JobHandle jh = new ProjectileCollisionJob()
        {
            enemyGroup            = GetComponentDataFromEntity <EnemyComponentData>(),
            activeProjectileGroup = GetComponentDataFromEntity <ProjectileFired>(),
            ecb = es_ecb_Job.CreateCommandBuffer()
        }.Schedule(spw.Simulation, ref bpw.PhysicsWorld, Dependency);

        es_ecb_Job.AddJobHandleForProducer(jh);
    }