Advertisement
Guest User

EndlessTileGenerator.as

a guest
Oct 6th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package
  2. {
  3.     import flash.display.Bitmap;
  4.     import flash.display.BitmapData;
  5.     import flash.display.Sprite;
  6.     import flash.events.Event;
  7.     import flash.events.KeyboardEvent;
  8.     import flash.geom.Rectangle;
  9.     import flash.utils.Dictionary;
  10.    
  11.     public class Main extends Sprite
  12.     {
  13.         private const SCREEN_WIDTH:int = 800;
  14.         private const SCREEN_HEIGHT:int = 600;
  15.        
  16.         private var controlFlags:Vector.<Boolean> = Vector.<Boolean>([false, false, false, false]);
  17.         private var canvas:Bitmap;
  18.         private var canvasBD:BitmapData;
  19.         private var camLeftX:int;
  20.         private var camBotY:int;
  21.        
  22.         private var tiles:Dictionary;
  23.        
  24.         public function Main():void
  25.         {
  26.             if (stage) init();
  27.             else addEventListener(Event.ADDED_TO_STAGE, init);
  28.         }
  29.        
  30.         private function init(e:Event = null):void
  31.         {
  32.             removeEventListener(Event.ADDED_TO_STAGE, init);
  33.            
  34.             // entry point
  35.             canvasBD = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT);
  36.             canvas = new Bitmap(canvasBD);
  37.             addChild(canvas);
  38.             canvasBD.fillRect(canvasBD.rect, 0xff000000);
  39.             tiles = new Dictionary();
  40.             camLeftX = camBotY = 0;
  41.            
  42.             tiles["-1x-1"] = new Tile(0xff000000, -800, 0, -600, 0);
  43.             tiles["0x-1"] = new Tile(0xffff0000, 0, 800, -600, 0);
  44.             tiles["1x-1"] = new Tile(0xff000000, 800, 1600, -600, 0);
  45.             tiles["-1x0"] = new Tile(0xffff0000, -800, 0, 0, 600);
  46.             tiles["0x0"] = new Tile(0xff000000, 0, 800, 0, 600);
  47.             tiles["1x0"] = new Tile(0xffff0000, 800, 1600, 0, 600);
  48.             tiles["-1x1"] = new Tile(0xff000000, -800, 0, 600, 1200);
  49.             tiles["0x1"] = new Tile(0xffff0000, 0, 800, 600, 1200);
  50.             tiles["1x1"] = new Tile(0xff000000, 800, 1600, 600, 1200);
  51.            
  52.             stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandle);
  53.             stage.addEventListener(KeyboardEvent.KEY_UP, keyHandle);
  54.             addEventListener(Event.ENTER_FRAME, cycle);
  55.         }
  56.        
  57.         private function cycle(event:Event):void
  58.         {
  59.             moveCamera();
  60.             createTiles();
  61.             render();
  62.         }
  63.        
  64.         private function keyHandle(keyboardEvent:KeyboardEvent):void
  65.         {
  66.             var flag:Boolean = false;
  67.             if (keyboardEvent.type == "keyDown") {
  68.                 flag = true;
  69.             }
  70.             switch (keyboardEvent.keyCode)
  71.             {
  72.                 case 87: // W
  73.                     controlFlags[0] = flag;
  74.                 break;
  75.                 case 65: // A
  76.                     controlFlags[1] = flag;
  77.                 break;
  78.                 case 83: // S
  79.                     controlFlags[2] = flag;
  80.                 break;
  81.                 case 68: // D
  82.                     controlFlags[3] = flag;
  83.                 break;
  84.                 default:
  85.                 break;
  86.             }
  87.         }
  88.        
  89.         private function moveCamera():void
  90.         {
  91.             if (controlFlags[0]) {
  92.                 camBotY += 5;
  93.             }
  94.             if (controlFlags[1]) {
  95.                 camLeftX -= 5;
  96.             }
  97.             if (controlFlags[2]) {
  98.                 camBotY -= 5;
  99.             }
  100.             if (controlFlags[3]) {
  101.                 camLeftX += 5;
  102.             }
  103.         }
  104.        
  105.         private function createTiles():void
  106.         {
  107.             var showingTileAbove:Boolean, showingTileRight:Boolean;
  108.             var tX:int, tY:int;
  109.             tX = camLeftX / SCREEN_WIDTH; // tX set to Left side of screen Tile
  110.             tY = camBotY / SCREEN_HEIGHT; // tY set to Bottom side of screen Tile
  111.             if (camLeftX < 0) tX--;
  112.             if (camBotY < 0) tY--;
  113.             if (camLeftX % SCREEN_WIDTH != 0) showingTileRight = true;
  114.             if (camBotY % SCREEN_HEIGHT != 0) showingTileAbove = true;
  115.             makeTileExist(tX, tY);
  116.             if (showingTileRight) {
  117.                 if (showingTileAbove) {
  118.                     tX++;
  119.                     makeTileExist(tX, tY);
  120.                     tY++;
  121.                     makeTileExist(tX, tY);
  122.                     tX--;
  123.                     makeTileExist(tX, tY);
  124.                 } else {
  125.                     tX++;
  126.                     makeTileExist(tX, tY);
  127.                 }
  128.             } else {
  129.                 if (showingTileAbove) {
  130.                     tY++;
  131.                     makeTileExist(tX, tY);
  132.                 }
  133.             }
  134.         }
  135.        
  136.         private function render():void
  137.         {
  138.             var rect:Rectangle;
  139.             var color:uint;
  140.             var x:int, y:int;
  141.             for each (var tile:Tile in tiles)
  142.             {
  143.                 x = tile.left - camLeftX;
  144.                 y = camBotY - tile.top;
  145.                 rect = new Rectangle(x,y,SCREEN_WIDTH, SCREEN_HEIGHT);
  146.                 color = tile.tileColor;
  147.                 canvasBD.fillRect(rect, color);
  148.             }
  149.         }
  150.        
  151.         private function makeTileExist(tileX:int, tileY:int):void
  152.         {
  153.             var tilesKeyName:String;
  154.             tilesKeyName = tileX + "x" + tileY;
  155.             if (tiles[tilesKeyName] == null) {
  156.                 tiles[tilesKeyName] = new Tile(getRandomColor(), SCREEN_WIDTH * tileX, SCREEN_WIDTH * (tileX+1), SCREEN_HEIGHT * tileY, SCREEN_HEIGHT * (tileY+1));
  157.             }
  158.         }
  159.        
  160.         private function getRandomColor():uint
  161.         {
  162.             var c:uint = 0xff000000;
  163.             c += Math.random() * 0xff0000;
  164.             c += Math.random() * 0xff00;
  165.             c += Math.random() * 0xff;
  166.             return c;
  167.         }
  168.        
  169.     }
  170. }
  171. internal class Tile
  172. {
  173.     public var tileColor:uint;
  174.     public var left:int;
  175.     public var right:int;
  176.     public var top:int;
  177.     public var bot:int;
  178.    
  179.     public function Tile(color:uint, left:int, right:int, top:int, bot:int):void
  180.     {
  181.         this.tileColor = color;
  182.         this.left = left;
  183.         this.right = right;
  184.         this.bot = bot;
  185.         this.top = top;
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement