Commit facebc3a authored by Zac Olsen's avatar Zac Olsen

override

parent 1e098a3d
fileFormatVersion: 2
guid: 243bb794b9f808b409bcd50557270e5b
timeCreated: 1477007304
guid: d3fd44e8ccb7ad54b8f9d534900cdb93
timeCreated: 1477085344
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d84b90404a95ff74c813d205e25dfd0c
timeCreated: 1477085344
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 74d7f1f922d265a4cb8e8989f9608bbf
timeCreated: 1477086866
licenseType: Free
TextureImporter:
fileIDToRecycleName: {}
serializedVersion: 2
mipmaps:
mipMapMode: 0
enableMipMap: 1
linearTexture: 0
correctGamma: 0
fadeOut: 0
borderMipMap: 0
mipMapFadeDistanceStart: 1
mipMapFadeDistanceEnd: 3
bumpmap:
convertToNormalMap: 0
externalNormalMap: 0
heightScale: 0.25
normalMapFilter: 0
isReadable: 1
grayScaleToAlpha: 0
generateCubemap: 0
cubemapConvolution: 0
cubemapConvolutionSteps: 7
cubemapConvolutionExponent: 1.5
seamlessCubemap: 0
textureFormat: -1
maxTextureSize: 2048
textureSettings:
filterMode: 0
aniso: 16
mipBias: -1
wrapMode: 1
nPOTScale: 0
lightmap: 0
rGBM: 0
compressionQuality: 50
allowsAlphaSplitting: 0
spriteMode: 1
spriteExtrude: 1
spriteMeshType: 1
alignment: 0
spritePivot: {x: 0.5, y: 0.5}
spriteBorder: {x: 0, y: 0, z: 0, w: 0}
spritePixelsToUnits: 100
alphaIsTransparency: 1
spriteTessellationDetail: -1
textureType: 5
buildTargetSettings: []
spriteSheet:
serializedVersion: 2
sprites: []
outline: []
spritePackingTag:
userData:
assetBundleName:
assetBundleVariant:
Assets/Resources/Prefab/1_front.prefab
0
0
0
Assets/Resources/Prefab/5_back.prefab
255
255
255
Assets/Resources/Prefab/crate_16.prefab
255
0
0
Assets/Resources/Prefab/Tree_01.prefab
0
255
0
fileFormatVersion: 2
guid: b994f2c1c032bdf47a3135af993751bf
timeCreated: 1477086128
licenseType: Free
TextScriptImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 87ed77aef35072942a2705888df1a98f
timeCreated: 1477086529
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: fe45769cb9d72ef458a5450c4d6cb5f6
timeCreated: 1477086550
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 47cd14cbf89bb8c4e9312e93e76b8811
timeCreated: 1477086506
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 8e3fef4918cc34643b1b36754416b44c
guid: 9286c3aa67775f045a5055f3f164abde
folderAsset: yes
timeCreated: 1477029607
timeCreated: 1477085342
licenseType: Free
DefaultImporter:
userData:
......
fileFormatVersion: 2
guid: 9ebfaa068bf3ce248a1b8b8036d59c46
timeCreated: 1477086128
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: d9bbbcc5e70ac6c4bbd42b3651aefea7
timeCreated: 1477611955
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 2c983046a45213c438316fd281ecc003
timeCreated: 1477182966
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 4560b1c3b268ec94ca00c66f9b71ea9d
folderAsset: yes
timeCreated: 1477085342
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
/*
public class Heap <Tile> where Tile : HeapItem<Tile>{
Tile[] heapTiles;
int itemCount;
//replace openList with Heap
//add heapIndex, compareTo to tile class
//compare fCost of node then hCost
//item.compareTo(item2) should return pos if item is less than item2, neg if item is greater than item2
//create heap
public Heap(int maxSize)
{
heapTiles = new Tile[maxSize];
}
//add item into heap and sort it into position
public void Add(Tile item)
{
item.heapIndex = itemCount;
heapTiles[itemCount] = item;
SortUp(item);
itemCount++;
}
//remove first tile and resort heap
public Tile RemoveFirst()
{
Tile first = heapTiles[0];
itemCount--;
heapTiles[0] = heapTiles[itemCount];
heapTiles[0].heapIndex = 0;
SortDown(heapTiles[0]);
return first;
}
//resort item position
public void UpdateItem(Tile item)
{
SortUp(item);
}
public int Count
{
get
{
return itemCount;
}
}
//does heap contain item
public bool Contains(Tile item)
{
return Equals(heapTiles[item.heapIndex], item);
}
//sort tile lower into heap
void SortDown(Tile item)
{
while (true)
{
int leftChildIndex = item.heapIndex * 2 + 1;
int rightChildIndex = (item.heapIndex * 2) + 2;
int swapIndex = 0;
if (leftChildIndex < itemCount)
{
swapIndex = leftChildIndex;
if (rightChildIndex < itemCount)
{
if (heapTiles[leftChildIndex].CompareTo(heapTiles[rightChildIndex]) < 0)
{
swapIndex = rightChildIndex;
}
}
if (item.CompareTo(heapTiles[swapIndex]) < 0)
{
Swap(item, heapTiles[swapIndex]);
}
else
{
return;
}
}
else
{
return;
}
}
}
//sort item higher into heap
void SortUp(Tile item)
{
int indexOfParent = (item.heapIndex - 1) / 2;
while (true)
{
Tile parent = heapTiles[indexOfParent];
if (item.CompareTo(parent) > 0)
{
Swap(item, parent);
}
else
{
break;
}
indexOfParent = (item.heapIndex - 1) / 2;
}
}
//swap tiles
void Swap(Tile item1, Tile item2)
{
heapTiles[item1.heapIndex] = item2;
heapTiles[item2.heapIndex] = item1;
int tempIndex = item1.heapIndex;
item1.heapIndex = item2.heapIndex;
item2.heapIndex = tempIndex;
}
}
public interface HeapItem<Tile> : IComparable<Tile>
{
int heapIndex
{
get;
set;
}
}
*/
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MapMaker : MonoBehaviour {
public GameObject emptyTile;
private Text rowsText;
private Text colsText;
private Tile[,] map;
void Start () {
rowsText = GameObject.Find ("Rows").transform.FindChild("Text").GetComponent<Text> ();
colsText = GameObject.Find ("Cols").transform.FindChild("Text").GetComponent<Text> ();
}
public void GenerateEmptyTiles () {
int rows = 0;
int cols = 0;
int.TryParse (rowsText.text, out rows);
int.TryParse (colsText.text, out cols);
map = new Tile[cols, rows];
float startx = -(float)cols / 2f * emptyTile.transform.lossyScale.x;
float starty = (float)rows / 2f * emptyTile.transform.lossyScale.y;
for (int y = 0; y < rows; y++) {
for (int x = 0; x < cols; x++) {
map[x, y] = ((GameObject) Instantiate (emptyTile, new Vector2(startx + x * emptyTile.GetComponent<BoxCollider2D>().size.x,
starty - y * emptyTile.GetComponent<BoxCollider2D>().size.x), Quaternion.identity)).GetComponent<Tile> ();
map [x, y].x = x;
map [x, y].y = y;
}
}
}
}
fileFormatVersion: 2
guid: b20dbd9f19ada364ca36dceb48c769a1
timeCreated: 1477071770
guid: 01d8f8592a701b6458d43a3ab7d32040
timeCreated: 1477612509
licenseType: Free
MonoImporter:
serializedVersion: 2
......
fileFormatVersion: 2
guid: 9dbbd5fe69390d5489f18914e7f5f7ad
folderAsset: yes
timeCreated: 1477085342
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
......@@ -2,7 +2,7 @@
using System.Collections;
using System.Collections.Generic;
public class MapGen : MonoBehaviour {
public class AStar : MonoBehaviour {
public int rows = 6;
public int cols = 6;
......@@ -60,7 +60,7 @@ public class MapGen : MonoBehaviour {
if (start == finish) {
return;
}
open.Add (start);
start.goal = 0;
start.fitness = EstimateHeuristic (start, finish);
......@@ -76,7 +76,7 @@ public class MapGen : MonoBehaviour {
continue;
}
float tempGoal = current.goal + DistBetween(current, neighbors[i]) + 1 / neighbors[i].speedPercent;
float tempGoal = current.goal + DistBetween(current, neighbors[i]) + neighbors[i].weight;
if (!open.Contains (neighbors[i])) {
neighbors [i].prev = current;
......@@ -91,7 +91,9 @@ public class MapGen : MonoBehaviour {
}
// PrintPathRetrace (finish);
enemy.GetComponent<TraversePath> ().BeginTraversingPath (CreatePath (finish));
if (finish.prev != null) {
enemy.GetComponent<TraversePath> ().BeginTraversingPath (CreatePath (finish));
}
}
float EstimateHeuristic (Tile current, Tile finish) {
......
......@@ -40,6 +40,13 @@ public class Heap {
return first;
}
public void Remove (Tile item){
heapTiles [item.heapIndex].fitness = float.MinValue;
UpdateItem (item);
RemoveFirst ();
}
//resort item position
public void UpdateItem(Tile item)
{
......@@ -128,14 +135,4 @@ public class Heap {
item1.heapIndex = item2.heapIndex;
item2.heapIndex = tempIndex;
}
}
public interface HeapItem<Tile> : IComparable<Tile>
{
int heapIndex
{
get;
set;
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 243bb794b9f808b409bcd50557270e5b
guid: 2d7c8f1877f1ef34290c4e5d768fd5e4
timeCreated: 1477007304
licenseType: Free
MonoImporter:
......
using UnityEngine;
using System.Collections;
public class MapMaker : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ThetaStar : MonoBehaviour {
public int rows = 6;
public int cols = 6;
public GameObject tile;
public GameObject enemy;
public static Tile[,] map;
public Tile startPos;
public Tile endPos;
public Heap open;
void Start () {
map = new Tile[rows, cols];
float startx = -(float)rows / 2f * tile.transform.lossyScale.x;
float starty = (float)cols / 2f * tile.transform.lossyScale.y;
for (int y = 0; y < cols; y++) {
for (int x = 0; x < rows; x++) {
map[x, y] = ((GameObject) Instantiate (tile, new Vector2(startx + x * tile.GetComponent<BoxCollider2D>().size.x,
starty - y * tile.GetComponent<BoxCollider2D>().size.y), Quaternion.identity)).GetComponent<Tile> ();
map [x, y].x = x;
map [x, y].y = y;
}
}
open = new Heap (rows * cols);
enemy = (GameObject)(Instantiate (enemy, map[0, 0].transform.position, Quaternion.identity));
}
void FixedUpdate () {
if (map[rows - 1, cols - 1].prev != null) {
Tile current = map [rows - 1, cols - 1];
while (current.prev != null) {
Debug.DrawLine (current.transform.position, current.prev.transform.position, Color.red);
current = current.prev;
}
}
}
public void BeginPath () {
Path (map[0, 0], map[rows - 1, cols - 1]);
}
void Path (Tile start, Tile finish) {
start.goal = 0;
start.fitness = EstimateHeuristic (start, finish);
open.Add (start);
while (open.Count > 0) {
Tile current = open.RemoveFirst ();
if (current == finish) {
break;
}
current.closed = true;
List<Tile> neighbors = current.GetNeighbors ();
for (int i = 0; i < neighbors.Count; i++) {
if (!neighbors[i].closed) {
if (!open.Contains(neighbors[i])) {
neighbors [i].goal = float.MaxValue;
neighbors [i].prev = null;
}
UpdateVertices (current, neighbors[i], finish);
}
}
}
PrintPathRetrace (finish);
if (finish.prev != null) {
enemy.GetComponent<TraversePath> ().BeginTraversingPath (CreatePath (finish));
}
}
void UpdateVertices (Tile current, Tile neighbor, Tile finish) {
Debug.Log (neighbor.goal);
if (current.goal + DistBetween(current, neighbor) < neighbor.goal) {
neighbor.goal = current.goal + DistBetween (current, neighbor);
neighbor.prev = current;
if (open.Contains (neighbor)) {
open.Remove (neighbor);
}
neighbor.fitness = neighbor.goal + EstimateHeuristic(neighbor, finish);
open.Add (neighbor);
}
Debug.Log (neighbor.goal);
}
void ComputeCosts (Tile current, Tile neighbor) {
if (current.goal + DistBetween(current, neighbor) + neighbor.weight < neighbor.goal) {
neighbor.prev = current;
neighbor.goal = current.goal + DistBetween(current, neighbor) + neighbor.weight;
}
}
float EstimateHeuristic (Tile current, Tile finish) {
return Mathf.Abs (current.x - finish.x) + Mathf.Abs (current.y - finish.y);
}
float DistBetween (Tile current, Tile neighbor) {
return Mathf.Pow(current.x - neighbor.x, 2) + Mathf.Pow(current.y - neighbor.y, 2);
}
void PrintPathRetrace (Tile end) {
while (end != null) {
Debug.Log (end.x + ", " + end.y);
end = end.prev;
}
}
Path CreatePath (Tile finish) {
Path path = new Path ();
while (finish != null) {
path.Push (finish);
finish = finish.prev;
}
return path;
}
}
fileFormatVersion: 2
guid: 73c2a34237ac9dc4e98a964e7145a761
timeCreated: 1476403999
guid: 00b711ab1a936404798437e001aab668
timeCreated: 1477184481
licenseType: Free
MonoImporter:
serializedVersion: 2
......
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class Tile : MonoBehaviour, IComparable {
public bool passable = true;
public Color canPas = new Color (255f, 255f, 255f);
public Color cantPas = new Color (0f, 0f, 0f);
private SpriteRenderer sprite;
public int x;
public int y;
public Tile prev;
public float goal = float.MaxValue;
public float fitness = float.MaxValue;
public float speedPercent = 1f;
public int heapIndex;
public bool closed;
void Start () {
sprite = GetComponent<SpriteRenderer> ();
}
void OnMouseDown () {
passable = !passable;
if (passable) {
sprite.color = canPas;
} else {
sprite.color = cantPas;
}
}
public int CompareTo (object obj) {
if (!(obj is Tile)) {
return -1;
}
return fitness - ((Tile) obj).fitness < 0 ? 1 : -1;
}
public List<Tile> GetNeighbors () {
List<Tile> neighbors = new List<Tile> ();
if (x < MapGen.map.GetLength(0) - 1 && MapGen.map[x + 1, y].passable) {
neighbors.Add (MapGen.map[x + 1, y]);
}
if (x < MapGen.map.GetLength(0) - 1 && y < MapGen.map.GetLength(1) - 1 && MapGen.map[x + 1, y + 1].passable
&& MapGen.map[x, y + 1].passable && MapGen.map[x + 1, y].passable) {
neighbors.Add (MapGen.map[x + 1, y + 1]);
}
if (y < MapGen.map.GetLength (1) - 1 && MapGen.map [x, y + 1].passable) {
neighbors.Add (MapGen.map[x, y + 1]);
}
if (x > 0 && y < MapGen.m