mirror of
https://github.com/Xevion/Paths.git
synced 2026-01-31 10:25:10 -06:00
support Width and Height properly (rectangles), automatic scaling & fit within Camera boundaries, add timescale clamp + clampedIncrement
This commit is contained in:
@@ -246,9 +246,12 @@ MonoBehaviour:
|
||||
m_Script: {fileID: 11500000, guid: ca142250b9964eb1adf66ac7c5cc3e3e, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
mainCamera: {fileID: 519420031}
|
||||
gridObject: {fileID: 2092623184}
|
||||
gridController: {fileID: 2092623185}
|
||||
debugText: {fileID: 1436240699}
|
||||
speed: 7.18
|
||||
speed: 1000
|
||||
clampIncrement: 1
|
||||
--- !u!1 &1436240698
|
||||
GameObject:
|
||||
m_ObjectHideFlags: 0
|
||||
@@ -442,7 +445,7 @@ RectTransform:
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 1436240698}
|
||||
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
|
||||
m_LocalPosition: {x: 0, y: 0, z: 0}
|
||||
m_LocalPosition: {x: 0, y: 0, z: -0.3}
|
||||
m_LocalScale: {x: 1, y: 1, z: 1}
|
||||
m_Children: []
|
||||
m_Father: {fileID: 0}
|
||||
@@ -576,7 +579,8 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
gridMaterial: {fileID: 2100000, guid: 780d53bea4df7b0418e9c8ed8afd6410, type: 2}
|
||||
size: 32
|
||||
width: 130
|
||||
height: 61
|
||||
--- !u!23 &2092623186
|
||||
MeshRenderer:
|
||||
m_ObjectHideFlags: 0
|
||||
|
||||
@@ -131,6 +131,7 @@ namespace Algorithms {
|
||||
/// </summary>
|
||||
/// <param name="generator">A instantiated level generator (ILevelGenerator) object</param>
|
||||
public void ApplyGenerator(ILevelGenerator generator) {
|
||||
generator.Generate(this);
|
||||
}
|
||||
|
||||
public IEnumerable<Node> Iterator() {
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
|
||||
public enum PropertyName {
|
||||
GridSize,
|
||||
GridWidth,
|
||||
GridHeight,
|
||||
ValueLength,
|
||||
Values
|
||||
}
|
||||
@@ -13,8 +13,9 @@ public enum PropertyName {
|
||||
/// </summary>
|
||||
public class GridController : MonoBehaviour {
|
||||
public Material gridMaterial; // Maintain reference to the Grid Material the Shader is implanted upon
|
||||
public int size = 32; // Size of the grid, width and height
|
||||
|
||||
public int width;
|
||||
public int height;
|
||||
|
||||
// Value management
|
||||
private int[] _values;
|
||||
private ComputeBuffer _buffer;
|
||||
@@ -22,10 +23,11 @@ public class GridController : MonoBehaviour {
|
||||
// Get all property IDs
|
||||
private static readonly int ValueLength = Shader.PropertyToID("_valueLength");
|
||||
private static readonly int Values = Shader.PropertyToID("_values");
|
||||
private static readonly int GridSize = Shader.PropertyToID("_GridSize");
|
||||
private static readonly int GridWidth = Shader.PropertyToID("_GridWidth");
|
||||
private static readonly int GridHeight = Shader.PropertyToID("_GridHeight");
|
||||
|
||||
private void Start() {
|
||||
_values = new int[size * size];
|
||||
_values = new int[width * height];
|
||||
_buffer = new ComputeBuffer((int) Mathf.Pow(2048, 2), 4);
|
||||
|
||||
// Update all Shader properties
|
||||
@@ -40,8 +42,11 @@ public class GridController : MonoBehaviour {
|
||||
/// <exception cref="ArgumentOutOfRangeException"></exception>
|
||||
public void UpdateShader(PropertyName property) {
|
||||
switch (property) {
|
||||
case PropertyName.GridSize:
|
||||
gridMaterial.SetFloat(GridSize, size);
|
||||
case PropertyName.GridWidth:
|
||||
gridMaterial.SetFloat(GridWidth, width);
|
||||
break;
|
||||
case PropertyName.GridHeight:
|
||||
gridMaterial.SetFloat(GridHeight, height);
|
||||
break;
|
||||
case PropertyName.Values:
|
||||
_buffer.SetData(_values);
|
||||
@@ -81,7 +86,7 @@ public class GridController : MonoBehaviour {
|
||||
/// <param name="y">the Y coordinate</param>
|
||||
/// <param name="value">the integer value</param>
|
||||
public void SetValue(int x, int y, int value) {
|
||||
_values[size * y + x] = value;
|
||||
_values[width * y + x] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -91,7 +96,7 @@ public class GridController : MonoBehaviour {
|
||||
/// <param name="y">the Y coordinate</param>
|
||||
/// <returns>a integer value</returns>
|
||||
public int GetValue(int x, int y) {
|
||||
return _values[size * y + x];
|
||||
return _values[width * y + x];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -101,6 +106,6 @@ public class GridController : MonoBehaviour {
|
||||
/// <param name="y">the Y coordinate</param>
|
||||
/// <returns>the integer array index</returns>
|
||||
public int GetIndex(int x, int y) {
|
||||
return size * y + x;
|
||||
return width * y + x;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Algorithms;
|
||||
using LevelGeneration;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -15,11 +16,14 @@ public class Manager : MonoBehaviour {
|
||||
private float _lastStart;
|
||||
private float _runtime;
|
||||
|
||||
public Camera mainCamera;
|
||||
public GameObject gridObject;
|
||||
public GridController gridController;
|
||||
public TextMeshPro debugText;
|
||||
public float speed;
|
||||
public float clampIncrement;
|
||||
|
||||
public int CurrentIndex {
|
||||
private int CurrentIndex {
|
||||
get => (int) _runtime;
|
||||
set => _runtime = value;
|
||||
}
|
||||
@@ -27,15 +31,19 @@ public class Manager : MonoBehaviour {
|
||||
public void Start() {
|
||||
_states = new List<GridState>();
|
||||
GeneratePath();
|
||||
Resize();
|
||||
}
|
||||
|
||||
// public void OnDrawGizmos() {
|
||||
// float size = (float) (10.0 / gridController.size);
|
||||
// Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size));
|
||||
// float size = (float) (10.0 / gridController.size);
|
||||
// Gizmos.DrawWireCube(transform.position, new Vector3(size, size, size));
|
||||
// }
|
||||
|
||||
public void Update() {
|
||||
_runtime += Time.deltaTime * speed;
|
||||
var increment = Time.deltaTime * speed;
|
||||
if (clampIncrement > 0)
|
||||
increment = Mathf.Clamp(increment, 0, _states.Count * Time.deltaTime / clampIncrement);
|
||||
_runtime += increment;
|
||||
|
||||
if (CurrentIndex < _states.Count)
|
||||
this.LoadNextState();
|
||||
@@ -52,17 +60,14 @@ public class Manager : MonoBehaviour {
|
||||
}
|
||||
|
||||
private void GeneratePath() {
|
||||
var nodeGrid = new NodeGrid(gridController.size, gridController.size);
|
||||
var nodeGrid = new NodeGrid(gridController.width, gridController.height);
|
||||
_algorithm = new AStar(nodeGrid);
|
||||
|
||||
Vector2Int start = nodeGrid.RandomPosition();
|
||||
// Vector2Int start = new Vector2Int(30, 30);
|
||||
Vector2Int end = nodeGrid.RandomPosition();
|
||||
|
||||
|
||||
int wallCount = (int) (gridController.size * gridController.size * 0.25);
|
||||
for (int unused = 0; unused < wallCount; unused++)
|
||||
nodeGrid.AddRandomWall();
|
||||
nodeGrid.ApplyGenerator(new RandomPlacement(0.25f, true));
|
||||
|
||||
nodeGrid.GetNode(start).Walkable = true;
|
||||
nodeGrid.GetNode(end).Walkable = true;
|
||||
@@ -70,8 +75,6 @@ public class Manager : MonoBehaviour {
|
||||
_path = _algorithm.FindPath(start, end);
|
||||
|
||||
_states = _algorithm.GetStates();
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void LoadNextState() {
|
||||
@@ -80,7 +83,24 @@ public class Manager : MonoBehaviour {
|
||||
|
||||
float change = state.Time - _lastStart;
|
||||
string pathCount = _path != null ? $"{_path.Count}" : "N/A";
|
||||
debugText.text =
|
||||
$"{change * 1000.0:F1}ms\n{this.CurrentIndex:000} / {this._states.Count:000}\nPath: {pathCount} tiles";
|
||||
debugText.text = $"{change * 1000.0:F1}ms\n" +
|
||||
$"{this.CurrentIndex:000} / {this._states.Count:000}\n" +
|
||||
$"Path: {pathCount} tiles";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales the GridController GameObject to fit within the Camera
|
||||
/// </summary>
|
||||
public void Resize() {
|
||||
float ratioImage = (float) gridController.width / gridController.height;
|
||||
float ratioScreen = mainCamera.aspect;
|
||||
|
||||
var orthographicSize = mainCamera.orthographicSize;
|
||||
var image = new Vector2(gridController.width, gridController.height);
|
||||
var screen = new Vector2(2 * orthographicSize * mainCamera.aspect, orthographicSize * 2);
|
||||
|
||||
gridObject.transform.localScale = ratioScreen > ratioImage
|
||||
? new Vector3(image.x * screen.y / image.y, screen.y, 0.001f)
|
||||
: new Vector3(screen.x, image.y * screen.x / image.x, 0.001f);
|
||||
}
|
||||
}
|
||||
@@ -49,7 +49,8 @@ Shader "PDT Shaders/TestGrid"
|
||||
float4(166 / 255.0, 2 / 255.0, 51 / 255.0, 1.0) // Path
|
||||
};
|
||||
|
||||
float _GridSize;
|
||||
float _GridWidth;
|
||||
float _GridHeight;
|
||||
float _LineSize;
|
||||
|
||||
// DX11 needed to run shader at high grid sizes
|
||||
@@ -75,18 +76,18 @@ Shader "PDT Shaders/TestGrid"
|
||||
float2 uv = IN.uv_MainTex;
|
||||
|
||||
fixed4 c = float4(0.0, 0.0, 0.0, 0.0);
|
||||
float gsize = floor(_GridSize);
|
||||
gsize += _LineSize;
|
||||
float gsize_width = floor(_GridWidth) + _LineSize;
|
||||
float gsize_height = floor(_GridHeight) + _LineSize;
|
||||
|
||||
float2 id = float2(
|
||||
floor(uv.x / (1.0 / gsize)), floor(uv.y / (1.0 / gsize))
|
||||
floor(uv.x / (1.0 / gsize_width)), floor(uv.y / (1.0 / gsize_height))
|
||||
);
|
||||
|
||||
float4 color = _InactiveColor;
|
||||
float brightness = _InactiveColor.w;
|
||||
|
||||
// Line Color Check
|
||||
if (_LineSize > 0.0 && (frac(uv.x * gsize) <= _LineSize || frac(uv.y * gsize) <= _LineSize))
|
||||
if (_LineSize > 0.0 && (frac(uv.x * gsize_width) <= _LineSize || frac(uv.y * gsize_height) <= _LineSize))
|
||||
{
|
||||
color = _LineColor;
|
||||
brightness = color.w;
|
||||
@@ -94,7 +95,7 @@ Shader "PDT Shaders/TestGrid"
|
||||
}
|
||||
else
|
||||
{
|
||||
float pos = id.y * _GridSize + id.x;
|
||||
float pos = id.y * _GridWidth + id.x;
|
||||
if (pos < _valueLength)
|
||||
{
|
||||
float index = _values[pos];
|
||||
|
||||
Reference in New Issue
Block a user