Screen menedžers

Nolēmu jau pašā sākumā izveidot ļoti vienkaršu Screen(State) menedžeri.

Kas ir screen vai state menedžers?

Spēle var būt dažādos “stāvokļos”, piemeram MENU, PLAY, PAUSE utt. Katram no šiem stāvokļiem būs atbilstoša klase, Screen menedžers koordinēs pārslēgšanos no viena Screen uz nakamo. No sākuma es izveidošu ļoti elementāru medžedzeri bez pārslēgšanās efektiem, loading screeniem utt.

Tātad pievienoju projektam mapi SCREENS, ScreenManager klasi, abstraktu klasi Screen un iesākumam divas konkrētas klases MenuScreen un PlayScreen, kuras izriet no Screen. ScreenMenedzer izriet no DrawableGameComponent un tiek pievienots compontntiem Game klases konstruktorā.

ScreenManager screenManager = new ScreenManager(this);
screenManager.SetScreen(new MainMenuScreen(screenManager));
Components.Add(screenManager);

 


public class ScreenManager : DrawableGameComponent
{
#region FIELDS
private Stack<Screen> screens;
private Screen screenToUpdate;

private SpriteBatch spriteBatch;
#endregion

#region CONSTRUCTORS
public ScreenManager(Game game)
: base(game)
{
screens = new Stack<Screen>();
screenToUpdate = null;
}
#endregion

#region SCREEN MANAGER INTERFACE
public void SetScreen(Screen screen)
{
foreach (Screen scr in screens)
{
scr.UnloadContent();
}
screens.Clear();

screen.LoadContent();
screens.Push(screen);
}
#endregion

#region GAME INTERFACE
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
}

public override void Update(GameTime gameTime)
{
if (screens.Count > 0)
{
screenToUpdate = screens.Peek();
screenToUpdate.Update(gameTime);
}
}

public override void Draw(GameTime gameTime)
{
if (screens.Count > 0)
{
foreach (Screen screen in screens)
{
screen.Draw(spriteBatch);
}
}
}

protected override void UnloadContent()
{
foreach (Screen scr in screens)
{
scr.UnloadContent();
}
base.UnloadContent();
}
}

public class Screen
{
#region FIELDS
private ScreenManager screenManager;
#endregion

#region PROPERTIES
public Screen(ScreenManager screenManager)
{
this.screenManager = screenManager;
}
#endregion

#region CONSTRUCTORS
public ScreenManager ScreenManager
{
get { return screenManager; }
}
#endregion

#region PUBLIC METHODS
public virtual void LoadContent()
{

}

public virtual void Update(GameTime gameTime)
{

}

public virtual void Draw(SpriteBatch spriteBatch)
{

}

public virtual void UnloadContent()
{

}
#endregion
}
class MainMenuScreen : Screen
{
#region TMP
private int testFrames = 0;
#endregion

#region CONSTRUCTORS
public MainMenuScreen(ScreenManager screenManager)
: base(screenManager)
{

}
#endregion

#region GAME INTERFACE
public override void LoadContent()
{
Console.WriteLine("MenuScreen LoadContent()");
}

public override void Update(GameTime gameTime)
{
Console.WriteLine("MenuScreen update()");

#region TMP
if (testFrames > 10)
{
ScreenManager.SetScreen(new PlayScreen(ScreenManager));
}

testFrames++;
#endregion
}

public override void Draw(SpriteBatch spriteBatch)
{
Console.WriteLine("MenuScreen draw()");
}

public override void UnloadContent()
{
Console.WriteLine("MenuScreen UnloadContent()");
}
}
#endregion
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s