PageRenderTime 76ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/FluffUtils/src/com/sfdk/graphics/engine/GraphicEngine.as

http://shining-fluff-devkit.googlecode.com/
ActionScript | 423 lines | 336 code | 64 blank | 23 comment | 86 complexity | 89edbb7f706c9d20a36959dfc768c4a7 MD5 | raw file
  1. package com.sfdk.graphics.engine
  2. {
  3. import com.sfdk.graphics.core.SpriteEx;
  4. import com.sfdk.graphics.engine.api.IGraphicElement;
  5. import com.sfdk.graphics.engine.api.IGraphicRenderer;
  6. import com.sfdk.graphics.engine.api.ILayerInformation;
  7. import com.sfdk.graphics.engine.api.IMapObject;
  8. import com.sfdk.graphics.engine.core.TileRenderer;
  9. import com.sfdk.graphics.engine.core.enums.RendererType;
  10. import com.sfdk.utils.IntPoint;
  11. import flash.display.Bitmap;
  12. import flash.display.BitmapData;
  13. import flash.display.Graphics;
  14. import flash.display.Sprite;
  15. import flash.events.Event;
  16. import flash.geom.Point;
  17. import flash.geom.Rectangle;
  18. public class GraphicEngine extends Sprite
  19. {
  20. private var _tileSize:int;
  21. private var _screenWidth:int;
  22. private var _screenHeight:int;
  23. private var _displayScreenLower:Bitmap;
  24. private var _displayScreenJoueur:Bitmap;
  25. private var _displayScreenUpper:Bitmap;
  26. private var _lowerDisplayScreenData:BitmapData;
  27. private var _middleDisplayScreenData:BitmapData;
  28. private var _upperDisplayScreenData:BitmapData;
  29. private var _gridLayer:SpriteEx;
  30. private var _overGridLayer : SpriteEx;
  31. private var _cameraObject:IGraphicElement;
  32. private var _cameraMoving:int;
  33. private var _cameraSpeedX:Number;
  34. private var _cameraSpeedY:Number;
  35. private var _paintElement:IGraphicElement;
  36. private var _centerTileX:int;
  37. private var _centerTileY:int;
  38. private var _scale:Number;
  39. private var _renderer:IGraphicRenderer;
  40. private var _copyRect:Rectangle;
  41. private const _elementList:Vector.<IGraphicElement> = new Vector.<IGraphicElement>();
  42. private var _isLoaded:Boolean;
  43. private var _mapObject:IMapObject;
  44. private const _basePoint:Point = new Point();
  45. private var _cameraHadMoved:Boolean;
  46. private var _displayWalkable:Boolean;
  47. public function GraphicEngine(screenWidth:int, screenHeight:int, scale:Number = 1, displayWalkable:Boolean = true):void {
  48. _screenWidth = screenWidth;
  49. _screenHeight = screenHeight;
  50. _scale = scale;
  51. _displayWalkable = displayWalkable;
  52. _lowerDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  53. _displayScreenLower = new Bitmap(_lowerDisplayScreenData);
  54. _displayScreenLower.scaleX = scale;
  55. _displayScreenLower.scaleY = scale;
  56. _middleDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  57. _displayScreenJoueur = new Bitmap(_middleDisplayScreenData);
  58. _displayScreenJoueur.scaleX = scale;
  59. _displayScreenJoueur.scaleY = scale;
  60. _upperDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  61. _displayScreenUpper = new Bitmap(_upperDisplayScreenData);
  62. _displayScreenUpper.scaleX = scale;
  63. _displayScreenUpper.scaleY = scale;
  64. _gridLayer = new SpriteEx();
  65. _gridLayer.mouseEnabled = false;
  66. _gridLayer.scaleX = _scale;
  67. _gridLayer.scaleY = _scale;
  68. _overGridLayer = new SpriteEx();
  69. _overGridLayer.mouseEnabled = false;
  70. _overGridLayer.scaleX = _scale;
  71. _overGridLayer.scaleY = _scale;
  72. _centerTileX = (int(_screenWidth / 24) >> 1) * 24;
  73. _centerTileY = (int(_screenHeight / 24) >> 1) * 24;
  74. _copyRect = new Rectangle(0, 0, _screenWidth, _screenHeight);
  75. _tileSize = 24;
  76. _gridLayer.visible = false;
  77. addChild(_displayScreenLower);
  78. addChild(_gridLayer);
  79. addChild(_overGridLayer);
  80. addChild(_displayScreenJoueur);
  81. addChild(_displayScreenUpper);
  82. drawGrid();
  83. }
  84. public function resizeScreen(screenWidth:int, screenHeight:int, resetPosition:Boolean = false):void {
  85. _screenWidth = screenWidth;
  86. _screenHeight = screenHeight;
  87. _lowerDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  88. _displayScreenLower.bitmapData = _lowerDisplayScreenData;
  89. _middleDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  90. _displayScreenJoueur.bitmapData = _middleDisplayScreenData;
  91. _upperDisplayScreenData = new BitmapData(screenWidth, screenHeight, true, 0x00555566);
  92. _displayScreenUpper.bitmapData = _upperDisplayScreenData;
  93. _copyRect.width = _screenWidth;
  94. _copyRect.height = _screenHeight;
  95. _centerTileX = (int(_screenWidth / 24 / _scale) >> 1) * 24;
  96. _centerTileY = (int(_screenHeight / 24 / _scale) >> 1) * 24;
  97. if (resetPosition) {
  98. _copyRect.x = 0;
  99. _copyRect.y = 0;
  100. }
  101. update();
  102. drawGrid();
  103. }
  104. public function loadMap(mapObject:IMapObject):void {
  105. if (mapObject.rendererType.kind == RendererType.TILE_BASED.kind)
  106. _renderer = new TileRenderer() as IGraphicRenderer;
  107. _mapObject = mapObject;
  108. // _copyRect.x = 0;
  109. // _copyRect.y = 0;
  110. _renderer.addEventListener("layerBuilt", renderView);
  111. _renderer.loadMap(mapObject);
  112. }
  113. public function drawGrid():void {
  114. var graphics:Graphics = _gridLayer.graphics;
  115. graphics.clear();
  116. graphics.lineStyle(1, 0, 0.5);
  117. var tileWidth:int = _screenWidth / _tileSize;
  118. var tileHeight:int = _screenHeight / _tileSize;
  119. var offsetX:Number = scrollX % _tileSize
  120. var offsetY:Number = scrollY % _tileSize
  121. var i:int, j:int;
  122. for (i = 0; i <= tileWidth; i++) {
  123. graphics.moveTo(- offsetX + i * 24, 0);
  124. graphics.lineTo(- offsetX + i * 24, _screenHeight);
  125. }
  126. for (i = 0; i <= tileHeight; i++) {
  127. graphics.moveTo(0, - offsetY + i * 24);
  128. graphics.lineTo(_screenWidth, - offsetY + i * 24);
  129. }
  130. }
  131. public function getThumbnail():BitmapData {
  132. return _renderer.getThumbnail();
  133. }
  134. public function forceRefresh(layer:ILayerInformation = null, usedTiles:Vector.<IntPoint> = null):void {
  135. _renderer.forceRefresh(layer, usedTiles);
  136. }
  137. public function renderView(e:Event):void {
  138. _renderer.removeEventListener("layerBuilt", renderView);
  139. _isLoaded = true;
  140. _mapObject.cleanTilesets();
  141. update(null, true);
  142. }
  143. public function refreshView():void {
  144. // _lowerDisplayScreenData.lock();
  145. // _middleDisplayScreenData.lock();
  146. // _upperDisplayScreenData.lock();
  147. // _lowerDisplayScreenData.fillRect(new Rectangle(0 , 0 , screenWidth_ , screenHeight_) , 0x00FFFFFF);
  148. // _middleDisplayScreenData.fillRect(new Rectangle(0 , 0 , screenWidth_ , screenHeight_) , 0x00FFFFFF);
  149. // _upperDisplayScreenData.fillRect(new Rectangle(0 , 0 , screenWidth_ , screenHeight_) , 0x00FFFFFF);
  150. // _renderer.copySolBitmap(_lowerDisplayScreenData , _copyRect , _basePoint);
  151. // _renderer.copyJoueurBitmap(_middleDisplayScreenData , _copyRect , _basePoint);
  152. // _renderer.copyDessusBitmap(_upperDisplayScreenData , _copyRect , _basePoint);
  153. // _renderer.copyWalkableBitmap(_upperDisplayScreenData , _copyRect , _basePoint);
  154. // _lowerDisplayScreenData.unlock();
  155. // _middleDisplayScreenData.unlock();
  156. // _upperDisplayScreenData.unlock();
  157. // dispatchEvent(new Event("mapRefreshed"));
  158. }
  159. public function scroll(xPos:int, yPos:int):void {
  160. _copyRect.x = xPos * 24;
  161. _copyRect.y = yPos * 24;
  162. refreshView();
  163. }
  164. public function get scrollX():Number {
  165. return _copyRect.x;
  166. }
  167. public function get scrollY():Number {
  168. return _copyRect.y;
  169. }
  170. public function addElement(graphicElement:IGraphicElement, setAsCamera:Boolean = false):void {
  171. if (setAsCamera)
  172. _cameraObject = graphicElement;
  173. _elementList.push(graphicElement);
  174. }
  175. public function setAsCamera(graphicElement:IGraphicElement, smoothTransition:Boolean = false):void {
  176. _cameraObject = graphicElement;
  177. _cameraObject.isOnScreen = true;
  178. var position:Point;
  179. position = _cameraObject.tilePosition;
  180. if (position.x > (_screenWidth >> 1) || position.x < (_mapObject.width - (_screenWidth >> 1))) { // Center X
  181. var diffX:int = _centerTileX - (position.x - _copyRect.x);
  182. }
  183. if (position.y > (_screenHeight >> 1) || position.y < (_mapObject.height - (_screenHeight >> 1))) { // Center Y
  184. var diffY:int = _centerTileY - (position.y - _copyRect.y);
  185. }
  186. if (!smoothTransition) {
  187. _copyRect.x -= diffX;
  188. _copyRect.y -= diffY;
  189. }
  190. else {
  191. _cameraMoving = 10;
  192. _cameraSpeedX = diffX / 10;
  193. _cameraSpeedY = diffY / 10;
  194. }
  195. }
  196. public function setPaintElement(paintElement:IGraphicElement):void {
  197. _paintElement = paintElement;
  198. }
  199. private var _upperLayerCutRect:Vector.<IntPoint>;
  200. public function update(scrollDirection:Point = null, forceRender:Boolean = false, forceElements : Boolean = false):void {
  201. if (scrollDirection != null) {
  202. _copyRect.x += scrollDirection.x * 24;
  203. _copyRect.y += scrollDirection.y * 24;
  204. }
  205. if (_isLoaded) {
  206. var position:Point;
  207. var cameraMoved:Boolean = forceRender;
  208. var topSpeed:int = 2;
  209. var normalSpeed:int = 1;
  210. if (_cameraObject != null) {
  211. position = _cameraObject.tilePosition;
  212. topSpeed = 2;
  213. if (_cameraMoving) {
  214. _copyRect.x -= _cameraSpeedX;
  215. _copyRect.y -= _cameraSpeedY;
  216. _cameraMoving--;
  217. cameraMoved = true;
  218. }
  219. else
  220. {
  221. var diffX:Number = (_centerTileX - (position.x - _copyRect.x));
  222. var diffY:Number = (_centerTileY - (position.y - _copyRect.y));
  223. // Allow for a 3x3 square around the center
  224. if (diffX != 0 && (diffX < -_tileSize || diffX > _tileSize)) {
  225. if (diffX >= (3 * _tileSize) || diffX <= -(3 * _tileSize))
  226. _copyRect.x -= topSpeed * (diffX / Math.abs(diffX));
  227. else
  228. _copyRect.x -= normalSpeed * (diffX / Math.abs(diffX));
  229. cameraMoved = true;
  230. }
  231. if (diffY != 0 && (diffY < -_tileSize || diffY > _tileSize)) {
  232. if (diffY >= 3 * _tileSize || diffY <= -(3 * _tileSize))
  233. _copyRect.y -= topSpeed * (diffY / Math.abs(diffY));
  234. else
  235. _copyRect.y -= normalSpeed * (diffY / Math.abs(diffY));
  236. cameraMoved = true;
  237. }
  238. }
  239. if (!cameraMoved && _cameraHadMoved && !isNaN(diffX) && (diffX % 24) != 0) {
  240. if (diffX >= 1 || diffX <= -1)
  241. _copyRect.x -= diffX / Math.abs(diffX);
  242. else
  243. _copyRect.x -= Math.abs(diffX) * (diffX / Math.abs(diffX));
  244. cameraMoved = true;
  245. }
  246. if (!cameraMoved && _cameraHadMoved && !isNaN(diffY) && (diffY % 24) != 0) {
  247. if (diffY >= 1 || diffY <= -1)
  248. _copyRect.y -= diffY / Math.abs(diffY);
  249. else
  250. _copyRect.y -= Math.abs(diffY) * (diffY / Math.abs(diffY));
  251. cameraMoved = true;
  252. }
  253. if (cameraMoved) {
  254. _copyRect.x = Math.min(Math.max(0, _copyRect.x), _mapObject.width - _screenWidth);
  255. _copyRect.y = Math.min(Math.max(0, _copyRect.y), _mapObject.height - _screenHeight);
  256. }
  257. _cameraHadMoved = cameraMoved;
  258. }
  259. // Locking the bitmapdata
  260. _lowerDisplayScreenData.lock();
  261. _middleDisplayScreenData.lock();
  262. _upperDisplayScreenData.lock();
  263. if (cameraMoved) {
  264. _lowerDisplayScreenData.fillRect(new Rectangle(0, 0, _screenWidth, _screenHeight), 0x00FFFFFF);
  265. _upperDisplayScreenData.fillRect(new Rectangle(0, 0, _screenWidth, _screenHeight), 0x00FFFFFF);
  266. }
  267. _middleDisplayScreenData.fillRect(new Rectangle(0, 0, _screenWidth, _screenHeight), 0x00FFFFFF);
  268. if (cameraMoved)
  269. _renderer.copySolBitmap(_lowerDisplayScreenData, _copyRect, _basePoint);
  270. _renderer.copyJoueurBitmap(_middleDisplayScreenData, _copyRect, _basePoint);
  271. var elementTmp:IGraphicElement;
  272. var isCleaned:Boolean = false;
  273. var i:int = _elementList.length;
  274. while (--i >= 0) {
  275. elementTmp = _elementList[i];
  276. if (forceElements || (elementTmp.isVisible && elementTmp != _cameraObject && (elementTmp.isOnScreen || elementTmp.hasMoved))) {
  277. elementTmp.hasChanged = false;
  278. // if(!isCleaned)
  279. // {
  280. // isCleaned = true;
  281. // }
  282. position = elementTmp.position.clone();
  283. position.x -= _copyRect.x;
  284. position.y -= _copyRect.y;
  285. if ((elementTmp.isOnScreen && !elementTmp.hasMoved) ||
  286. ((forceElements || elementTmp.hasMoved) && position.x >= 0 && position.y >= 0 && position.x < _copyRect.width && position.y < _copyRect.height)) {
  287. _middleDisplayScreenData.copyPixels(elementTmp.view, elementTmp.view.rect, position, null, position, true);
  288. elementTmp.isOnScreen = true;
  289. }
  290. else {
  291. elementTmp.isOnScreen = false
  292. }
  293. elementTmp.hasMoved = false;
  294. }
  295. }
  296. if(_cameraObject != null && _cameraObject.isOnScreen && _cameraObject.isVisible)
  297. {
  298. position = _cameraObject.position.clone();
  299. position.x -= _copyRect.x;
  300. position.y -= _copyRect.y;
  301. _middleDisplayScreenData.copyPixels(_cameraObject.view, _cameraObject.view.rect, position, null, position, true);
  302. }
  303. if (_paintElement && _paintElement.isOnScreen) {
  304. position = _paintElement.position.clone();
  305. position.x -= _copyRect.x;
  306. position.y -= _copyRect.y;
  307. _middleDisplayScreenData.copyPixels(_paintElement.view, _paintElement.view.rect, position);
  308. elementTmp.isOnScreen = true
  309. }
  310. if (_cameraObject) {
  311. position = _cameraObject.position;
  312. var upperLayerCutRect:Vector.<IntPoint> = _mapObject.getUpperLayerCutRect(position.x / _tileSize, position.y / _tileSize);
  313. if (upperLayerCutRect != null && (_upperLayerCutRect == null || upperLayerCutRect[0].equals(_upperLayerCutRect[0])))
  314. _renderer.forceRefresh(_mapObject.upperLayers[1], upperLayerCutRect, true);
  315. else if (upperLayerCutRect == null && _upperLayerCutRect != null)
  316. _renderer.forceRefresh(_mapObject.upperLayers[1], _upperLayerCutRect, false);
  317. _upperLayerCutRect = upperLayerCutRect;
  318. }
  319. if (cameraMoved)
  320. _renderer.copyDessusBitmap(_upperDisplayScreenData, _copyRect, _basePoint);
  321. if (_displayWalkable)
  322. _renderer.copyWalkableBitmap(_upperDisplayScreenData, _copyRect, _basePoint);
  323. // Unlock the bitmapdata
  324. _lowerDisplayScreenData.unlock();
  325. _middleDisplayScreenData.unlock();
  326. _upperDisplayScreenData.unlock();
  327. }
  328. if (gridLayer.visible)
  329. drawGrid();
  330. }
  331. public function getView():BitmapData {
  332. var outBitmapData:BitmapData = new BitmapData(_screenWidth, _screenHeight);
  333. _renderer.copySolBitmap(outBitmapData, _copyRect, _basePoint);
  334. _renderer.copyJoueurBitmap(outBitmapData, _copyRect, _basePoint);
  335. _renderer.copyDessusBitmap(outBitmapData, _copyRect, _basePoint);
  336. _renderer.copyWalkableBitmap(outBitmapData, _copyRect, _basePoint);
  337. return outBitmapData;
  338. }
  339. public function get maxWidth():int {
  340. if (_renderer == null)
  341. return 0;
  342. return _renderer.mapWidth;
  343. }
  344. public function get maxHeight():int {
  345. if (_renderer == null)
  346. return 0;
  347. return _renderer.mapHeight;
  348. }
  349. public function get gridLayer():SpriteEx {
  350. return _gridLayer;
  351. }
  352. public function get overGridLayer():SpriteEx {
  353. return _overGridLayer;
  354. }
  355. public function removeElement(graphicElement:IGraphicElement):void {
  356. _elementList.splice(_elementList.indexOf(graphicElement), 1);
  357. }
  358. }
  359. }