PageRenderTime 49ms CodeModel.GetById 38ms RepoModel.GetById 0ms app.codeStats 1ms

/com/lele/Map/BitMapUtil.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 162 lines | 142 code | 11 blank | 9 comment | 23 complexity | c4912e048be8eb495a528a6ba44d9d05 MD5 | raw file
  1. package com.lele.Map
  2. {
  3. import com.lele.Map.Interface.IBitMapUtil;
  4. import flash.display.Bitmap;
  5. import flash.display.BitmapData;
  6. import flash.display.Sprite;
  7. /**
  8. * ...
  9. * @author Lele
  10. */
  11. public class BitMapUtil implements IBitMapUtil //目前功能,sprite转成bitmap并包含上色功能 //包装类建议不继承private var colorObj:Object = { canGo:4289555498, notGo:4294967295, special:4278229503 };
  12. {
  13. private var _bitData:BitmapData;
  14. private var _bitMap:Bitmap;
  15. private var _bitBlock:int;
  16. private var _bitSprite:Sprite;
  17. private var _bitMapWidth:int;
  18. private var _bitMapHight:int;
  19. private var _bitColorObj:Object
  20. public function BitMapUtil(sprite:Sprite,width:int,hight:int,block:int,bitColorObj:Object)
  21. {
  22. _bitMapWidth = width;
  23. _bitMapHight = hight;
  24. _bitSprite = sprite;
  25. _bitBlock = block >= 2?block:2;
  26. _bitData = new BitmapData(_bitMapWidth, _bitMapHight);
  27. _bitData.draw(_bitSprite);
  28. _bitColorObj = bitColorObj;
  29. }
  30. //各种get set
  31. public function get BitMap():Bitmap
  32. {
  33. _bitMap = new Bitmap(_bitData);//创建最新数据的bitmap并返回
  34. return _bitMap;
  35. }
  36. public function get BitData():BitmapData
  37. {
  38. return _bitData;
  39. }
  40. public function get BitSourceWidth():int
  41. {
  42. return _bitMapWidth;
  43. }
  44. public function get BitSourceHight():int
  45. {
  46. return _bitMapHight;
  47. }
  48. public function get BitBlock():int
  49. {
  50. return _bitBlock;
  51. }
  52. //轻量函数
  53. public function CleanMapData()
  54. {
  55. _bitData = null;
  56. _bitData = new BitmapData(_bitMapWidth, _bitMapHight);
  57. }
  58. public function GetLengthX():int//加上block影响后的
  59. {
  60. return _bitMapWidth / _bitBlock;
  61. }
  62. public function GetLengthY():int//加上block影响后的
  63. {
  64. return _bitMapHight / _bitBlock;
  65. }
  66. public function GetRectColor(x:int,y:int):uint//获取小矩形的颜色,处理过后的数据才可用这个函数
  67. {
  68. return _bitData.getPixel32(x*_bitBlock, y*_bitBlock);
  69. }
  70. public function GetRectState(x:int, y:int):int//处理过后的数据,经过block获取状态,0=notGo,1=canGo,2=special
  71. {
  72. var color:uint = GetRectColor(x, y);
  73. if (color == _bitColorObj.notGo)
  74. return 0;
  75. if (color == _bitColorObj.canGo)
  76. return 1;
  77. if (color == _bitColorObj.special)
  78. return 2;
  79. return 0;//默认不可走
  80. }
  81. //在图上绘制的函数
  82. public function DrawRect(x:int, y:int, color:uint, block:int)//从某一点开始画
  83. {
  84. for (var yy:int = y; yy < (y + 1) * block; yy++ )
  85. {
  86. for (var xx:int = x; xx < (x + 1) * block; xx++ )
  87. {
  88. _bitData.setPixel32(xx, yy, color);
  89. }
  90. }
  91. }
  92. public function DrawRectInMapResolution(x:int, y:int, color:uint)
  93. {
  94. x = x >= GetLengthX()?GetLengthX() - 1:x;
  95. y = y >= GetLengthY()?GetLengthY() - 1:y;
  96. SetRectPixelColor(x, y, color);
  97. }
  98. //一下是初始的DrawPixelMap和各内部函数
  99. public function DrawPixelMap():Bitmap//_bitColorObj不准为空//标准初始化函数
  100. {
  101. _bitData = new BitmapData(_bitMapWidth, _bitMapHight);
  102. _bitData.draw(_bitSprite);
  103. if (_bitColorObj == null) { trace("标识色彩为空!"); return BitMap; }
  104. for (var y:int = 0; y < _bitMapHight/_bitBlock; y++)
  105. {
  106. for (var x:int = 0; x < _bitMapWidth/_bitBlock; x ++ )
  107. {
  108. var tempColor:uint = GetRectPixelColor(x, y);
  109. SetRectPixelColor(x, y, tempColor);
  110. }
  111. }
  112. return BitMap;
  113. }
  114. private function SetRectPixelColor(x:int, y:int,color:uint):void//,某分辨率缩放后的点,自动使用block,这时的状态已经是粗化后的点了,这个方法其实是未来不需要的
  115. {
  116. for (var yy:int = y * _bitBlock; yy < (y + 1) * _bitBlock; yy++ )
  117. {
  118. for (var xx:int = x * _bitBlock; xx < (x + 1) * _bitBlock; xx++ )
  119. {
  120. _bitData.setPixel32(xx, yy, color);
  121. }
  122. }
  123. }
  124. private function GetRectPixelColor(x:int, y:int):uint//根据block自动适应 测试用函数//处理模糊rect
  125. {
  126. var tempX:int = x * _bitBlock + _bitBlock / 2;
  127. var tempY:int = y * _bitBlock + _bitBlock / 2;
  128. var tempColor:uint = _bitData.getPixel32(tempX, tempY);//矩形中点颜色
  129. if (tempColor!=_bitColorObj.canGo&&tempColor!=_bitColorObj.notGo&&tempColor!=_bitColorObj.special)
  130. {
  131. //寻找新的可用像素
  132. tempColor = GetValuablePixelColor(x, y);
  133. }
  134. return tempColor;
  135. }
  136. private function GetValuablePixelColor(tx:int, ty:int):uint//,根据block自适应这个要保留,当色彩不对时用这个函数重采样
  137. {
  138. var outPixel32:uint;
  139. for (var yy:int = ty*_bitBlock; yy < (ty+1)*_bitBlock; yy++ )
  140. {
  141. for (var xx:int = tx*_bitBlock; xx < (tx+1)*_bitBlock; xx++ )
  142. {
  143. outPixel32 = _bitData.getPixel32(xx, yy);
  144. if (outPixel32 == _bitColorObj.canGo || outPixel32 == _bitColorObj.notGo || outPixel32 == _bitColorObj.special)
  145. return outPixel32;
  146. }
  147. }
  148. return _bitColorObj.notGo;
  149. }
  150. }
  151. }