using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Tiny.Audio;
namespace TinyKitchen
{
///
/// Check if the food has entered the pot hole
///
public class HitPot : SystemBase
{
protected override void OnUpdate()
{
var game = GetSingleton();
// Get access to dynamic sound
var dynamicAudio = GetSingleton();
var touchPotSound = dynamicAudio.TouchPotAudio;
var touchPotSoundData = EntityManager.GetComponentData(touchPotSound);
var successSound = dynamicAudio.SuccesAudio;
var successSoundData = EntityManager.GetComponentData(successSound);
var settings = GetSingleton();
Entity potEntity = EntityManager.CreateEntityQuery(typeof(PotComponent)).GetSingletonEntity();
// Get access to pot transform datas
var potPosition = EntityManager.GetComponentData(potEntity);
var potRotation = EntityManager.GetComponentData(potEntity);
var potSize = EntityManager.GetComponentData(potEntity);
Entities.ForEach((Entity entity, ref Translation pos, in FoodInstanceComponent food) =>
{
// Define position of the food
var diff = pos.Value - potPosition.Value;
var normal = math.mul(potRotation.Value, math.up());
var dist = math.dot(normal, diff);
var proj = pos.Value - dist * normal;
diff = proj - potPosition.Value;
// Define properties of the pot hole
var bounds = math.float2(potSize.Value.x, potSize.Value.z) * 0.5f;
var contain = math.length(diff) < bounds.x - settings.potEdgeThickness;
contain = contain && math.abs(dist) < bounds.y * 0.5f;
// check if the food is in the pot
if (contain)
{
// Play audio
EntityManager.AddComponent(touchPotSound);
EntityManager.AddComponent(successSound);
// Destroy food
EntityManager.DestroyEntity(entity);
// Set new game values
++game.score;
game.scoredLast = true;
game.gameState = GameState.Scored;
SetSingleton(game);
}
}).WithStructuralChanges().WithoutBurst().Run();
}
}
}