/// ---------------------------------------------
/// Behavior Designer
/// Copyright (c) Opsive. All Rights Reserved.
/// https://www.opsive.com
/// ---------------------------------------------
namespace Opsive.BehaviorDesigner.Samples
{
using Unity.Burst;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
using UnityEngine;
///
/// The amount to rotate the entity by.
///
public struct LocalRotation : IComponentData
{
[Tooltip("The rotation speed.")]
public float3 RotationSpeed;
}
///
/// Applies the rotation specified by the LocalRotation component.
///
[DisableAutoCreation]
public partial struct LocalRotationSystem : ISystem
{
private EntityQuery m_Query;
///
/// Creates the required objects for use within the job system.
///
/// The current SystemState.
[BurstCompile]
private void OnCreate(ref SystemState state)
{
m_Query = new EntityQueryBuilder(Allocator.Temp)
.WithAllRW().WithAll()
.Build(ref state);
}
///
/// Starts the RotateJob.
///
/// The current SystemState.
private void OnUpdate(ref SystemState state)
{
var deltaTime = SystemAPI.Time.DeltaTime;
state.Dependency = new RotateJob()
{
DeltaTime = SystemAPI.Time.DeltaTime,
}.ScheduleParallel(m_Query, state.Dependency);
}
///
/// Sets the rotation to the transform
///
[BurstCompile]
private partial struct RotateJob : IJobEntity
{
[Tooltip("The current frame's DeltaTime.")]
public float DeltaTime;
///
/// Updates the logic.
///
/// The entity's transform.
/// The entity's local rotation component.
[BurstCompile]
public void Execute(ref LocalTransform transform, LocalRotation localRotation)
{
transform.Rotation = math.mul(transform.Rotation, quaternion.EulerZYX(localRotation.RotationSpeed * DeltaTime));
}
}
}
}