Level & Level Manager Classes

The Level and Level Manager classes are the core classes which deal with pulling the resources together and getting each level ready to be played. Level manager runs from a manifest file which is just a text file listing the url’s to the level data files, in the order they should be played. It only has a few methods for reloading a method, getting the next level or resetting the game altogether (starting again at level 0). Source code for this can be seen below;

/*!
* Generic Game Level Manager Class
* http://8weekgame.shawson.co.uk/
*
* Copyright 2010, Shaw Young
* Released under the MIT, BSD and GPL Licenses.
* http://www.opensource.org/licenses/bsd-license.php
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-2.0.php
*
* Date: Mon Jul 17 17:00:00 2010 
*/
function LevelManager(manifest_file, resource_manager, objectFactory) {
	this.ManifestFile = manifest_file;
	this.ResourceManager = resource_manager;
	this.ObjectFactory = objectFactory;
	this.CurrentLevel = 0;

	this.Levels = this.ManifestFile.split('\r\n');

}
LevelManager.prototype.GetNextLevel = function () {
    return new Level(this.ResourceManager.GetObject(this.Levels[this.CurrentLevel++]), this.ObjectFactory, 25, 25, 2);
}
LevelManager.prototype.ReloadLevel = function () {
    return new Level(this.ResourceManager.GetObject(this.Levels[this.CurrentLevel - 1]), this.ObjectFactory, 25, 25, 2);
}
LevelManager.prototype.Reset = function () {
	this.CurrentLevel = 0;
}

The Level class actually parses the text data for each level, building it tile at a time using the supplied object factory. The ascii value for each tile is passed to the object factory as a string, and this returns an instance of an object derived from GameObject.

/*!
* Manic Spaceman Level Class
* http://8weekgame.shawson.co.uk/
*
* Copyright 2010, Shaw Young
* Released under the MIT, BSD and GPL Licenses.
* http://www.opensource.org/licenses/bsd-license.php
* http://www.opensource.org/licenses/mit-license.php
* http://www.opensource.org/licenses/gpl-2.0.php
*
* Date: Mon Jul 17 17:00:00 2010 
*/
function Level(string_data, obj_fact, w, h, skip_rows) {
	this.levelData = string_data;
	this.levelName = '';
	this.levelTimeLimit = 0;
	
	this.tileSpacingWidth = w;
	this.tileSpacingHeight = h;
	
	this.headerOffset = skip_rows;
				
	this.arrayData = new Array();
	
	this.GameTiles = new Array();
	this.GameObjectFactory = obj_fact;
	
	var rows = this.levelData.split('\n');

	// load the level into a 2d character array..
	for (var y=0; y < rows.length; y++) {
	
		switch (y) {
			case 0:
				this.levelName = rows[y].toString();
				break;
			case 1:
				this.levelTimeLimit = rows[y];
				break;
			default :
				line = rows[y];
				row = new Array();
				for (var x=0; x < line.length; x++)
				{
					row[row.length] = line[x];
					o = this.GameObjectFactory.GetGameObject(line[x], x, y - this.headerOffset); // the grid x & y, not the actual x & y
                    // real positions are decided by the factory..

					if (o != null) {
						this.GameTiles[this.GameTiles.length] = o;
					}
				}
				this.arrayData[this.arrayData.length] = row;
		}
	}
}
  1. No comments yet.
(will not be published)