using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;
using Unity.Tiny.Text;
namespace TinyKitchen
{
///
/// Update UI texts values and add simple motion
///
public class UpdateUI : SystemBase
{
protected override void OnCreate()
{
RequireSingletonForUpdate();
RequireSingletonForUpdate();
base.OnCreate();
}
protected override void OnUpdate()
{
// Get access to UI entities
var scoreUI = GetSingletonEntity();
var fanSpeedUI = GetSingletonEntity();
var game = GetSingleton();
if (game.gameState == GameState.Initialization)
return;
// Set Score text value
TextLayout.SetEntityTextRendererString(EntityManager, scoreUI, game.score.ToString());
// Set fan force text value
var fan = GetSingleton();
var amt = fan.fanForce;
TextLayout.SetEntityTextRendererString(EntityManager, fanSpeedUI, amt.ToString());
var time = (float) Time.ElapsedTime;
// Add simple motion to the texts
Entities.ForEach((ref Translation pos, in UIAnimated ui) =>
{
var t = time * ui.swaySpeed;
var rx = noise.snoise(math.float2(t + 0));
var ry = noise.snoise(math.float2(t + 1));
pos.Value = ui.origin + math.float3(rx, ry, 0.0f) * ui.swayAmount;
}).ScheduleParallel();
}
}
}