Commit df4aa5ac authored by Tanner Grehawick's avatar Tanner Grehawick
Browse files

Merge branch 'master' of git.gmu.edu:gadig/Birb

parents acc42375 96b774b7
fileFormatVersion: 2
guid: 5f812a5ea7a2e42418c52dc5bda62714
timeCreated: 1486267268
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 8c5d1c74f70f3894abe80d24323d2df5
timeCreated: 1486269107
licenseType: Free
NativeFormatImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 2d34d0e204e58a94eb8137d98beeeee2
timeCreated: 1486269157
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: aea9a272aebd3cc40adf9a426a53fda2
folderAsset: yes
timeCreated: 1486265827
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEditor;
[InitializeOnLoad][ExecuteInEditMode]
public class MapEditor : MonoBehaviour
{
#region Static Fields
private static bool on = false;
private static GameObject tile;
private static int layer = 0;
private static Transform tiles;
public static bool On
{
get{ return on; }
set{ on = value; }
}
public static GameObject Tile
{
get{ return tile; }
set{ tile = value; }
}
public static int Layer
{
get{ return layer; }
set{ layer = value; }
}
public static Transform Tiles
{
get{ return tiles; }
set{ tiles = value; }
}
#endregion
public static bool toggle() { return (on = !on); }
void Update()
{
SceneView.onSceneGUIDelegate += OnSceneGUI;
}
private void setupTilesParent()
{
if (Tiles == null)
{
GameObject go;
go = GameObject.FindGameObjectWithTag("Tiles Parent");
if (go == null)
go = new GameObject("Tiles");
Tiles = go.transform;
Tiles.tag = "Tiles Parent";
}
}
static void OnSceneGUI(SceneView sv)
{
if(MapEditor.On)
{
if(Event.current.button == 0 && (Event.current.type == EventType.MouseDown || Event.current.type == EventType.MouseDrag))
{
Vector3 mouse = Event.current.mousePosition;
mouse.y = -mouse.y;
Vector2 pos = Camera.current.ScreenToWorldPoint(mouse);
createTileAt(mousePosCorrection(pos));
Event.current.Use();
}
}
}
static Vector2 mousePosCorrection(Vector2 pos)
{
pos.x = Mathf.Floor(pos.x + .5f);
pos.y = Mathf.Floor(pos.y + .5f + Camera.current.orthographicSize * 2);
return pos;
}
public static void createTileAt(Vector2 pos)
{
if(!World.Ready())
{
Debug.LogError("World not initialized! Please set a width, height, and depth in the inspector and click \"Set\".");
return;
}
if(World.posInBounds(pos, Layer) && (Tile == null || !World.posIsThis(pos, Layer, Tile)))
{
if(Tiles == null)
GameObject.FindObjectOfType<MapEditor>().setupTilesParent();
if(Tile != null)
{
if(!World.posIsThis(pos, Tile))
{
Debug.Log("new");
GameObject go = (GameObject)GameObject.Instantiate(Tile, new Vector3(pos.x, pos.y, Layer), Quaternion.identity);
go = PrefabUtility.ConnectGameObjectToPrefab(go, Tile);
go.transform.position = new Vector3(pos.x, pos.y, Layer);
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
if(sr != null)
{
sr.sortingOrder = layer * -World.SORTING_ORDER_MULT;
}
go.transform.parent = Tiles;
World.set(pos, layer, go);
}
else
Debug.Log("new");
}
else
{
World.set(pos, layer, null);
}
}
}
}
[CustomEditor(typeof(MapEditor))]
public class MapEditorEditor : Editor
{
// Dimensions
int x = (World.Width != 0) ? World.Width : 50;
int y = (World.Height != 0) ? World.Height : 50;
int z = (World.Depth != 0) ? World.Depth : 3;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// World size fields and setup button
x = EditorGUILayout.IntSlider("Width", x, 1, 500);
y = EditorGUILayout.IntSlider("Height", y, 1, 500);
z = EditorGUILayout.IntSlider("Depth", z, 1, 500);
if(GUILayout.Button("Set"))
World.setup(x, y, z);
// Tile field
MapEditor.Tile = (GameObject) EditorGUILayout.ObjectField("Tile", MapEditor.Tile, typeof(GameObject), false);
// Layer field
MapEditor.Layer = EditorGUILayout.IntSlider("Layer", MapEditor.Layer, 0, World.Depth-1);
// Toggle on field
MapEditor.On = GUILayout.Toggle(MapEditor.On, "ON");
}
}
fileFormatVersion: 2
guid: a5aa9870776a33046bdab8421e3dde5c
timeCreated: 1486265828
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
using UnityEngine;
using UnityEditor;
public static class World
{
private static GameObject[][][] map;
private static int width, height, depth;
public const int SORTING_ORDER_MULT = 10;
public static int Width
{
get { return width; }
}
public static int Height
{
get { return height; }
}
public static int Depth
{
get { return depth; }
}
public static bool Ready()
{
return map != null;
}
public static void setup(int w, int h, int d)
{
width = w;
height = h;
depth = d;
instantiateMap();
setupWithGOs();
}
private static void instantiateMap()
{
map = new GameObject[width][][];
for(int i = 0; i < width; i++)
{
map[i] = new GameObject[height][];
for(int j = 0; j < height; j++)
{
map[i][j] = new GameObject[depth];
}
}
}
public static void giveMap(GameObject[][][] newMap) { map = newMap; }
public static bool set(Vector3 pos, GameObject go) { return set(pos.x, pos.y, pos.z, go); }
public static bool set(Vector2 pos, float z, GameObject go) { return set(pos.x, pos.y, z, go); }
public static bool set(Vector2 pos, int z, GameObject go) { return set((int)pos.x, (int)pos.y, z, go); }
public static bool set(float x, float y, float z, GameObject go) { return set((int)x, (int)y, (int)z, go); }
public static bool set(int x, int y, int z, GameObject go)
{
if(!posInBounds(x, y, z))
return false;
destroyPos(x, y, z);
map[x][y][z] = go;
return true;
}
public static void destroyPos(Vector3 pos) { destroyPos(pos.x, pos.y, pos.z); }
public static void destroyPos(Vector2 pos, float z) { destroyPos(pos.x, pos.y, z); }
public static void destroyPos(Vector2 pos, int z) { destroyPos((int)pos.x, (int)pos.y, z); }
public static void destroyPos(float x, float y, float z) { destroyPos((int)x, (int)y, (int)z); }
public static void destroyPos(int x, int y, int z)
{
PrefabUtility.DisconnectPrefabInstance(map[x][y][z]);
GameObject.DestroyImmediate(map[x][y][z]);
}
public static bool posIsEmpty(Vector3 pos) { return posIsEmpty(pos.x, pos.y, pos.z); }
public static bool posIsEmpty(Vector2 pos, float z) { return posIsEmpty(pos.x, pos.y, z); }
public static bool posIsEmpty(Vector2 pos, int z) { return posIsEmpty((int)pos.x, (int)pos.y, z); }
public static bool posIsEmpty(float x, float y, float z){ return posIsEmpty((int)x, (int)y, (int)z); }
public static bool posIsEmpty(int x, int y, int z)
{
if(!posInBounds(x, y, z))
return false;
return map[x][y][z] == null;
}
public static bool posIsThis(Vector3 pos, GameObject go) { return posIsThis(pos.x, pos.y, pos.z, go); }
public static bool posIsThis(Vector2 pos, float z, GameObject go) { return posIsThis(pos.x, pos.y, z, go); }
public static bool posIsThis(Vector2 pos, int z, GameObject go) { return posIsThis((int)pos.x, (int)pos.y, z, go); }
public static bool posIsThis(float x, float y, float z, GameObject go) { return posIsThis((int)x, (int)y, (int)z, go); }
public static bool posIsThis(int x, int y, int z, GameObject go)
{
if(!posInBounds(x, y, z))
return false;
return PrefabUtility.GetPrefabParent(map[x][y][z]) == go;
}
public static bool posInBounds(Vector3 pos) { return posInBounds(pos.x, pos.y, pos.z); }
public static bool posInBounds(Vector2 pos, float z) { return posInBounds(pos.x, pos.y, z); }
public static bool posInBounds(Vector2 pos, int z) { return posInBounds((int)pos.x, (int)pos.y, z); }
public static bool posInBounds(float x, float y, float z) { return posInBounds((int)x, (int)y, (int)z); }
public static bool posInBounds(int x, int y, int z)
{
if(x < 0 || y < 0 || z < 0 || x >= width || y >= height || z >= depth)
{
Debug.LogWarning("Pos out of bounds!");
return false;
}
return true;
}
public static GameObject get(Vector3 pos) { return get(pos.x, pos.y, pos.z); }
public static GameObject get(Vector2 pos, float z) { return get(pos.x, pos.y, z); }
public static GameObject get(Vector2 pos, int z) { return get((int)pos.x, (int)pos.y, z); }
public static GameObject get(float x, float y, float z) { return get((int)x, (int)y, (int)z); }
public static GameObject get(int x, int y, int z)
{
if(!posInBounds(x, y, z))
{
return null;
}
return map[x][y][z];
}
private static void setupWithGOs()
{
GameObject tileParent = GameObject.FindGameObjectWithTag("Tiles Parent");
if(tileParent != null)
{
for(int i = 0; i < tileParent.transform.childCount; i++)
{
GameObject go = tileParent.transform.GetChild(i).gameObject;
SpriteRenderer sr = go.GetComponent<SpriteRenderer>();
if(sr != null)
{
if(!set(go.transform.position.x, go.transform.position.y, sr.sortingOrder / SORTING_ORDER_MULT, go))
GameObject.DestroyImmediate(go);
}
}
}
}
}
fileFormatVersion: 2
guid: 1808a7de1b896704fb2e7f06e7369069
timeCreated: 1486265828
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