function Level() { this.name; this.tiles; this.width = 16; this.height = 16; this.images = []; } function levelExists(level) { return fileExists("assets/levels/" + level); } Level.prototype.loadLevel = function(level) { // Check if the level exists before attempting // to load the file's content if (levelExists(level) != true) { return echo("(Load Level - Fail): " + level); } // Get the level's content var feedback = getFileContent("assets/levels/" + level); // Parse text lines into array var lines = feedback.split("\n"); var lineTilesNew = []; var tileIDArray = []; var tileID; var tiles = []; var ignoreLineChars = ["#", "@"]; for (var i = 0; i < lines.length; i++) { // Work with given properties if (lines[i].substring(0, 1) == "@") { var args = lines[i].substring(1).split(":"); switch (args[0]) { case "tileset": { break; } } } // Skip properties and comments if (ignoreLineChars.indexOf(lines[i].substring(0, 1)) > -1) continue; // Split line text into tile ID array var lineTiles = lines[i].split(","); // Create empty array ready to store // tile objects var lineTilesNew = []; //Loop through tile ID's for (var j = 0; j < lineTiles.length; j++) { // Create new tile object with looped // tile ID var tile = new Tile(parseInt(lineTiles[j])); // Add the tile ID to the new tile // array lineTilesNew.push(tile); } // Add the new tile array to the final // tiles array tiles.push(lineTilesNew); } // Store the tile data in the level's // tile property this.tiles = tiles; // Automatically adjust the width and height // of the level to match the width and height // of the level file passed in this.width = this.tiles[0].length; this.height = this.tiles.length; } /** * Gets a tile object at a given x and y in * the level @ 0:x (number) X position in level @ 1:y (number) Y position in level # (object(Tile)) Tile object */ Level.prototype.getTileAtPoint = function(x, y) { var iX = tileEngine.tileSize * Math.floor(x / tileEngine.tileSize) / tileEngine.tileSize; var iY = tileEngine.tileSize * Math.floor(y / tileEngine.tileSize) / tileEngine.tileSize; return this.tiles[iY][iX]; } /** * Sets every level tile's ID back to 0 * effectively clearing the level # (void) */ Level.prototype.clearAll = function() { // Loop through level's tile objects for (var i = 0; i < this.tiles.length; i++) { for (var j = 0; j < this.tiles[i].length; j++) { // Set the tile ID back to 0 this.tiles[i][j].setID(0); } } } /** * Gathers tile data and other level data * and constructs a string that can then be * saved into a level file # (string) Constructed level string */ Level.prototype.getLevelDataAsText = function() { // Create content string var content = ""; // Loop through level tile rows for (var i = 0; i < this.tiles.length; i++) { // Loop through level tile columns for (var j = 0; j < this.tiles[i].length; j++) { // Add tile IDs to the content string content += this.tiles[i][j].getID(); // Add a comma separator if needed if (j < (this.tiles[i].length - 1)) content += ","; } // If finishing a row, add a new line character if (i < (this.tiles.length - 1)) content += "\n"; } // Return the string return content; }