Level.as – Level Parser



// Authors: Team 2: Andrew Bertino, Aaron Sagan, Matt Bloise, Michael Luongo
// Email: abertino@fiea.ucf.edu, asagan@fiea.ucf.edu, mbloise@fiea.ucf.edu, mluongo@fiea.ucf.edu
// File: Level.as - Creates the ogmo level and all its tiles

package  
{
     import flash.geom.Point;
     import flash.utils.ByteArray;
     import net.flashpunk.masks.Grid
     import net.flashpunk.Entity;
     import net.flashpunk.graphics.Tilemap;
     import net.flashpunk.World;
     import net.flashpunk.FP;
     
     public class Level extends Entity 
     {
          // Member Variables
          private var levelMap:Tilemap;
          private var collisionBox:Grid;
          public var levelXML:XML;
          public var castleBlockType:String;
          
          // Embeds
          [Embed(source = '/../assets/art/tilesyay.png')] 
          private const TILE_SET:Class;
          
          // Constructor Function - Creates level map and collisions.
          public function Level(xml:Class):void 
          {
               castleBlockType = "none";
               
               levelMap = new Tilemap(TILE_SET, 800, 2352, 32, 32);
               collisionBox = new Grid(800, 2352, 32, 32, 0, 0);
               
               mask = collisionBox;
               graphic = levelMap;
               
               loadLevel(xml);
          }
          
          //parses in raw XML data for level
          public function loadLevel(xml:Class):void
          {
               var rawData:ByteArray = new xml;
               var dataString:String = rawData.readUTFBytes(rawData.length);
               levelXML = new XML(dataString);
               
               var dataList:XMLList;
               var dataElement:XML;
               
               //for mapped out tiles in the level editor
               dataList = levelXML.layout.tile;
               for each (dataElement in dataList) {
                    levelMap.setTile(int(dataElement.@x / 32), int(dataElement.@y / 32), int(dataElement.@tx) / 32);
                    if ((dataElement.@tx) / 32 >= 9) {
                         collisionBox.setTile(int(dataElement.@x) / 32, int(dataElement.@y) / 32, true);
                         //sets the collision box to water
                         type = "Water";
                         }
                    }
               //for mapped out rects in the level editor
               //as rects are different than just tiles, the set conditions are different and therfore cannot
               //be put into a common private function.
               dataList = levelXML.layout.rect;
               for each (dataElement in dataList) {
                    levelMap.setRect(int(dataElement.@x) / 32, int(dataElement.@y) / 32, int(dataElement.@w) / 32, int(dataElement.@h) / 32, int(dataElement.@tx) / 32);
                    if ((dataElement.@tx) / 32 >= 9) {
                         //sets the collision box to water
                         collisionBox.setRect(int(dataElement.@x) / 32, int(dataElement.@y) / 32, int(dataElement.@w) / 32, int(dataElement.@h) / 32, true);
                         type = "Water";
                         }
                    }
               //levelMap.setRect(0, 0, 32, 10, 0);
               }
     }

}