using Unity.Entities;
using Unity.Tiny.Audio;
namespace TinyKitchen
{
///
/// Play dynamic audio according to the game state and player actions
///
public class PlayAudio : SystemBase
{
protected override void OnUpdate()
{
// Get access to the game states
var game = GetSingleton();
// Get access to dynamic audio
var audioManager = GetSingleton();
var audioFlyingFoodEntity = audioManager.FlyingFoodAudio;
var flyingFoodAudioSource = EntityManager.GetComponentData(audioFlyingFoodEntity);
var audioBouncingFoodEntity = audioManager.BouncingFoodAudio;
var bouncingFoodAudioSource = EntityManager.GetComponentData(audioBouncingFoodEntity);
if (game.gameState == GameState.Flying)
{
// Play flying audio
if (!flyingFoodAudioSource.isPlaying)
EntityManager.AddComponent(audioFlyingFoodEntity);
}
else
{
// Stop flying audio
if (flyingFoodAudioSource.isPlaying)
EntityManager.AddComponent(audioFlyingFoodEntity);
}
// Play rebound audio
Entities.ForEach((Entity entity, ref FoodInstanceComponent foodInstanceComponent) =>
{
if (foodInstanceComponent.playBouncingAudio && !foodInstanceComponent.hasPlayedBouncingAudio)
{
foodInstanceComponent.hasPlayedBouncingAudio = true;
EntityManager.AddComponent(audioBouncingFoodEntity);
}
}).WithStructuralChanges().Run();
}
}
}