Commit abe8dc49 authored by Luke Smith's avatar Luke Smith

Added Missing Files For MapMaker

parent 5b34e3c8
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();
}
}
}
This diff is collapsed.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class MapMaker : MonoBehaviour {
public GameObject emptyTile;
private Text rowsText;
private Text colsText;
private MapMakerTile[,] map;
private Vector2 previousMousePosition;
void Start () {
rowsText = GameObject.Find ("Rows").transform.FindChild("Text").GetComponent<Text> ();
colsText = GameObject.Find ("Cols").transform.FindChild("Text").GetComponent<Text> ();
}
void Update () {
if (Input.GetAxisRaw("Mouse ScrollWheel") != 0) {
Camera.main.orthographicSize += Input.GetAxisRaw("Mouse ScrollWheel") * -10;
}
if (Input.GetMouseButton(1)) {
Vector2 mouseDelta = (Vector2)(Camera.main.ScreenToWorldPoint (Input.mousePosition)) - previousMousePosition;
Camera.main.transform.position = new Vector3(Camera.main.transform.position.x - mouseDelta.x, Camera.main.transform.position.y - mouseDelta.y, -10f);
}
previousMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
public void GenerateEmptyTiles () {
int rows = 0;
int cols = 0;
int.TryParse (rowsText.text, out rows);
int.TryParse (colsText.text, out cols);
if (rows > 0 && cols > 0) {
GenerateEmptyTiles (rows, cols);
}
}
public MapMakerTile[,] GenerateEmptyTiles (int rows, int cols) {
GameObject[] tiles = GameObject.FindGameObjectsWithTag ("Empty Tile");
for (int i = 0; i < tiles.Length; i++) {
Destroy (tiles [i]);
}
map = new MapMakerTile[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<MapMakerTile> ();
map [x, y].x = x;
map [x, y].y = y;
}
}
return map;
}
public void GetMapDimensions (out int x, out int y) {
x = map.GetLength (0);
y = map.GetLength (1);
}
}
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