Skip to content

Commit

Permalink
player: Add slow-motion feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Feb 11, 2021
1 parent bce1894 commit 9654530
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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

0 comments on commit 9654530

Please sign in to comment.