Skip to content

Commit

Permalink
1.5.5 - Better StateMachine samples
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelAsherRivello committed May 29, 2024
1 parent 69bb092 commit 1a7931d
Show file tree
Hide file tree
Showing 76 changed files with 1,334 additions and 126 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Unity.Entities;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
public struct MyMovementEntityTag : IComponentData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using RMC.DOTS.Systems.StateMachine;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;

namespace RMC.DOTS.Demos.StateMachine.Light
{
public class MyMovementEntityTagAuthoring : MonoBehaviour
{
public Vector3 RotationDelta = new float3(0, 1f, 0);
public float RotationDurationInSeconds = 1;

[Header("Translation")]
public Vector3 TranslationDelta = new float3(0, 0.5f, 0);
public float TranslationDurationInSeconds = 1;

public class Test01AuthoringBaker : Baker<MyMovementEntityTagAuthoring>
{
public override void Bake(MyMovementEntityTagAuthoring authoring)
{
var entity = GetEntity(TransformUsageFlags.Dynamic);

// TODO: Remove this? How else can I turn on the first state easily?
//(This is more of a "how to easily find an entity?" question than an SM question)
AddComponent<MyMovementEntityTag>(entity);

//Give this entity 1) an ID and 2) a state to be in
AddComponent<StateID>(entity);
AddComponent<MyMovementStateSystemTag>(entity);

AddComponent<RotationComponent>(entity,
new RotationComponent()
{
RotationDelta = authoring.RotationDelta,
DurationInSeconds = authoring.RotationDurationInSeconds
});

AddComponent<TranslationComponent>(entity,
new TranslationComponent()
{
TranslationDelta = authoring.TranslationDelta,
DurationInSeconds = authoring.TranslationDurationInSeconds
});

}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Unity.Entities;
using Unity.Mathematics;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
public struct RotationComponent : IComponentData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Unity.Entities;
using Unity.Mathematics;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
public struct TranslationComponent : IComponentData
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "RMC.DOTS.Demos.StateMachine01_Light.Runtime",
"rootNamespace": "",
"references": [
"GUID:d28bf24cef16ed84cb4cb180b9c77ac3",
"GUID:330ce21097b4d554fa95a74295bc3b8f",
"GUID:2665a8d13d1b3f18800f46e256720795",
"GUID:734d92eba21c94caba915361bd5ac177",
"GUID:63afb046c8423dd448ae7aba042ea63d",
"GUID:a5baed0c9693541a5bd947d336ec7659",
"GUID:e0cd26848372d4e5c891c569017e11f1",
"GUID:d8b63aba1907145bea998dd612889d6b",
"GUID:5f3cf485eb0554709a8abbeace890c86",
"GUID:8819f35a0fc84499b990e90a4ca1911f",
"GUID:75469ad4d38634e559750d17036d5f7c",
"GUID:f2381d29563bc3c49ab35691478f3482"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using RMC.DOTS.Systems.StateMachine;
using UnityEngine;
using Unity.Entities;

namespace RMC.DOTS.Demos.StateMachine.Light
{
public class MyMovementBaseState : StateMachineSystemBase.State
{
protected float StateElapsedTimeInSeconds { get; private set; }

protected virtual void RequestStateChangePerTransitions(Entity entity)
{
// Toggle
if (IsInState<MyMovementRotationState>(entity))
{
RequestStateChange<MyMovementTranslationState>(entity);
}
else if (IsInState<MyMovementTranslationState>(entity))
{
RequestStateChange<MyMovementRotationState>(entity);
}
else
{
Debug.LogError("Unknown State");
}
}

public override void OnEnter(Entity entity)
{
base.OnEnter(entity);
StateElapsedTimeInSeconds = 0;
Debug.LogFormat("{0}.OnEnter() {1}\n\n", entity.ToString(), GetType().Name);
}

public override void OnUpdate(Entity entity)
{
base.OnUpdate(entity);
StateElapsedTimeInSeconds += World.Time.DeltaTime;
//Debug.LogFormat("{0}.OnUpdate() {1}\n\n", entity.ToString(), GetType().Name);
}

public override void OnExit(Entity entity)
{
base.OnExit(entity);
//Debug.LogFormat("{0}.OnExit() {1}\n\n", entity.ToString(), GetType().Name);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using RMC.DOTS.Systems.StateMachine;

namespace RMC.DOTS.Demos.StateMachine.Light
{
partial class MyMovementStateMachineSystem : StateMachineSystem<MyMovementStateSystemTag>
{
protected override void OnCreate()
{
base.OnCreate();

//Requires
RequireForUpdate<RotationComponent>();
RequireForUpdate<TranslationComponent>();

//Registers
RegisterState<MyMovementTranslationState>();
RegisterState<MyMovementRotationState>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using RMC.DOTS.Systems.StateMachine;
using Unity.Entities;

namespace RMC.DOTS.Demos.StateMachine.Light
{
struct MyMovementStateSystemTag : IComponentData{}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using RMC.DOTS.Systems.StateMachine;
using UnityEngine;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
public class MyMovementRotationState : MyMovementBaseState
{
public override void OnUpdate(Entity entity)
{
base.OnUpdate(entity);
var deltaTime = System.World.Time.DeltaTime;
var deltaTime = StateMachineSystemBase.World.Time.DeltaTime;

// Check Component
if (!EntityManager.HasComponent<LocalTransform>(entity) ||
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Unity.Entities;
using Unity.Transforms;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
public class MyMovementTranslationState : MyMovementBaseState
{
public override void OnUpdate(Entity entity)
{
base.OnUpdate(entity);
var deltaTime = System.World.Time.DeltaTime;
var deltaTime = StateMachineSystemBase.World.Time.DeltaTime;

// Check Component
if (!EntityManager.HasComponent<LocalTransform>(entity) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
using Unity.Entities;
using UnityEngine;

namespace RMC.DOTS.Demos.StateMachine
namespace RMC.DOTS.Demos.StateMachine.Light
{
/// <summary>
/// See <see cref="StateMachineSystemBase"/>
///
/// There are multiple demos
///
/// * <see cref="StateMachine01_Light"/> - More logic outside the StateMachine itself
/// * <see cref="StateMachine02_Full"/> - More logic inside the StateMachine itself
///
/// </summary>
public class StateMachine : MonoBehaviour
public class StateMachine01_Light : MonoBehaviour
{
// Initialization --------------------------------
protected void Start()
{
Debug.Log("StateMachine Demo. Watch the console.");
Debug.Log("StateMachine01_Light Demo. Watch the console.");

MyMovementStateMachineSystem myMovementStateMachineSystem =
World.DefaultGameObjectInjectionWorld.CreateSystemManaged<MyMovementStateMachineSystem>();
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 1a7931d

Please sign in to comment.