Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package
- {
- import flash.display.Bitmap;
- import flash.display.BitmapData;
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.events.KeyboardEvent;
- import flash.geom.Rectangle;
- import flash.utils.Dictionary;
- public class Main extends Sprite
- {
- private const SCREEN_WIDTH:int = 800;
- private const SCREEN_HEIGHT:int = 600;
- private var controlFlags:Vector.<Boolean> = Vector.<Boolean>([false, false, false, false]);
- private var canvas:Bitmap;
- private var canvasBD:BitmapData;
- private var camLeftX:int;
- private var camBotY:int;
- private var tiles:Dictionary;
- public function Main():void
- {
- if (stage) init();
- else addEventListener(Event.ADDED_TO_STAGE, init);
- }
- private function init(e:Event = null):void
- {
- removeEventListener(Event.ADDED_TO_STAGE, init);
- // entry point
- canvasBD = new BitmapData(SCREEN_WIDTH, SCREEN_HEIGHT);
- canvas = new Bitmap(canvasBD);
- addChild(canvas);
- canvasBD.fillRect(canvasBD.rect, 0xff000000);
- tiles = new Dictionary();
- camLeftX = camBotY = 0;
- tiles["-1x-1"] = new Tile(0xff000000, -800, 0, -600, 0);
- tiles["0x-1"] = new Tile(0xffff0000, 0, 800, -600, 0);
- tiles["1x-1"] = new Tile(0xff000000, 800, 1600, -600, 0);
- tiles["-1x0"] = new Tile(0xffff0000, -800, 0, 0, 600);
- tiles["0x0"] = new Tile(0xff000000, 0, 800, 0, 600);
- tiles["1x0"] = new Tile(0xffff0000, 800, 1600, 0, 600);
- tiles["-1x1"] = new Tile(0xff000000, -800, 0, 600, 1200);
- tiles["0x1"] = new Tile(0xffff0000, 0, 800, 600, 1200);
- tiles["1x1"] = new Tile(0xff000000, 800, 1600, 600, 1200);
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandle);
- stage.addEventListener(KeyboardEvent.KEY_UP, keyHandle);
- addEventListener(Event.ENTER_FRAME, cycle);
- }
- private function cycle(event:Event):void
- {
- moveCamera();
- createTiles();
- render();
- }
- private function keyHandle(keyboardEvent:KeyboardEvent):void
- {
- var flag:Boolean = false;
- if (keyboardEvent.type == "keyDown") {
- flag = true;
- }
- switch (keyboardEvent.keyCode)
- {
- case 87: // W
- controlFlags[0] = flag;
- break;
- case 65: // A
- controlFlags[1] = flag;
- break;
- case 83: // S
- controlFlags[2] = flag;
- break;
- case 68: // D
- controlFlags[3] = flag;
- break;
- default:
- break;
- }
- }
- private function moveCamera():void
- {
- if (controlFlags[0]) {
- camBotY += 5;
- }
- if (controlFlags[1]) {
- camLeftX -= 5;
- }
- if (controlFlags[2]) {
- camBotY -= 5;
- }
- if (controlFlags[3]) {
- camLeftX += 5;
- }
- }
- private function createTiles():void
- {
- var showingTileAbove:Boolean, showingTileRight:Boolean;
- var tX:int, tY:int;
- tX = camLeftX / SCREEN_WIDTH; // tX set to Left side of screen Tile
- tY = camBotY / SCREEN_HEIGHT; // tY set to Bottom side of screen Tile
- if (camLeftX < 0) tX--;
- if (camBotY < 0) tY--;
- if (camLeftX % SCREEN_WIDTH != 0) showingTileRight = true;
- if (camBotY % SCREEN_HEIGHT != 0) showingTileAbove = true;
- makeTileExist(tX, tY);
- if (showingTileRight) {
- if (showingTileAbove) {
- tX++;
- makeTileExist(tX, tY);
- tY++;
- makeTileExist(tX, tY);
- tX--;
- makeTileExist(tX, tY);
- } else {
- tX++;
- makeTileExist(tX, tY);
- }
- } else {
- if (showingTileAbove) {
- tY++;
- makeTileExist(tX, tY);
- }
- }
- }
- private function render():void
- {
- var rect:Rectangle;
- var color:uint;
- var x:int, y:int;
- for each (var tile:Tile in tiles)
- {
- x = tile.left - camLeftX;
- y = camBotY - tile.top;
- rect = new Rectangle(x,y,SCREEN_WIDTH, SCREEN_HEIGHT);
- color = tile.tileColor;
- canvasBD.fillRect(rect, color);
- }
- }
- private function makeTileExist(tileX:int, tileY:int):void
- {
- var tilesKeyName:String;
- tilesKeyName = tileX + "x" + tileY;
- if (tiles[tilesKeyName] == null) {
- tiles[tilesKeyName] = new Tile(getRandomColor(), SCREEN_WIDTH * tileX, SCREEN_WIDTH * (tileX+1), SCREEN_HEIGHT * tileY, SCREEN_HEIGHT * (tileY+1));
- }
- }
- private function getRandomColor():uint
- {
- var c:uint = 0xff000000;
- c += Math.random() * 0xff0000;
- c += Math.random() * 0xff00;
- c += Math.random() * 0xff;
- return c;
- }
- }
- }
- internal class Tile
- {
- public var tileColor:uint;
- public var left:int;
- public var right:int;
- public var top:int;
- public var bot:int;
- public function Tile(color:uint, left:int, right:int, top:int, bot:int):void
- {
- this.tileColor = color;
- this.left = left;
- this.right = right;
- this.bot = bot;
- this.top = top;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement