protected override JobHandle OnUpdate(JobHandle inputDeps) { // take input and move the player float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); PlayerUpdateJob playerMoverJob = new PlayerUpdateJob() { h = h, v = v, dt = Time.deltaTime, healthDegenRate = healthDegenRate, }; JobHandle jobHandle = playerMoverJob.Schedule(this, 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]; BulletFireJob fireBulletJob = new BulletFireJob() { clickPos = clickPos, playerPos = playerPosition.Value, }; jobHandle = fireBulletJob.Schedule(this, jobHandle); } jobHandle.Complete(); // update ui elements based on player data Player player = GetComponentDataFromEntity <Player>(true)[GameHandler.playerEntity]; hud.SetScore(player.score); Health playerHealth = GetComponentDataFromEntity <Health>(true)[GameHandler.playerEntity]; hud.SetHealth(playerHealth.curr); if (playerHealth.curr <= 0) { // player died, end the game hud.ShowGameOver(); } return(jobHandle); }
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); }