LoadingALevelInYourGame

SteveDunn edited this page Mar 13, 2012 · 7 revisions

Loading a level in your game

Overview

When you've created a level in Gleed 2D, you'll want to use it in your game.

To do this, include the associated Gleed 2D assembly in your game, load the XML from Content, and pass it to the LevelLoader type.

Note that nothing is drawable. After loading a Level you'll need to enumerate the items in the level and create your drawable game objects (e.g. Texture2D). This is intentional. Every game has different ways of storing internal game objects. Gleed2D simply allows you to design the levels visually and gives you simple .NET objects to work with when loading the level data in your game.

Details

Include the Gleed2D.InGame.dll. For instance, for an XBox 360 game, include Gleed2D.InGame.dll from the XBox folder where you built XBox components.

Now import your level data (*.gleed) into your Resources. It is important to set each .gleed files' properties to:

  • Build Action - 'None'.
  • Copy to Output Directory - 'Copy if newer'.
  • Content Importer = 'XML Content - XNA Framework'
  • Content Processor - 'No Processing Required'.

When you've referenced the assembly and included the Gleed levels, you can then instantiate a new LevelLoader object and give it your XML:

using( Stream stream = TitleContainer.OpenStream( "Content/Levels/Level1.gleed" ) )
{
  XElement xml = XElement.Load( stream ) ;
  Level level = LevelLoader.Load( xml ) ;
  return level ;
}

This returns a Level object, which just has two properties:

public LevelProperties Properties
{
  get
  {
    return _properties ;
  }
}

public IEnumerable<Layer> Layers
{
  get
  {
    return _layers ;
  }
}

To build up your level, enumerate the Layers which is a collection of Layer objects. A Layer has just two properties:

public LayerProperties Properties
{
  get
  {
    return _properties ;
  }
}

public IEnumerable<LayerItem> Items
{
  get
  {
    return _items ;
  }
}

Then, enumerate the Items. Each LayerItem has just one property:

public ItemProperties Properties
{
  get ;
}

ItemProperties is a base class. All items in a level have ItemProperties or a derived class. It is here you can see which variation of ItemProperties you have and then build your own in-game object.

ItemProperties looks like:

public class ItemProperties
{
  public string Name
  {
    get ;
    set ;
  }

  public int Id
  {
    get ;
    set ;
  }

  public bool Visible
  {
    get ;
    set ;
  }

  public Vector2 Position
  {
    get ;
    set ;
  }

  public CustomProperties CustomProperties
  {
    get ;
    set ;
  }
}

These are properties common to all items withan a level (include the Layer)

Things that derive from ItemProperties are things such as:

  • CircleItemProperties
  • PathItemProperties
  • TextureItemProperties
  • RectangleItemProperties

For example, here's TextureItemProperties:

public class TextureItemProperties : ItemProperties
{
  public float Rotation
  {
    get ;
    set ;
  }

  public Vector2 Scale
  {
    get ;
    set ;
  }

  public Color TintColor
  {
    get ;
    set ;
  }

  public bool FlipHorizontally
  {
    get ;
    set ;
  }

  public bool FlipVertically
  {
    get ;
    set ;
  }

  public bool IsTemplate
  {
    get ;
    set ;
  }

  public string TexturePathRelativeToContentRoot
  {
    get ;
    set ;
  }

  public string AssetName
  {
    get ;
  }

  public Vector2 Origin
  {
    get ;
    set ;
  }
}

Other things, such as things in plugins, also derive from ItemProperties. Here's some examples from the Krypton Lighting plug-in:

  • CircularHullProperties
  • ConvexHullProperties
  • LightProperties