Skip to content

Instantly share code, notes, and snippets.

@stevekane
Last active October 12, 2020 19:14
Show Gist options
  • Save stevekane/57eee76bb4782ff953528c7b4f16672a to your computer and use it in GitHub Desktop.
Save stevekane/57eee76bb4782ff953528c7b4f16672a to your computer and use it in GitHub Desktop.
using System;
using Unity.NetCode;
using Unity.Entities;
[Serializable]
[GenerateAuthoringComponent]
public struct TriggerPlate : IComponentData {
public enum TriggerState { JustTriggered, Triggered, JustUnTriggered, UnTriggered }
[GhostField] public bool Active;
[GhostField] public TriggerState State;
[GhostField] public float TriggerDuration;
[GhostField] public float TriggerTimer;
[GhostField] public int TriggerCost;
}
using Unity.Entities;
using Unity.Rendering;
using Unity.NetCode;
[UpdateInGroup(typeof(ClientPresentationSystemGroup))]
public class TriggerPlateRenderingSystem : SystemBase {
protected override void OnUpdate() {
Entities
.WithoutBurst()
.WithStructuralChanges()
.ForEach((Entity entity, in TriggerPlateRenderer triggerPlateRenderer) => {
var triggerPlate = EntityManager.GetComponentData<TriggerPlate>(triggerPlateRenderer.TriggerPlate);
var renderMesh = EntityManager.GetSharedComponentData<RenderMesh>(entity);
var baseColor = UnityEngine.Color.white;
var activeColor = UnityEngine.Color.blue;
var color = UnityEngine.Color.Lerp(baseColor, activeColor, triggerPlate.TriggerTimer / triggerPlate.TriggerDuration);
renderMesh.material.SetColor("_Color", color);
EntityManager.SetSharedComponentData<RenderMesh>(entity, renderMesh);
}).Run();
}
}
using Unity.Entities;
using Unity.Jobs;
using Unity.Physics;
using Unity.Physics.Systems;
using Unity.NetCode;
using Unity.Burst;
using Unity.Collections;
using Unity.Rendering;
[UpdateInGroup(typeof(ServerSimulationSystemGroup))]
public class TriggerPlateSystem : JobComponentSystem {
[BurstCompile]
public struct TriggerJob : ITriggerEventsJob {
[ReadOnly] public float dt;
[ReadOnly] public ComponentDataFromEntity<Player> players;
public ComponentDataFromEntity<TriggerPlate> triggerPlates;
static TriggerPlate UpdateTriggerPlate(TriggerPlate plate, float dt) {
// Kind of a lame way to detect if this plate has already been interacted with this frame...
if (plate.Active) return plate;
switch (plate.State) {
case TriggerPlate.TriggerState.JustTriggered:
case TriggerPlate.TriggerState.Triggered:
plate.Active = true;
plate.State = TriggerPlate.TriggerState.Triggered;
plate.TriggerTimer += dt;
break;
case TriggerPlate.TriggerState.UnTriggered:
plate.Active = true;
plate.State = TriggerPlate.TriggerState.JustTriggered;
break;
}
return plate;
}
public void Execute(TriggerEvent triggerEvent) {
if (triggerPlates.HasComponent(triggerEvent.EntityA) && players.HasComponent(triggerEvent.EntityB)) {
triggerPlates[triggerEvent.EntityA] = UpdateTriggerPlate(triggerPlates[triggerEvent.EntityA], dt);
}
if (triggerPlates.HasComponent(triggerEvent.EntityB) && players.HasComponent(triggerEvent.EntityA)) {
triggerPlates[triggerEvent.EntityB] = UpdateTriggerPlate(triggerPlates[triggerEvent.EntityB], dt);
}
}
}
BuildPhysicsWorld buildPhysicsWorld;
StepPhysicsWorld stepPhysicsWorld;
protected override void OnCreate() {
buildPhysicsWorld = World.GetOrCreateSystem<BuildPhysicsWorld>();
stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
}
protected override JobHandle OnUpdate(JobHandle inputDeps) {
var prePass = Entities
.ForEach((Entity e, ref TriggerPlate triggerPlate) => {
triggerPlate.Active = false;
}).Schedule(inputDeps);
var triggerPass = new TriggerJob {
dt = Time.DeltaTime,
players = GetComponentDataFromEntity<Player>(true),
triggerPlates = GetComponentDataFromEntity<TriggerPlate>()
}.Schedule(stepPhysicsWorld.Simulation, ref buildPhysicsWorld.PhysicsWorld, prePass);
var postPass = Entities
.ForEach((Entity e, ref TriggerPlate triggerPlate) => {
if (triggerPlate.Active)
return;
triggerPlate.TriggerTimer = 0;
switch (triggerPlate.State) {
case TriggerPlate.TriggerState.Triggered:
case TriggerPlate.TriggerState.JustTriggered:
triggerPlate.State = TriggerPlate.TriggerState.JustUnTriggered;
break;
case TriggerPlate.TriggerState.UnTriggered:
case TriggerPlate.TriggerState.JustUnTriggered:
triggerPlate.State = TriggerPlate.TriggerState.UnTriggered;
break;
}
}).Schedule(triggerPass);
return postPass;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment