SHARE
TWEET

Untitled

a guest May 9th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import h2d.Graphics;
  2. import h2d.TileGroup;
  3. import h2d.Bitmap;
  4. import h2d.Tile;
  5.  
  6. class Main extends hxd.App
  7. {
  8.     override function init()
  9.     {
  10.         // Use `Project.create()` to parse an Ogmo Project file and get it's values.
  11.     var project = ogmo.Project.create(hxd.Res.test.entry.getText());
  12.         trace(project.name);
  13.         engine.backgroundColor = 0xffffff;
  14.  
  15.         // Use `Level.create()` to parse an Ogmo Level file and get it's values.
  16.     var level = ogmo.Level.create(hxd.Res.levels.uno.entry.getText());
  17.        
  18.         // Get the Tileset Image
  19.         var tileImage = hxd.Res.img.tiles.toTile();
  20.         // Subtile the Tileset Image into 16x16 tiles
  21.         var tileSize = 16;
  22.         var tileset = [
  23.         for(y in 0 ... Std.int(tileImage.height / tileSize))
  24.         for(x in 0 ... Std.int(tileImage.width / tileSize))
  25.         tileImage.sub(x * 16, y * tileSize, tileSize, tileSize)
  26.       ];
  27.  
  28.         // Register a callback function for any Tile layers exported as a 1D Array containing IDs.
  29.         level.onTileLayerLoaded = (tiles, layer) ->
  30.         {
  31.             var tileGroup = new TileGroup(tileImage, s2d);
  32.             tileGroup.x += layer.offsetX;
  33.             tileGroup.y += layer.offsetY;
  34.  
  35.             for (i in 0...tiles.length)
  36.             {
  37.                 if (tiles[i] > -1)
  38.                 {
  39.                     var x = i % layer.gridCellsX;
  40.                     var y = Math.floor(i / layer.gridCellsX);
  41.                     tileGroup.add(x * layer.gridCellWidth, y * layer.gridCellHeight, tileset[tiles[i]]);
  42.                 }
  43.             }
  44.         }
  45.  
  46.         // Register a callback function for any Tile layers exported as a 2D Array containing IDs.
  47.         level.onTile2DLayerLoaded = (tiles, layer) ->
  48.         {
  49.             var tileGroup = new TileGroup(tileImage, s2d);
  50.             tileGroup.x += layer.offsetX;
  51.             tileGroup.y += layer.offsetY;
  52.  
  53.             for (x in 0...tiles.length) for (y in 0...tiles[x].length)
  54.             {
  55.                 if (tiles[x][y] > -1) tileGroup.add(x * layer.gridCellWidth, y * layer.gridCellHeight, tileset[tiles[x][y]]);
  56.             }
  57.         }
  58.  
  59.         // Register a callback function for any Tile layers exported as a 1D Array containing Coords.
  60.         level.onTileCoordsLayerLoaded = (tiles, layer) ->
  61.         {
  62.             // TODO - example of loading Tile Layer with 1D Array of Coords
  63.         }
  64.  
  65.         // Register a callback function for any Tile layers exported as a 2D Array containing Coords.
  66.         level.onTileCoords2DLayerLoaded = (tiles, layer) ->
  67.         {
  68.             // TODO - example of loading Tile Layer with 2D Array of Coords
  69.         }
  70.  
  71.         // Register a callback function for any Grid layers exported as a 1D Array.
  72.         level.onGridLayerLoaded = (grid, layer) ->
  73.         {
  74.             var graphic = new Graphics(s2d);
  75.             graphic.x += layer.offsetX;
  76.             graphic.y += layer.offsetY;
  77.  
  78.             graphic.beginFill();
  79.  
  80.             for (i in 0...grid.length)
  81.             {
  82.                 if (grid[i] == "0") continue;
  83.                 var x = i % layer.gridCellsX;
  84.                 var y = Math.floor(i / layer.gridCellsX);
  85.                
  86.                 graphic.drawRect(x * layer.gridCellWidth, y * layer.gridCellHeight, layer.gridCellWidth, layer.gridCellHeight);
  87.             }
  88.  
  89.             graphic.endFill();
  90.         }
  91.  
  92.         // Register a callback function for any Grid layers exported as a 2D Array.
  93.         level.onGrid2DLayerLoaded = (grid, layer) ->
  94.         {
  95.             var graphic = new Graphics(s2d);
  96.             graphic.x += layer.offsetX;
  97.             graphic.y += layer.offsetY;
  98.  
  99.             graphic.beginFill();
  100.  
  101.             for (x in 0...grid.length) for (y in 0...grid[x].length)
  102.             {
  103.                 if (grid[x][y] != "0") graphic.drawRect(x * layer.gridCellWidth, y * layer.gridCellHeight, layer.gridCellWidth, layer.gridCellHeight);
  104.             }
  105.  
  106.             graphic.endFill();
  107.         }
  108.  
  109.         // Register a callback function for any Entity layers.
  110.         level.onEntityLayerLoaded = (entities, layer) ->
  111.         {
  112.             for (entity in entities) {
  113.                 var e = new Bitmap(Tile.fromColor(0xFF0000, entity.width == null ? 16: 16, entity.height == null ? 16: 16), s2d);
  114.                 e.name = entity.name;
  115.                 e.setPosition(entity.x + layer.offsetX, entity.y + layer.offsetY);
  116.             }
  117.         }
  118.  
  119.         // Register a callback function for any Decal layers.
  120.         level.onDecalLayerLoaded = (decals, layer) ->
  121.         {
  122.             for (decal in decals) {
  123.                 var e = new Bitmap(hxd.Res.load('img/${decal.texture}').toTile(), s2d);
  124.                 e.tile.dx -= e.tile.width * 0.5;
  125.                 e.tile.dy -= e.tile.height * 0.5;
  126.                 e.setPosition(decal.x + layer.offsetX, decal.y + layer.offsetY);
  127.             }
  128.         }
  129.  
  130.         // Call `load()` on the Level to parse through each layer, triggering any layers have callbacks registered
  131.         level.load();
  132.     }
  133.  
  134.     static function main()
  135.     {
  136.     hxd.Res.initEmbed();
  137.         new Main();
  138.     }
  139. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top