예제 #1
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            // take input and move the player
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            float dt        = World.Time.DeltaTime;
            float degenRate = healthDegenRate;

            JobHandle jobHandle = Entities
                                  .WithAll <Player>()
                                  .ForEach((Entity entity, int entityInQueryIndex, ref Movement movement,
                                            ref Translation position, ref BoundingVolume vol, ref HealthFloat health) =>
            {
                movement.direction.x = h;
                movement.direction.y = v;
                movement.direction   = math.normalizesafe(new float3(h, v, 0));
                movement.direction.z = 0;

                position.Value   += movement.direction * movement.speed * dt;
                vol.volume.center = position.Value;

                // decrease health
                health.curr -= dt * degenRate;
            }).Schedule(inputDeps);

            if (Input.GetMouseButtonDown(0)) // left click
            {
                // bullet was fired, finish the player job first
                jobHandle.Complete();
                float3 clickPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                Translation playerPosition = GetComponentDataFromEntity <Translation>(true)[GameHandler.playerEntity];

                bool foundBullet = false;

                Entities.ForEach((Entity entity, int entityInQueryIndex, ref Bullet bullet,
                                  ref Movement movement, ref Translation position, ref BoundingVolume vol) =>
                {
                    if (!foundBullet && !bullet.isActive)
                    {
                        bullet.isActive      = true;
                        vol.volume.center    = position.Value = playerPosition.Value;
                        movement.speed       = 7;
                        movement.direction   = math.normalizesafe(clickPos - playerPosition.Value);
                        movement.direction.z = 0;

                        foundBullet = true;
                    }
                }).Run();
            }

            jobHandle.Complete();

            // update ui elements based on player data
            Player player = GetComponentDataFromEntity <Player>(true)[GameHandler.playerEntity];

            hud.SetScore(player.score);

            HealthFloat playerHealth = GetComponentDataFromEntity <HealthFloat>(true)[GameHandler.playerEntity];

            hud.SetHealth(playerHealth.curr);

            if (playerHealth.curr <= 0)
            {
                // player died, end the game
                hud.ShowGameOver();
            }

            return(jobHandle);
        }
예제 #2
0
 public static void ModifyHealth(ref HealthFloat health, float mod)
 {
     health.curr += mod;
     health.curr  = math.clamp(health.curr, 0, health.max);
 }
예제 #3
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            EntityCommandBuffer.Concurrent commandBuffer = endSimulationEntityCommandBufferSystem.CreateCommandBuffer().ToConcurrent();
            BoundingVolume playerBounds = GetComponentDataFromEntity <BoundingVolume>(true)[GameHandler.playerEntity];
            HealthFloat    playerHealth = GetComponentDataFromEntity <HealthFloat>(true)[GameHandler.playerEntity];
            Player         player       = GetComponentDataFromEntity <Player>(true)[GameHandler.playerEntity];

            // job for collision between player and enemies
            Entity    playerEntity = GameHandler.playerEntity;
            JobHandle jobHandle    = Entities
                                     .WithNone <Bullet>()
                                     .ForEach((Entity entity, int entityInQueryIndex, ref BoundingVolume vol, ref HealthModifier healthMod) =>
            {
                if (vol.volume.Intersects(playerBounds.volume))
                {
                    // there was a collision, modify the player's health
                    Utils.ModifyHealth(ref playerHealth, healthMod.value);
                    commandBuffer.SetComponent <HealthFloat>(entityInQueryIndex, playerEntity, playerHealth);

                    // get rid of the damager
                    commandBuffer.DestroyEntity(entityInQueryIndex, entity);
                }
            }).Schedule(inputDeps);

            EntityQuery bulletQuery = EntityManager.CreateEntityQuery(typeof(Bullet), ComponentType.ReadOnly <BoundingVolume>(), ComponentType.ReadOnly <HealthModifier>());

            bulletQuery.AddDependency(jobHandle);

            NativeArray <BoundingVolume> bulletColliders  = bulletQuery.ToComponentDataArray <BoundingVolume>(Allocator.TempJob);
            NativeArray <HealthModifier> bulletHealthMods = bulletQuery.ToComponentDataArray <HealthModifier>(Allocator.TempJob);
            NativeArray <Bullet>         bulletInfos      = bulletQuery.ToComponentDataArray <Bullet>(Allocator.TempJob);
            NativeArray <Entity>         bullets          = bulletQuery.ToEntityArray(Allocator.TempJob);

            // job for checking collisions between enemies and bullets
            jobHandle = Entities
                        .WithDeallocateOnJobCompletion(bulletColliders)
                        .WithDeallocateOnJobCompletion(bulletHealthMods)
                        .WithDeallocateOnJobCompletion(bulletInfos)
                        .WithDeallocateOnJobCompletion(bullets)
                        .WithReadOnly(bulletColliders)
                        .WithReadOnly(bulletHealthMods)
                        .WithReadOnly(bulletInfos)
                        .WithReadOnly(bullets)
                        .WithNone <Player>()
                        .WithAll <Enemy>()
                        .ForEach((Entity entity, int entityInQueryIndex, ref BoundingVolume damageableCollider, ref HealthFloat damageableHealth) =>
            {
                for (int i = 0; i < bulletColliders.Length; i++)
                {
                    // bullet isn't active, leave
                    if (!bulletInfos[i].isActive)
                    {
                        continue;
                    }

                    if (damageableCollider.volume.Intersects(bulletColliders[i].volume))
                    {
                        // bullet hit a damageable, reduce it's health
                        Utils.ModifyHealth(ref damageableHealth, bulletHealthMods[i].value);

                        // deactivate the bullet
                        Bullet b   = bulletInfos[i];
                        b.isActive = false;
                        b.age      = 0;
                        commandBuffer.SetComponent <Bullet>(entityInQueryIndex, bullets[i], b);
                    }
                }
            }).Schedule(jobHandle);
            jobHandle.Complete();

            jobHandle = Entities
                        .ForEach((Entity entity, int entityInQueryIndex, ref Enemy enemy, ref HealthFloat health) =>
            {
                if (health.curr <= 0)
                {
                    // destroy any entity who's health has dropped below 0
                    commandBuffer.DestroyEntity(entityInQueryIndex, entity);
                    player.score += enemy.points;
                    commandBuffer.SetComponent <Player>(entityInQueryIndex, playerEntity, player);
                }
            }).Schedule(jobHandle);
            jobHandle.Complete();

            endSimulationEntityCommandBufferSystem.AddJobHandleForProducer(jobHandle);
            return(jobHandle);
        }