Skip to content

Commit

Permalink
jobs: Fix ball roller.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Oct 15, 2023
1 parent a4781d5 commit d931f67
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
52 changes: 24 additions & 28 deletions VisualPinball.Unity/VisualPinball.Unity/Game/BallRollerComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// 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.Collections;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;
Expand All @@ -24,15 +22,14 @@ namespace VisualPinball.Unity
{
public class BallRollerComponent : MonoBehaviour
{
private PhysicsEngine _physicsEngine;
private PlayfieldComponent _playfield;
private Matrix4x4 _ltw;
private Matrix4x4 _wtl;

private Plane _playfieldPlane;

private EntityManager _entityManager;
private Entity _ballEntity = Entity.Null;
private EntityQuery _ballEntityQuery;
private int _ballId = 0;

private void Awake()
{
Expand All @@ -46,9 +43,7 @@ private void Awake()
var p2 = _ltw.MultiplyPoint(new Vector3(100f, 100f, z));
var p3 = _ltw.MultiplyPoint(new Vector3(100f, -100f, z));
_playfieldPlane.Set3Points(p1, p2, p3);

_entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;
_ballEntityQuery = _entityManager.CreateEntityQuery(typeof(BallState));
_physicsEngine = GetComponentInParent<PhysicsEngine>();
}

private void Update()
Expand All @@ -60,21 +55,24 @@ private void Update()
// find nearest ball
if (Mouse.current.middleButton.wasPressedThisFrame) {
if (GetCursorPositionOnPlayfield(out var mousePosition)) {
var ballEntities = _ballEntityQuery.ToEntityArray(Allocator.Temp);
var nearestDistance = float.PositiveInfinity;
BallState nearestBall = default;
var ballFound = false;
foreach (var ballEntity in ballEntities) {
var ballData = _entityManager.GetComponentData<BallState>(ballEntity);
if (ballData.IsFrozen) {
continue;
}
var distance = math.distance(mousePosition, ballData.Position.xy);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestBall = ballData;
ballFound = true;
_ballEntity = ballEntity;

using (var enumerator = _physicsEngine.Balls.GetEnumerator()) {
while (enumerator.MoveNext()) {
var ball = enumerator.Current.Value;

if (ball.IsFrozen) {
continue;
}
var distance = math.distance(mousePosition, ball.Position.xy);
if (distance < nearestDistance) {
nearestDistance = distance;
nearestBall = ball;
ballFound = true;
_ballId = ball.Id;
}
}
}

Expand All @@ -83,26 +81,24 @@ private void Update()
}
}

} else if (Mouse.current.middleButton.isPressed && _ballEntity != Entity.Null) {
} else if (Mouse.current.middleButton.isPressed && _ballId != 0) {
if (GetCursorPositionOnPlayfield(out var mousePosition)) {
var ballData = _entityManager.GetComponentData<BallState>(_ballEntity);
UpdateBall(ref ballData, mousePosition);
ref var ball = ref _physicsEngine.BallState(_ballId);
UpdateBall(ref ball, mousePosition);
}
}

if (Mouse.current.middleButton.wasReleasedThisFrame && _ballEntity != Entity.Null) {
var ballData = _entityManager.GetComponentData<BallState>(_ballEntity);
if (Mouse.current.middleButton.wasReleasedThisFrame && _ballId != 0) {
ref var ballData = ref _physicsEngine.BallState(_ballId);
ballData.ManualControl = false;
_entityManager.SetComponentData(_ballEntity, ballData);
_ballEntity = Entity.Null;
_ballId = 0;
}
}

private void UpdateBall(ref BallState ballState, float2 position)
{
ballState.ManualControl = true;
ballState.ManualPosition = position;
_entityManager.SetComponentData(_ballEntity, ballState);
}

private bool GetCursorPositionOnPlayfield(out float2 position)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public void ScheduleAction(uint timeoutMs, Action action)

internal delegate void InputAction(ref PhysicsState state);

internal ref NativeParallelHashMap<int, BallState> Balls => ref _ballStates;
internal void Schedule(InputAction action) => _inputActions.Enqueue(action);
internal ref BallState BallState(int itemId) => ref _ballStates.GetValueByRef(itemId);
internal ref BumperState BumperState(int itemId) => ref _bumperStates.GetValueByRef(itemId);
Expand Down

0 comments on commit d931f67

Please sign in to comment.