Skip to content

Commit

Permalink
jobs: Fix InsideOfs and dequeue events.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Oct 8, 2023
1 parent 727e31b commit 13c6c0b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 58 deletions.
11 changes: 7 additions & 4 deletions VisualPinball.Unity/VisualPinball.Unity/Game/InsideOfs.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Unity.Collections;
using VisualPinball.Unity.Collections;

namespace VisualPinball.Unity
{
Expand All @@ -19,17 +20,19 @@ internal void SetInsideOf(int itemId, int ballId)
if (!_insideOfs.ContainsKey(itemId)) {
_insideOfs.Add(itemId, new BitField64());
}

_insideOfs[itemId].SetBits(GetBitIndex(ballId), true);

ref var bits = ref _insideOfs.GetValueByRef(itemId);
bits.SetBits(GetBitIndex(ballId), true);
}

internal void SetOutsideOf(int itemId, int ballId)
{
if (!_insideOfs.ContainsKey(itemId)) {
return;
}

_insideOfs[itemId].SetBits(GetBitIndex(ballId), false);

ref var bits = ref _insideOfs.GetValueByRef(itemId);
bits.SetBits(GetBitIndex(ballId), false);
ClearBitIndex(ballId);
ClearItems(itemId);
}
Expand Down
37 changes: 26 additions & 11 deletions VisualPinball.Unity/VisualPinball.Unity/Game/PhysicsEngine.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Visual Pinball Engine
// Copyright (C) 2023 freezy and VPE Team
// Copyright (C) 2023 freezy and VPE Team
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand All @@ -25,7 +24,6 @@
using Unity.Mathematics;
using UnityEngine;
using VisualPinball.Engine.Common;
using VisualPinball.Unity.Collections;
using VisualPinballUnity;
using Debug = UnityEngine.Debug;

Expand Down Expand Up @@ -57,6 +55,8 @@ public class PhysicsEngine : MonoBehaviour

[NonSerialized] private readonly Queue<InputAction> _inputActions = new();

[NonSerialized] private Player _player;

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

internal void Register<T>(T item) where T : MonoBehaviour
Expand Down Expand Up @@ -84,10 +84,13 @@ internal void Schedule(InputAction action)
_inputActions.Enqueue(action);
}

private void Awake()
{
_player = GetComponent<Player>();
}

private void Start()
{
var player = GetComponent<Player>();

// init state
var env = new PhysicsEnv(NowUsec, GetComponent<Player>());
_insideOfs = new InsideOfs(Allocator.Persistent);
Expand All @@ -98,7 +101,7 @@ private void Start()
Debug.Log($"Found {colliderItems.Length} collidable items.");
var colliders = new ColliderReference(Allocator.TempJob);
foreach (var colliderItem in colliderItems) {
colliderItem.GetColliders(player, ref colliders, 0);
colliderItem.GetColliders(_player, ref colliders, 0);
}

// allocate colliders
Expand Down Expand Up @@ -146,7 +149,7 @@ private void Update()
var events = _eventQueue.AsParallelWriter();
var updatePhysics = new UpdatePhysicsJob {
InitialTimeUsec = NowUsec,
DeltaTime = Time.deltaTime * 1000,
DeltaTimeMs = Time.deltaTime * 1000,
PhysicsEnv = _physicsEnv,
Octree = _octree,
Colliders = _colliders,
Expand Down Expand Up @@ -180,6 +183,11 @@ private void Update()
// run physics loop
updatePhysics.Run();

// dequeue events
while (_eventQueue.TryDequeue(out var eventData)) {
_player.OnEvent(in eventData);
}

// retrieve updated data
_balls = updatePhysics.Balls;
_physicsEnv = updatePhysics.PhysicsEnv;
Expand Down Expand Up @@ -278,8 +286,7 @@ internal struct UpdatePhysicsJob : IJob
[ReadOnly]
public ulong InitialTimeUsec;

[ReadOnly]
public float DeltaTime;
public float DeltaTimeMs;

public NativeArray<PhysicsEnv> PhysicsEnv;
public NativeOctree<int> Octree;
Expand Down Expand Up @@ -355,14 +362,22 @@ public void Execute()
while (enumerator.MoveNext()) {
ref var bumperState = ref enumerator.Current.Value;
if (bumperState.RingItemId != 0) {
BumperRingAnimation.Update(ref bumperState.RingAnimation, DeltaTime);
BumperRingAnimation.Update(ref bumperState.RingAnimation, DeltaTimeMs);
}
if (bumperState.SkirtItemId != 0) {
BumperSkirtAnimation.Update(ref bumperState.SkirtAnimation, DeltaTime);
BumperSkirtAnimation.Update(ref bumperState.SkirtAnimation, DeltaTimeMs);
}
}
}

// trigger
using (var enumerator = TriggerStates.GetEnumerator()) {
while (enumerator.MoveNext()) {
ref var triggerState = ref enumerator.Current.Value;
TriggerAnimation.Update(ref triggerState.Animation, ref triggerState.Movement, in triggerState.Static, DeltaTimeMs);
}
}

#endregion

env.CurPhysicsFrameTime = env.NextPhysicsFrameTime;
Expand Down
88 changes: 45 additions & 43 deletions VisualPinball.Unity/VisualPinball.Unity/Game/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -454,49 +454,51 @@ private void RegisterCollider(int itemId, IApiColliderGenerator apiColl)

public void OnEvent(in EventData eventData)
{
switch (eventData.eventId) {
case EventId.HitEventsHit:
if (!_hittables.ContainsKey(eventData.ItemId)) {
Debug.LogError($"Cannot find entity {eventData.ItemId} in hittables.");
}
_hittables[eventData.ItemId].OnHit(eventData.BallId);
break;

case EventId.HitEventsUnhit:
_hittables[eventData.ItemId].OnHit(eventData.BallId, true);
break;

case EventId.LimitEventsBos:
_rotatables[eventData.ItemId].OnRotate(eventData.FloatParam, false);
break;

case EventId.LimitEventsEos:
_rotatables[eventData.ItemId].OnRotate(eventData.FloatParam, true);
break;

case EventId.SpinnerEventsSpin:
_spinnables[eventData.ItemId].OnSpin();
break;

case EventId.FlipperEventsCollide:
_collidables[eventData.ItemId].OnCollide(eventData.BallId, eventData.FloatParam);
break;

case EventId.SurfaceEventsSlingshot:
_slingshots[eventData.ItemId].OnSlingshot(eventData.BallId);
break;

case EventId.TargetEventsDropped:
_droppables[eventData.ItemId].OnDropStatusChanged(true, eventData.BallId);
break;

case EventId.TargetEventsRaised:
_droppables[eventData.ItemId].OnDropStatusChanged(false, eventData.BallId);
break;

default:
throw new InvalidOperationException($"Unknown event {eventData.eventId} for entity {eventData.ItemId}");
}
Debug.Log(eventData);
// todo re-enable
// switch (eventData.eventId) {
// case EventId.HitEventsHit:
// if (!_hittables.ContainsKey(eventData.ItemId)) {
// Debug.LogError($"Cannot find item {eventData.ItemId} in hittables.");
// }
// _hittables[eventData.ItemId].OnHit(eventData.BallId);
// break;
//
// case EventId.HitEventsUnhit:
// _hittables[eventData.ItemId].OnHit(eventData.BallId, true);
// break;
//
// case EventId.LimitEventsBos:
// _rotatables[eventData.ItemId].OnRotate(eventData.FloatParam, false);
// break;
//
// case EventId.LimitEventsEos:
// _rotatables[eventData.ItemId].OnRotate(eventData.FloatParam, true);
// break;
//
// case EventId.SpinnerEventsSpin:
// _spinnables[eventData.ItemId].OnSpin();
// break;
//
// case EventId.FlipperEventsCollide:
// _collidables[eventData.ItemId].OnCollide(eventData.BallId, eventData.FloatParam);
// break;
//
// case EventId.SurfaceEventsSlingshot:
// _slingshots[eventData.ItemId].OnSlingshot(eventData.BallId);
// break;
//
// case EventId.TargetEventsDropped:
// _droppables[eventData.ItemId].OnDropStatusChanged(true, eventData.BallId);
// break;
//
// case EventId.TargetEventsRaised:
// _droppables[eventData.ItemId].OnDropStatusChanged(false, eventData.BallId);
// break;
//
// default:
// throw new InvalidOperationException($"Unknown event {eventData.eventId} for entity {eventData.ItemId}");
// }
}

internal void BallCreated(Entity ballEntity, GameObject ball)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,7 @@ public EventData(EventId eventId, int itemId, float floatParam, bool groupEvent
FloatParam = floatParam;
GroupEvent = groupEvent;
}

public override string ToString() => $"Event {eventId} for item {ItemId} by ball {BallId} ({FloatParam})";
}
}

0 comments on commit 13c6c0b

Please sign in to comment.