Skip to content

Instantly share code, notes, and snippets.

@JohnnyTurbo
Created April 29, 2022 05:23
Show Gist options
  • Save JohnnyTurbo/511282e5999c92a0af4120b07455497a to your computer and use it in GitHub Desktop.
Save JohnnyTurbo/511282e5999c92a0af4120b07455497a to your computer and use it in GitHub Desktop.
Code used in [Topic Name] tutorial video on the Turbo Makes Games YouTube Channel - https://youtu.be/WgAKAMJOnMc
using TMPro;
using Unity.Entities;
using UnityEngine;
namespace TMG.IJE
{
public class IJEBattleController : MonoBehaviour
{
[SerializeField] private TMP_Dropdown _effectTypeDropdown;
[SerializeField] private TMP_Dropdown _teamSelectDropdown;
[SerializeField] private TMP_InputField _hitPointsInputField;
private Entity _spellController;
private EntityManager _entityManager;
private void Start()
{
_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
_spellController = _entityManager.CreateEntityQuery(typeof(SpellController)).GetSingletonEntity();
}
public void OnButtonExecute()
{
var effectModifier = _effectTypeDropdown.value == 0 ? 1 : -1;
var effectAmount = int.Parse(_hitPointsInputField.text) * effectModifier;
var newSpellExecutionData = new SpellExecutionData
{
EffectAmount = effectAmount,
TeamID = _teamSelectDropdown.value
};
_entityManager.AddComponentData(_spellController, newSpellExecutionData);
}
}
}
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace TMG.IJE
{
public partial class MovementSystem : SystemBase
{
private EntityQuery _regularMoveQuery;
private EntityQuery _specialMoveQuery;
private EntityQuery _invalidQuery;
protected override void OnStartRunning()
{
_regularMoveQuery = EntityManager.CreateEntityQuery(
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadOnly<MoveSpeed>(),
ComponentType.Exclude<SpecialMoveTag>());
_specialMoveQuery = EntityManager.CreateEntityQuery(
ComponentType.ReadWrite<Translation>(),
ComponentType.ReadOnly<MoveSpeed>(),
ComponentType.ReadOnly<SpecialMoveTag>());
_invalidQuery = EntityManager.CreateEntityQuery(
ComponentType.ReadOnly<SpecialMoveTag>(),
ComponentType.Exclude<MoveSpeed>());
}
protected override void OnUpdate()
{
var newMoveJob = new MoveJob
{
DeltaTime = Time.DeltaTime,
MoveMod = 1f
};
newMoveJob.ScheduleParallel(_regularMoveQuery);
newMoveJob.MoveMod = 0.25f;
newMoveJob.ScheduleParallel(_specialMoveQuery);
newMoveJob.ScheduleParallel(_invalidQuery);
}
}
[BurstCompile]
public partial struct MoveJob : IJobEntity
{
public float DeltaTime;
public float MoveMod;
void Execute(ref Translation translation, in MoveSpeed moveSpeed)
{
translation.Value += math.forward() * moveSpeed.Value * DeltaTime * MoveMod;
}
}
}
using Unity.Burst;
using Unity.Entities;
using Unity.Mathematics;
namespace TMG.IJE
{
[BurstCompile]
public partial struct PerformSpellJob : IJobEntity
{
public int EffectAmount;
public EntityCommandBuffer.ParallelWriter ECB;
public void Execute(Entity e, [EntityInQueryIndex] int sortKey, ref HitPoints hitPoints)
{
var curHitPoints = hitPoints.Current;
curHitPoints -= EffectAmount;
curHitPoints = math.clamp(curHitPoints, 0, hitPoints.Max);
if (curHitPoints <= 0)
{
ECB.AddComponent<DestroyEntityTag>(sortKey, e);
}
hitPoints.Current = curHitPoints;
}
}
}
using Unity.Entities;
namespace TMG.IJE
{
public partial class PerformSpellSystem : SystemBase
{
private EndSimulationEntityCommandBufferSystem _ecbSystem;
protected override void OnCreate()
{
RequireSingletonForUpdate<SpellExecutionData>();
}
protected override void OnStartRunning()
{
_ecbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
}
protected override void OnUpdate()
{
var ecb = _ecbSystem.CreateCommandBuffer().AsParallelWriter();
var spellExecution = GetSingleton<SpellExecutionData>();
var battleEntityQuery = EntityManager.CreateEntityQuery(typeof(HitPoints), typeof(TeamID));
switch (spellExecution.TeamID)
{
case 0:
var team1 = new TeamID { Value = 0 };
battleEntityQuery.SetSharedComponentFilter(team1);
break;
case 1:
var team2 = new TeamID { Value = 1 };
battleEntityQuery.SetSharedComponentFilter(team2);
break;
}
var newSpell = new PerformSpellJob
{
EffectAmount = spellExecution.EffectAmount,
ECB = ecb
};
newSpell.ScheduleParallel(battleEntityQuery);
EntityManager.RemoveComponent<SpellExecutionData>(GetSingletonEntity<SpellExecutionData>());
}
}
}
using Unity.Entities;
namespace TMG.IJE
{
public struct SpellExecutionData : IComponentData
{
public int EffectAmount;
public int TeamID;
}
}
using Unity.Entities;
namespace TMG.IJE
{
public struct TeamID : ISharedComponentData
{
public int Value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment