Skip to content

Commit

Permalink
jobs: Minor cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Oct 4, 2023
1 parent 54c058c commit a25ae74
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 40 deletions.
18 changes: 12 additions & 6 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace VisualPinball.Unity
{
public class PhysicsEngine : MonoBehaviour
{
internal delegate void InputAction(ref PhysicsState state);

[NonSerialized] private NativeArray<PhysicsEnv> _physicsEnv;
[NonSerialized] private NativeOctree<int> _octree;
[NonSerialized] private BlobAssetReference<ColliderBlob> _colliders;
Expand All @@ -44,8 +46,7 @@ public class PhysicsEngine : MonoBehaviour
[NonSerialized] private readonly Dictionary<int, PhysicsBall> _ballLookup = new();
[NonSerialized] private readonly Dictionary<int, Transform> _transforms = new();

[NonSerialized] internal readonly Queue<InputAction> InputActions = new();
internal delegate void InputAction(ref PhysicsState state);
[NonSerialized] private readonly Queue<InputAction> _inputActions = new();

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

Expand All @@ -55,6 +56,11 @@ public void Register<T>(T item) where T : MonoBehaviour
_transforms.Add(go.GetInstanceID(), go.transform);
}

internal void Schedule(InputAction action)
{
_inputActions.Enqueue(action);
}

private void Start()
{
var player = GetComponent<Player>();
Expand Down Expand Up @@ -153,8 +159,8 @@ private void Update()
var state = new PhysicsState(ref env, ref _octree, ref _colliders, ref events, ref _insideOfs, ref _balls, ref _flipperStates, ref _bumperStates);

// process input
while (InputActions.Count > 0) {
var action = InputActions.Dequeue();
while (_inputActions.Count > 0) {
var action = _inputActions.Dequeue();
action(ref state);
}

Expand Down Expand Up @@ -189,10 +195,10 @@ private void Update()
while (enumerator.MoveNext()) {
ref var bumperState = ref enumerator.Current.Value;
if (bumperState.SkirtItemId != 0) {
BumperTransformation.UpdateSkirt(in bumperState.SkirtAnimation, _transforms[bumperState.SkirtItemId]);
BumperTransform.UpdateSkirt(in bumperState.SkirtAnimation, _transforms[bumperState.SkirtItemId]);
}
if (bumperState.RingItemId != 0) {
BumperTransformation.UpdateRing(bumperState.RingItemId, in bumperState.RingAnimation, _transforms[bumperState.RingItemId]);
BumperTransform.UpdateRing(bumperState.RingItemId, in bumperState.RingAnimation, _transforms[bumperState.RingItemId]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,19 +97,19 @@ protected override void OnUpdate()
switch (coll.Type) {
case ColliderType.Bumper: {
var bumperStaticData = GetComponent<BumperStaticData>(coll.ItemId);
var animateRing = HasComponent<BumperRingAnimationData>(coll.ItemId);
var animateSkirt = HasComponent<BumperSkirtAnimationData>(coll.ItemId);
var ringData = animateRing ? GetComponent<BumperRingAnimationData>(coll.ItemId) : default;
var skirtData = animateSkirt ? GetComponent<BumperSkirtAnimationData>(coll.ItemId): default;
BumperCollider.Collide(ref ballData, ref events, ref collEvent, ref ringData, ref skirtData,
in coll, bumperStaticData, ref random);
if (animateRing) {
SetComponent(coll.ItemId, ringData);
}
if (animateSkirt) {
SetComponent(coll.ItemId, skirtData);
}
// var bumperStaticData = GetComponent<BumperStaticData>(coll.ItemId);
// var animateRing = HasComponent<BumperRingAnimationData>(coll.ItemId);
// var animateSkirt = HasComponent<BumperSkirtAnimationData>(coll.ItemId);
// var ringData = animateRing ? GetComponent<BumperRingAnimationData>(coll.ItemId) : default;
// var skirtData = animateSkirt ? GetComponent<BumperSkirtAnimationData>(coll.ItemId): default;
// BumperCollider.Collide(ref ballData, ref events, ref collEvent, ref ringData, ref skirtData,
// in coll, bumperStaticData, ref random);
// if (animateRing) {
// SetComponent(coll.ItemId, ringData);
// }
// if (animateSkirt) {
// SetComponent(coll.ItemId, skirtData);
// }
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void FlipperRotateToEnd(in int itemId)
{
var id = itemId;
var physicsEngine = _tableComponent.GetComponent<PhysicsEngine>();
physicsEngine.InputActions.Enqueue((ref PhysicsState state) => {
physicsEngine.Schedule((ref PhysicsState state) => {
var timeMsec = (uint)((state.Env.CurPhysicsFrameTime - state.Env.StartTimeUsec) / 1000);
ref var flipperState = ref state.FlipperStates.GetValueByRef(id);
flipperState.Movement.EnableRotateEvent = 1;
Expand All @@ -117,8 +117,7 @@ public void FlipperRotateToStart(in int itemId)
{
var id = itemId;
var physicsEngine = _tableComponent.GetComponent<PhysicsEngine>();
physicsEngine.InputActions.Enqueue((ref PhysicsState state) => {
var timeMsec = (uint)((state.Env.CurPhysicsFrameTime - state.Env.StartTimeUsec) / 1000);
physicsEngine.Schedule((ref PhysicsState state) => {
ref var flipperState = ref state.FlipperStates.GetValueByRef(id);
flipperState.Movement.EnableRotateEvent = -1;
flipperState.Solenoid.Value = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

using System;
using System.Collections.Generic;
using Unity.Entities;
using UnityEngine;
using VisualPinball.Engine.VPT.Bumper;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using Unity.Entities;

namespace VisualPinball.Unity
{
internal struct BumperRingAnimationData : IComponentData
internal struct BumperRingAnimationData
{
public bool IsHit;
public float DropOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using Unity.Entities;
using Unity.Mathematics;

namespace VisualPinball.Unity
{
internal struct BumperSkirtAnimationData : IComponentData
internal struct BumperSkirtAnimationData
{
// dynamic
public bool HitEvent;
Expand All @@ -32,6 +31,5 @@ internal struct BumperSkirtAnimationData : IComponentData

// static
public float2 Center;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using Unity.Entities;

namespace VisualPinball.Unity
{
internal struct BumperStaticData : IComponentData
internal struct BumperStaticData
{
public float Force;
public float Threshold;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
// along with this program. If not, see <https://www.gnu.org/licenses/>.

using System.Collections.Generic;
using Unity.Entities;
using Unity.Profiling;
using UnityEngine;
using VisualPinball.Unity;
using VisualPinball.Unity.VisualPinball.Unity.Game;
using Physics = UnityEngine.Physics;

namespace VisualPinballUnity
namespace VisualPinball.Unity
{
internal static class BumperTransformation
/// <summary>
/// Applies the state to the scene, aka the transform of the game objects.
/// </summary>
internal static class BumperTransform
{
private static readonly Dictionary<int, float> InitialOffset = new();

Expand All @@ -37,7 +35,7 @@ internal static void UpdateRing(int itemId, in BumperRingAnimationData data, Tra
var localLimit = InitialOffset[itemId] + limit;
var localOffset = localLimit / limit * data.Offset;

worldPos.y = InitialOffset[itemId] + VisualPinball.Unity.Physics.ScaleToWorld(localOffset);
worldPos.y = InitialOffset[itemId] + Physics.ScaleToWorld(localOffset);
transform.position = worldPos;
}

Expand Down

0 comments on commit a25ae74

Please sign in to comment.