-
Notifications
You must be signed in to change notification settings - Fork 1
/
TileGame.as
executable file
·156 lines (134 loc) · 4.61 KB
/
TileGame.as
1
package { import flash.display.*; import flash.events.*; import flash.utils.*; import flash.geom.*; import flash.text.*; import tools.*; import visual.* public class TileGame extends Sprite{ private var _bmp:BitmapData; private var _xmlLoader:XmlLoader; private var _xmlData:XML; private var _screenParser:Parser; private var _screenArray:Array; private var _screen:Screen; private var _tileset:ImageLoader; private var _mouseDown:Boolean; private var _tileCoords:Point = new Point(0, 0); public static const FRAME_RATE:int = 10; private var _period:Number = 1000 / FRAME_RATE; private var _beforeTime:int = 0; private var _afterTime:int = 0; private var _timeDiff:int = 0; private var _sleepTime:int = 0; private var _overSleepTime:int = 0; private var _excess:int = 0; private var gameTimer:Timer; public function TileGame(){ try{ _xmlLoader = new XmlLoader("http://bradya.com/circles/screen1.xml"); } catch(e:Error){ trace("couldn't load XML file") } // wait till the XmlLoader will finish loading the data _xmlLoader.addEventListener(Event.COMPLETE,onXmlReady); saveButton.addEventListener(MouseEvent.CLICK, saveClickHandler); loadButton.addEventListener(MouseEvent.CLICK, loadClickHandler); } private function onXmlReady(event:Event):void{ //recieve the XML data from the event _xmlData = XmlLoader(event.target).xml as XML; //parse and build a screen from parsed data _screenParser = new Parser(_xmlData); _screenArray = _screenParser.rows; aboveLevelTextInput.text = _screenParser.levelAbove; leftLevelTextInput.text = _screenParser.levelLeft; belowLevelTextInput.text = _screenParser.levelBelow; rightLevelTextInput.text = _screenParser.levelRight; _tileset = new ImageLoader("http://bradya.com/circles/tileset.png"); _tileset.addEventListener(Event.COMPLETE,onTilesetReady); } private function onTilesetReady(event:Event):void{ _bmp = new BitmapData(1024,768); _bmp.draw(_tileset.loader); var bitmap = new Bitmap(_bmp); _screen = new Screen(_screenArray, _bmp); _screen.x = 0; _screen.y = 0; addChild(_screen); _tileset.x = 1048; addChild(_tileset); stage.addEventListener(MouseEvent.MOUSE_DOWN, doMouseDown, false, 0, true); stage.addEventListener(MouseEvent.MOUSE_UP, doMouseUp, false, 0, true); stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down); gameTimer=new Timer(_period,1); //changed in part 3 from 50 gameTimer.addEventListener(TimerEvent.TIMER, runGame); gameTimer.start(); } function key_down(e:KeyboardEvent):void { //if(e.keyCode == 32) //{ // _screenParser.saveXml(_screenArray, fileNameTextInput.text); //} } function saveClickHandler(event_object:MouseEvent) { _screenParser.saveXml(_screenArray, fileNameTextInput.text, aboveLevelTextInput.text, leftLevelTextInput.text, belowLevelTextInput.text, rightLevelTextInput.text); } function loadClickHandler(event_object:MouseEvent) { try { _xmlLoader = new XmlLoader("http://bradya.com/circles/" + fileNameTextInput.text + ".xml"); _xmlLoader.addEventListener(Event.COMPLETE,onXmlReady); } catch(e:Error){ trace("couldn't load XML file") } } private function doMouseDown($evt:MouseEvent):void { //trace("MOUSE DOWN!"); //trace($evt.stageX); //trace($evt.relatedObject); _mouseDown = true; // Clicking over tileset if (($evt.stageX > _tileset.x) && ($evt.stageX < (_tileset.x + _tileset.width))) { if (($evt.stageY > _tileset.y) && ($evt.stageY < (_tileset.y + _tileset.height))) { var x:uint = Math.floor($evt.localX/16); var y:uint = Math.floor($evt.localY/16); _tileCoords = new Point(x,y); trace("Tile Selected: " + _tileCoords); } } // Clicking over map if (($evt.stageX > 0) && ($evt.stageX < 1024)) { if (($evt.stageY > 0) && ($evt.stageY < 768)) { //(typePoint:Point,locationX:uint,locationY:uint,tileset:BitmapData var tile:Tile = new Tile(_tileCoords, 0, 0, _bmp); var locationX:uint = Math.floor($evt.stageX/16)*16; var locationY:uint = Math.floor($evt.stageY/16)*16; tile.x = locationX; tile.y = locationY; addChild(tile); var tilesRow:Array = _screenArray[locationY/16]; var tilePoint:Point = tilesRow[locationX/16]; tilePoint.x = _tileCoords.x; tilePoint.y = _tileCoords.y; } } } private function doMouseUp($evt:MouseEvent):void { //trace("MOUSE UP!"); _mouseDown = false; } private function runGame(e:TimerEvent) { e.updateAfterEvent(); gameTimer.reset(); gameTimer.start(); } }}