Skip to content

Instantly share code, notes, and snippets.

@acoppes
Created October 19, 2020 13:30
Show Gist options
  • Save acoppes/2766a115449b8fef5866e3af90d2d02b to your computer and use it in GitHub Desktop.
Save acoppes/2766a115449b8fef5866e3af90d2d02b to your computer and use it in GitHub Desktop.
using NaiveNetworkGame.Server.Components;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Transforms;
namespace NaiveNetworkGame.Server.Systems
{
[UpdateAfter(typeof(MovementActionSystem))]
[UpdateInGroup(typeof(ServerSimulationSystemGroup))]
public class DynamicObstacleSystem : SystemBase
{
private EntityQuery obstaclesQuery;
protected override void OnCreate()
{
base.OnCreate();
obstaclesQuery = GetEntityQuery(typeof(Translation), typeof(DynamicObstacle));
}
protected override void OnUpdate()
{
// var query = Entities.WithAll<Translation, DynamicObstacle>().ToEntityQuery();
var translations = obstaclesQuery.ToComponentDataArray<Translation>(Allocator.TempJob);
var obstacles = obstaclesQuery.ToComponentDataArray<DynamicObstacle>(Allocator.TempJob);
uint currentInternalIndex = 0;
Entities
.WithNone<IsAlive>()
.WithAll<Health, DynamicObstacle>()
.ForEach(delegate(ref DynamicObstacle d)
{
// reduce priority if not alive but has health.
if (d.priority > 0)
d.priority = 1;
}).ScheduleParallel();
Entities
.WithAll<DynamicObstacle>()
.ForEach(delegate(ref DynamicObstacle d)
{
d.rangeSq = d.range * d.range;
d.index = currentInternalIndex++;
}).Run();
Entities
.WithAll<Translation, DynamicObstacle>()
.ForEach(delegate(Entity e, ref DynamicObstacle d0, in Translation t0)
{
// we have the logic disabled...
if (d0.priority == 0)
return;
for (var j = 0; j < translations.Length; j++)
{
var d1 = obstacles[j];
if (d0.index == d1.index)
continue;
if (d0.priority > d1.priority)
continue;
var t1 = translations[j];
var m = t1.Value - t0.Value;
var d = math.lengthsq(m);
var r = d0.rangeSq + d1.rangeSq;
if (d > r)
continue;
// if both of the units are moving, then only move half the distance.
var mult = 0.5f;
if (d0.priority < d1.priority)
mult = 1.0f;
var mlen = (d - r) * mult;
d0.movement += math.normalizesafe(m, float3.zero) * mlen;
}
}).Run();
Entities
.WithAll<Translation, DynamicObstacle>()
.ForEach(delegate(ref Translation t, ref DynamicObstacle d)
{
t.Value += d.movement;
d.movement = float3.zero;
}).ScheduleParallel();
// entities.Dispose();
obstacles.Dispose();
translations.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment