Commit 863c81b6 authored by ffperes's avatar ffperes

LAB 2 - 4 Final

Implementation of a pathfiding using A*
parent b1057fb4
# =============== #
# Unity generated #
# =============== #
Temp/
Library/
sysinfo.txt
# ===================================== #
# Visual Studio / MonoDevelop generated #
# ===================================== #
ExportedObj/
obj/
*.svd
*.userprefs
/*.csproj
*.pidb
*.suo
/*.sln
*.user
*.unityproj
*.booproj
# ============ #
# OS generated #
# ============ #
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
\ No newline at end of file
<Properties>
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
<MonoDevelop.Ide.Workbench ActiveDocument="Assets\Scripts\Grid.cs">
<Files>
<File FileName="Assets\Scripts\Grid.cs" Line="109" Column="3" />
<File FileName="Assets\Scripts\Grid.js" Line="1" Column="1" />
<File FileName="Assets\Scripts\Cell.cs" Line="1" Column="1" />
<File FileName="Assets\Scripts\CellScript.js" Line="1" Column="1" />
</Files>
</MonoDevelop.Ide.Workbench>
<MonoDevelop.Ide.DebuggingService.Breakpoints>
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
\ No newline at end of file
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Cell : MonoBehaviour {
public Vector3 Position;
public bool IsOpened;
}
\ No newline at end of file
#pragma strict
public var Position : Vector3;
public var IsOpened : boolean;
\ No newline at end of file
using UnityEngine;
using System.Collections;
using System.Collections.Generic; // needed to work with lists
public class Grid : MonoBehaviour {
//* Assigned in Inspector
public Transform CellPrefab;
public Vector3 GridSize;
public float Buffer;
public List<Transform> Set = new List<Transform> ();
public List<Transform> CompletedSet = new List<Transform> ();
// Use this for initialization
void Start () {
//CreateGrid ();
//SetStart (0, 0);
}
Transform[,] GridArr;
public void CreateGrid(){
int x = (int)GridSize.x;
int z = (int)GridSize.z;
int maxXZ = Mathf.Max(x, z);
Camera.main.transform.position = new Vector3 (maxXZ / 2f - 0.5f, maxXZ, maxXZ / 8f);
GridArr = new Transform[x, z];
Transform newCell;
for(int ix = 0; ix < x; ix++){
for(int iz = 0; iz < z; iz++){
newCell = (Transform)Instantiate (CellPrefab, new Vector3 (ix, 0, iz) * Buffer, Quaternion.identity);
newCell.name = string.Format("({0},0,{1})", ix, iz);
newCell.parent = transform;
newCell.GetComponent<Cell>().Position = new Vector3 (ix, 0, iz);
GridArr[ix, iz] = newCell;
}
}
}
void SetStart(int x, int z){
AddToSet (GridArr [x, z]);
}
void AddToSet(Transform n){
Set.Insert (0, n);
Cell nScript = n.GetComponent<Cell> ();
nScript.IsOpened = true;
n.GetComponent<Renderer>().material.color = Color.grey;
}
public List<Vector3> Directions; //* To assigned in Inspector
void FindNext (){
if(Set.Count == 0){
Debug.Log("We're done! Took " + Time.timeSinceLevelLoad + " seconds for a " + GridSize.x + " by " + GridSize.z + " grid.");
CancelInvoke("FindNext");
return;
}
Transform previous = Set [0];
Cell pScript = Set [0].GetComponent<Cell> ();
Transform next;
Cell nScript; // next script
int prevX = (int)pScript.Position.x;
int prevZ = (int)pScript.Position.z;
int nextX;
int nextZ;
int randSeed = Random.Range (0, 4);
int counter = 0;
do {
do{
Vector3 randDirection = Directions[randSeed];
nextX = prevX + (int)randDirection.x;
nextZ = prevZ + (int)randDirection.z;
randSeed = (randSeed + 1) % 4;
counter++;
if(counter > 4){
AddToCompletedSet(previous);
return;
}
}while(nextX < 0 || nextZ < 0 || nextX >= GridSize.x || nextZ >= GridSize.z);
next = GridArr[nextX, nextZ];
nScript = next.GetComponent<Cell>();
} while(nScript.IsOpened);
DrawDebugLines (10, previous, next);
ClearWalls (previous, next);
AddToSet (next);
}
void AddToCompletedSet(Transform toAdd){
Set.Remove (toAdd);
CompletedSet.Add (toAdd);
toAdd.GetComponent<Renderer>().material.color = Color.black;
}
void DrawDebugLines(int lines, Transform p, Transform n){
for (int i = 0; i < lines; i++) {
Debug.DrawLine(p.position, n.position, Color.red, 100f);
}
}
void ClearWalls(Transform p, Transform n){
RaycastHit[] hitInfo;
hitInfo = Physics.RaycastAll (p.position + Vector3.up, n.position - p.position, Buffer);
foreach (RaycastHit hit in hitInfo) {
Destroy(hit.transform.gameObject);
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Tab) || Input.GetKey (KeyCode.Space)) {
InvokeRepeating("FindNext", 0, 0.001f); //Find Next();
}
if (Input.GetKeyDown (KeyCode.LeftShift)) {
FindNext();
}
if (Input.GetKeyDown (KeyCode.F1)) {
Application.LoadLevel(0);
}
}
bool start = true;
void OnGUI(){
GUILayout.Label ("[Space] to create maze; \n[Shift] to step though maze, \n[F1] to restart");
if (!start) {
return;
}
GUILayout.BeginArea (new Rect (Screen.width / 2f - 100, Screen.height / 2f, 200, 500));
if (GUILayout.Button ("10 x 3", GUILayout.Width (100), GUILayout.Height (30))) {
GridSize = new Vector3(3, 0, 10);
start = false;
CreateGrid();
SetStart (0, 0);
}
if(GUILayout.Button (" 9 x 4", GUILayout.Width(100), GUILayout.Height(30))){
GridSize = new Vector3(4, 0, 9);
start = false;
CreateGrid();
SetStart (0, 0);
}
if(GUILayout.Button ("10 x 10", GUILayout.Width(100), GUILayout.Height(30))){
GridSize = new Vector3(10, 0, 10);
start = false;
CreateGrid();
SetStart (0, 0);
}
if(GUILayout.Button ("25 x 25", GUILayout.Width(100), GUILayout.Height(30))){
GridSize = new Vector3(25, 0, 25);
start = false;
CreateGrid();
SetStart (0, 0);
}
GUILayout.EndArea();
}
}
fileFormatVersion: 2
guid: daf96c0f43cd569409856dcb6f0fbf57
timeCreated: 1444754732
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
#pragma strict
#pragma downcast
//* -> Assigned in Inspector
public var CellPrefab : Transform;
public var GridSize : Vector3;
public var GridArr : Transform[,];
public var Set = new Array();
public var CompletedSet = new Array();
function Start () {
CreateGrid();
SetStart(0,0);
}
function CreateGrid(){
var maxXZ : int = Mathf.Max(GridSize.x, GridSize.z);
Camera.main.transform.position = Vector3(maxXZ/2f -0.5, maxXZ, maxXZ/8f);
GridArr = new Transform[GridSize.x, GridSize.z];
for(var ix = 0; ix < GridSize.x; ix++){
for(var iz = 0; iz < GridSize.z; iz++){
var newCell : Transform = Instantiate(CellPrefab, Vector3(ix, 0, iz), Quaternion.identity);
newCell.name = "("+ix+","+iz+")";
newCell.parent = transform;
var newCellScript : CellScript = newCell.GetComponent("CellScript");
newCellScript.Position = Vector3(ix, 0, iz);
}
}
}
function SetStart(x : int, z : int){
AddToSet(GridArr[x, z]);
}
function AddToSet(toAdd : Transform){
toAdd.GetComponent.<Renderer>().material.color = Color.blue;
var taScript : CellScript = toAdd.GetComponent("CellScript");
taScript.IsOpened = true;
Set.Unshift(toAdd);
}
var Directions : Vector3[]; //* To be assigned in Inspector
function FindNext(){
/*if(set.length == 0){
Debug.Log("We're done.");
retun;
} */
var previous : Transform = Set[Set.length-1];
var pScript : CellScript = previous.GetComponent("CellScript");
var next : Transform;
var nextScript : CellScript;
var prevX : int = pScript.Position.x;
var prevZ : int = pScript.Position.z;
var randDirection : Vector3;
var randSeed : int = Random.Range(0,4);
var nextX : int;
var nextZ : int;
var counter : int;
do{
do{
randDirection = Directions[randSeed];
nextX = prevX + randDirection.x;
nextZ = prevZ + randDirection.z;
randSeed = (randSeed + 1) % 4;
counter++;
if(counter > 4){
Set.RemoveAt(0);
previous.GetComponent.<Renderer>().material.color = Color.black;
yield WaitForEndOfFrame();
return;
}
}while(true); //nextX < 0 || nextZ < 0 || nextX >= GridArr.x || nextZ >= GridSize.z);
next = GridArr[nextX, nextZ];
nextScript = next.GetComponent("CellScript");
}while(nextScript.IsOpened);
}
function Update () {
}
fileFormatVersion: 2
guid: 2292f82989083c140bc4f536ee3ecdbc
timeCreated: 1444759323
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
// Breadth-First Search
/*
public interface Graph<N> {
bool addNode(N a); // true if node added
bool addEdge(N a, N b, int cost); // true if edge added
List<N> nodes();
List<N> neighbours(N a);
int cost(N a, N b); // -1 if no edge
}
public interface GraphSearch<N> {
void setGraph(Graph<N> g);
List<N> findPath(N a, N b, bool trace);
}
public class bfs : MonoBehaviour, Graph, GraphSearch {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
Graph<int>{
// see sparse graph from slide
bool addNode(int a);
bool addEdge(int a, int b, int cost);
List<int> nodes();
List<int> neighbours(int a);
int cost(int a, int b);
}
GraphSearch<int g>{
void setGraph(Graph<int> g);
List<int> findPath(int a, int b, bool trace);
}
*/
fileFormatVersion: 2
guid: ac2727100c3cca944ad8c27870627ff9
timeCreated: 1444296782
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.20506</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{ED7A9401-A338-8492-CA10-893F9841BAA6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace></RootNamespace>
<AssemblyName>Assembly-CSharp-Editor</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<BaseDirectory>Assets</BaseDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>Temp\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;UNITY_5_1_2;UNITY_5_1;UNITY_5;ENABLE_NEW_BUGREPORTER;ENABLE_2D_PHYSICS;ENABLE_4_6_FEATURES;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_FRAME_DEBUGGER;ENABLE_GENERICS;ENABLE_HOME_SCREEN;ENABLE_IMAGEEFFECTS;ENABLE_LIGHT_PROBES_LEGACY;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_PHYSICS_PHYSX3;ENABLE_PLUGIN_INSPECTOR;ENABLE_SHADOWS;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_SPRITES;ENABLE_TERRAIN;ENABLE_UNET;ENABLE_UNITYEVENTS;ENABLE_VR;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_LICENSE;ENABLE_AUDIOMIXER_SUSPEND;ENABLE_EDITOR_METRICS;INCLUDE_DYNAMIC_GI;INCLUDE_GI;INCLUDE_IL2CPP;INCLUDE_DIRECTX12;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;ENABLE_LOCALIZATION;UNITY_STANDALONE_WIN;UNITY_STANDALONE;ENABLE_SUBSTANCE;ENABLE_TEXTUREID_MAP;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_WEBSOCKET_HOST;ENABLE_MONO;ENABLE_PROFILER;DEBUG;TRACE;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0169</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>Temp\bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0169</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.XML" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="UnityEngine">
<HintPath>C:/Program Files/Unity/Editor/Data/Managed/UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEditor">
<HintPath>C:/Program Files/Unity/Editor/Data/Managed/UnityEditor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\Editor\NodeEditor.cs" />
<Reference Include="UnityEngine.UI">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/UnityEngine.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.UI">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/GUISystem/Editor/UnityEditor.UI.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.Networking">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/Networking/UnityEngine.Networking.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.Networking">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/Networking/Editor/UnityEditor.Networking.dll</HintPath>
</Reference>
<Reference Include="UnityEngine.Analytics">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityAnalytics/UnityEngine.Analytics.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.Graphs">
<HintPath>C:/Program Files/Unity/Editor/Data/Managed/UnityEditor.Graphs.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.WebGL.Extensions">
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/webglsupport/UnityEditor.WebGL.Extensions.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.LinuxStandalone.Extensions">
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/linuxstandalonesupport/UnityEditor.LinuxStandalone.Extensions.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.WindowsStandalone.Extensions">
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/windowsstandalonesupport/UnityEditor.WindowsStandalone.Extensions.dll</HintPath>
</Reference>
<Reference Include="UnityEditor.OSXStandalone.Extensions">
<HintPath>C:/Program Files/Unity/Editor/Data/PlaybackEngines/macstandalonesupport/UnityEditor.OSXStandalone.Extensions.dll</HintPath>
</Reference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.20506</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<RootNamespace></RootNamespace>
<ProjectGuid>{ED7A9401-A338-8492-CA10-893F9841BAA6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<AssemblyName>Assembly-CSharp-Editor</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<BaseDirectory>Assets</BaseDirectory>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>Temp\bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;UNITY_5_2_1;UNITY_5_2;UNITY_5;ENABLE_NEW_BUGREPORTER;ENABLE_2D_PHYSICS;ENABLE_4_6_FEATURES;ENABLE_AUDIO;ENABLE_CACHING;ENABLE_CLOTH;ENABLE_DUCK_TYPING;ENABLE_FRAME_DEBUGGER;ENABLE_GENERICS;ENABLE_HOME_SCREEN;ENABLE_IMAGEEFFECTS;ENABLE_LIGHT_PROBES_LEGACY;ENABLE_MICROPHONE;ENABLE_MULTIPLE_DISPLAYS;ENABLE_PHYSICS;ENABLE_PLUGIN_INSPECTOR;ENABLE_SHADOWS;ENABLE_SINGLE_INSTANCE_BUILD_SETTING;ENABLE_SPRITES;ENABLE_TERRAIN;ENABLE_RAKNET;ENABLE_UNET;ENABLE_UNITYEVENTS;ENABLE_VR;ENABLE_WEBCAM;ENABLE_WWW;ENABLE_CLOUD_SERVICES;ENABLE_CLOUD_SERVICES_ADS;ENABLE_CLOUD_HUB;ENABLE_CLOUD_PROJECT_ID;ENABLE_CLOUD_SERVICES_ANALYTICS;ENABLE_CLOUD_SERVICES_UNET;ENABLE_CLOUD_SERVICES_BUILD;ENABLE_CLOUD_LICENSE;ENABLE_EDITOR_METRICS;ENABLE_REFLECTION_BUFFERS;INCLUDE_DYNAMIC_GI;INCLUDE_GI;INCLUDE_IL2CPP;INCLUDE_DIRECTX12;PLATFORM_SUPPORTS_MONO;RENDER_SOFTWARE_CURSOR;ENABLE_LOCALIZATION;ENABLE_ANDROID_ATLAS_ETC1_COMPRESSION;UNITY_STANDALONE_WIN;UNITY_STANDALONE;ENABLE_SUBSTANCE;ENABLE_TEXTUREID_MAP;ENABLE_RUNTIME_GI;ENABLE_MOVIES;ENABLE_NETWORK;ENABLE_CRUNCH_TEXTURE_COMPRESSION;ENABLE_LOG_MIXED_STACKTRACE;ENABLE_UNITYWEBREQUEST;ENABLE_EVENT_QUEUE;ENABLE_WEBSOCKET_HOST;ENABLE_MONO;ENABLE_PROFILER;UNITY_ASSERTIONS;UNITY_EDITOR;UNITY_EDITOR_64;UNITY_EDITOR_WIN</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0169</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>Temp\bin\Release\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<NoWarn>0169</NoWarn>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.XML" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="UnityEngine">
<HintPath>C:/Program Files/Unity/Editor/Data/Managed/UnityEngine.dll</HintPath>
</Reference>
<Reference Include="UnityEditor">
<HintPath>C:/Program Files/Unity/Editor/Data/Managed/UnityEditor.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets\Editor\NodeEditor.cs" />
<Reference Include="UnityEditor.Advertisements">
<HintPath>C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/Advertisements/Editor/UnityEditor.Advertisements.dll</HintPath>