Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions VisualPinball.Engine/Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public static class InputConstants

public const string ActionCreateBall = "Create Ball";
public const string ActionKicker = "Kicker";
public const string ActionSlowMotion = "Slow Motion";
public const string ActionTimeLapse = "Time Lapse";

public const string ActionUpperLeftFlipper = "Upper Left Flipper";
public const string ActionUpperRightFlipper = "Upper Right Flipper";
Expand Down
47 changes: 47 additions & 0 deletions VisualPinball.Unity/VisualPinball.Unity/Game/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using NLog;
using Unity.Entities;
using Unity.Mathematics;
using UnityEngine;
Expand All @@ -37,6 +38,7 @@
using VisualPinball.Engine.VPT.Trigger;
using VisualPinball.Engine.VPT.Trough;
using Light = VisualPinball.Engine.VPT.Light.Light;
using Logger = NLog.Logger;

namespace VisualPinball.Unity
{
Expand Down Expand Up @@ -74,6 +76,11 @@ public class Player : MonoBehaviour
[NonSerialized] private readonly WirePlayer _wirePlayer = new WirePlayer();
[NonSerialized] private readonly List<(InputAction, Action<InputAction.CallbackContext>)> _actions = new List<(InputAction, Action<InputAction.CallbackContext>)>();

private const float SlowMotionMax = 0.1f;
private const float TimeLapseMax = 2.5f;

private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

public Player()
{
TableApi = new TableApi(this);
Expand Down Expand Up @@ -101,6 +108,7 @@ private void Awake()
Table = tableComponent.CreateTable(tableComponent.Data);
BallManager = new BallManager(Table, TableToWorld);
_inputManager = new InputManager();
_inputManager.Enable(HandleInput);

if (engineComponent != null) {
GamelogicEngine = engineComponent;
Expand Down Expand Up @@ -139,6 +147,7 @@ private void OnDestroy()
i.OnDestroy();
}

_inputManager.Disable(HandleInput);
_coilPlayer.OnDestroy();
_switchPlayer.OnDestroy();
_lampPlayer.OnDestroy();
Expand Down Expand Up @@ -356,6 +365,44 @@ public void OnEvent(in EventData eventData)

#endregion

private static void HandleInput(object obj, InputActionChange change)
{
if (obj is InputAction action && action.actionMap.name == InputConstants.MapDebug) {
var value = action.ReadValue<float>();
switch (action.name) {
case InputConstants.ActionSlowMotion: {
switch (change) {
case InputActionChange.ActionPerformed when value > 0.1:
Time.timeScale = math.lerp(1f, SlowMotionMax, value);
break;
case InputActionChange.ActionPerformed:
Time.timeScale = 1;
break;
case InputActionChange.ActionStarted:
Time.timeScale = SlowMotionMax;
break;
case InputActionChange.ActionCanceled:
Time.timeScale = 1;
break;
}
Logger.Info("Timescale = " + Time.timeScale);
break;
}
case InputConstants.ActionTimeLapse: {
if (change == InputActionChange.ActionPerformed) {
if (value > 0.1) {
Time.timeScale = math.lerp(1f, TimeLapseMax, value);
} else {
Time.timeScale = 1;
}
}
Logger.Info("Timescale = " + Time.timeScale);
break;
}
}
}
}

public float3 GetGravity()
{
var slope = Table.Data.AngleTiltMin + (Table.Data.AngleTiltMax - Table.Data.AngleTiltMin) * Table.Data.GlobalDifficulty;
Expand Down
2 changes: 2 additions & 0 deletions VisualPinball.Unity/VisualPinball.Unity/Input/InputManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public static InputActionAsset GetDefaultInputActionAsset()
map = new InputActionMap(InputConstants.MapDebug);
map.AddAction(InputConstants.ActionCreateBall, InputActionType.Button, "<Keyboard>/b");
map.AddAction(InputConstants.ActionKicker, InputActionType.Button, "<Keyboard>/n");
map.AddAction(InputConstants.ActionSlowMotion, InputActionType.Button, "<Keyboard>/s").AddBinding("<Gamepad>/leftStick/down");
map.AddAction(InputConstants.ActionTimeLapse, InputActionType.Button, "<Gamepad>/leftStick/up");

asset.AddActionMap(map);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -36,11 +37,15 @@ internal struct CircleCollider
public ColliderType Type => _header.Type;
public Entity Entity => _header.Entity;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("CircleCollider.Create");

public static void Create(BlobBuilder builder, HitCircle src, ref BlobPtr<Collider> dest, ColliderType type = ColliderType.Circle)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<CircleCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src, type);
PerfMarker.End();
}

public static CircleCollider Create(HitCircle src, ColliderType type = ColliderType.Circle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Physics;

namespace VisualPinball.Unity
Expand All @@ -31,11 +32,15 @@ internal struct Line3DCollider
private float _zHigh;
private float3x3 _matrix;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("Line3DCollider.Create");

public static void Create(BlobBuilder builder, HitLine3D src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<Line3DCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitLine3D src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -42,11 +43,15 @@ internal struct LineCollider
public float V1y { set => _v1.y = value; }
public float V2y { set => _v2.y = value; }

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineCollider.Create");

public static void Create(BlobBuilder builder, LineSeg src, ref BlobPtr<Collider> dest, ColliderType type = ColliderType.Line)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src, type);
PerfMarker.End();
}

public static LineCollider Create(LineSeg src, ColliderType type = ColliderType.Line)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Game;
using VisualPinball.Engine.Physics;

Expand All @@ -37,11 +38,15 @@ internal struct LineSlingshotCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineSlingshotCollider.Create");

public static void Create(BlobBuilder builder, LineSegSlingshot src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineSlingshotCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(LineSegSlingshot src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -33,11 +34,15 @@ internal struct LineZCollider

public float XyY { set => _xy.y = value; }

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("LineZCollider.Create");

public static void Create(BlobBuilder builder, HitLineZ src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var linePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<LineZCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref linePtr);
collider.Init(src);
PerfMarker.End();
}

public static LineZCollider Create(HitLineZ src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -31,11 +32,15 @@ internal struct PlaneCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("PlaneCollider.Create");

public static void Create(BlobBuilder builder, HitPlane src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<PlaneCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitPlane src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;

Expand All @@ -31,11 +32,15 @@ internal struct PointCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("PointCollider.Create");

public static void Create(BlobBuilder builder, HitPoint src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var colliderPtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<PointCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref colliderPtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitPoint src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Physics;
using VisualPinball.Engine.VPT;
Expand All @@ -37,11 +38,15 @@ internal struct TriangleCollider

public float3 Normal() => _normal;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("TriangleCollider.Create");

public static void Create(BlobBuilder builder, HitTriangle src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var trianglePtr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<TriangleCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref trianglePtr);
collider.Init(src);
PerfMarker.End();
}

private void Init(HitTriangle src)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using System.Linq;
using NLog;
using Unity.Entities;
using Unity.Profiling;
using UnityEngine;
using VisualPinball.Engine.Physics;
using Logger = NLog.Logger;
Expand All @@ -29,17 +30,28 @@ internal static class QuadTreeCreationSystem
{
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();

private static readonly ProfilerMarker PerfMarkerTotal = new ProfilerMarker("QuadTreeCreationSystem");
private static readonly ProfilerMarker PerfMarkerInitItems = new ProfilerMarker("QuadTreeCreationSystem (1 - init items)");
private static readonly ProfilerMarker PerfMarkerGenerateColliders = new ProfilerMarker("QuadTreeCreationSystem (2 - generate colliders)");
private static readonly ProfilerMarker PerfMarkerCreateQuadTree = new ProfilerMarker("QuadTreeCreationSystem (3 - create quad tree)");
private static readonly ProfilerMarker PerfMarkerAllocate = new ProfilerMarker("QuadTreeCreationSystem (4 - allocate)");
private static readonly ProfilerMarker PerfMarkerSaveToEntity = new ProfilerMarker("QuadTreeCreationSystem (5 - save to entity)");

public static void Create(EntityManager entityManager)
{
PerfMarkerTotal.Begin();
var table = Object.FindObjectOfType<TableAuthoring>().Table;
var stopWatch = new Stopwatch();

stopWatch.Start();
PerfMarkerInitItems.Begin();
foreach (var playable in table.Playables) {
playable.Init(table);
}
PerfMarkerInitItems.End();

// index hittables
PerfMarkerGenerateColliders.Begin();
var hittables = table.Hittables.Where(hittable => hittable.IsCollidable).ToArray();
var hitObjects = new List<HitObject>();
var id = 0;
Expand All @@ -59,16 +71,20 @@ public static void Create(EntityManager entityManager)
}
stopWatch.Stop();
Logger.Info("Collider Count:\n" + log + "\nTotal: " + c + " colliders in " + stopWatch.ElapsedMilliseconds + "ms");
PerfMarkerGenerateColliders.End();

// construct quad tree
PerfMarkerCreateQuadTree.Begin();
var quadTree = new Engine.Physics.QuadTree(hitObjects, table.BoundingBox);
var quadTreeBlobAssetRef = QuadTreeBlob.CreateBlobAssetReference(
quadTree,
table.GeneratePlayfieldHit(), // todo use `null` if separate playfield mesh exists
table.GenerateGlassHit()
);
PerfMarkerCreateQuadTree.End();

// playfield and glass need special treatment, since not part of the quad tree
PerfMarkerAllocate.Begin();
var playfieldHitObject = table.GeneratePlayfieldHit();
var glassHitObject = table.GenerateGlassHit();
playfieldHitObject.Id = id++;
Expand All @@ -78,14 +94,18 @@ public static void Create(EntityManager entityManager)

// construct collider blob
var colliderBlob = ColliderBlob.CreateBlobAssetReference(hitObjects, playfieldHitObject.Id, glassHitObject.Id);
PerfMarkerAllocate.End();

// save it to entity
PerfMarkerSaveToEntity.Begin();
var collEntity = entityManager.CreateEntity(ComponentType.ReadOnly<QuadTreeData>(), ComponentType.ReadOnly<ColliderData>());
//DstEntityManager.SetName(collEntity, "Collision Data Holder");
entityManager.SetComponentData(collEntity, new QuadTreeData { Value = quadTreeBlobAssetRef });
entityManager.SetComponentData(collEntity, new ColliderData { Value = colliderBlob });
PerfMarkerSaveToEntity.End();

Logger.Info("Static QuadTree initialized.");
PerfMarkerTotal.End();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using Unity.Collections.LowLevel.Unsafe;
using Unity.Entities;
using Unity.Mathematics;
using Unity.Profiling;
using VisualPinball.Engine.Common;
using VisualPinball.Engine.Game;
using VisualPinball.Engine.VPT.Flipper;
Expand All @@ -34,11 +35,15 @@ internal struct FlipperCollider

public ColliderType Type => _header.Type;

private static readonly ProfilerMarker PerfMarker = new ProfilerMarker("FlipperCollider.Create");

public static void Create(BlobBuilder builder, FlipperHit src, ref BlobPtr<Collider> dest)
{
PerfMarker.Begin();
ref var ptr = ref UnsafeUtility.As<BlobPtr<Collider>, BlobPtr<FlipperCollider>>(ref dest);
ref var collider = ref builder.Allocate(ref ptr);
collider.Init(src);
PerfMarker.End();
}

private void Init(FlipperHit src)
Expand Down
Loading