PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/com/lele/Map/MapBase.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 208 lines | 138 code | 5 blank | 65 comment | 29 complexity | c6d1850ce7904d0f0a012660ca1cffdf MD5 | raw file
  1. package com.lele.Map
  2. {
  3. import com.lele.Controller.Avatar.Interface.IAvatar;
  4. import flash.display.BitmapEncodingColorSpace;
  5. import flash.display.Sprite;
  6. import flash.display.MovieClip;
  7. import com.lele.Map.Events.SwapObjEvent;
  8. import flash.events.Event;
  9. /**
  10. * ...
  11. * @author Lele
  12. */
  13. public class MapBase extends MovieClip
  14. {
  15. protected var maskedObj:Array;//这个地图对象包含的maskedObj组
  16. protected var depth:MovieClip;//玩家存在层
  17. private var hasStart:Boolean;
  18. private var playerArray:Array;
  19. //private var sourceLayer:int;//原层次
  20. //protected var tempBeMask:Array;//临时存放被遮罩对象的信息//考虑是否用来存放所有角色
  21. public function MapBase()
  22. {
  23. maskedObj = new Array();
  24. hasStart = false;
  25. //tempBeMask = new Array();
  26. }
  27. private function PlayersMask(evt:Event)//对玩家之间的遮罩
  28. {
  29. playerArray = new Array();
  30. for (var a:int = 0; a < depth.numChildren; a++ )
  31. {
  32. if ((depth.getChildAt(a) is MaskedObj) || (depth.getChildAt(a) is MaskedObj_Sprite)) { continue; }
  33. playerArray.push(depth.getChildAt(a));
  34. }//得到玩家组
  35. for (var a:int = 0; a < depth.numChildren; a++ )
  36. {
  37. for (var b:int = a; b < depth.numChildren-1; b++ )
  38. {
  39. if ((depth.getChildAt(b + 1) is MaskedObj || depth.getChildAt(b + 1) is MaskedObj_Sprite)&&(depth.getChildAt(b ) is MaskedObj || depth.getChildAt(b ) is MaskedObj_Sprite))
  40. {
  41. continue;
  42. }
  43. if (depth.getChildAt(b).y > depth.getChildAt(b + 1).y)
  44. {
  45. depth.addChildAt(depth.getChildAt(b + 1), b);
  46. }
  47. }
  48. }
  49. /*var indexHead:int = -1;
  50. for (var a:int =0; a < depth.numChildren; a++ )
  51. {
  52. if (depth.getChildAt(a) is MaskedObj_Sprite || depth.getChildAt(a) is MaskedObj)
  53. {
  54. LineAreaPlayerBetween(indexHead, a);
  55. indexHead = a;
  56. }
  57. }
  58. LineAreaPlayerBetween(indexHead, depth.numChildren);*/
  59. }
  60. private function LineAreaPlayerBetween(indexHead:int, indexTail:int)//对一段区间的玩家进行深度排序
  61. {
  62. if (indexTail - indexHead < 3) {return; }
  63. for (var a:int = indexHead + 1; a < indexTail-1; a++ )
  64. {
  65. for (var b:int = a + 1; b < indexTail; b++ )
  66. {
  67. if ((depth.getChildAt(a) as Sprite).y > (depth.getChildAt(b) as Sprite).y)
  68. {
  69. depth.addChildAt(depth.getChildAt(b), a);
  70. }
  71. }
  72. }
  73. }
  74. public function AddActorToMaskLayer(actor:Sprite)
  75. {
  76. maskedObj.push(actor);
  77. DepthAdd(actor);
  78. }
  79. private function DepthAdd(sp:Sprite)
  80. {
  81. var hasAdd:Boolean = false;
  82. for (var a:int = 0; a < depth.numChildren; a++ )
  83. {
  84. if ((depth.getChildAt(a) is MaskedObj || depth.getChildAt(a) is MaskedObj_Sprite)&&depth.getChildAt(a).y<sp.y)
  85. {
  86. depth.addChildAt(sp, a+1>depth.numChildren-1?depth.numChildren-1:a+1);
  87. hasAdd = true;
  88. }
  89. }
  90. if (!hasAdd)
  91. {
  92. depth.addChildAt(sp,0);
  93. }
  94. }
  95. public function StartMask(target:IAvatar,isHit:Boolean=false,OnEnter:Function=null,OnLeave:Function=null)
  96. {
  97. var targetObj = target.Avatar;
  98. //这里加入时应该给一个正确的深度位置
  99. DepthAdd(targetObj);
  100. //原版是这样,但是现在暂时不用
  101. /*for (var a in maskedObj)//当需要交换时maskedObj发出事件,通知map交换Obj
  102. {
  103. if (maskedObj[a] as MaskedObj == null) { continue; }
  104. (maskedObj[a] as MaskedObj).addEventListener(SwapObjEvent.SWAPOBJ_MASK, OnSwapObj);//对maskedObj添加OnSwapObj的监听器,当交换时
  105. (maskedObj[a] as MaskedObj).Start(targetObj, isHit, OnEnter, OnLeave);//调用maskedObj开始进行循环碰撞检测和监听
  106. }
  107. for (var a in maskedObj)
  108. {
  109. if (maskedObj[a] as MaskedObj_Sprite == null) { continue; }
  110. (maskedObj[a] as MaskedObj_Sprite).addEventListener(SwapObjEvent.SWAPOBJ_MASK, OnSwapObj);//对maskedObj添加OnSwapObj的监听器,当交换时
  111. (maskedObj[a] as MaskedObj_Sprite).Start(targetObj, isHit, OnEnter, OnLeave);//调用maskedObj开始进行循环碰撞检测和监听
  112. }*/
  113. if (!hasStart)
  114. {
  115. this.addEventListener(Event.ENTER_FRAME, PlayersMask);
  116. hasStart = true;
  117. }
  118. }
  119. public function StopMask(targetObj:Sprite)
  120. {
  121. //原版是这样,但是现在暂时不用
  122. /*for (var a in maskedObj)
  123. {
  124. if (maskedObj[a] as MaskedObj == null) { continue; }
  125. (maskedObj[a] as MaskedObj).Stop(targetObj);
  126. }
  127. for (var a in maskedObj)
  128. {
  129. if (maskedObj[a] as MaskedObj_Sprite == null) { continue; }
  130. (maskedObj[a] as MaskedObj_Sprite).Stop(targetObj);
  131. }*/
  132. depth.removeChild(targetObj);
  133. }
  134. public function StopMaskListener():void //这个在后期要改写,也许在map中加入要mask的对象的数组
  135. {
  136. for (var a in maskedObj)
  137. {
  138. if (maskedObj[a] as MaskedObj == null) { continue; }
  139. (maskedObj[a] as MaskedObj).removeEventListener(SwapObjEvent.SWAPOBJ_MASK, OnSwapObj);
  140. }
  141. for (var a in maskedObj)
  142. {
  143. if (maskedObj[a] as MaskedObj_Sprite == null) { continue; }
  144. (maskedObj[a] as MaskedObj_Sprite).removeEventListener(SwapObjEvent.SWAPOBJ_MASK, OnSwapObj);
  145. }
  146. if(hasStart)
  147. this.removeEventListener(Event.ENTER_FRAME, PlayersMask);
  148. }
  149. public function Clean()
  150. {
  151. for (var a:int = 0; a < depth.numChildren; a++ )
  152. {
  153. depth.removeChildAt(0);
  154. }
  155. }
  156. protected function OnSwapObj(evt:SwapObjEvent):void
  157. {
  158. /*var beMaskObj:Object = GetBeMaskedObj(evt.SwapObj as Sprite);//evt.target是maskedObj就是地图上的遮挡景物发出的
  159. if (beMaskObj == null)
  160. {
  161. beMaskObj= new Object();
  162. beMaskObj.maskedObj = evt.SwapObj as Sprite;
  163. beMaskObj.depth = this.getChildIndex(evt.SwapObj as Sprite);
  164. tempBeMask.push(beMaskObj);
  165. //说明是新加入的则加入
  166. }*/
  167. if (evt.AddIn)//加入
  168. {
  169. //beMaskObj.depth = this.getChildIndex(evt.SwapObj as Sprite);
  170. //sourceLayer = depth.getChildIndex(evt.SwapObj);
  171. if (depth.getChildIndex(evt.SwapObj as Sprite) < depth.getChildIndex(evt.target as Sprite)) { return; }
  172. var tempPos:int = depth.getChildIndex(evt.target as Sprite);
  173. depth.addChildAt(evt.SwapObj as Sprite,tempPos);
  174. }
  175. else//移除
  176. {
  177. if (depth.getChildIndex(evt.target as Sprite) < depth.getChildIndex(evt.SwapObj as Sprite)) { return; }
  178. var tempPos:int = depth.getChildIndex(evt.target as Sprite);
  179. AddPlayerAt(evt.SwapObj as Sprite,tempPos);
  180. //AddPlayerAt(evt.SwapObj as Sprite,sourceLayer-1);
  181. }
  182. }
  183. protected function AddPlayer(player:Sprite):void //将play放置在地图合适的层中
  184. {
  185. depth.addChild(player);//需要重写吧
  186. }
  187. protected function AddPlayerAt(player:Sprite,index:int):void//在指定位置加入
  188. {
  189. depth.addChildAt(player, index);
  190. }
  191. /*protected function GetBeMaskedObj(maskedObj:Sprite):Object//这个Obj是包含原深度信息的 Obj.maskedObj 是被包装的Sprite Obj.depth 是对应的原始深度
  192. {
  193. for (var a in tempBeMask)
  194. {
  195. if (tempBeMask[a].maskedObj is Sprite)
  196. {
  197. return tempBeMask[a];
  198. }
  199. }
  200. return null;
  201. }*/
  202. }
  203. }