Afternoon,
Wonder if someone could help a novice programmer with a method of adding an item to my C# list if it does'nt exist already.
This is Game related, however for the purpose of what I'm trying to achieve no game related elements are involved
Let me explain what I have:
LevelManager Class
Level Class
The LevelManager class calls the Update and Draw for any Level which has a State of .PLAYING
The Level class will process it's own updates/draw function and eventually will load it's own content from an XML file. At the momment the Level class has a timer and when it reaches it's limited changes it's state to finished and changes it's netLevel string variable to the name of the next level. At the moment I have used a random number to simulate this level name. What I want is to add the level to my LevelManager list if it doesn not exist and if it does, then simply reset the called levels state to .PLAYING via it's Init() call.
Example code below.
LevelManager.cs
Level.cs
any help would be appreciated
Cheers
Wonder if someone could help a novice programmer with a method of adding an item to my C# list if it does'nt exist already.
This is Game related, however for the purpose of what I'm trying to achieve no game related elements are involved
Let me explain what I have:
LevelManager Class
Level Class
The LevelManager class calls the Update and Draw for any Level which has a State of .PLAYING
The Level class will process it's own updates/draw function and eventually will load it's own content from an XML file. At the momment the Level class has a timer and when it reaches it's limited changes it's state to finished and changes it's netLevel string variable to the name of the next level. At the moment I have used a random number to simulate this level name. What I want is to add the level to my LevelManager list if it doesn not exist and if it does, then simply reset the called levels state to .PLAYING via it's Init() call.
Example code below.
LevelManager.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;
namespace BJ1
{
public class LevelManager
{
ContentManager content;
public List<Level> levelStack = new List<Level>();
Level level;
string loadLevelName = "START";
public LevelManager(ContentManager content)
{
this.content = content;
level = new Level(loadLevelName, this.content);
levelStack.Add(level);
}
public void LoadContent()
{
}
public void Update(GameTime gameTime)
{
for (int i = 0; i < levelStack.Count; i++)
{
if (levelStack[i].LevelState == LEVELSTATE.PLAYING)
{
levelStack[i].Update(gameTime);
Debug.WriteLine("Level Manager Update for level " + levelStack[i].LevelName);
}
//Does the nextlevel already exist in the LevelManager list?
//if not create a new level, this calls the leve's Init() and sets state to playing by default
//This is what I am struggling to make work, how this aspect of the update would be.
//else initalize an existing level in our list of levels (level.Init() of each level resets variables and stat to LEVELSTATE.PLAYING
}
}
public void Draw(SpriteBatch spriteBatch)
{
for (int i = 0; i < levelStack.Count; i++)
{
if (levelStack[i].LevelState == LEVELSTATE.PLAYING)
{
levelStack[i].Draw(spriteBatch);
Debug.WriteLine("Level Manager Draw for level " + levelStack[i].LevelName);
}
}
}
}
}
Level.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;
namespace BJ1
{
public enum LEVELSTATE { CREATED, PLAYING, FINISHED }
public class Level
{
Random random = new Random();
int tempTimer = 0;
private string levelName = null;
public string LevelName
{
get { return levelName; }
}
private string nextLevelName = null;
public string NextLevelName
{
get { return nextLevelName; }
}
private LEVELSTATE levelState;
public LEVELSTATE LevelState
{
get { return levelState; }
set { levelState = value; }
}
public Level(string levelName, ContentManager content)
{
Debug.WriteLine("LEVEL Constructor for " + levelName);
this.levelName = levelName;
levelState = LEVELSTATE.CREATED;
LoadContent(content);
Init();
}
public void LoadContent(ContentManager content)
{
Debug.WriteLine("LEVEL Loading the content of " + levelName);
//load the content from a file based on the level name
}
public void Init()
{
Debug.WriteLine("LEVEL Initalizing " + levelName);
//provide default settings for the level
levelState = LEVELSTATE.PLAYING;
nextLevelName = "";
tempTimer = 0;
}
public void Update(GameTime gameTime)
{
//updated the current levels content
tempTimer++;
if (tempTimer > 200)
{
levelState = LEVELSTATE.FINISHED;
nextLevelName = random.Next(0, 100).ToString();
}
}
public void Draw(SpriteBatch spriteBatch)
{
//draw the current levels content
}
}
}
any help would be appreciated
Cheers