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. Reset(); // resetting by copying values instead of mutating might be easier.
else { else {
for (int i = 0; i < n; i++) { 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; Current[cur.X, cur.Y] = cur.Old;
SetDirty(new Vector2Int(cur.X, cur.Y)); SetDirty(new Vector2Int(cur.X, cur.Y));
} }

View File

@@ -3,8 +3,9 @@ using System.Collections.Generic;
using Algorithms; using Algorithms;
using LevelGeneration; using LevelGeneration;
using TMPro; using TMPro;
using UnityEditor;
using UnityEngine; using UnityEngine;
using UnityEngine.Analytics; using UnityEngine.UI;
/// <summary> /// <summary>
/// The primary controller of the entire application, managing state, events and sending commands /// 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 Stack<Node> _path;
private float _runtime; private float _runtime;
public float speed;
public float clampIncrement;
public bool moving = true;
public Camera mainCamera; public Camera mainCamera;
public GameObject gridObject; public GameObject gridObject;
public GridController gridController; public GridController gridController;
public TextMeshPro debugText; public TextMeshPro debugText;
public float speed; public Slider progressSlider;
public float clampIncrement; private float? _moveTo;
private bool _allowPausing = true;
private int CurrentIndex { private int CurrentIndex {
get => (int) _runtime; get => (int) _runtime;
@@ -31,6 +37,8 @@ public class Manager : MonoBehaviour {
public void Start() { public void Start() {
GeneratePath(); GeneratePath();
Resize(); Resize();
progressSlider.onValueChanged.AddListener((value) => MoveToSlider(value));
} }
// public void OnDrawGizmos() { // public void OnDrawGizmos() {
@@ -45,10 +53,10 @@ public class Manager : MonoBehaviour {
private float CurrentMultiplier() { private float CurrentMultiplier() {
if (_state.Index == -1) if (_state.Index == -1)
return 1; return 1;
switch (_state.CurrentChange.New) { switch (_state.CurrentChange.New) {
case GridNodeType.Path: case GridNodeType.Path:
return 1/5f; return 1 / 5f;
case GridNodeType.Empty: case GridNodeType.Empty:
break; break;
case GridNodeType.Wall: case GridNodeType.Wall:
@@ -64,24 +72,41 @@ public class Manager : MonoBehaviour {
default: default:
throw new ArgumentOutOfRangeException(); throw new ArgumentOutOfRangeException();
} }
return 1; 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;
public void Update() {
// 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) if (CurrentIndex < _state.Count)
LoadNextState(); LoadNextState();
else { else {
// No new states to load, generate new grid
GeneratePath(); GeneratePath();
CurrentIndex = 0;
} }
// Update progress slider silently
progressSlider.SetValueWithoutNotify(_runtime / _state.Count);
} }
/// <summary>
/// Generates a new grid and runs pathfinding.
/// </summary>
private void GeneratePath() { private void GeneratePath() {
CurrentIndex = 0;
var nodeGrid = new NodeGrid(gridController.width, gridController.height); var nodeGrid = new NodeGrid(gridController.width, gridController.height);
Vector2Int start = nodeGrid.RandomPosition(); Vector2Int start = nodeGrid.RandomPosition();
@@ -98,6 +123,9 @@ public class Manager : MonoBehaviour {
_state = _algorithm.ChangeController; _state = _algorithm.ChangeController;
} }
/// <summary>
/// Loads the appropriate grid state into the shader via the ChangeController instance.
/// </summary>
private void LoadNextState() { private void LoadNextState() {
_state.MoveTo(CurrentIndex); _state.MoveTo(CurrentIndex);
gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags); gridController.LoadDirtyGridState(_state.Current, _state.DirtyFlags);

View File

@@ -41,7 +41,7 @@ Shader "PDT Shaders/TestGrid"
static const float4 _gridColors[7] = { static const float4 _gridColors[7] = {
float4(255 / 255.0, 255 / 255.0, 255 / 255.0, 1.0), // Empty 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(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(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 float4(252 / 255.0, 236 / 255.0, 3 / 255.0, 1.0), // Seen