- Home /
Grid 'Infection' though lists c#
Hi all
I've been working on a grid based game where tiles on the outside become 'infected'. The goal is to have the infection move in a semi-random nature towards the center of the grid. The grid tiles are created at the beginning of the level and added to a list "gridList", from here I add all perimeter tiles to the "perimList". The infection is then started through this
public void StartPoint(){
int indexP = Random.Range (0, perimList.Count);
int darkLength = darkList.Count;
if(darkLength < perimLength){
if(!darkList.Contains(perimList[indexP])){
darkList.Add (perimList[indexP]);
Debug.Log ("dark");
string tileName;
tileName = perimList[indexP].name;
gameObject.SendMessage("RecPerimName", tileName);
}else{
if(darkList.Contains (perimList[indexP])){
Recall(true);
}
}
}
}
This sends the name of the tile to a Manager script on the same object which then sets the tile texture to a dark color. If the tile is already dark it restarts.
I am unsure from this point as to the best method of making the infection spread to the center of the map. I tried using a series of list checks to move the infection in one tile, this works however it will get convoluted quickly if I have to check for each "ring" of the grid.
public void PerimSpread(){
int darkP = Random.Range (0, darkList.Count);
if(darkList[darkP].transform.position.x == 0 || darkList[darkP].transform.position.x == (Size.x - 1) && darkList[darkP].transform.position.z != 0 && darkList[darkP].transform.position.z != (Size.z - 1)){
if(darkList[darkP].transform.position.x == 0){
PerimXAdd (darkList[darkP]);
}
if(darkList[darkP].transform.position.x == (Size.x - 1)){
PerimXMinus (darkList[darkP]);
}
}
if(darkList[darkP].transform.position.z == 0 || darkList[darkP].transform.position.z == (Size.z - 1) && darkList[darkP].transform.position.x != 0 && darkList[darkP].transform.position.x != (Size.z - 1)){
if(darkList[darkP].transform.position.z == 0){
PerimZAdd (darkList[darkP]);
}
if(darkList[darkP].transform.position.z == (Size.z - 1)){
PerimZMinus(darkList[darkP]);
}
}
if(darkList[darkP].transform.position.z == 0 && (darkList[darkP].transform.position.x == 0)){
PerimOrigin (darkList[darkP]);
}
if(darkList[darkP].transform.position.z == (Size.z - 1) && darkList[darkP].transform.position.x == (Size.x - 1)){
PerimEnd (darkList[darkP]);
}
}
Sample function that is called from above;
public void PerimXAdd(GameObject temp){
Debug.Log ("PerimXAdd");
for(int i = 0; i < gridList.Count; i++){
if(gridList[i].transform.position.z == temp.transform.position.z && (gridList[i].transform.position.x == temp.transform.position.x + 1)){
string inName;
inName = gridList[i].name;
gameObject.SendMessage("RecInnerName", inName);
}
}
}
Can anyone suggest a better approach to this?
Thanks
A picture would be helpful. Are you talking about a rectangular grid? For ring are you referring to neighbors?
Hey thanks for replying, here is an image of what its like so far. I labeled 1,2,3 to show what I mean by rings.
Your answer

Follow this Question
Related Questions
Targeting multiple tiles in grid, updating with player movement 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
3D level generator on a grid C# 1 Answer