Editor de niveles (Parte 3)

En esta tercera y última parte del editor de niveles he hecho la parte donde podemos editar las casillas, pintarlas, modificarlas y guardar el archivo en un archivo JSON y cargar los niveles en el editor para seguir trabajando.


El objetivo final de editor es poder generar archivos que son imprescindibles para mostrar el tablero en las diversas etapas del juego. Una vez los generemos con el editor de niveles podremos generar el tablero con su configuración en el núcleo del juego.



El principal problema que genera un editor de niveles en Unity, es que es necesario tener sincronizada la información entre la parte visual del editor y la estructura que guarda la información que en definitiva será la que a través de la rutina guardaremos en el fichero físico.

En concreto, en nuestro caso, tenemos un objeto en memoria que almacena la información parar todo el editor y un conjunto de celdas (cells) que contiene el componente para cada una de las casillas en pantalla con la información.


Entre el editor visual y la estructura de datos necesitamos rutinas de intercambio que nos permitan pasar de una estructura a la otra para mantener la coherencia de los datos.

Información de un nivel:
 [Serializable]  
 public class StageCell  
 {  
   public int x;  
   public int y;  
   public int listPos;  
   public int type;  
   public bool isFinal;  
   public bool isObjective;  
   public bool isStart;  
   public int water;  
   public int food;  
   public int troops;  
   public int weapons;  
   public int gold;    
 }  
 [Serializable]  
 public class Stage  
 {  
   public int version;  
   public int numStage;  
   public string nameStage;  
   public string fileName;  
   public List<StageCell> gridStage = new List<StageCell>();  
   public Stage()  
   {      
     DefaultCells cond = new DefaultCells();  
     version = cond.version;  
     numStage = 0;  
     nameStage = "";  
     fileName = "";      
     for (int i = 0; i < 64; i++)  
     {  
       StageCell defaultCell = new StageCell();  
       defaultCell.x = 0;  
       defaultCell.y = 0;  
       defaultCell.listPos = 0;  
       defaultCell.type = cond.type;  
       defaultCell.isFinal = false;  
       defaultCell.isObjective = false;  
       defaultCell.isStart = false;  
       defaultCell.water = cond.water;  
       defaultCell.food = cond.food;  
       defaultCell.troops = cond.troops;  
       defaultCell.weapons = cond.weapons;  
       defaultCell.gold = cond.gold;  
       gridStage.Add(defaultCell);  
     }  
   }  
 }  

Cell.cs (gestión de las celdas visuales):
   public void Start()  
   {  
     cellIcon = GameObject.Find("Tile");  
     type = GameObject.Find("Type").GetComponent<Text>();  
     x = GameObject.Find("XText").GetComponent<Text>();  
     y = GameObject.Find("YText").GetComponent<Text>();  
     troops = GameObject.Find("TroopsText").GetComponent<Text>();  
     weapons = GameObject.Find("WeaponsText").GetComponent<Text>();  
     water = GameObject.Find("WaterText").GetComponent<Text>();  
     food = GameObject.Find("FoodText").GetComponent<Text>();  
     gold = GameObject.Find("GoldText").GetComponent<Text>();  
   }  
   public void ImClicked()  
   {  
     if (GlobalInfo.isEditing)  
     {        
       SetInfo(GlobalInfo.paintCellType);  
     }  
     if (GlobalInfo.isPainting)  
     {        
       PaintAll(GlobalInfo.paintCellType);  
     }  
     GlobalInfo.editingCell = info.listPos;  
     ShowInfo();  
   }  
   public void ShowCell()  
   {  
     this.gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.Find("EditorManager").GetComponent<EditorClickManager>().TypeSprite(info.type);  
     finalImage.SetActive(info.isFinal);  
     objectiveImage.SetActive(info.isObjective);  
     startImage.SetActive(info.isStart);  
   }  
   public void ShowInfo()  
   {  
     cellIcon.GetComponent<Image>().sprite = GameObject.Find("EditorManager").GetComponent<EditorClickManager>().TypeSprite(info.type);      
     type.text = TypeName(info.type);  
     x.text = info.x.ToString();  
     y.text = info.y.ToString();  
     troops.text = info.troops.ToString("#,#");  
     weapons.text = info.weapons.ToString("#,#");  
     water.text = info.water.ToString("#,#");  
     food.text = info.food.ToString("#,#");  
     gold.text = info.gold.ToString("#,#");  
     finalImage.SetActive(info.isFinal);         
     objectiveImage.SetActive(info.isObjective);      
     startImage.SetActive(info.isStart);  
     ShowBorder();  
   }  
   private void PaintAll(int num)  
   {  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("EditorCell");  
     foreach (GameObject cell in cells)  
     {  
       cell.GetComponent<Cell>().SetInfo(GlobalInfo.paintCellType);        
     }  
   }  
   public void SetInfo(int num)  
   {  
     DefaultCells cond = new DefaultCells();  
     cond.SetValues(num);  
     this.gameObject.GetComponent<SpriteRenderer>().sprite = GameObject.Find("EditorManager").GetComponent<EditorClickManager>().TypeSprite(num);  
     info.type = num;  
     info.water = cond.water;  
     info.food = cond.food;  
     info.troops = cond.troops;  
     info.weapons = cond.weapons;  
     info.gold = cond.gold;  
     info.isFinal = cond.isFinal;  
     info.isObjective = cond.isObjective;  
     info.isStart = cond.isStart;  
     finalImage.SetActive(info.isFinal);  
     objectiveImage.SetActive(info.isObjective);  
     startImage.SetActive(info.isStart);  
   }  




Editor.cs (Gestión del los datos del nivel):
 public class Editor : MonoBehaviour  
 {  
   public GameObject saveBox;  
   public GameObject newBox;  
   public GameObject modifyBox;  
   public GameObject fileInfoItem;  
   public GameObject editTile;  
   private Stage editorStage;  
   private void ShowFiles(string filePath, string where)  
   {  
     DirectoryInfo dir = new DirectoryInfo(filePath);  
     FileInfo[] info = dir.GetFiles("*.json");  
     GameObject itemsParent = GameObject.Find(where);  
     foreach (Transform child in itemsParent.transform)  
     {  
       Destroy(child.gameObject);  
     }  
     //Find files  
     foreach (FileInfo f in info)  
     {  
       Debug.Log(f.ToString());  
       //Read File info  
       Stage stage = new Stage();  
       stage = DataSaver.loadData<Stage>(f.ToString(),"");  
       //Edit Prefab before Instantiate  
       Transform levelTitle = fileInfoItem.GetComponentInChildren<Transform>().Find("Name");  
       levelTitle.GetComponent<Text>().text = stage.nameStage;  
       Transform levelNum = fileInfoItem.GetComponentInChildren<Transform>().Find("Num");  
       levelNum.GetComponent<Text>().text = stage.numStage.ToString();  
       Transform levelFilename = fileInfoItem.GetComponentInChildren<Transform>().Find("File");  
       levelFilename.GetComponent<Text>().text = Path.GetFileName(f.ToString());  
       Instantiate(fileInfoItem, new Vector3(0, 0, 0), Quaternion.identity, itemsParent.transform);        
     }  
   }  
   public void OpenFile(string file, string name, string num)  
   {          
     editorStage = DataSaver.loadData<Stage>(file, "");  
     editorStage.nameStage = name;  
     editorStage.numStage = int.Parse(num);  
     GlobalInfo.editingCell = -1;  
     GameObject.Find("Grid").GetComponent<Grid>().Clean();  
     Invoke("UpdateGrid", 1.0f);      
     GameObject.Find("StageNumText").GetComponent<Text>().text = editorStage.numStage.ToString();  
   }  
   private void UpdateGrid()  
   {  
     GameObject.Find("Grid").GetComponent<Grid>().PaintCells();  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("EditorCell");  
     int iAux = 0;  
     foreach (GameObject cell in cells)  
     {  
       cell.GetComponent<Cell>().info.x = editorStage.gridStage[iAux].x;  
       cell.GetComponent<Cell>().info.y = editorStage.gridStage[iAux].y;  
       cell.GetComponent<Cell>().info.listPos = editorStage.gridStage[iAux].listPos;  
       cell.GetComponent<Cell>().info.type = editorStage.gridStage[iAux].type;  
       cell.GetComponent<Cell>().info.isFinal = editorStage.gridStage[iAux].isFinal;  
       cell.GetComponent<Cell>().info.isObjective = editorStage.gridStage[iAux].isObjective;  
       cell.GetComponent<Cell>().info.isStart = editorStage.gridStage[iAux].isStart;  
       cell.GetComponent<Cell>().info.water = editorStage.gridStage[iAux].water;  
       cell.GetComponent<Cell>().info.food = editorStage.gridStage[iAux].food;  
       cell.GetComponent<Cell>().info.troops = editorStage.gridStage[iAux].troops;  
       cell.GetComponent<Cell>().info.weapons = editorStage.gridStage[iAux].weapons;  
       cell.GetComponent<Cell>().info.gold = editorStage.gridStage[iAux].gold;  
       cell.GetComponent<Cell>().ShowCell();  
       iAux++;  
     }  
   }  
   public void OpenNewPanel()  
   {  
     GlobalInfo.isModifying = true;  
     newBox.SetActive(true);  
     GameObject inputNumField = GameObject.Find("InputStageNum");  
     inputNumField.GetComponent<InputField>().text = "0";  
   }  
   public void NewStage()  
   {  
     GameObject inputNumField = GameObject.Find("InputStageNum");      
     if (inputNumField.GetComponent<InputField>().text != "0")  
     {  
       editorStage.numStage = int.Parse(inputNumField.GetComponent<InputField>().text);  
       GameObject.Find("Grid").GetComponent<Grid>().Clean();  
       Invoke("UpdateStage", 1.0f);  
       GameObject.Find("StageNumText").GetComponent<Text>().text = editorStage.numStage.ToString();  
       GlobalInfo.editingCell = -1;  
       CloseNewPanel();  
     }  
   }  
   private void UpdateStage()  
   {  
     GameObject.Find("Grid").GetComponent<Grid>().PaintCells();  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("EditorCell");  
     int iAux = 0;  
     foreach (GameObject cell in cells)  
     {  
       editorStage.gridStage[iAux].x = cell.GetComponent<Cell>().info.x;  
       editorStage.gridStage[iAux].y = cell.GetComponent<Cell>().info.y;  
       editorStage.gridStage[iAux].listPos = cell.GetComponent<Cell>().info.listPos;  
       editorStage.gridStage[iAux].type = cell.GetComponent<Cell>().info.type;  
       editorStage.gridStage[iAux].isFinal = cell.GetComponent<Cell>().info.isFinal;  
       editorStage.gridStage[iAux].isObjective = cell.GetComponent<Cell>().info.isObjective;  
       editorStage.gridStage[iAux].isStart = cell.GetComponent<Cell>().info.isStart;  
       editorStage.gridStage[iAux].water = cell.GetComponent<Cell>().info.water;  
       editorStage.gridStage[iAux].food = cell.GetComponent<Cell>().info.food;  
       editorStage.gridStage[iAux].troops = cell.GetComponent<Cell>().info.troops;  
       editorStage.gridStage[iAux].weapons = cell.GetComponent<Cell>().info.weapons;  
       editorStage.gridStage[iAux].gold = cell.GetComponent<Cell>().info.gold;  
       iAux++;  
     }  
   }  
   public void CloseNewPanel()  
   {  
     newBox.SetActive(false);  
     GlobalInfo.isModifying = false;  
   }  
   public void OpenSavePanel()  
   {  
     if (editorStage.numStage > 0)  
     {  
       GlobalInfo.isModifying = true;  
       saveBox.SetActive(true);  
       GameObject inputNameField = GameObject.Find("InputStageName");  
       inputNameField.GetComponent<InputField>().text = editorStage.nameStage;  
       GameObject inputFileNameField = GameObject.Find("InputFileName");  
       inputFileNameField.GetComponent<InputField>().text = editorStage.fileName;  
     }      
   }  
   public void SaveFile()  
   {  
     GameObject inputNameField = GameObject.Find("InputStageName");      
     GameObject inputFileNameField = GameObject.Find("InputFileName");  
     if (inputFileNameField.GetComponent<InputField>().text !="")  
     {  
       editorStage.nameStage = inputNameField.GetComponent<InputField>().text;  
       editorStage.fileName = inputFileNameField.GetComponent<InputField>().text;        
       string fileName = Path.Combine(Application.persistentDataPath, "levels");  
       fileName = Path.Combine(fileName, editorStage.fileName + "." + "json");  
       UpdateEditorStage();  
       DataSaver.saveData(editorStage, fileName, "");  
       ShowFiles(Path.Combine(Application.persistentDataPath, "levels"), "ObjectGrid");  
       CloseSavePanel();  
     }      
   }  
   public void CloseSavePanel()  
   {  
     saveBox.SetActive(false);  
     GlobalInfo.isModifying = false;  
   }  
   public void OpenModifyPanel()  
   {      
     if (GlobalInfo.editingCell >= 0)  
     {  
       GameObject clickedCell = GameObject.Find("Cell" + (GlobalInfo.editingCell + 1).ToString() + "(Clone)");        
       if (clickedCell.GetComponent<Cell>().info.type > 0)  
       {  
         GlobalInfo.isModifying = true;  
         modifyBox.SetActive(true);          
         clickedCell.GetComponent<Cell>().ShowStaticBorder();  
         GameObject inputTroopsField = GameObject.Find("InputTroops");  
         GameObject inputWeaponsField = GameObject.Find("InputWeapons");  
         GameObject inputWaterField = GameObject.Find("InputWater");  
         GameObject inputFoodField = GameObject.Find("InputFood");  
         GameObject inputGoldField = GameObject.Find("InputGold");  
         inputTroopsField.GetComponent<InputField>().text = clickedCell.GetComponent<Cell>().info.troops.ToString();  
         inputWeaponsField.GetComponent<InputField>().text = clickedCell.GetComponent<Cell>().info.weapons.ToString();  
         inputWaterField.GetComponent<InputField>().text = clickedCell.GetComponent<Cell>().info.water.ToString();  
         inputFoodField.GetComponent<InputField>().text = clickedCell.GetComponent<Cell>().info.food.ToString();  
         inputGoldField.GetComponent<InputField>().text = clickedCell.GetComponent<Cell>().info.gold.ToString();  
         GameObject inputFinalField = GameObject.Find("isFinal");  
         GameObject inputObjectiveField = GameObject.Find("isObjective");  
         GameObject inputStartField = GameObject.Find("isStart");  
         inputFinalField.GetComponent<Toggle>().isOn = clickedCell.GetComponent<Cell>().info.isFinal;  
         inputObjectiveField.GetComponent<Toggle>().isOn = clickedCell.GetComponent<Cell>().info.isObjective;  
         inputStartField.GetComponent<Toggle>().isOn = clickedCell.GetComponent<Cell>().info.isStart;  
       }        
     }      
   }  
   public void Modify()  
   {  
     GameObject clickedCell = GameObject.Find("Cell" + (GlobalInfo.editingCell + 1).ToString() + "(Clone)");  
     GameObject inputTroopsField = GameObject.Find("InputTroops");  
     GameObject inputWeaponsField = GameObject.Find("InputWeapons");  
     GameObject inputWaterField = GameObject.Find("InputWater");  
     GameObject inputFoodField = GameObject.Find("InputFood");  
     GameObject inputGoldField = GameObject.Find("InputGold");  
     GameObject inputFinalField = GameObject.Find("isFinal");  
     GameObject inputObjectiveField = GameObject.Find("isObjective");  
     GameObject inputStartField = GameObject.Find("isStart");  
     clickedCell.GetComponent<Cell>().info.troops = int.Parse(inputTroopsField.GetComponent<InputField>().text);  
     clickedCell.GetComponent<Cell>().info.weapons = int.Parse(inputWeaponsField.GetComponent<InputField>().text);  
     clickedCell.GetComponent<Cell>().info.water = int.Parse(inputWaterField.GetComponent<InputField>().text);  
     clickedCell.GetComponent<Cell>().info.food = int.Parse(inputFoodField.GetComponent<InputField>().text);  
     clickedCell.GetComponent<Cell>().info.gold = int.Parse(inputGoldField.GetComponent<InputField>().text);  
     clickedCell.GetComponent<Cell>().info.isFinal = inputFinalField.GetComponent<Toggle>().isOn;  
     clickedCell.GetComponent<Cell>().info.isObjective = inputObjectiveField.GetComponent<Toggle>().isOn;  
     clickedCell.GetComponent<Cell>().info.isStart = inputStartField.GetComponent<Toggle>().isOn;  
     UpdateEditorStage();  
     clickedCell.GetComponent<Cell>().ShowInfo();  
     CloseModifyPanel();  
   }  
   public void CloseModifyPanel()  
   {  
     GameObject clickedCell = GameObject.Find("Cell" + (GlobalInfo.editingCell + 1).ToString() + "(Clone)");  
     clickedCell.GetComponent<Cell>().HideBorder();  
     modifyBox.SetActive(false);  
     GlobalInfo.isModifying = false;  
   }  
   private void UpdateEditorStage()  
   {  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("EditorCell");  
     int iAux = 0;  
     foreach (GameObject cell in cells)  
     {        
       editorStage.gridStage[iAux].type = cell.GetComponent<Cell>().info.type;  
       editorStage.gridStage[iAux].isFinal = cell.GetComponent<Cell>().info.isFinal;  
       editorStage.gridStage[iAux].isObjective = cell.GetComponent<Cell>().info.isObjective;  
       editorStage.gridStage[iAux].isStart = cell.GetComponent<Cell>().info.isStart;  
       editorStage.gridStage[iAux].water = cell.GetComponent<Cell>().info.water;  
       editorStage.gridStage[iAux].food = cell.GetComponent<Cell>().info.food;  
       editorStage.gridStage[iAux].troops = cell.GetComponent<Cell>().info.troops;  
       editorStage.gridStage[iAux].weapons = cell.GetComponent<Cell>().info.weapons;  
       editorStage.gridStage[iAux].gold = cell.GetComponent<Cell>().info.gold;  
       iAux++;  
     }  
   }  
   // Start is called before the first frame update  
   void Start()  
   {  
     editorStage = new Stage();  
     string filePath = Path.Combine(Application.persistentDataPath, "levels");  
     ShowFiles(filePath, "ObjectGrid");  
     editorStage.nameStage = "";  
     editorStage.fileName = "";  
     editorStage.numStage = 0;  
     GlobalInfo.editingCell = -1;  
     DefaultCells cond = new DefaultCells();  
     editorStage.version = cond.version;  
   }  
 }  

Ahora que puedo gestionar las dos vertientes de la información, a través de la clase EditorClickManager, puedo gestionar la UI de usuario y atender cuando hacemos click sobre una celda en los diferentes modos de edición de la información.

 public class EditorClickManager : MonoBehaviour {  
   public Sprite edit;  
   public Sprite noEdit;  
   public Sprite paint;  
   public Sprite noPaint;  
   public GameObject editButton;  
   public GameObject editTile;  
   public GameObject paintButton;  
   public Sprite tundra;  
   public Sprite desert;  
   public Sprite wood;  
   public Sprite town;  
   public Sprite city;  
   public Sprite army;  
   public Sprite crop;  
   public Sprite lake;  
   public Sprite river;  
   public Sprite mine;  
   public Sprite objective;  
   public Sprite mountain;  
   public Sprite empty;  
   private bool paintFistTime;  
   public void Start()  
   {  
     GlobalInfo.isEditing = false;  
     GlobalInfo.isPainting = false;  
     paintFistTime = true;  
   }  
   public void onClickEditor()  
   {  
     GlobalInfo.isEditing = !(bool)GlobalInfo.isEditing;  
     if (GlobalInfo.isEditing == true)  
     {  
       GlobalInfo.isPainting = false;  
       paintButton.GetComponent<Image>().sprite = noPaint;  
       editButton.GetComponent<Image>().sprite = edit;  
       if (paintFistTime)  
       {  
         GlobalInfo.paintCellType = 0;          
       }  
       editTile.GetComponent<Image>().sprite = TypeSprite(GlobalInfo.paintCellType);  
       editTile.SetActive(true);  
     } else  
     {  
       editButton.GetComponent<Image>().sprite = noEdit;  
       editTile.SetActive(false);  
     }  
   }  
   public void onClickPainter()  
   {  
     GlobalInfo.isPainting = !(bool)GlobalInfo.isPainting;  
     if (GlobalInfo.isPainting == true)  
     {  
       GlobalInfo.isEditing = false;  
       editButton.GetComponent<Image>().sprite = noEdit;  
       paintButton.GetComponent<Image>().sprite = paint;  
       if (paintFistTime)  
       {  
         GlobalInfo.paintCellType = 0;  
       }  
       editTile.GetComponent<Image>().sprite = TypeSprite(GlobalInfo.paintCellType);  
       editTile.SetActive(true);  
     }  
     else  
     {  
       paintButton.GetComponent<Image>().sprite = noPaint;  
       editTile.SetActive(false);  
     }  
   }  
   public void onClickTundra()  
   {  
     if (GlobalInfo.isEditing || GlobalInfo.isPainting)  
     {  
       GlobalInfo.paintCellType = 1;  
       editTile.GetComponent<Image>().sprite = TypeSprite(GlobalInfo.paintCellType);  
     }      
   }  
   ...  
   public Sprite TypeSprite(int num)  
   {  
     if (num == 1) { return tundra; }  
     if (num == 2) { return desert; }  
     if (num == 3) { return wood; }  
     if (num == 4) { return town; }  
     if (num == 5) { return city; }  
     if (num == 6) { return army; }  
     if (num == 7) { return crop; }  
     if (num == 8) { return lake; }  
     if (num == 9) { return river; }  
     if (num == 10) { return mine; }  
     if (num == 11) { return objective; }  
     if (num == 12) { return mountain; }  
     return empty;  
   }  
   void Update()  
   {  
     if (Input.GetMouseButtonDown(0))  
     {  
       Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);  
       Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);  
       RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);  
       if (hit.collider != null)  
       {  
         if (hit.collider.gameObject.tag == "EditorCell" && GlobalInfo.isModifying == false)  
         {  
           GameObject.Find(hit.collider.gameObject.name).GetComponent<Cell>().ImClicked();            
         }          
       }  
     }  
   }  
 }  

No hay comentarios:

Publicar un comentario

Como hacer copias de tu código de Unity con GitHub

Podriamos escribir un libro entero de las bondades de Git para el trabajo colaborativo y la gestión de versiones en un entorno como Unity. D...