Ir al contenido principal

Back to work!

¡Hola! Estoy emocionado de anunciar que he retomado la actividad en mi blog "Jugando Haciendo Juegos" después de una pausa. En esta nueva etapa, el blog se centrará en proporcionar información valiosa desde dentro de la industria de los videojuegos, incluyendo trucos, curiosidades, herramientas, recursos y consejos sobre cómo crear videojuegos desde la comodidad de tu habitación utilizando Unity. El blog ya cuenta con varios artículos útiles que pueden servir como recursos iniciales. Por ejemplo, hay guías detalladas sobre cómo hacer copias de seguridad de tu código de Unity con GitHub, lo que es crucial para la gestión de versiones y el trabajo colaborativo. También encontrarás tutoriales sobre el diseño de interfaces de usuario, el desarrollo de controladores de personajes y cómo crear un PressKit para tu juego. Mi objetivo es compartir conocimientos prácticos y experiencias personales para ayudar tanto a principiantes como a desarrolladores más avanzados a mejorar sus hab

Attila. Mecánica de juego

Una vez terminado el editor de niveles, podemos crear algunos y gravarlos en la carpeta resources para que al instalar el juego en un dispositivo móvil podamos seguir accediendo a los archivos individualmente.


El siguiente paso, más allá de crear las escenas intermedias (menú principal, menú de etapas) que más adelante implementaremos nos permiten llegar a la escena del juego. En esta escena queremos mostrar el tablero en formato isométrico para poder jugar con el nivel previamente diseñado.


En concreto yo utilizo Isometric Builder para para implementar las escenas isométricas ya que me permite, más adelante, tratar cada casilla como un objetos independiente y en este caso añadir clases para cada casilla independiente.


El primer paso es diseñar el tablero máximo para distribuir los elementos de juego (tablero HUD, controles) en el espacio. En este caso yo he utilizado un gráfico isométrico genérico para implementar las casillas vacías.


A partir de aquí la implementación del juego consiste en la interrelación de las clases compuestas por el GameManager, el Player, y las casillas del tablero.



El GameManager se encarga de cargar el tablero a partir del archivo en la carpeta resources y generar el tablero en pantalla. Así mismo debemos calcular los movimientos disponibles después de mover el jugador así como procesar los eventos que se generen al mover el caballo. También es su función estar permanentemente a la escucha para saber cuándo el jugador ha pulsado alguna casilla de destino.


GameManager.cs
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 using UnityEngine.SceneManagement;  
 using UnityEngine.UI;  
 public class GameManager : MonoBehaviour  
 {  
   public Text troops;  
   public Text weapons;  
   public Text water;  
   public Text food;  
   public Text gold;  
   public Text stageName;  
   public Sprite tundra;  
   public Sprite desert;  
   public Sprite woods;  
   public Sprite town;  
   public Sprite city;  
   public Sprite army;  
   public Sprite crops;  
   public Sprite lake;  
   public Sprite river;  
   public Sprite mine;  
   public Sprite objective;  
   public Sprite mountains;  
   public Sprite empty;  
   public Sprite horse;  
   private int troopsO;  
   private int weaponsO;  
   private int waterO;  
   private int foodO;  
   private int goldO;  
   private int scoreO;  
   // Start is called before the first frame update  
   void Start()  
   {  
     SaveOriginals();  
     GlobalInfo.isPlaying = false;  
     GlobalInfo.stagesCount++;  
     SetEnviroment();  
     StartPlay();  
   }  
   public void SetEnviroment()  
   {  
     troops.text = GlobalInfo.troops.ToString("#,#");  
     weapons.text = GlobalInfo.weapons.ToString("#,#");  
     water.text = GlobalInfo.water.ToString("#,#");  
     food.text = GlobalInfo.food.ToString("#,#");  
     gold.text = GlobalInfo.gold.ToString("#,#");  
     Levels.LoadLevel(GlobalInfo.actualStage);  
     GlobalInfo.objectivesNum = 0;  
     GlobalInfo.finalNum = 0;  
     stageName.text = GlobalInfo.stageName;  
     PaintStage();  
     PaintInfo();  
   }  
   public void SaveOriginals()  
   {  
     //Save original values  
     troopsO = GlobalInfo.troops;  
     weaponsO = GlobalInfo.weapons;  
     waterO = GlobalInfo.water;  
     foodO = GlobalInfo.food;  
     goldO = GlobalInfo.gold;  
     scoreO = GlobalInfo.score;  
   }  
   public void RestoreOriginals()  
   {  
     //Restore original values  
     GlobalInfo.troops = troopsO;  
     GlobalInfo.weapons = weaponsO;  
     GlobalInfo.water = waterO;  
     GlobalInfo.food = foodO;  
     GlobalInfo.gold = goldO;  
     GlobalInfo.score = scoreO;  
   }   
   private void StartPlay()  
   {  
     GlobalInfo.isPlaying = true;  
   }  
   private void PaintStage()  
   {  
     int xx = 0;  
     int yy = 0;  
     for (int i = 1; i <= 64; i++)  
     {  
       GameObject cell = GameObject.Find("Cell" + i.ToString());  
       GameObject regular = GetChildWithName(cell, "Regualr_Collider_Union");  
       GameObject spriteCell = GetChildWithName(regular, "Iso2DObject_Union");  
       // GameCell Class info  
       cell.GetComponent<GameCell>().num = i;  
       cell.GetComponent<GameCell>().x = xx;  
       cell.GetComponent<GameCell>().y = yy;  
       xx++;  
       if (xx == 8)  
       {  
         xx = 0;  
         yy++;  
       }  
       //Sprite  
       if (GlobalInfo.gridStage[i - 1].type > 0)  
       {  
         spriteCell.GetComponent<SpriteRenderer>().sprite = TypeSprite(GlobalInfo.gridStage[i - 1].type);  
         if (GlobalInfo.gridStage[i - 1].isObjective)  
         {            
           GameObject ObjFlag = GeneralUtils.FindObject(cell, "Flag");  
           ObjFlag.SetActive(true);  
           cell.GetComponent<GameCell>().objective = true;  
           GlobalInfo.objectivesNum++;            
         }  
         if (GlobalInfo.gridStage[i - 1].isFinal)  
         {           
           GameObject FinFlag = GeneralUtils.FindObject(cell, "FlagF");  
           FinFlag.SetActive(true);  
           cell.GetComponent<GameCell>().final = true;  
           GlobalInfo.finalNum++;  
         }  
       } else  
       {  
         Destroy(cell);  
       }  
       //Player info  
       if (GlobalInfo.gridStage[i - 1].isStart == true)  
       {  
         GlobalInfo.playerPos = i;  
       }        
     }  
     //Set player  
     GameObject player = GameObject.Find("Player");  
     player.transform.position = GameObject.Find("Cell" + GlobalInfo.playerPos.ToString()).GetComponent<Transform>().position;  
     //Set player cell  
     GameObject cellStart = GameObject.Find("Cell" + GlobalInfo.playerPos.ToString());  
     GameObject regularStart = GetChildWithName(cellStart, "Regualr_Collider_Union");  
     GameObject spriteCellStart = GetChildWithName(regularStart, "Iso2DObject_Union");  
     spriteCellStart.GetComponent<SpriteRenderer>().sprite = horse;  
     Invoke("CalculateMovementsAvaliable", 0.5f);  
   }  
   private void CalculateMovementsAvaliable()  
   {  
     //Calculate movements avaliable  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("GameCell");  
     foreach (GameObject cell in cells)  
     {  
       cell.GetComponent<GameCell>().CalculateMovements();  
     }  
     GameObject cellStart = GameObject.Find("Cell" + GlobalInfo.playerPos.ToString());  
     cellStart.GetComponent<GameCell>().SetMoveables();  
     cellStart.GetComponent<GameCell>().ShowMoveables();  
   }  
   private GameObject GetChildWithName(GameObject obj, string name)  
   {  
     Transform trans = obj.transform;  
     Transform childTrans = trans.Find(name);  
     if (childTrans != null)  
     {  
       return childTrans.gameObject;  
     }  
     else  
     {  
       return null;  
     }  
   }  
   public void PaintInfo()  
   {  
     GameObject.Find("Player").GetComponent<MovePlayer>().ShowInfo();  
   }  
   public Sprite TypeSprite(int num)  
   {  
     if (num == 1) { return tundra; }  
     if (num == 2) { return desert; }  
     if (num == 3) { return woods; }  
     if (num == 4) { return town; }  
     if (num == 5) { return city; }  
     if (num == 6) { return army; }  
     if (num == 7) { return crops; }  
     if (num == 8) { return lake; }  
     if (num == 9) { return river; }  
     if (num == 10) { return mine; }  
     if (num == 11) { return objective; }  
     if (num == 12) { return mountains; }  
     return empty;  
   }  
   public void ToStageMenu()  
   {  
     SceneManager.LoadScene("StageSelector");  
   }  
   public void RestartLevel()  
   {  
     RestoreOriginals();  
     SceneManager.LoadScene("Attila");  
   }  
   public void MoveHorse(string origen, string final)  
   {  
     GameObject.Find("Player").GetComponent<MovePlayer>().Move(final);  
   }  
   private bool DestionationAvaliable(GameObject dest)  
   {      
     return dest.GetComponent<GameCell>().moveable;  
   }  
   // Update is called once per frame  
   void Update()  
   {  
     if (Input.GetMouseButtonDown(0))  
     {  
       Ray screenRay = Camera.main.ScreenPointToRay(Input.mousePosition);  
       RaycastHit hit;  
       if (Physics.Raycast(screenRay, out hit))  
       {  
         if (hit.collider != null)  
         {  
           if (hit.collider.gameObject.name == "Regualr_Collider_Union"   
             && GlobalInfo.isPlaying == true   
             && GlobalInfo.isPlayerMoving == false)  
           {  
             if (DestionationAvaliable(GameObject.Find(hit.collider.gameObject.transform.parent.name)))  
             {  
               MoveHorse("Cell" + GlobalInfo.playerPos.ToString(), hit.collider.gameObject.transform.parent.name);  
             }              
           }            
         }          
       }  
     }  
   }  
 }  

Hay que tener en cuenta que el caballo tarda un tiempo en moverse por lo cual debemos gestionar ese tiempo de movimiento y actuar al llegar a la casilla destino y no dejar que se produzca ningún evento mientras el caballo está en movimiento. La clase MovePlayer se encarga de este trabajo, mover el caballo utilizando el NavMesh que hemos creado encima del tablero. También será la encargada de gestionar los eventos al mover el jugador e interaccionar con una casilla.

MovePlayer.cs
 using System.Collections;  
 using UnityEngine;  
 using UnityEngine.AI;  
 using UnityEngine.UI;  
 public class MovePlayer : MonoBehaviour  
 {  
   public Text objectivesText;  
   public Text finalText;  
   private NavMeshAgent agent;  
   private string destination;  
   public void Start()  
   {  
     destination = "";  
     GlobalInfo.isOldCellDestroyed = true;  
   }  
   public void Move(string mCell)  
   {  
     GameObject dest = GameObject.Find(mCell);  
     if (dest != null)  
     {  
       //Don't move at the same position  
       if ("Cell" + GlobalInfo.playerPos.ToString() != mCell)  
       {  
         NavMeshAgent agent = GetComponent<NavMeshAgent>();  
         agent.destination = dest.transform.position;  
         GlobalInfo.isPlayerMoving = true;  
         GlobalInfo.isOldCellDestroyed = false;  
         destination = mCell;  
         dest.GetComponent<GameCell>().SetMoveables();  
         GameObject origin = GameObject.Find("Cell" + GlobalInfo.playerPos.ToString());  
         origin.GetComponent<GameCell>().HideMoveables();  
         StartCoroutine(DestroyCell(origin));  
       }        
     }      
   }  
   IEnumerator DestroyCell(GameObject cellToDestroy)  
   {  
     yield return new WaitForSeconds(1f);      
     Destroy(cellToDestroy);  
     yield return new WaitForSeconds(0.5f);  
     CalculateNewMovements();  
     GlobalInfo.isOldCellDestroyed = true;  
   }  
   private void CalculateNewMovements()  
   {  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("GameCell");  
     foreach (GameObject cell in cells)  
     {  
       cell.GetComponent<GameCell>().CalculateMovements();  
     }  
   }  
   private bool PathComplete()  
   {  
     NavMeshAgent mNavMeshAgent = GetComponent<NavMeshAgent>();  
     if (!mNavMeshAgent.pathPending)  
     {  
       if (mNavMeshAgent.remainingDistance <= mNavMeshAgent.stoppingDistance)  
       {  
         if (!mNavMeshAgent.hasPath || mNavMeshAgent.velocity.sqrMagnitude == 0f)  
         {  
           return true;  
         }  
         return false;  
       }  
       return false;  
     }  
     return false;  
   }  
   public void ProcessEvents()  
   {  
     //Update values  
     if (GameObject.Find(destination).GetComponent<GameCell>().objective == true)  
     {  
       GlobalInfo.objectivesNum--;  
     }  
     if (GameObject.Find(destination).GetComponent<GameCell>().final == true)  
     {  
       GlobalInfo.finalNum--;  
     }  
     if (GlobalInfo.objectivesNum == 0)  
     {  
       if (GlobalInfo.finalNum == 0)  
       {  
         //Good job!  
       }  
     }  
     if (GlobalInfo.movementsNum == 0)  
     {  
       //Game over;  
     }  
     ShowInfo();  
     GlobalInfo.isPlayerMoving = false;  
   }  
   public void ShowInfo()  
   {  
     objectivesText.text = GlobalInfo.objectivesNum.ToString();      
   }  
   private void Update()  
   {  
     if (GlobalInfo.isPlayerMoving == true)  
     {  
       if (PathComplete() && GlobalInfo.isOldCellDestroyed)  
       {          
         GameObject.Find(destination).GetComponent<GameCell>().ShowMoveables();  
         GlobalInfo.playerPos = GameObject.Find(destination).GetComponent<GameCell>().num;  
         //Set destionation cell  
         GameObject cellStart = GameObject.Find("Cell" + GlobalInfo.playerPos.ToString());  
         GameObject regularStart = GetChildWithName(cellStart, "Regualr_Collider_Union");  
         GameObject spriteCellStart = GetChildWithName(regularStart, "Iso2DObject_Union");  
         spriteCellStart.GetComponent<SpriteRenderer>().sprite = GameObject.Find("GameManager").GetComponent<GameManager>().horse;  
         ProcessEvents();          
       }        
     }  
   }  
   private GameObject GetChildWithName(GameObject obj, string name)  
   {  
     Transform trans = obj.transform;  
     Transform childTrans = trans.Find(name);  
     if (childTrans != null)  
     {  
       return childTrans.gameObject;  
     }  
     else  
     {  
       return null;  
     }  
   }  
 }  

Y finalmente tenemos la clase GameCell que se encarga de calcular las posibilidades de movimiento teniendo en cuenta el movimiento del caballo y las casillas que nos quedan disponibles después de cada tirada, mostrar u ocultar las posibilidades en pantalla.


GameCell.cs
 using System.Collections;  
 using System.Collections.Generic;  
 using UnityEngine;  
 using System;  
 public class GameCell : MonoBehaviour  
 {  
   public int x;  
   public int y;  
   public int num;  
   public bool moveable;  
   public bool final;  
   public bool objective;  
   public int[] moves = new int[8] ;  
   // Start is called before the first frame update  
   void Start()  
   {  
     ResetMovements();      
   }  
   private void ResetMovements()  
   {  
     moves[0] = 0;  
     moves[1] = 0;  
     moves[2] = 0;  
     moves[3] = 0;  
     moves[4] = 0;  
     moves[5] = 0;  
     moves[6] = 0;  
     moves[7] = 0;  
   }  
   public void CalculateMovements()  
   {  
     ResetMovements();  
     if ((x + 1 < 8) && (y - 2 >= 0))  
     {  
       moves[0] = FindGameCell(x+1,y-2);  
     }  
     if ((x + 2 < 8) && (y + 1 < 8))  
     {  
       moves[1] = FindGameCell(x + 2, y + 1);  
     }  
     if ((x + 2 < 8) && (y - 1 >= 0))  
     {  
       moves[2] = FindGameCell(x + 2, y - 1);  
     }  
     if ((x + 1 < 8) && (y + 2 < 8))  
     {  
       moves[3] = FindGameCell(x + 1, y + 2);  
     }  
     if ((x - 1 >= 0) && (y + 2 < 8))  
     {  
       moves[4] = FindGameCell(x - 1, y + 2);  
     }  
     if ((x - 2 >= 0) && (y + 1 < 8))  
     {  
       moves[5] = FindGameCell(x - 2, y + 1);  
     }  
     if ((x - 2 >= 0) && (y - 1 >= 0))  
     {  
       moves[6] = FindGameCell(x - 2, y - 1);  
     }  
     if ((x - 1 >= 0) && (y - 2 >= 0))  
     {  
       moves[7] = FindGameCell(x - 1, y - 2);  
     }  
   }    
   private int FindGameCell(int xx, int yy)  
   {  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("GameCell");  
     foreach (GameObject cell in cells)  
     {  
       if (cell.GetComponent<GameCell>().x == xx && cell.GetComponent<GameCell>().y == yy)  
       {  
         return cell.GetComponent<GameCell>().num;  
       }  
     }  
     return 0;  
   }  
   public void SetMoveables()  
   {  
     GameObject[] cells = GameObject.FindGameObjectsWithTag("GameCell");  
     foreach (GameObject cell in cells)  
     {  
       cell.GetComponent<GameCell>().moveable = false;  
       int pos = Array.IndexOf(moves, cell.GetComponent<GameCell>().num);  
       if (pos > -1)  
       {  
         cell.GetComponent<GameCell>().moveable = true;          
       }  
     }  
   }  
   public void ShowMoveables()  
   {  
     GlobalInfo.movementsNum = 0;  
     for (int i = 0; i < 8; i++)  
     {  
       if (moves[i]>0)  
       {  
         GameObject cell = GameObject.Find("Cell" + moves[i].ToString());  
         GameObject selector = GeneralUtils.FindObject(cell, "Selector");  
         selector.SetActive(true);  
         GlobalInfo.movementsNum++;  
       }  
     }  
   }  
   public int GetMoveables()  
   {  
     int num = 0;  
     for (int i = 0; i < 8; i++)  
     {  
       if (moves[i] > 0)  
       {  
         num++;  
       }  
     }  
     return num;  
   }  
   public void HideMoveables()  
   {  
     for (int i = 0; i < 8; i++)  
     {  
       if (moves[i] > 0)  
       {  
         GameObject cell = GameObject.Find("Cell" + moves[i].ToString());  
         GameObject selector = GeneralUtils.FindObject(cell, "Selector");  
         selector.SetActive(false);  
       }  
     }  
   }  
 }  

Comentarios

Entradas populares de este blog

El diseño de la interfaz de usuario

El estudio del diseño de interfaz de usuario en videojuegos es un tema que se ha estudiado en profundidad pero que muchos desarrolladores que empiezan no prestan mucha atención centrando su energía en las mecánicas del juego y especialmente el arte ya que muchas veces el éxito o el fracaso de un juego dependen de ello. Pero más lejos de la realidad la capacidad lúdica de un juego muchas veces también viene determinada por el diseño de la interfaz que hace de dialogo entre el jugador y el juego. Uno de los mejores análisis de las interfaces de usuario en videojuegos lo encontramos en los estudios realizados por Anthony Stonehouse y Marcus Andrews . El diseño de la interfaz de usuario en los juegos difiere de otro diseño de interfaz de usuario porque implica un elemento adicional: la ficción. La ficción involucra un avatar del usuario real, o jugador. El jugador se convierte en un elemento invisible, pero clave de la historia, como un narrador en una novela o película. Esta ficc

GitHub y Unity

Para el proyecto que estoy desarrollando voy a utilizar GitHub como repositorio de archivos y versiones. Aunque muchas veces uso el propio sistema de Unity, por el motivo que sea también vamos a utilizar un sistema general ampliamente utilizado por los desarrolladores de software. En primer lugar debemos tener una cuenta GitHub que nos podemos hacer gratuitamente. Una vez hecha vamos a utilizar un programa de gestión de versiones como es Sourcetree para gestionar el flujo de las versiones, ramas, etc. de nuestro juego. Una vez tenemos la cuenta de GitJub, si queremos utilizar SourcreTree necesitas una cuenta de Atlassian Bitbucket. Sin no disponemos de ella la podemos crear en el momento de la instalación o previamente a través de su página web. A partir de aquí solo tenemos que ejecutar Sourcetree y enlazar las cuentas. En el vídeo tenéis todo el proceso completo. Una vez tenemos en enlace hecho debemos enlazar con la cuenta de GitHub con la opción de Edit account

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. De hecho hay mucha literatura en internet para aprender a utilizar Git en todo tipo de entornos de programación. Así mismo para aquellos que empiezan seguramente lo primero que deberían aprender como hacer una copia de seguridad periódica de sus proyectos.. y ya habrá tiempo para, poco a poco aprender todo el potencial de una herramienta como esta. En su momento hice un tutorial para usar Git con una interfaz gráfica como SourceTree como punto de entrada al mundo de Git, pero en esta ocasión me gustaría explicar, paso a paso como utilizar GitHub para hacer copias de seguridad de tu proyecto en Unity. Para iniciar este proceso se deben hacer tres pasos: 1.- Crear una cuenta en GitHub En la pantalla principal de GitHub debemos crear una cuenta nueva (Sign up) y seguir el asistente para tener una cuenta gratuita con los parametros por defecto que nos