Commit d6dc3643 authored by Jordan B Mckenney's avatar Jordan B Mckenney

Merge branch 'character' of git.gmu.edu:gadig/four into jmckenne

# Conflicts:
#	Assets/Prefabs/Ability - Water Shield Projectile.prefab
#	Assets/Scenes/player_animation_test.unity
parents a0dcf274 9eb1fc2b
fileFormatVersion: 2
guid: 9e2b88c6e90966c4893c40b3770da65a
guid: 7a81b522c3eaea24ca8fcc228b393f19
folderAsset: yes
timeCreated: 1475761198
timeCreated: 1479440498
licenseType: Free
DefaultImporter:
userData:
......
fileFormatVersion: 2
guid: 026ac4c911a0e114891980ac911e46c6
guid: b2d2ab70ad127a6488be99d9bbbce7a9
folderAsset: yes
timeCreated: 1475761198
timeCreated: 1477028252
licenseType: Free
DefaultImporter:
userData:
......
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:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using System.IO;
public class LoadLevel : MonoBehaviour
{
public string levelName; // Name of the level in the Resources/Levels folder
// There should be a .png for the tile map of the level and a text file detailing which prefabs
// correlate to which RGB value
// The text file lists the path of the prefab to be used and the RGB value assigned to that prefab
public Material mat;
private GameObject tilesParent;
private bool updated = false;
void Start()
{
create();
}
void Update()
{
if (!updated)
setMaterials();
}
public void create()
{
clear();
Vector3 scale = transform.localScale; // Store scale to rescale when finished creating map
transform.localScale = Vector3.one; // Set scale to 1 so new objects get scaled down
Texture2D level = pathToTex(levelName); // Image of level
Color32[] pixels = level.GetPixels32(); // Pixels of the image
World.setup(level.width, level.height);
Dictionary<byte, Dictionary<byte, Dictionary<byte, GameObject[]>>> rgbDic = new Dictionary<byte, Dictionary<byte, Dictionary<byte, GameObject[]>>>(); // RGB mapping of prefabs
Dictionary<byte, GameObject> aDic = new Dictionary<byte, GameObject>(); // Alpha mapping of prefabs
pathToTiles(levelName, out rgbDic, out aDic);
tilesParent = new GameObject();
tilesParent.transform.parent = transform;
tilesParent.name = "Tiles";
for (int x = 0; x < level.width; x++)
{
byte r, g, b, a;
int pos;
GameObject pref, border;
for (int y = 0; y < level.height; y++)
{
pos = x + (y * level.width);
a = pixels[pos].a;
if (a > 0) // Dont create objects for enpty pixels
{
r = pixels[pos].r;
g = pixels[pos].g;
b = pixels[pos].b;
pref = rgbDic[r][g][b][0];
border = rgbDic[r][g][b][1];
GameObject go = (GameObject)Instantiate(pref, new Vector3(x, y), Quaternion.identity);
go.transform.parent = tilesParent.transform;
go.name = "(" + x + "," + y + ")";
// Add borders if same tiles are not next to it and it has a border
if(border != null)
{
int xOffset, yOffset;
int posN; // Pos of neighbor
Color32 colorN; // Color32 of neighbor
for (int i = 0; i < 4; i++)
{
// DOWN
if(i == 0)
{
xOffset = 0;
yOffset = -1;
}
// RIGHT
else if (i == 1)
{
xOffset = 1;
yOffset = 0;
}
// UP
else if (i == 2)
{
xOffset = 0;
yOffset = 1;
}
// LEFT
else // (i == 3)
{
xOffset = -1;
yOffset = 0;
}
if (x + xOffset >= 0 && x + xOffset < level.width && y + yOffset >= 0 && y + yOffset < level.height)
{
posN = (x + xOffset) + ((y + yOffset) * level.width);
colorN = pixels[posN];
if (r != colorN.r || g != colorN.g || b != colorN.b)
{
GameObject borderGO = (GameObject)Instantiate(border, new Vector3(x + .5f, y + .5f), Quaternion.Euler(0, 0, (90 * i)));
borderGO.transform.parent = go.transform;
borderGO.name = "Border " + i;
}
}else
{
GameObject borderGO = (GameObject)Instantiate(border, new Vector3(x + .5f, y + .5f), Quaternion.Euler(0, 0, (90 * i)));
borderGO.transform.parent = go.transform;
//borderGO.transform.position = Vector3.zero;
borderGO.name = "Border " + i;
}
}
}
//world.set(x, y, go.GetComponentInChildren<Tile>()); // Add tile to world
Tile t = go.GetComponentInChildren<Tile>();
if (t == null)
Debug.LogError(pref.name + " has no Tile!");
t.x = x;
t.y = y;
World.set(x, y, t);
if (aDic.TryGetValue(a, out pref))
{
GameObject goA = (GameObject)Instantiate(pref, new Vector3(x, y), Quaternion.identity);
SpriteRenderer sr = goA.GetComponent<SpriteRenderer>();
if(sr != null)
{
sr.sortingOrder = 100;
}
goA.transform.parent = transform;
go.name = pref.name;
}
}
}
}
transform.localScale = scale; // Restore scale
fixCamera(World.Width, World.Height);
}
public void clear()
{
if (tilesParent != null)
{
/*Transform[] children = tilesParent.GetComponentsInChildren<Transform>();
foreach (Transform trans in children)
{
if (trans != null)
DestroyImmediate(trans.gameObject);
}*/
DestroyImmediate(tilesParent);
}
}
// Returns Texture2D from the given filename
private Texture2D pathToTex(string path)
{
Texture2D tex;
path = "Levels/" + path;
try
{
tex = Resources.Load<Texture2D>(path);
}
catch (System.Exception)
{
Debug.LogError(path + " Not Found!");
throw;
}
return tex;
}
// Returns a Dictionary of Dictionaries of Dictionaries of GameObject using the key r(ed),g(reen),b(lue)
// and a Dictionary of GameObjects using the key a(lpha) from the Resources/Levels text file
// The file lists the path of the prefab to be used and the RGB or alpha value assigned to that prefab
// A line with just ":" seperates the RGB mapping from the alpha mapping
//
// Example mapping Tiles/Tile_0 to full red:
// Tiles/Tile_0
// 255
// 0
// 0
//
// Example mapping Enemies/Enemy_0 to alpha value 50:
// Tiles/Tile_0
// 50
private void pathToTiles(string path, out Dictionary<byte, Dictionary<byte, Dictionary<byte, GameObject[]>>> rgbDic /*Dictionary for RGB lookup*/,
out Dictionary<byte, GameObject> aDic /*Dictionary for alpha lookup*/)
{
rgbDic = new Dictionary<byte, Dictionary<byte, Dictionary<byte, GameObject[]>>>();
aDic = new Dictionary<byte, GameObject>();
StreamReader sr;
path = Application.dataPath + "/Resources/Levels/" + path + ".txt";
try
{
GameObject go, 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);
Dictionary<byte, Dictionary<byte, GameObject[]>> dicR;
if (rgbDic.TryGetValue(r, out dicR)) // Lookup red key in dic
{
Dictionary<byte, GameObject[]> dicG;
if (dicR.TryGetValue(g, out dicG)) // Lookup green key in dicR
{
GameObject[] goB;
if (dicG.TryGetValue(b, out goB)) // Lookup blue key in dicB
{
Debug.LogWarning(r + ", " + g + ", " + b + " Already Used With: " + goB[0].name);
}
else
{
// Create entry for b, go
dicG.Add(b, new GameObject[] { go, border });
}
}
else
{
// Create entry for g, b, go
dicG = new Dictionary<byte, GameObject[]>();
dicG.Add(b, new GameObject[] { go, border });
dicR.Add(g, dicG);
}
}
else
{
// Create entry for r, g, b, go
dicR = new Dictionary<byte, Dictionary<byte, GameObject[]>>();
Dictionary<byte, GameObject[]> dicG = new Dictionary<byte, GameObject[]>();
dicG.Add(b, new GameObject[] { go, border });
dicR.Add(g, dicG);
rgbDic.Add(r, dicR);
}
}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);
GameObject goA;
if(aDic.TryGetValue(r, out goA))
{
Debug.LogWarning(r + " Already Used With: " + goA.name);
}else
{
// Create entry for a, go
aDic.Add(r, go);
}
}
}
}
catch (System.Exception)
{
throw;
}
sr.Close();
}
// Move and resize main camera to show entire level
private void fixCamera(int width, int height)
{
float x = (width / 2) * transform.localScale.x;
float y = (height / 2) * transform.localScale.y;
Camera cam = Camera.main;
cam.orthographicSize = Mathf.Max(x ,y);
cam.transform.position = new Vector3(x, y, -10);
}
private void setMaterials()
{
if (mat != null)
{
Renderer[] renderers = tilesParent.GetComponentsInChildren<Renderer>();
foreach (Renderer rend in renderers)
{
rend.material = mat;
}
}
}
}
[CustomEditor(typeof(LoadLevel))]
public class LoadLevelEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Create"))
{
((LoadLevel)target).create();
}
if (GUILayout.Button("Clear"))
{
((LoadLevel)target).clear();
}
}
}
fileFormatVersion: 2
guid: 2eb8d042094fca74aacf1bbf0b5029bc
timeCreated: 1477028252
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;