add movable slider, add space pause and left click freeze keybinds, fix change controller issues with reversing

This commit is contained in:
Xevion
2020-11-25 02:28:43 -06:00
parent 6bc5baa8ba
commit 67dd0f0771
3 changed files with 42 additions and 14 deletions

View File

@@ -92,7 +92,7 @@ public class ChangeController {
Reset(); // resetting by copying values instead of mutating might be easier.
else {
for (int i = 0; i < n; i++) {
Change cur = _changes[--Index];
Change cur = _changes[Index--]; // post decrement as we apply the current Change's old, not the previous
Current[cur.X, cur.Y] = cur.Old;
SetDirty(new Vector2Int(cur.X, cur.Y));
}

View File

@@ -3,8 +3,9 @@ using System.Collections.Generic;
using Algorithms;
using LevelGeneration;
using TMPro;
using UnityEditor;
using UnityEngine;
using UnityEngine.Analytics;
using UnityEngine.UI;
/// <summary>
/// The primary controller of the entire application, managing state, events and sending commands
@@ -16,12 +17,17 @@ public class Manager : MonoBehaviour {
private Stack<Node> _path;
private float _runtime;
public float speed;
public float clampIncrement;
public bool moving = true;
public Camera mainCamera;
public GameObject gridObject;
public GridController gridController;
public TextMeshPro debugText;
public float speed;
public float clampIncrement;
public Slider progressSlider;
private float? _moveTo;
private bool _allowPausing = true;
private int CurrentIndex {
get => (int) _runtime;
@@ -31,6 +37,8 @@ public class Manager : MonoBehaviour {
public void Start() {
GeneratePath();
Resize();
progressSlider.onValueChanged.AddListener((value) => MoveToSlider(value));
}
// public void OnDrawGizmos() {
@@ -48,7 +56,7 @@ public class Manager : MonoBehaviour {
switch (_state.CurrentChange.New) {
case GridNodeType.Path:
return 1/5f;
return 1 / 5f;
case GridNodeType.Empty:
break;
case GridNodeType.Wall:
@@ -64,24 +72,41 @@ public class Manager : MonoBehaviour {
default:
throw new ArgumentOutOfRangeException();
}
return 1;
}
public void Update() {
var increment = Time.deltaTime * speed * CurrentMultiplier();
if (clampIncrement > 0)
increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement);
_runtime += increment;
// Toggle pause with space
if (_allowPausing && Input.GetKeyDown(KeyCode.Space)) {
moving = !moving;
}
// Increment index if unpaused and not clicking (implying slider may be interacted with)
if (moving && !Input.GetMouseButton(0)) {
var increment = Time.deltaTime * speed * CurrentMultiplier();
if (clampIncrement > 0)
increment = Mathf.Clamp(increment, 0, _state.Count * Time.deltaTime / clampIncrement);
_runtime += increment;
}
// Load next state in grid or update text
if (CurrentIndex < _state.Count)
LoadNextState();
else {
// No new states to load, generate new grid
GeneratePath();
CurrentIndex = 0;
}
// Update progress slider silently
progressSlider.SetValueWithoutNotify(_runtime / _state.Count);
}
/// <summary>
/// Generates a new grid and runs pathfinding.
/// </summary>
private void GeneratePath() {
CurrentIndex = 0;
var nodeGrid = new NodeGrid(gridController.width, gridController.height);
Vector2Int start = nodeGrid.RandomPosition();
@@ -98,6 +123,9 @@ public class Manager : MonoBehaviour {
_state = _algorithm.ChangeController;
}
/// <summary>
/// Loads the appropriate grid state into the shader via the ChangeController instance.
/// </summary>
private void LoadNextState() {
_state.MoveTo(CurrentIndex);
gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags);

View File

@@ -41,7 +41,7 @@ Shader "PDT Shaders/TestGrid"
static const float4 _gridColors[7] = {
float4(255 / 255.0, 255 / 255.0, 255 / 255.0, 1.0), // Empty
float4(0 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // Wall
float4(5 / 255.0, 5 / 255.0, 5 / 255.0, 1.0), // Wall
float4(0 / 255.0, 255 / 255.0, 0 / 255.0, 1.0), // Start
float4(255 / 255.0, 0 / 255.0, 0 / 255.0, 1.0), // End
float4(252 / 255.0, 236 / 255.0, 3 / 255.0, 1.0), // Seen