Added algorithm integer selection & additional UI corrections
This commit is contained in:
@@ -6,21 +6,34 @@ public class nMain : MonoBehaviour
|
||||
{
|
||||
|
||||
[SerializeField] private CellMesh cellMesh;
|
||||
|
||||
public LogicGrid world;
|
||||
char placementMode;
|
||||
|
||||
private LogicGrid world;
|
||||
int placementValue;
|
||||
|
||||
int algorithm;
|
||||
bool astarSelected;
|
||||
bool bfsSelected;
|
||||
bool dfsSelected;
|
||||
|
||||
|
||||
// Coordinates for pathfinding
|
||||
public Vector2Int agent = new Vector2Int();
|
||||
public List<Vector2Int> obstacles = new List<Vector2Int>();
|
||||
public List<Vector2Int> samples = new List<Vector2Int>();
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
// Create world
|
||||
world = new LogicGrid(32, 16, 5f, new Vector3(-76, -34));
|
||||
|
||||
int width = 32;
|
||||
int height = 16;
|
||||
float cellSize = 5f;
|
||||
Vector3 origin = new Vector3(-77, -34);
|
||||
|
||||
world = new LogicGrid(width, height, cellSize, origin);
|
||||
|
||||
// Set default placement to Obstacle
|
||||
placementValue = 1;
|
||||
placementMode = 'o';
|
||||
|
||||
cellMesh.SetGrid(world);
|
||||
|
||||
@@ -28,58 +41,55 @@ public class nMain : MonoBehaviour
|
||||
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
// Update contains keyboard shortcut options
|
||||
void Update()
|
||||
{
|
||||
// Change Placement Modes:
|
||||
|
||||
// Change Placement Mode Keyboard Shortcuts:
|
||||
////////////////////////////////
|
||||
// Place Obstacle in grid
|
||||
if (Input.GetKeyDown("o"))
|
||||
{
|
||||
Debug.Log("Placing Mode: Obstacle");
|
||||
placementMode = 'o';
|
||||
placementValue = 1;
|
||||
}
|
||||
// Place Sample in cell
|
||||
if(Input.GetKeyDown("s"))
|
||||
if (Input.GetKeyDown("s"))
|
||||
{
|
||||
Debug.Log("Placing Mode: Sample");
|
||||
placementMode = 's';
|
||||
placementValue = 2;
|
||||
}
|
||||
// Place agent in cell
|
||||
if (Input.GetKeyDown("a"))
|
||||
{
|
||||
Debug.Log("Placing Mode: Agent");
|
||||
placementMode = 'a';
|
||||
placementValue = 3;
|
||||
}
|
||||
// Reset Canvas
|
||||
if (Input.GetKeyDown("r"))
|
||||
{
|
||||
|
||||
ResetGrid();
|
||||
}
|
||||
|
||||
if (Input.GetKeyDown("g"))
|
||||
{
|
||||
StartAlgorithm();
|
||||
}
|
||||
|
||||
//////////////////////////
|
||||
|
||||
// Update Cell:
|
||||
// Change cell to obstacle
|
||||
// Place value in cell based on selected placementValue with left click
|
||||
if (Input.GetMouseButton(0))
|
||||
{
|
||||
world.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), placementValue);
|
||||
}
|
||||
// Clear cell
|
||||
// Clear cell with right click
|
||||
if (Input.GetMouseButton(1))
|
||||
{
|
||||
world.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), 0);
|
||||
}
|
||||
|
||||
// Make functionality for when "start" button is pressed.
|
||||
// should call some sort of function that will take in the world
|
||||
// and perform the selected algorithm
|
||||
|
||||
}
|
||||
|
||||
|
||||
public void SetModeObstacle()
|
||||
{
|
||||
placementValue = 1;
|
||||
@@ -88,7 +98,6 @@ public class nMain : MonoBehaviour
|
||||
{
|
||||
placementValue = 2;
|
||||
}
|
||||
|
||||
public void SetModeAgent()
|
||||
{
|
||||
placementValue = 3;
|
||||
@@ -98,4 +107,80 @@ public class nMain : MonoBehaviour
|
||||
world.reset();
|
||||
}
|
||||
|
||||
public void StartAlgorithm()
|
||||
{
|
||||
Vector2Int coord = new Vector2Int();
|
||||
|
||||
// Collect information about world
|
||||
for (int x = 0; x < world.GetWidth(); x++)
|
||||
for (int y = 0; y < world.GetHeight(); y++)
|
||||
{
|
||||
int index = x * world.GetHeight() + y;
|
||||
int value = world.GetValue(x, y);
|
||||
|
||||
Debug.Log("x: " + x + " y: " + y + " | value: " + value);
|
||||
|
||||
// Add coordinate to obstacle array
|
||||
if (value == 1)
|
||||
{
|
||||
coord.x = x;
|
||||
coord.y = y;
|
||||
|
||||
if (!obstacles.Contains(coord))
|
||||
obstacles.Add(coord);
|
||||
}
|
||||
// Add coordinate to sample array
|
||||
if (value == 2)
|
||||
{
|
||||
coord.x = x;
|
||||
coord.y = y;
|
||||
|
||||
if (!samples.Contains(coord))
|
||||
samples.Add(coord);
|
||||
}
|
||||
// Add coordinate to obstacle array
|
||||
if (value == 3)
|
||||
{
|
||||
agent.x = x;
|
||||
agent.y = y;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (true)
|
||||
{
|
||||
Pathfinder astar = new Pathfinder(world, agent, obstacles, samples, algorithm, 0);
|
||||
astar.go();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public void AstarToggle(bool tickOn)
|
||||
{
|
||||
astarSelected = tickOn;
|
||||
Debug.Log(astarSelected);
|
||||
algorithm = 0;
|
||||
Debug.Log(algorithm);
|
||||
|
||||
}
|
||||
|
||||
public void DFSToggle(bool tickOn)
|
||||
{
|
||||
dfsSelected = tickOn;
|
||||
Debug.Log(dfsSelected);
|
||||
algorithm = 1;
|
||||
Debug.Log(algorithm);
|
||||
}
|
||||
|
||||
public void BFSToggle(bool tickOn)
|
||||
{
|
||||
bfsSelected = tickOn;
|
||||
Debug.Log(bfsSelected);
|
||||
algorithm = 2;
|
||||
Debug.Log(algorithm);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user