- Home /
Help with Game Mechanics
Im creating a game and need some help with the game mechanics. I created a Grid of gameObjects (transforms) and them gameObjects have a cell script attached which is supposed to tell what their neighbors are which Im failing at so far. The end result (which I just dont know how to figure out) is how to ultimately have the sphere I shoot in the picture below hit the other 1's and destroy all of the connected 1's (which only get destroyed if there are 3 or more).
This is my first time creating a grid and trying this type of game so its a learning experience (a tough one at that) and the code is also posted below.
CellScript:
var adjacents : List.<Transform>; //not implemented yet???
var position : Vector3;
var number : int;
GridScript:
var cellPrefab : Transform;
var size : Vector3;
var grid : Transform[,];//cannot display 2d arrays in the inspector
function Start ()
{
CreateGrid();
SetRandomNumbers();
SetAdjacents();
}
function CreateGrid()
{
grid = new Transform[size.x, size.z];
for(var x = 0; x < size.x; x++)
{
for(var z = 0; z < size.z; z++)
{
//We create a new Transform to manipulate later.
var newCell : Transform;
newCell = Instantiate(cellPrefab, new Vector3(x, 0, z), Quaternion.identity);
newCell.name = String.Format("({0},0,{1})",x,z);
newCell.parent = transform;
//this puts the position of itself into the var position of cellscript
newCell.GetComponent(CellScript).position = new Vector3(x, 0, z);
grid[x,z] = newCell;
}
}
}
function SetRandomNumbers()
{
for (var child : Transform in transform)
{
var number = Random.Range(1,4);
//assign the random number to the textMesh
child.GetComponentInChildren(TextMesh).text = number.ToString();
//assign the random number it is into the cell script
child.GetComponent(CellScript).number = number;
}
}
function SetAdjacents()
{
for(var x = 0; x < size.x; x++)
{
for(var z = 0; z < size.z; z++)
{
var cell : Transform;
cell = grid[x,z];//the reason we are creating the grid
var cS : CellScript = cell.GetComponent(CellScript);//abbreiate CellScript to cScr
//access neighbors somehow and Add them to the list?
}
}
}
I've come along way with this and I need help solving the rest (neighbors and destroying same type if 3 or more are connected). I know its a lot to ask but Im searched out.
Or is there a simpler way to group 1's, 2's and 3's so that I can connect them together and once hit with one destroy that group? I cant seem to find a solution.
Answer by aldonaletto · Dec 28, 2012 at 03:39 AM
For each cell, you should check the 8 immediate neighbors, and add to the adjacents List those which had the same number:
...
function SetAdjacents()
{
for(var x = 0; x < size.x; x++)
{
for(var z = 0; z < size.z; z++)
{
var cell : Transform;
cell = grid[x,z];//the reason we are creating the grid
var cS : CellScript = cell.GetComponent(CellScript);
// set flags to show if there are neighbors at left, right, down and up:
var hasLeft = x > 0;
var hasRight = x < size.x-1;
var hasDown = z > 0;
var hasUp = z < size.z-1;
// then verify the immediate neighbors:
if (hasLeft) CheckNeighbor(cS, x-1, z);
if (hasRight) CheckNeighbor(cS, x+1, z);
if (hasDown) CheckNeighbor(cS, x, z-1);
if (hasUp) CheckNeighbor(cS, x, z+1);
// verify the diagonal neighbors, if you want to include them:
if (hasLeft && hasDown) CheckNeighbor(cS, x-1, z-1);
if (hasLeft && hasUp) CheckNeighbor(cS, x-1, z+1);
if (hasRight && hasDown) CheckNeighbor(cS, x+1, z-1);
if (hasRight && hasUp) CheckNeighbor(cS, x+1, z+1);
}
}
}
// this function verifies whether the neighbor at coordinates [x,z]
// has the same number as the cell, and if so add it to the List:
function CheckNeighbor(me: CellScript, x: int, z: int){
// get the neighbor:
var other: Transform = grid[x, z];
// if the neighbor has the same number...
if (me.number == other.GetComponent(CellScript).number){
// add it to this cell's adjacents List:
me.adjacent.Add(other);
}
}
NOTE: Simply declaring a List variable doesn't actually allocate it - you must do it explicitly with the new keyword (in the CellScript):
var adjacents : List.<Transform> = new List.<Transform>();
...
Thank You Very Very Very $$anonymous$$uch! Absolutely Amazing! Again Thank you for sharing it helps tremendously.
Your answer
Follow this Question
Related Questions
Change each cell size based on amount of text in grid layout group 1 Answer
transform.position error 1 Answer
Finding adjacent tiles in a grid 1 Answer
Change transform.position.y in a loop 1 Answer
Drag to grid script help? 1 Answer