I've got a token over a map in my (TEG/RISK wannabe) 2D game, I've divided the map in 16 different zones with a collider for each one so I can take the clicks over them and move the token towards it. I was able to do the basic move: token position = zone position, and it worked perfectly. The problem comes when I try to the algorithm restricting the movement to the token's neighboring areas, I get a lot of errors and when I get none it doesn't work.
The script is 200~ lines long (yeah, pretty sure is -100% optimized) so I'm gonna shorten some parts. It's supposed to be attached to each one of the 16 zones.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Movimiento : MonoBehaviour {
//Prefabs of each zone
public Transform OBJ_ZONA_1;
public Transform OBJ_ZONA_2;
.
.
.
public Transform OBJ_ZONA_16;
//List of zones prefabs to optimize searchs
public List<Transform> LISTA_OBJ_ZONAS;
//List of each zone's neighbors
public List<int> LISTA_VECINOS_ZONA_1;
public List<int> LISTA_VECINOS_ZONA_2;
.
.
.
public List<int> LISTA_VECINOS_ZONA_16;
//List of lists of each zone's neighbors to optimize searchs
public List<List<int>> LISTA_LISTAS_VECINOS;
//The token object
public Transform FICHA;
public void MoverFicha(){
Vector3 TP = FICHA.transform.position;
TP.x = this.transform.position.x;
TP.y = this.transform.position.y;
FICHA.transform.position = TP;
}
//Function to clarify the neighbors of the token at the momment
public List<int> Vecinos(){
for (int i=0; i<15; i++) {
if (FICHA.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && FICHA.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return LISTA_LISTAS_VECINOS[i];
}
return LISTA_VECINOS_ZONA_1;
}
//Function to define the zone that got triggered by the click
public int DefinirZonaClick(){
for (int i=0; i<15; i++) {
if (this.transform.position.x == LISTA_OBJ_ZONAS[i].transform.position.x && this.transform.position.y == LISTA_OBJ_ZONAS[i].transform.position.y)
return i+1;
}
return 1;
}
void Awake(){
//Fill the list of zone objects
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_1);
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_2);
.
.
.
LISTA_OBJ_ZONAS.Add (OBJ_ZONA_16);
//Clarify the neighbors of each zone
LISTA_VECINOS_ZONA_1.Add (2);
LISTA_VECINOS_ZONA_2.Add (1);
.
.
.
LISTA_VECINOS_ZONA_16.Add (15);
}
// Use this for initialization
void Start () {
//Fill the list of lists of neighbors of each zone
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_1);
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_2);
.
.
.
LISTA_LISTAS_VECINOS.Add (LISTA_VECINOS_ZONA_16);
}
// Update is called once per frame
void Update () {
//Define the list of neighbors for the token at the momment of trigger
List<int> VECINOS = Vecinos ();
//Define the clicked zone
int INT_ZONA_CLICK = DefinirZonaClick ();
//If the triggered zone is in the token's neighbors list then it moves towards that zone
if (VECINOS.Contains (INT_ZONA_CLICK))
MoverFicha ();
}
}
This is my setup:
These are the errors I'm getting at the moment: