Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.  
  4.     import flash.display.MovieClip;
  5.     import flash.display.BitmapData;
  6.     import flash.display.Bitmap;
  7.     import flash.display.BitmapDataChannel;
  8.     import flash.geom.Point;
  9.     import flash.events.Event;
  10.     import flash.display.Sprite;
  11.  
  12.     public class Stage extends MovieClip
  13.     {
  14.  
  15.         protected var tilesInWorld:Array = new Array();
  16.         public var worldTiles:Sprite;
  17.         protected var tile:MovieClip;
  18.  
  19.         protected var TILE_SIZE:int = 5;
  20.         var pmap:BitmapData = new BitmapData(400,400);
  21.         var _seed:uint = Math.round(Math.random() * 100);
  22.         var grid_width:uint = new uint(800/TILE_SIZE);
  23.         var grid_height:uint = new uint(600/TILE_SIZE);
  24.         var heightmap:Array = new Array();
  25.         var pixelPoint:Point = new Point();
  26.         var darkest_pixel:Number = 1;
  27.         var brightest_pixel:Number = 0;
  28.  
  29.  
  30.         public function Stage()
  31.         {
  32.             pmap.perlinNoise(400,400, 6, _seed, true, false, 1, true);
  33.             for (var i:uint=0; i < grid_width; i++)
  34.             {
  35.                 heightmap[i] = new Array();
  36.                 for (var j:uint=0; j < grid_height; j++)
  37.                 {
  38.                     heightmap[i][j] = new uint();
  39.                 }
  40.             }
  41.             //Divide the map in to a 7x7 grid and take data at each interval
  42.             for (i=0; i < grid_width; i++)
  43.             {
  44.                 for (j=0; j < grid_height; j++)
  45.                 {
  46.                     pixelPoint.x = Math.round((i/grid_width) * pmap.width)+1;
  47.                     pixelPoint.y = Math.round((j/grid_width) * pmap.height)+1;
  48.                     heightmap[i][j] = pmap.getPixel(pixelPoint.x,pixelPoint.y);
  49.                     heightmap[i][j] /=  0xffffff;
  50.  
  51.                     if (heightmap[i][j] < darkest_pixel)
  52.                     {
  53.                         darkest_pixel = heightmap[i][j];
  54.                     }
  55.                 }
  56.             }
  57.             //Adjust values to a min of 0
  58.             for (i=0; i < grid_width; i++)
  59.             {
  60.                 for (j=0; j < grid_height; j++)
  61.                 {
  62.                     heightmap[i][j] -=  darkest_pixel;
  63.  
  64.                     if (heightmap[i][j] > brightest_pixel)
  65.                     {
  66.                         brightest_pixel = heightmap[i][j];
  67.                     }
  68.                 }
  69.             }
  70.  
  71.             //Adjust values to highest value of 1
  72.             for (i=0; i < grid_width; i++)
  73.             {
  74.                 for (j=0; j < grid_height; j++)
  75.                 {
  76.                     heightmap[i][j] /=  brightest_pixel;
  77.                 }
  78.             }
  79.             worldTiles = new Sprite();
  80.             addChild(worldTiles);
  81.             placeTile();
  82.         }
  83.         protected function placeTile()
  84.         {
  85.             for (var i=0; i < grid_width; i++)
  86.             {
  87.                 tilesInWorld[i] = new Array();
  88.                 for (var j=0; j < grid_height; j++)
  89.                 {
  90.                     tilesInWorld[i][j] = new Array();
  91.                     if(heightmap[i][j] >= 0.8){
  92.                         tile = new Water();
  93.                     }else if(heightmap[i][j] >= 0.7 && heightmap[i][j] < 0.8){
  94.                         tile = new Sand();
  95.                     }else if(heightmap[i][j] >= 0.3 && heightmap[i][j] < 0.7){
  96.                         tile = new Tile();
  97.                     }else{
  98.                         tile = new Stone();
  99.                     }
  100.                     tile.width = TILE_SIZE;
  101.                     tile.height = TILE_SIZE;
  102.                     worldTiles.x = 0;
  103.                     worldTiles.y = 0;
  104.                     tile.x = TILE_SIZE * (i % grid_width);
  105.                     tile.y = TILE_SIZE * (j % grid_width);
  106.                     tilesInWorld.push(tile);
  107.                     worldTiles.addChild(tile);
  108.                 }
  109.             }
  110.         }
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement