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();            
         }          
       }  
     }  
   }  
 }  

Editor de niveles (Parte 2)

En esta segunda parte del editor de niveles nos centraremos en la creación de las estructuras internas parar poder definir correctamente un nivel y la generación del archivo base a partir de un nivel con datos por defecto.


El objetivo es poder pulsar al botón de añadir nivel, que nos pregunte el número de etapa y genere la cuadrícula con una clase adjuntada en cada celda con la información interna que procesa, para que posteriormente poder editar las casillas y modificar los datos en esta estructura dinámicamente.


Una vez generado el nivel a través de la grid visual e interna en memoria, debemos poder salvar la información en un fichero para poder comprobar que estamos guardando correctamente la información y posteriormente mostrar y actualizar los ficheros encontrados en la carpeta de etapas a través de la lista que creamos en el primera parte del tutorial.



Clase Stage con el contructor y las variables de ficheros:
 using System.Collections.Generic;  
 using UnityEngine;  
 using System;  
 using System.Text;  
 [Serializable]  
 public class StageCell  
 {  
   public int x;  
   public int y;  
   public int listPos;  
   public int type;  
   public bool isFinal;  
   public bool isObjective;  
   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.water = cond.water;  
       defaultCell.food = cond.food;  
       defaultCell.troops = cond.troops;  
       defaultCell.weapons = cond.weapons;  
       defaultCell.gold = cond.gold;  
       gridStage.Add(defaultCell);  
     }  
   }  
 }  
 public class Levels : MonoBehaviour  
 {  
   public static void LoadLevel(int levelNum)  
   {  
     //load info from levels folder      
     TextAsset jsonTextFile = Resources.Load<TextAsset>("Levels/Attila" + levelNum.ToString());  
     object resultValue = JsonUtility.FromJson<Stage>(Encoding.ASCII.GetString(jsonTextFile.bytes));  
     Stage loadlevel = (Stage)Convert.ChangeType(resultValue, typeof(Stage));  
     GlobalInfo.stageVersion = loadlevel.version;   
     GlobalInfo.actualStage = loadlevel.numStage;  
     GlobalInfo.gridStage = loadlevel.gridStage;  
   }  
 }  

Hay que tener en cuenta que el sistema de ficheros no funciona en sistemas Android, ya que el sistema de protección del sistema impide acceder libremente en todas las carpetas del SO. Así mismo el editor esta pensado parar ser ejecutado en ordenador, diseñar las etapas y pasar el contenido a la carpeta Resouces donde el juego accederá a los niveles.

Clase Editor:
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 using UnityEngine.UI;  
 using System.IO;  
 public class DefaultCells  
 {  
   public int version;  
   //Game  
   public int type;  
   public int water;  
   public int food;  
   public int troops;  
   public int weapons;  
   public int gold;  
   public DefaultCells()  
   {      
     IsEmpy();  
   }  
   public void IsTundra()  
   {  
     version = 1;  
     type = 1;  
     water = 0;  
     food = 5;  
     troops = 100;  
     weapons = 50;  
     gold = 0;  
   }  
   public void IsDessert()  
   {  
     version = 1;  
     type = 2;  
     water = 0;  
     food = 0;  
     troops = -100;  
     weapons = 0;  
     gold = 0;  
   }  
   public void IsWood()  
   {  
     version = 1;  
     type = 3;  
     water = 5;  
     food = 10;  
     troops = 0;  
     weapons = 5;  
     gold = 0;  
   }  
   public void IsTown()  
   {  
     version = 1;  
     type = 4;  
     water = 10;  
     food = 50;  
     troops = 50;  
     weapons = 50;  
     gold = 50;  
   }  
   public void IsCity()  
   {  
     version = 1;  
     type = 5;  
     water = 100;  
     food = 500;  
     troops = 800;  
     weapons = 100;  
     gold = 500;  
   }  
   public void IsArmy()  
   {  
     version = 1;  
     type = 6;  
     water = 0;  
     food = 50;  
     troops = 1000;  
     weapons = 500;  
     gold = 800;  
   }  
   public void IsCrop()  
   {  
     version = 1;  
     type = 7;  
     water = 5;  
     food = 500;  
     troops = 10;  
     weapons = 5;  
     gold = 5;  
   }  
   public void IsLake()  
   {  
     version = 1;  
     type = 8;  
     water = 500;  
     food = 10;  
     troops = 0;  
     weapons = 0;  
     gold = 5;  
   }  
   public void IsRiver()  
   {  
     version = 1;  
     type = 9;  
     water = 500;  
     food = 10;  
     troops = 0;  
     weapons = 0;  
     gold = 5;  
   }  
   public void IsMine()  
   {  
     version = 1;  
     type = 10;  
     water = 5;  
     food = 5;  
     troops = 20;  
     weapons = 20;  
     gold = 500;  
   }  
   public void IsObjective()  
   {  
     version = 1;  
     type = 11;  
     water = 50;  
     food = 50;  
     troops = 50;  
     weapons = 50;  
     gold = 50;  
   }  
   public void IsMountain()  
   {  
     version = 1;  
     type = 12;  
     water = 50;  
     food = 50;  
     troops = -50;  
     weapons = 5;  
     gold = 10;  
   }  
   public void IsEmpy()  
   {  
     version = 1;  
     type = 0;  
     water = 0;  
     food = 0;  
     troops = 0;  
     weapons = 0;  
     gold = 0;  
   }  
 }  
 public class Editor : MonoBehaviour  
 {  
   public GameObject saveBox;  
   public GameObject newBox;  
   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 OpenNewPanel()  
   {  
     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>().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].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++;  
       }  
       GameObject.Find("StageNumText").GetComponent<Text>().text = editorStage.numStage.ToString();  
       CloseNewPanel();  
     }  
   }  
   public void CloseNewPanel()  
   {  
     newBox.SetActive(false);  
   }  
   public void OpenSavePanel()  
   {  
     if (editorStage.numStage > 0)  
     {  
       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");        
       DataSaver.saveData(editorStage, fileName, "");  
       ShowFiles(Path.Combine(Application.persistentDataPath, "levels"), "ObjectGrid");  
       CloseSavePanel();  
     }      
   }  
   public void CloseSavePanel()  
   {  
     saveBox.SetActive(false);  
   }  
   // 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;  
     DefaultCells cond = new DefaultCells();  
     editorStage.version = cond.version;  
   }  
 }  

Editor de niveles (Parte 1)

Hoy empezaremos con el editor de niveles. Este editor nos permitirá crear las etapas del juego a partir de ficheros JSON guardados en la carpeta de recursos. Este editor nos permitirá generar información parar poder proseguir con el juego donde lo habíamos dejado.


En este artículo sólo me centraré en la creación de la pantalla visual del editor, poniendo atención en la creación automática de cuadrículas (grids) a partir de una rutina pasando el objeto casilla como un prefab de la aplicación y definiendo el tamaño de cuadrícula con un Box collider.



Podéis ver el vídeo del proceso aquí.


Gizmo del Box Collider:
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 public class ShowColliders : MonoBehaviour {  
   private BoxCollider2D objectCollider2D;  
   private void OnDrawGizmos()  
   {  
     objectCollider2D = GetComponent<BoxCollider2D>();  
     Gizmos.color = Color.green;  
     Gizmos.DrawWireCube(objectCollider2D.bounds.center, new Vector2(objectCollider2D.size.x, objectCollider2D.size.y));  
   }  
 }  

Grid dinámico:
 using UnityEngine;  
 public class Grid : MonoBehaviour  
 {  
   //Grid  
   public int rows;  
   public int cols;    
   public Vector2 gridOffset;  
   public GameObject cell;  
   private BoxCollider2D gridSize;  
   private Vector2 originalCellSize;  
   private Vector2 cellScale;  
   private Vector2 cellSize;  
   private Vector2 offset;  
   // Start is called before the first frame update  
   void Start()  
   {  
     PaintCells();  
   }  
   public void Clean()  
   {  
     foreach (Transform child in gameObject.transform)  
     {  
       Destroy(child.gameObject);  
     }  
   }  
   public void PaintCells()  
   {  
     gridSize = GetComponent<BoxCollider2D>();  
     originalCellSize = cell.GetComponent<SpriteRenderer>().sprite.bounds.size;  
     cellSize = new Vector2(gridSize.size.x / (float)cols, gridSize.size.y / (float)rows);  
     cellScale.x = cellSize.x / (originalCellSize.x + gridOffset.x);  
     cellScale.y = cellSize.y / (originalCellSize.y + gridOffset.y);  
     cell.transform.localScale = new Vector2(cellScale.x, cellScale.y);  
     offset.x = gridSize.offset.x - ((gridSize.size.x) / 2 ) + cellSize.x / 2;  
     offset.y = gridSize.offset.y - ((gridSize.size.y) / 2 ) + cellSize.y / 2;  
     int sufixName = 0;  
     for (int row = 0; row < rows; row++)  
     {  
       for (int col = 0; col < cols; col++)  
       {  
         sufixName++;  
         //add the cell size so that no two cells will have the same x and y position  
         Vector2 pos = new Vector2(col * cellSize.x + transform.position.x + offset.x, row * cellSize.y + transform.position.y + offset.y);  
         //Add Cell information  
         cell.name = "Cell" + sufixName.ToString();  
         //instantiate the game object, at position pos, with rotation set to identity  
         GameObject cO = Instantiate(cell, pos, Quaternion.identity) as GameObject;  
         //set the parent of the cell to GRID so you can move the cells together with the grid;  
         cO.transform.parent = transform;  
       }  
     }      
   }  
 }  

El resto del editor sólo se utilizan elementos de la UI parar definir la interfaz de trabajo.

Tipos Abstractos de Datos (TAD) para el juego

Antes de seguir con la parte visual del juego necesito 2 elementos básicos que todo juego necesita. Por un lado tenemos los tipos abstractos de datos (TAD) que guardaran en sus estructuras internas la evolución de la partida. Una vez definidas estas estructuras necesitaremos un editor de niveles para implementar estas estructuras con información para poder trabajar con ella.


En nuestros caso, para Attila, tierra quemada necesitamos dos elementos claves: por un lado la información de nuestro ejército, que aunque visual estará representada por un caballo contienen un conjunto de variables que configuran su estado de salud durante el proceso del juego.

   public int water;  
   public int food;  
   public int troops;  
   public int weapons;  
   public int gold;  

Por otro lado necesitamos saber cómo está compuesto el tablero o sea, el nivel de juego que estamos jugando en este momento. Cada etapa estará compuesto de un tablero de 8x8 casillas que pueden estar llenas o no, donde cada una tendrá unos atributos y eventos asociados cuando interactúan con el jugador.

Las casillas de este tablero tendrán unas variables que definirán estos aspectos: 

   public int x;  
   public int y;  
   public int listPos;  
   public int type;  
   public bool isFinal;  
   public bool isObjective;  
   public int water;  
   public int food;  
   public int troops;  
   public int weapons;  
   public int gold;  


Esta información estará guardada en una lista de 100 posiciones donde los elementos serán objetos de la clase serializada StageCell.


Por otro lado podríamos trabajar con un Array de estas dimensiones para el mismo propósito, pero mucho más complejo de serializar para Unity. 


Por otro lado debemos tener claro donde guardar la información de cada elemento. Por un lado la información del jugador estará guardada en el archivo general de configuración. Así mismo los niveles serán archivos independientes que accederemos a ellos a través de la carpeta Resources del proyecto para que puedan ser copiados externamente cuando generemos el fichero APK para Android.

 using System.Collections.Generic;  
 using UnityEngine;  
 using System;  
 using System.Text;  
 [Serializable]  
 public class StageCell  
 {  
   public int x;  
   public int y;  
   public int listPos;  
   public int type;  
   public bool isFinal;  
   public bool isObjective;  
   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 List<StageCell> gridStage;  
 }  
 public class Levels : MonoBehaviour  
 {  
   public static void LoadLevel(int levelNum)  
   {  
     //load info from levels folder      
     TextAsset jsonTextFile = Resources.Load<TextAsset>("Levels/Attila" + levelNum.ToString());  
     object resultValue = JsonUtility.FromJson<Stage>(Encoding.ASCII.GetString(jsonTextFile.bytes));  
     Stage loadlevel = (Stage)Convert.ChangeType(resultValue, typeof(Stage));  
     GlobalInfo.stageVersion = loadlevel.version;   
     GlobalInfo.actualStage = loadlevel.numStage;  
     GlobalInfo.gridStage = loadlevel.gridStage;  
   }  
 }  

Para ellos tenemos implementada una rutina en la clase Levels que lee un fichero pasado por parámetro de la carpeta Resources y traslada la información del nivel actual al fichero de variables globales para poder acceder a él a través de cualquier parte del proyecto.

Ahora ya sólo resta modificarlas rutinas de acceso a la configuración en la escena del Título parar que lea esta información, actualice el fichero de variables globales i lea el nivel actual de juego.

 using UnityEngine;  
 using System.IO;  
 using System;  
 public class LoadConfig : MonoBehaviour {  
   private string configFileName;  
   private string highScoreFileName;  
   private string fileName;  
      // Use this for initialization  
      void Awake ()  
   {  
     //Configuration  
     configFileName = GlobalInfo.configFile;  
     fileName = Path.Combine(Application.persistentDataPath, "data");  
     fileName = Path.Combine(fileName, configFileName + ".txt");  
     if (!File.Exists(fileName))  
     {  
       PlayerInfo saveData = new PlayerInfo();  
       saveData.gameDateFirstTime = DateTime.Now.ToBinary().ToString();  
       saveData.playDateFirstTime = "";  
       IntialConditions cond = new IntialConditions();  
       saveData.maxStagesGame = cond.maxStagesGame;  
       saveData.actualStage = cond.actualStage;  
       saveData.maxStageCompleted = cond.maxStageCompleted;  
       saveData.score = cond.score;  
       saveData.water = cond.water;  
       saveData.food = cond.food;  
       saveData.troops = cond.troops;  
       saveData.weapons = cond.weapons;  
       saveData.gold = cond.gold;  
       //Save data from PlayerInfo to a file named players  
       DataSaver.saveData(saveData, configFileName);  
       GlobalInfo.gameFirstTime = true;  
       GlobalInfo.playFirstTime = true;  
       GlobalInfo.language = saveData.language;  
       GlobalInfo.soundPlay = true;  
       GlobalInfo.sessionsCount = 0;  
       GlobalInfo.stagesCount = 0;  
       GlobalInfo.maxStageCompleted = 0;  
       //Initial conditions        
       GlobalInfo.maxStagesGame = cond.maxStagesGame;  
       GlobalInfo.actualStage = cond.actualStage;  
       GlobalInfo.score = cond.score;  
       GlobalInfo.water = cond.water;  
       GlobalInfo.food = cond.food;  
       GlobalInfo.troops = cond.troops;  
       GlobalInfo.weapons = cond.weapons;  
       GlobalInfo.gold = cond.gold;  
     } else  
     {  
       PlayerInfo loadedData = DataSaver.loadData<PlayerInfo>(configFileName);  
       if (loadedData == null)  
       {  
         return;  
       }  
       GlobalInfo.gameFirstTime = false;  
       if (loadedData.playDateFirstTime != "")  
       {  
         GlobalInfo.playFirstTime = false;  
       } else  
       {  
         GlobalInfo.playFirstTime = true;  
       }  
       GlobalInfo.language = loadedData.language;  
       GlobalInfo.soundPlay = loadedData.soundPlay;  
       GlobalInfo.sessionsCount = loadedData.sessionsCount;  
       GlobalInfo.stagesCount = 0;  
       GlobalInfo.maxStagesGame = loadedData.maxStagesGame;  
       GlobalInfo.maxStageCompleted = loadedData.maxStageCompleted;  
       GlobalInfo.actualStage = loadedData.actualStage;  
       GlobalInfo.score = loadedData.score;  
       GlobalInfo.water = loadedData.water;  
       GlobalInfo.food = loadedData.food;  
       GlobalInfo.troops = loadedData.troops;  
       GlobalInfo.weapons = loadedData.weapons;  
       GlobalInfo.gold = loadedData.gold;  
     }  
   }  
 }  

Este punto nos plantea el siguiente reto del proyecto. Necesitamos un editor de información para crear de una manera fácil de información de cada etapa y así poder crear, corregir o ajustar el proceso y la dificultad del juego en cada una de sus etapas. Esto será en el próximo artículo.

Juegos, un mundo global en varios idiomas

En el juego que estamos desarrollando es primordial llegar al máximo público posible. Al ser un juego de estrategia es necesario explicar a...