Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Creating Interactive Grid for Tactics Game

Discussion in 'Scripting' started by 80ProofGaming, Jan 13, 2015.

  1. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    I'm making a tactics based RPG along the vein of Final Fantasy Tactics. The first thing I attempted was getting some code together that would create an interactive grid that would sit over the ground and make it easier to handle movement. I was new so I tried to understand someone else's script instead of trying to figure out my own.

    The script I found works, but it's time to script my player movement phase and I don't know how to display to the player how far they can move. I have a variable for it but I don't know how to take that variable and change the color of just the squares which the player can reach. The code also doesn't handle elevation at all. I can't change the height of specific cubes. I think I need a new system. Perhaps if I set out the cubes manually and assigned each of them a value, I could then interact with them that way? Any and all advice would be immensely appreciated. Below is my script as it exists now:

    In my manager script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class Manager : MonoBehaviour {
    6.     public GameObject TilePrefab;
    7.     public int mapSize = 11;
    8.     List <List<Tile>> map = new List<List<Tile>>();
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         generateMap ();  
    13.     }
    14.        
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.     }
    19.  
    20.     void generateMap(){
    21.         map = new List<List<Tile>> ();
    22.         for (int i = 0; i < mapSize; i++) {    //i and j are the two values on the eventual vector2
    23.             List <Tile> row = new List<Tile> ();
    24.             for (int j = 0; j < mapSize; j++) {
    25.                 Tile tile = ((GameObject)Instantiate (TilePrefab, new Vector3 (i - Mathf.Floor (mapSize / 2), 0, -j + Mathf.Floor (mapSize / 2)), Quaternion.Euler (new Vector3 ()))).GetComponent<Tile> ();
    26.                 tile.gridPosition = new Vector2 (i, j);
    27.                 row.Add (tile);
    28.             }
    29.             map.Add (row);
    30.         }
    31.     }
    32. }
    33.  
    In my Tile script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Tile : MonoBehaviour {
    5.    
    6.     public Vector2 gridPosition = Vector2.zero;
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.        
    15.     }
    16.     void OnMouseEnter(){
    17.         transform.renderer.material.color = Color.blue;
    18.         Debug.Log ("my position is (" + gridPosition.x + "," + gridPosition.y);
    19.     }
    20.     void OnMouseExit(){
    21.         transform.renderer.material.color = Color.white;
    22.     }
    23. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    Here are a couple of ideas to tinker with:

    At the Tile object level, support an interface that lets you colorize a given tile. Colorizing the renderer (or a sub-portion of the object) is probably the simplest way to show individual squares as different than other squares.

    At the Manager object level, expose support for calling the above colorization function under certain conditions:

    - colorize ALL tiles a certain color
    - colorize a single tile
    - colorize a list of tiles (x,y)

    Then when you select a Unit, that Unit would contain the intelligence to say "I can move this far," and it would produce a list of the notional tiles it can move to.

    Of course then you would ultimately need to apply constraints to that list, such as world bounds or obstacle avoidance.

    Finally, you would feed that list back into the above-mentioned Manager functions to first clear-to-white and then colorize as green (for example) the tiles that Unit can move to.

    That's just a few general purpose ways of looking at the problem, and probably the way I would start to tackle it.
     
  3. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    So if I were to, for example, add an if statement to the Tile script that checks to see if it is the player's movement turn, check the x and y position of the player, then check if its own x and y is within (playerMovement), then tell it to change color through the renderer? Or hell, it could change the sprite entirely. Do I have a grasp of the idea?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,487
    Knowing about the player really isn't a Tile's domain.

    You would generally have a GameManager class that you have a single instance of, and it would manage the "bigger picture stuff," like:

    - who is the player, where they are
    - who are the enemies, where they are
    - whose turn is it to go right now

    That script could accept input events and decide what to do next, and it would call through to the presentation side of things (in this case your Manager) and use that to visualize the choices that it is asking the player.

    Sometimes it can be helpful to get a piece of paper out and draw a few sketches of what the user will see as they play your game.

    - playfield with enemy and player

    - text displayed "Player 1 turn!"

    - available move squares filled in and "Please tap destination" displayed

    (waiting for user input)

    - player actually moving to destination

    - zoom camera to enemy who is about to move

    - announce "Enemy move!"

    - show enemy moving

    etc. Separate each of those in your mind and in your initial implementation and you will begin to develop a game flow that the GameManager can drive.
     
  5. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    So the manager should be the place to store the player's current position and then that can be passed to the tiles in order to decide what color should be used, but I don't know how to actually tell where the player is, using the code.
     
  6. 80ProofGaming

    80ProofGaming

    Joined:
    Jan 5, 2015
    Posts:
    17
    Well I'm an idiot. I didn't realize that transform.position was a thing. Glad I figured that one out.
     
    Last edited: Jan 13, 2015