Skip to content

Instantly share code, notes, and snippets.

@Zolomon
Created March 22, 2011 13:57
Show Gist options
  • Select an option

  • Save Zolomon/881239 to your computer and use it in GitHub Desktop.

Select an option

Save Zolomon/881239 to your computer and use it in GitHub Desktop.
Hur man använder klassen
<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="Breakout.Level">
<LevelIndex>1</LevelIndex>
<Rows>13</Rows>
<Columns>13</Columns>
<NextLevelAsset>Content/Levels/Level02</NextLevelAsset>
<StartLevel>true</StartLevel>
<MaxHits>9</MaxHits>
<Blocks>
<Row>1 0 0 0 0 0 0 0 0 0 0 0 1</Row>
<Row>0 1 0 0 0 0 0 0 0 0 0 1 0</Row>
<Row>0 0 1 0 0 0 0 0 0 0 1 0 0</Row>
<Row>0 0 0 1 0 0 0 0 0 1 0 0 0</Row>
<Row>0 0 0 0 1 0 0 0 1 0 0 0 0</Row>
<Row>0 0 0 0 0 1 0 1 0 0 0 0 0</Row>
<Row>0 0 0 0 0 0 1 0 0 0 0 0 0</Row>
<Row>0 0 0 0 0 1 0 1 0 0 0 0 0</Row>
<Row>0 0 0 0 1 0 0 0 1 0 0 0 0</Row>
<Row>0 0 0 1 0 0 0 0 0 1 0 0 0</Row>
<Row>0 0 1 0 0 0 0 0 0 0 1 0 0</Row>
<Row>0 1 0 0 0 0 0 0 0 0 0 1 0</Row>
<Row>1 0 0 0 0 0 0 0 0 0 0 0 1</Row>
</Blocks>
</Asset>
</XnaContent>
ContentManager localManager;
Level currentLevel;
List<Level> levels;
// ...
protected override void LoadContent()
{
// ...
levelManager = new LevelManager(@"Content/Levels/Level01", contentManager);
currentLevel = levelManager.CurrentLevel;
SpawnLevel(currentLevel);
// ...
base.LoadContent();
}
// Den här klassen ligger i ett separata Windows Game Library-projekt.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
namespace Breakout
{
public class Level
{
public Level()
{
//SetUp();
}
public void SetUp()
{
for (int y = 0; y < Rows; y++)
{
for (int x = 0; x < Columns; x++)
{
if (Blocks[y][x] > 0)
BlocksLeft = ++TotalBlocks;
}
}
}
// Level 1 = 0, Level 2 = 1, ..., Level N = N - 1
public int LevelIndex;
// Row count
public int Rows;
// Column count
public int Columns;
// The next level
[ContentSerializer(Optional = true)]
public string NextLevelAsset;
// True if it's the first level
[ContentSerializer(Optional = true)]
public bool StartLevel = false;
// True if it's the last level
[ContentSerializer(Optional = true)]
public bool LastLevel = false;
// The total amount of blocks that exist in this level
[ContentSerializerIgnore]
public int TotalBlocks = 0;
// The amount of blocks that are left in this level
[ContentSerializerIgnore]
public int BlocksLeft = 0;
public int MaxHits = 9;
// List with all the blocks!
[ContentSerializer(CollectionItemName = "Row")]
public List<List<int>> Blocks;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
namespace Breakout
{
public class LevelManager
{
private int currentLevelIndex = 0;
private ContentManager contentManager;
public List<Level> Levels;
public int LevelCount = 0;
public LevelManager(string firstLevelAsset, ContentManager contentManager)
{
this.contentManager = contentManager;
Levels = LoadAllLevels(firstLevelAsset);
CurrentLevelIndex = 0;
LevelCount = Levels.Count;
}
private List<Level> LoadAllLevels(string firstLevelAsset)
{
List<Level> levels = new List<Level>();
Level nextLevelData = contentManager.Load<Level>(firstLevelAsset);
nextLevelData.SetUp();
levels.Add(nextLevelData);
while (!String.IsNullOrEmpty(nextLevelData.NextLevelAsset))
{
nextLevelData = contentManager.Load<Level>(nextLevelData.NextLevelAsset);
nextLevelData.SetUp();
levels.Add(nextLevelData);
}
return levels;
}
public Level CurrentLevel
{
get { return Levels[CurrentLevelIndex]; }
}
private int CurrentLevelIndex
{
get
{
return currentLevelIndex;
}
set
{
currentLevelIndex = value;
}
}
public Level NextLevel()
{
if (CurrentLevel.LastLevel)
{
return null;
}
else
{
CurrentLevelIndex++;
return CurrentLevel;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment