Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.MovieClip;
- import flash.display.BitmapData;
- import flash.display.Bitmap;
- import flash.display.BitmapDataChannel;
- import flash.geom.Point;
- import flash.events.Event;
- import flash.display.Sprite;
- public class Stage extends MovieClip
- {
- protected var tilesInWorld:Array = new Array();
- public var worldTiles:Sprite;
- protected var tile:MovieClip;
- protected var TILE_SIZE:int = 5;
- var pmap:BitmapData = new BitmapData(400,400);
- var _seed:uint = Math.round(Math.random() * 100);
- var grid_width:uint = new uint(800/TILE_SIZE);
- var grid_height:uint = new uint(600/TILE_SIZE);
- var heightmap:Array = new Array();
- var pixelPoint:Point = new Point();
- var darkest_pixel:Number = 1;
- var brightest_pixel:Number = 0;
- public function Stage()
- {
- pmap.perlinNoise(400,400, 6, _seed, true, false, 1, true);
- for (var i:uint=0; i < grid_width; i++)
- {
- heightmap[i] = new Array();
- for (var j:uint=0; j < grid_height; j++)
- {
- heightmap[i][j] = new uint();
- }
- }
- //Divide the map in to a 7x7 grid and take data at each interval
- for (i=0; i < grid_width; i++)
- {
- for (j=0; j < grid_height; j++)
- {
- pixelPoint.x = Math.round((i/grid_width) * pmap.width)+1;
- pixelPoint.y = Math.round((j/grid_width) * pmap.height)+1;
- heightmap[i][j] = pmap.getPixel(pixelPoint.x,pixelPoint.y);
- heightmap[i][j] /= 0xffffff;
- if (heightmap[i][j] < darkest_pixel)
- {
- darkest_pixel = heightmap[i][j];
- }
- }
- }
- //Adjust values to a min of 0
- for (i=0; i < grid_width; i++)
- {
- for (j=0; j < grid_height; j++)
- {
- heightmap[i][j] -= darkest_pixel;
- if (heightmap[i][j] > brightest_pixel)
- {
- brightest_pixel = heightmap[i][j];
- }
- }
- }
- //Adjust values to highest value of 1
- for (i=0; i < grid_width; i++)
- {
- for (j=0; j < grid_height; j++)
- {
- heightmap[i][j] /= brightest_pixel;
- }
- }
- worldTiles = new Sprite();
- addChild(worldTiles);
- placeTile();
- }
- protected function placeTile()
- {
- for (var i=0; i < grid_width; i++)
- {
- tilesInWorld[i] = new Array();
- for (var j=0; j < grid_height; j++)
- {
- tilesInWorld[i][j] = new Array();
- if(heightmap[i][j] >= 0.8){
- tile = new Water();
- }else if(heightmap[i][j] >= 0.7 && heightmap[i][j] < 0.8){
- tile = new Sand();
- }else if(heightmap[i][j] >= 0.3 && heightmap[i][j] < 0.7){
- tile = new Tile();
- }else{
- tile = new Stone();
- }
- tile.width = TILE_SIZE;
- tile.height = TILE_SIZE;
- worldTiles.x = 0;
- worldTiles.y = 0;
- tile.x = TILE_SIZE * (i % grid_width);
- tile.y = TILE_SIZE * (j % grid_width);
- tilesInWorld.push(tile);
- worldTiles.addChild(tile);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement