Commit f14ca9e7 authored by Cethin's avatar Cethin

Added MapTool stuff for map generation, as well as a world data layer created...

Added MapTool stuff for map generation, as well as a world data layer created by it and a PriorityQueue
parent c884d330
fileFormatVersion: 2
guid: 7213ed333eec9ca44a24200f0794a9f5
folderAsset: yes
timeCreated: 1477029052
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 8e3fef4918cc34643b1b36754416b44c
folderAsset: yes
timeCreated: 1477029607
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
[System.Serializable]
public class goColor
{
public GameObject go;
public GameObject border;
public Color32 rgb;
public goColor(GameObject go, Color32 rgb, GameObject border = null)
{
this.go = go;
this.border = border;
this.rgb = rgb;
}
public goColor(GameObject go, byte r, byte g, byte b, GameObject border = null)
{
this.go = go;
this.border = border;
this.rgb = new Color32(r, g, b, 0);
}
}
[System.Serializable]
public class goAlpha
{
public GameObject go;
public int aPercent;
public goAlpha(GameObject go, int val)
{
this.go = go;
aPercent = val;
}
public static byte percentToByte(int val)
{
return BitConverter.GetBytes( Mathf.RoundToInt( 255 * ((float)(((((val < 100) ? val : 100) > 0) ? val : 0)) / 100)))[0];
}
public static int byteToPercent(byte val)
{
return val / 255 * 100;
}
}
public class EditorTileSelect : MonoBehaviour
{
public string levelName;
public goColor[] rgbMap;
public goAlpha[] aMap;
public void save()
{
string path = Application.dataPath + "/Resources/Levels/" + levelName + ".txt";
if (!File.Exists(path))
{
File.Create(path);
}
StreamWriter sw = new StreamWriter(path);
for (int i = 0; i < rgbMap.Length; i++) // Write RGB values
{
//Debug.Log(AssetDatabase.GetAssetPath(gos[i]));
sw.WriteLine(AssetDatabase.GetAssetPath(rgbMap[i].go));
if(rgbMap[i].border != null)
sw.WriteLine(AssetDatabase.GetAssetPath(rgbMap[i].border));
sw.WriteLine(rgbMap[i].rgb.r);
sw.WriteLine(rgbMap[i].rgb.g);
sw.WriteLine(rgbMap[i].rgb.b);
}
if(aMap.Length > 0)
{
sw.WriteLine(":");
for (int i = 0; i < aMap.Length; i++)
{
sw.WriteLine(AssetDatabase.GetAssetPath(aMap[i].go));
sw.WriteLine(goAlpha.percentToByte(aMap[i].aPercent));
}
}
sw.Close();
}
public void load()
{
List<goColor> colors = new List<goColor>();
List<goAlpha> alphas = new List<goAlpha>();
StreamReader sr;
string path = Application.dataPath + "/Resources/Levels/" + levelName + ".txt";
try
{
GameObject go;
GameObject border;
byte r, g, b;
string line;
bool doingAlphas = false;
sr = new StreamReader(path);
while (!sr.EndOfStream) // Read values until EOS
{
line = sr.ReadLine();
if (line == ":")
{
doingAlphas = true;
line = sr.ReadLine();
}
go = AssetDatabase.LoadAssetAtPath<GameObject>(line); // Load GameObject
line = sr.ReadLine();
if (!char.IsDigit(line[0])) // Has a border
{
border = AssetDatabase.LoadAssetAtPath<GameObject>(line);
line = sr.ReadLine();
}
else
border = null;
if (!doingAlphas) // Reading RGB values
{
byte.TryParse(line, out r); // Read red value
line = sr.ReadLine();
byte.TryParse(line, out g); // Read green value
line = sr.ReadLine();
byte.TryParse(line, out b); // Read blue value
//Debug.Log(r + ", " + g + ", " + b + " | " + go.name);
colors.Add(new goColor(go, r, g, b, border));
}else // Reading alpha values
{
byte.TryParse(line, out r); // Read alpha value (store in r because it wont be used any more)
//Debug.Log(r + " | " + go.name);
alphas.Add(new goAlpha(go, r));
}
}
sr.Close();
}
catch (System.Exception)
{
throw;
}
rgbMap = colors.ToArray();
aMap = alphas.ToArray();
}
}
[CustomEditor (typeof(EditorTileSelect))]
public class EditorTileSelectEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if(GUILayout.Button("Save"))
{
((EditorTileSelect)target).save();
}
if(GUILayout.Button("Load"))
{
((EditorTileSelect)target).load();
}
}
}
fileFormatVersion: 2
guid: 71193755354b6084f8c83dfae7e13884
timeCreated: 1477028252
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
This diff is collapsed.
fileFormatVersion: 2
guid: 2eb8d042094fca74aacf1bbf0b5029bc
timeCreated: 1477028252
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
public class PriorityQueue<T> where T : IComparable<T>
{
private List<T> data; // 1 Indexed, element 0 is left empty
public PriorityQueue()
{
data = new List<T>();
}
public T Push(T t)
{
int i = data.Count; // Starts at 1
data.Add(t);
for(;i > 2 && t.CompareTo(data[i/2]) < 0; i = (i / 2) - 1) // Compare t to parent and swap if needed
{
// Swap parent to child position
swap(i, (i / 2) - 1);
}
return t;
}
public T Pop()
{
if (data.Count <= 0)
return default(T);
T removed = data[0]; // Store what is removed to return
percDown(0);
return removed;
}
public T Peek()
{
if (data.Count <= 0)
return default(T);
return data[0];
}
public bool empty()
{
return data.Count <= 0;
}
public bool Remove(T r)
{
for (int i = 0; i < data.Count; i++)
{
if(r.CompareTo(data[i]) == 0)
{
percDown(i);
return true;
}
}
return false;
}
public String toString()
{
String str = "Priority Queue (" + (data.Count - 1) + ")\n";
foreach (T t in data)
{
if (EqualityComparer<T>.Default.Equals(t, default(T)))
str += "NULL";
else
str += t.ToString();
}
return str;
}
private void swap(int p1, int p2)
{
T temp = data[p1];
data[p1] = data[p2];
data[p2] = temp;
}
// Percolate down from i
// Fix the structure in order of priority
private void percDown(int i)
{
int il = (i * 2) + 1; // Index of left child
int ir = il + 1; // Index of right child
data[i] = data[data.Count - 1]; // Move last element to removed position
data.RemoveAt(data.Count - 1); // Remove the copy of the previous last element
while (il < data.Count) // Percolate down
{
if (ir >= data.Count || data[il].CompareTo(data[ir]) < 0) // Left is higher priority
{
swap(i, il);
i = il;
}
else // Right is higher priority
{
swap(i, ir);
i = ir;
}
// Calculate new child indexes
il = (i * 2) + 1;
ir = il + 1;
}
}
}
using UnityEngine;
using System.Collections;
using System;
public class Tile : MonoBehaviour, IComparable<Tile>
{
public bool walkable = false;
public int weight = 1; // The cost for moving through this tile
// Set negative for unwalkable
public int x;
public int y;
public Tile(bool walkable = true, int weight = 1)
{
this.walkable = walkable;
this.weight = weight;
}
public int CompareTo(Tile other)
{
if (other == null)
return 1;
return (x + y) - (other.x - other.y); // Same coordinate. Should be the same tiles
}
public String toString()
{
String str = "";
str += "(" + x + ", " + y + ") " + " " + weight;
return str;
}
}
fileFormatVersion: 2
guid: 8ab250ff019b9b5418425d26a4afa63c
timeCreated: 1470345735
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using System.Collections;
public static class World
{
private static Tile[][] map;
private static int width, height;
public static int Width
{
get
{
return width;
}
}
public static int Height
{
get
{
return height;
}
}
static World()
{ }
public static void setup(int w, int h)
{
width = w;
height = h;
instantiateMap();
}
private static void instantiateMap()
{
map = new Tile[width][];
for (int i = 0; i < width; i++)
{
map[i] = new Tile[height];
}
}
public static void giveMap(Tile[][] newMap) { map = newMap; }
public static void set(int x, int y, Tile t)
{
t.x = x;
t.y = y;
map[x][y] = t;
}
public static Tile get(int x, int y)
{
if (x < 0 || x >= width || y < 0 || y >= width)
{
return null;
}
return map[x][y];
}
}
fileFormatVersion: 2
guid: bc526bbd21e8c6c4085808ba046d01a4
timeCreated: 1470345315
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment