I'm currently working on a top-down shooter, and have reached a "hmm, I'm doing something terribly wrong" point. Basically, here's what I'm trying to accomplish:
- I'd like to allow the programmer to hard-code in the map (I don't understand how to make a tile editor yet, need to familiarze myself with WPF prior to beginning that project) so that it can be loaded into application for later use. This has been accomplished.
- After the map has been created, I alter the code so that I can load the map and draw the contents of it. Essentially, if there's a 0 in the text file, draw the grass tile. If there's a 1, draw the dirt tile, etc.
- I'd like to have mulitple worlds so that if the player goes to a certain location (for instance, they step over the left side of the screen), then the world will change entirely. Think of the old Zelda games.
The problem is that when I load the text file and draw the world to the screen, I receive extremely bad framerate lag. I've tried altering the source a bit, but now the world doesn't even draw. I've attached a zip that contains the project's code and assets. I'm looking for any type of suggestions with creating and loading tiled maps using C# and XNA. In case you don't wish to download the project, I went ahead and included three source code files which are likely the cause of the issue to this post.
WARNING: MY SOURCE CODE IS REALLY MESSY AT THE MOMENT!
WorldManager.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace MyGame
{
public class WorldManager
{
private Vector2[,] worldSize;
public Vector2[,] WorldSize
{
get { return worldSize; }
set { worldSize = value; }
}
public void CreateWorld(string path, string[,] world)
{
using (StreamWriter sw = new StreamWriter(path))
{
for(int row = 0; row < 19; row++)
{
for(int col = 0; col < 25; col++)
{
sw.Write(world.GetValue(row, col));
}
sw.WriteLine();
}
}
}
public char[] LoadWorld(string path)
{
string data;
char[] columns;
using (StreamReader sr = new StreamReader(path))
{
data = sr.ReadToEnd();
columns = data.ToCharArray();
}
return columns;
}
}
}
[/source]
[/spoiler]
Grass.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
namespace MyGame
{
class Grass : WorldManager
{
private string[,] world = new string[19, 25];
private int y = 0;
public Texture2D GrassTexture
{
get;
set;
}
public void Initialize()
{
WorldSize = new Vector2[19, 25];
for (int row = 0; row < 19; row++)
{
y = row * 32;
for (int col = 0; col < 25; col++)
{
WorldSize.SetValue(new Vector2(col * 32, y), row, col);
Console.WriteLine(WorldSize.GetValue(row, col));
}
}
}
public void LoadContent(ContentManager Content)
{
GrassTexture = Content.Load<Texture2D>("World/Tiles/Grass");
}
public void Create()
{
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
world.SetValue("0", row, col);
}
Console.WriteLine();
}
CreateWorld(@"C:\Games\MyGame\Worlds\GrassWorld.txt", world);
}
public void Load(SpriteBatch spriteBatch)
{
string path = @"C:\Games\MyGame\Worlds\GrassWorld.txt";
for (int i = 0; i < LoadWorld(path).Length; i++)
{
if(LoadWorld(path) == '0')
{
Draw(spriteBatch);
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int row = 0; row < 19; row++)
{
for (int col = 0; col < 25; col++)
{
spriteBatch.Begin();
spriteBatch.Draw(GrassTexture, WorldSize[row, col], Color.White);
spriteBatch.End();
}
}
}
}
}
[/source]
[/spoiler]
Game1.cs
[spoiler]
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
namespace MyGame
{
public class Game1 : Microsoft.Xna.Framework.Game
{
public static GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Grass grass = new Grass();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void Initialize()
{
this.Window.Title = "My Game";
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ApplyChanges();
Player.Initialize();
grass.Initialize();
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
Player.LoadContent(Content);
Crosshair.LoadContent(Content);
grass.LoadContent(Content);
grass.Load(spriteBatch);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
InputHandler.Update();
Crosshair.Update();
Player.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
Player.Draw(spriteBatch);
Crosshair.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
[/source]
[/spoiler]