Skip to content

Commit

Permalink
jobs: Pass whole state to input actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Oct 2, 2023
1 parent 11a66bc commit 0f419f1
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
22 changes: 13 additions & 9 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ public class PhysicsEngine : MonoBehaviour
[NonSerialized] private NativeHashMap<int, FlipperState> _flipperStates;

[NonSerialized] private readonly Dictionary<int, PhysicsBall> _ballLookup = new();
[NonSerialized] public Dictionary<int, GameObject> FlipperLookup = new();
[NonSerialized] public readonly Dictionary<int, GameObject> FlipperLookup = new();

[NonSerialized] internal Queue<Action<NativeHashMap<int, FlipperState>, PhysicsEnv>> Actions = new();
[NonSerialized] internal readonly Queue<InputAction> InputActions = new();
internal delegate void InputAction(ref PhysicsState state);

private static ulong NowUsec => (ulong)(Time.timeAsDouble * 1000000);

Expand All @@ -53,7 +54,7 @@ private void Start()
var player = GetComponent<Player>();

// init state
var physicsState = new PhysicsEnv(NowUsec, GetComponent<Player>());
var env = new PhysicsEnv(NowUsec, GetComponent<Player>());
_insideOfs = new InsideOfs(Allocator.Persistent);

// create static octree
Expand All @@ -80,7 +81,7 @@ private void Start()
var elapsedMs = sw.Elapsed.TotalMilliseconds;
var playfieldBounds = GetComponentInChildren<PlayfieldComponent>().Bounds;
_octree = new NativeOctree<int>(playfieldBounds, 32, 10, Allocator.Persistent);

sw.Restart();
var populateJob = new PopulatePhysicsJob {
Colliders = _colliders,
Expand All @@ -89,18 +90,18 @@ private void Start()
populateJob.Run();
_octree = populateJob.Octree;
Debug.Log($"Octree of {_colliders.Value.Colliders.Length} constructed (colliders: {elapsedMs}ms, tree: {sw.Elapsed.TotalMilliseconds}ms).");

// get balls
var balls = GetComponentsInChildren<PhysicsBall>();
_balls = new NativeList<BallData>(balls.Length, Allocator.Persistent);
foreach (var ball in balls) {
_balls.Add(ball.Data);
_ballLookup[ball.Id] = ball;
}

_eventQueue = new NativeQueue<EventData>(Allocator.Persistent);
_physicsEnv = new NativeArray<PhysicsEnv>(1, Allocator.Persistent);
_physicsEnv[0] = physicsState;
_physicsEnv[0] = env;
}

private static BlobAssetReference<ColliderBlob> AllocateColliders(IEnumerable<ICollider> managedColliders)
Expand All @@ -126,8 +127,11 @@ private void Update()
FlipperStates = _flipperStates,
};

foreach (var action in Actions) {
action(_flipperStates, _physicsEnv[0]);
var env = _physicsEnv[0];
var state = new PhysicsState(ref env, ref _octree, ref _colliders, ref events, ref _insideOfs, ref _balls, ref _flipperStates);

foreach (var action in InputActions) {
action(ref state);
}
updatePhysics.Run();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,27 +102,27 @@ public void FlipperRotateToEnd(in int itemId)
{
var id = itemId;
var physicsEngine = _tableComponent.GetComponent<PhysicsEngine>();
physicsEngine.Actions.Enqueue((flipperStates, state) => {
var timeMsec = (uint)((state.CurPhysicsFrameTime - state.StartTimeUsec) / 1000);
var flipperState = flipperStates[id];
physicsEngine.InputActions.Enqueue((ref PhysicsState state) => {
var timeMsec = (uint)((state.Env.CurPhysicsFrameTime - state.Env.StartTimeUsec) / 1000);
var flipperState = state.FlipperStates[id];
flipperState.Movement.EnableRotateEvent = 1;
flipperState.Movement.StartRotateToEndTime = timeMsec;
flipperState.Movement.AngleAtRotateToEnd = flipperState.Movement.Angle;
flipperState.Solenoid.Value = true;
flipperStates[id] = flipperState;
state.FlipperStates[id] = flipperState;
});
}

public void FlipperRotateToStart(in int itemId)
{
var id = itemId;
var physicsEngine = _tableComponent.GetComponent<PhysicsEngine>();
physicsEngine.Actions.Enqueue((flipperStates, state) => {
var timeMsec = (uint)((state.CurPhysicsFrameTime - state.StartTimeUsec) / 1000);
var flipperState = flipperStates[id];
physicsEngine.InputActions.Enqueue((ref PhysicsState state) => {
var timeMsec = (uint)((state.Env.CurPhysicsFrameTime - state.Env.StartTimeUsec) / 1000);
var flipperState = state.FlipperStates[id];
flipperState.Movement.EnableRotateEvent = -1;
flipperState.Solenoid.Value = false;
flipperStates[id] = flipperState;
state.FlipperStates[id] = flipperState;
});
}

Expand Down

0 comments on commit 0f419f1

Please sign in to comment.