i am working on improving this tower defence gtame that i finished from this tutorial http://xnatd.blogspot.com/ i now wish to load the levels from a text file but not quite sure how i would do this using a streamreader, any tips? heres the source code for my level class;
public class Level
{
protected int temperature;
protected int levelNo;
private Queue<Vector2> waypoints = new Queue<Vector2>();
public Queue<Vector2> Waypoints
{
get
{
return waypoints;
}
}
int[,] map = new int[,]
{
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,0,},
{0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,0,1,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,},
{0,0,1,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,0,0,},
{0,0,1,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,1,0,0,0,},
{0,0,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,1,1,1,1,},
};
public int Temperature // get the width of the level (how many numbers are in a row)
{
get
{
return temperature;
}
}
public int LevelNo // get the width of the level (how many numbers are in a row)
{
get
{
return levelNo;
}
set
{
levelNo = value;
}
}
public int Width // get the width of the level (how many numbers are in a row)
{
get
{
return map.GetLength(1);
}
}
public int Height // get the height of our level (how many numbers are there in a column)
{
get
{
return map.GetLength(0);
}
}
public int GetIndex(int cellX, int cellY) // return the index of the requested cell.
{
if (cellX < 0 || cellX > Width - 1 || cellY < 0 || cellY > Height - 1)
{
return 0;
}
else
{
return map[cellY, cellX];
}
}
public List<Texture2D> tileTextures = new List<Texture2D>(); // list to contain all the textures
/// <summary>
/// CONSTRUCTOR NEW LEVEL
/// </summary>
public Level()
{
SetWayPoints(map);
this.temperature = 1000;
this.levelNo = 2;
}
private void SetWayPoints(int[,] map)
{
int currentPosVal = map[0, 0];
int lPos = 0;
int rPos = 0;
int uPos = 0;
int dPos = 0;
int storedXPos = 99;
int storedYPos = 99;
int endstoredXPos = 99;
int endstoredYPos = 99;
int lastY = 0;
int lastX = 0;
//Search top ROW for start
for (int i = 0; i < Width; i++)
{
currentPosVal = map[0, i];
if (currentPosVal == 1)
{
storedXPos = i;
storedYPos = 0;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
lastX = storedXPos;
lastY = storedYPos;
break;
}
}
//if start not set
if (storedXPos == 99 && storedXPos == 99)
{
//look in 1st coloum for start
for (int i = 0; i < Height; i++)
{
currentPosVal = map[i, 0];
if (currentPosVal == 1)
{
storedXPos = 0;
storedYPos = i;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
lastX = storedXPos;
lastY = storedYPos;
break;
}
}
}
//search end COLOUM for end
for (int i = 0; i < Height; i++)
{
currentPosVal = map[i, Width - 1];
if (currentPosVal == 1)
{
endstoredXPos = Width - 1;
endstoredYPos = i;
}
}
//If end not set look in bottom row for end
if (endstoredXPos == 99 && endstoredYPos == 99)
{
for (int i = 0; i < Width; i++)
{
currentPosVal = map[7, i];
if (currentPosVal == 1)
{
endstoredXPos = i;
endstoredYPos = Height - 1;
}
}
if (endstoredXPos == 99 && endstoredYPos == 99)
{
}
}
// start midlle loop
while (true)
{
lPos = 0;
rPos = 0;
uPos = 0;
dPos = 0;
//If current pos is not down the left hand edge
if (storedXPos > 0) { lPos = map[storedYPos, storedXPos - 1]; }
//If current pos square is not down the right hand edge
if (storedXPos < Width - 1) { rPos = map[storedYPos, storedXPos + 1]; }
//If current pos square is not in the top row
if (storedYPos > 0) { uPos = map[storedYPos - 1, storedXPos]; }
//If current pos square is not in the bottom row
if (storedYPos < Height - 1) { dPos = map[storedYPos + 1, storedXPos]; }
if (lPos == 1 && (lastX != storedXPos - 1 || lastY != storedYPos))
{
lastX = storedXPos;
lastY = storedYPos;
storedXPos--;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
}
else if (rPos == 1 && (lastX != storedXPos + 1 || lastY != storedYPos))
{
lastX = storedXPos;
lastY = storedYPos;
storedXPos++;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
}
else if (dPos == 1 && (lastX != storedXPos || lastY != storedYPos + 1))
{
lastX = storedXPos;
lastY = storedYPos;
storedYPos++;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
}
else if (uPos == 1 && (lastX != storedXPos || lastY != storedYPos - 1))
{
lastX = storedXPos;
lastY = storedYPos;
storedYPos--;
waypoints.Enqueue(new Vector2(storedXPos, storedYPos) * 32);
}
if (storedXPos == endstoredXPos && storedYPos == endstoredYPos)
{
break;
}
}
}
public void AddTexture(Texture2D texture) // method adds a texture to our texture list.
{
tileTextures.Add(texture);
}
//Reads number from array, store its value in textureIndex, Use textureIndex to get the texture from tileTextures,
public void Draw(SpriteBatch batch) //Draw appropiate texture, Repeat through all elements of the array
{
int textureIndex;
Texture2D texture;
for (int x = 0; x < Width; x++)
{
for (int y = 0; y < Height; y++)
{
if (levelNo == 0)
{
textureIndex = map[y, x];
if (textureIndex == -1)
continue;
texture = tileTextures[textureIndex];
batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
}
if (levelNo > 0)
{
textureIndex = map[y, x];
textureIndex += (levelNo * 2);
if (textureIndex == -1)
continue;
texture = tileTextures[textureIndex];
batch.Draw(texture, new Rectangle(x * 32, y * 32, 32, 32), Color.White);
}
}
}
}
}
}
Since C# is compiled and can’t do evals like scripted languages can (not that you should be doing that anyway) you should probably use streamreader to read the data from file (formatted perhaps as delimited text: csv or tsv)
Assuming you’re loading something similar to the map you have up there then a map file could look something like
where you would then loop through the file and read each line
As each line is read in, you can then split the line by “,” to create a 1d array which can then be assigned to an index in the 2d array. Loop for each line to create the entire 2d map array
Take a look at this if you need help splitting strings in C#; note that the sample code splits at spaces ” ” and that you should use s.Split(‘,’); for csv