Skip to content

Instantly share code, notes, and snippets.

@JohnnyTurbo
Last active October 29, 2021 05:38
Show Gist options
  • Save JohnnyTurbo/ee7ed0709501d0e0ee12709a28c5a958 to your computer and use it in GitHub Desktop.
Save JohnnyTurbo/ee7ed0709501d0e0ee12709a28c5a958 to your computer and use it in GitHub Desktop.
Code used in System State Components tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/5iyk1GJinRU
using Unity.Entities;
namespace TMG.SystemStateComponents
{
[GenerateAuthoringComponent]
public struct CapsuleMoveData : IComponentData
{
public float Speed;
public float TimeAlive;
public float TimeToLive;
}
}
using Unity.Entities;
namespace TMG.SystemStateComponents
{
public class CleanupShadowSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
Entities
.WithNone<ShadowTag>()
.ForEach((Entity e, in ShadowStateData shadowStateData) =>
{
ecb.DestroyEntity(shadowStateData.ShadowEntity);
ecb.RemoveComponent<ShadowStateData>(e);
}).Run();
}
}
}
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace TMG.SystemStateComponents
{
public class MoveCapsuleSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
var deltaTime = Time.DeltaTime;
Entities.ForEach((Entity e, ref Translation translation, ref CapsuleMoveData moveData) =>
{
moveData.TimeAlive += deltaTime;
if (moveData.TimeAlive > moveData.TimeToLive)
{
ecb.DestroyEntity(e);
}
else
{
translation.Value.x += moveData.Speed * deltaTime;
translation.Value.y = math.cos(moveData.TimeAlive * 4f + math.PI) + 2f;
}
}).Run();
}
}
}
using Unity.Entities;
using Unity.Transforms;
namespace TMG.SystemStateComponents
{
public class MoveShadowSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnCreate()
{
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
Entities
.WithAll<ShadowTag>()
.ForEach((in ShadowStateData shadowStateData, in Translation translation) =>
{
var shadowEntity = shadowStateData.ShadowEntity;
Translation shadowTranslation;
shadowTranslation.Value = translation.Value;
shadowTranslation.Value.y = 0.0001f;
ecb.SetComponent(shadowEntity, shadowTranslation);
}).Run();
}
}
}
using Unity.Entities;
namespace TMG.SystemStateComponents
{
[GenerateAuthoringComponent]
public struct PrefabData : IComponentData
{
public Entity CapsulePrefab;
public Entity ShadowPrefab;
}
}
using Unity.Entities;
namespace TMG.SystemStateComponents
{
public struct ShadowStateData : ISystemStateComponentData
{
public Entity ShadowEntity;
}
}
using Unity.Entities;
namespace TMG.SystemStateComponents
{
[GenerateAuthoringComponent]
public struct ShadowTag : IComponentData {}
}
using Unity.Entities;
using UnityEngine;
namespace TMG.SystemStateComponents
{
public class SpawnCapsuleSystem : SystemBase
{
protected override void OnUpdate()
{
if (Input.GetKeyDown(KeyCode.Alpha1))
{
var capsulePrefab = GetSingleton<PrefabData>().CapsulePrefab;
EntityManager.Instantiate(capsulePrefab);
}
}
}
}
using Unity.Entities;
namespace TMG.SystemStateComponents
{
public class SpawnShadowSystem : SystemBase
{
private PrefabData _prefabDataSingleton;
private EndSimulationEntityCommandBufferSystem _endSimulationEntityCommandBufferSystem;
protected override void OnStartRunning()
{
_prefabDataSingleton = GetSingleton<PrefabData>();
_endSimulationEntityCommandBufferSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _endSimulationEntityCommandBufferSystem.CreateCommandBuffer();
var shadowPrefab = _prefabDataSingleton.ShadowPrefab;
Entities
.WithAll<ShadowTag>()
.WithNone<ShadowStateData>()
.ForEach((Entity e) =>
{
var newShadowStateData = new ShadowStateData
{
ShadowEntity = ecb.Instantiate(shadowPrefab)
};
ecb.AddComponent<ShadowStateData>(e);
ecb.SetComponent(e, newShadowStateData);
}).Run();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment