Skip to content

Instantly share code, notes, and snippets.

@jeffvella
Last active May 6, 2020 16:28
Show Gist options
  • Save jeffvella/57da54567c7127be4fff7e124a97f5f8 to your computer and use it in GitHub Desktop.
Save jeffvella/57da54567c7127be4fff7e124a97f5f8 to your computer and use it in GitHub Desktop.
public struct MovementData
{
public float SomethingRelevent;
}
[StructLayout(LayoutKind.Explicit)]
public unsafe struct Movement : IComponentData,IMovementBehavior
{
public enum MovementType : int
{
None = 0,
Stumbling,
Running,
Idle
}
[FieldOffset(0)]
public MovementType Type;
[FieldOffset(4)]
public MovementData Data;
[FieldOffset(4)]
private StumblingBehavior _stumbling;
[FieldOffset(4)]
private RunningBehavior _running;
[FieldOffset(4)]
private IdleBehavior _idle;
public void Move()
{
switch (Type)
{
case MovementType.None:
break;
case MovementType.Stumbling:
_stumbling.Move();
break;
case MovementType.Running:
_running.Move();
break;
case MovementType.Idle:
_idle.Move();
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public interface IMovementBehavior
{
void Move();
}
public struct StumblingBehavior : IMovementBehavior
{
public MovementData Data;
public void Move() {}
}
public struct RunningBehavior : IMovementBehavior
{
public MovementData Data;
public void Move() {}
}
public struct IdleBehavior : IMovementBehavior
{
public MovementData Data;
public void Move() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment