Skip to content

Instantly share code, notes, and snippets.

@guerro323
Created May 4, 2020 15:05
Show Gist options
  • Save guerro323/a12b7e15332406c03efb03b7050c0209 to your computer and use it in GitHub Desktop.
Save guerro323/a12b7e15332406c03efb03b7050c0209 to your computer and use it in GitHub Desktop.
namespace PataponGameHost.Systems
{
public struct Character
{
}
[RestrictToApplication(typeof(GameSimulationThreadingHost))]
public class MoveCharacterSystem : AppSystem
{
// Injected field
private readonly IManagedWorldTime worldTime;
public MoveCharacterSystem(IManagedWorldTime worldTime)
{
this.worldTime = worldTime;
}
private EntitySet characterSet;
private Inputs.Axis2D moveAction;
protected override void OnInit()
{
base.OnInit();
moveAction = World.GetOrCreate<InputManager>().GetAction<MyInputMap, Inputs.Axis2D>("move");
characterSet = World.Mgr.GetEntities()
.With<Character>()
.With<Vector3>()
.AsSet();
for (var i = 0; i != 1_000_000; i++)
{
var ch = World.Mgr.CreateEntity();
ch.Set(new Character());
ch.Set(new Vector3());
}
}
protected override void OnUpdate()
{
base.OnUpdate();
var moveVector = moveAction.Value;
var dt = worldTime.Delta;
moveVector = Vector2.NormalizeSafe(moveVector) * dt;
foreach (int batch in characterSet.ToParallelBatch(batchCountTarget: 1 / 4))
{
foreach (ref readonly var entity in batch)
{
entity.Get<Vector3>() += moveVector;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment