PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/com/lele/Manager/ResourceManager.as

https://gitlab.com/Treeky-NULL/LezaiNiubi
ActionScript | 147 lines | 123 code | 13 blank | 11 comment | 19 complexity | 13e5aa9a6000ca1d8a3791dd777dcd9d MD5 | raw file
  1. package com.lele.Manager
  2. {
  3. import adobe.utils.CustomActions;
  4. import com.lele.Manager.Events.DebugEvent;
  5. import com.lele.Manager.Events.Resource_Game_ManagerEvent;
  6. import com.lele.Manager.Interface.IResourceLoader;
  7. import com.lele.Manager.Interface.IReport;
  8. import flash.display.Loader;
  9. import flash.display.LoaderInfo;
  10. import flash.display.MovieClip;
  11. import flash.display.Sprite;
  12. import flash.events.Event;
  13. import flash.events.ProgressEvent;
  14. import flash.media.Sound;
  15. import flash.net.URLRequest;
  16. /**
  17. * ...
  18. * @author Lele
  19. */
  20. public class ResourceManager implements IResourceLoader
  21. {
  22. private var _loaders:Object;
  23. private var _loadLefts:int;
  24. private var _repoter:IReport;
  25. private var _urlArray:Array;
  26. //加载效果用
  27. private var _currentStyle:String;//"MAP","ITEM","NULL"
  28. private var _loadingCount:int;
  29. public function ResourceManager(report:IReport)
  30. {
  31. _loadLefts = 0;
  32. _loadingCount = 0;
  33. _urlArray = new Array();
  34. _loaders = new Object();
  35. _repoter = report;
  36. }
  37. public function LoadResource(hostId:String, url:String,OnComplete:Function,useUI:Boolean=false,UIType:String="NULL",isPrivate:Boolean=false):void//这个OnComplete必须是基于事件
  38. {
  39. if (_loaders[hostId+ "_"+url] != null)
  40. {//说明资源存在却请求重复加载
  41. trace("资源存在:" + "hostID:" + hostId + " url:" + url + "卸载资源并重新加载");
  42. var DebugEvt:DebugEvent = new DebugEvent("资源存在:" + "hostID:" + hostId + " url:" + url + "卸载资源并重新加载");
  43. _repoter.OnReport(DebugEvt);
  44. UnLoadResource(hostId, url);
  45. }
  46. CheckAdd(hostId + "_" + url);
  47. var urlRequest:URLRequest= new URLRequest(url);//Map/Map002.swf
  48. var currentLoader:Loader = new Loader();
  49. if (useUI)
  50. {
  51. _currentStyle = UIType;
  52. currentLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, OnProgress);
  53. _loadingCount++;
  54. }
  55. currentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function() { if (!isPrivate) { _loadLefts--; }} );//涉及到侦听顺序,所以本地优先
  56. currentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnComplete);
  57. currentLoader.load(urlRequest);
  58. _loaders[hostId + "_" + url] = currentLoader;
  59. _loaders[hostId + "_" + url + "_state"] = false;
  60. if(!isPrivate)
  61. _loadLefts++;
  62. }
  63. public function UnLoadResource(hostId:String,url:String):void
  64. {
  65. CheckRemove(hostId + "_" + url);
  66. if ((_loaders[hostId+"_"+url] as Loader) == null) { return; }
  67. (_loaders[hostId+"_"+url] as Loader).unloadAndStop(true);
  68. _loaders[hostId + "_" + url] = null;
  69. _loaders[hostId + "_" + url + "_state"] = null;
  70. }
  71. public function GetContentByID(hostID:String,url:String):Object
  72. {
  73. return _loaders[hostID+"_"+url];
  74. }
  75. /*private function OnLoaded(evt:Event)
  76. {
  77. _loadLefts--;
  78. }*/
  79. public function get IsLoading():Boolean//全局的 public 型
  80. {
  81. if (_loadLefts == 0)
  82. return false;
  83. return true;
  84. }
  85. public function get CurrentResourceLength():int
  86. {
  87. return _urlArray.length;
  88. }
  89. private function OnProgress(evt:ProgressEvent)//加载是否完成是看
  90. {
  91. if (evt.bytesLoaded / evt.bytesTotal == 1) { _loadingCount--; }
  92. if (_loadingCount == 0)
  93. {
  94. var loadedEvt:Resource_Game_ManagerEvent = new Resource_Game_ManagerEvent(Resource_Game_ManagerEvent.LOADINGPROCESSEVENT);
  95. loadedEvt._isComplete = true;
  96. loadedEvt._type = "MAP";
  97. _repoter.OnReport(loadedEvt);
  98. (evt.target as LoaderInfo).removeEventListener(ProgressEvent.PROGRESS, OnProgress);
  99. //trace("加载完毕")
  100. }
  101. else
  102. {
  103. var loadedEvt:Resource_Game_ManagerEvent = new Resource_Game_ManagerEvent(Resource_Game_ManagerEvent.LOADINGPROCESSEVENT);
  104. loadedEvt._isComplete = false;
  105. loadedEvt._type = "MAP";
  106. loadedEvt._process = (evt.bytesLoaded / evt.bytesTotal)*100;
  107. _repoter.OnReport(loadedEvt);
  108. }
  109. //trace(evt.bytesLoaded/evt.bytesTotal);
  110. }
  111. private function CheckRemove(urlTip:String):void
  112. {
  113. for (var a:int = 0; a < _urlArray.length; a++ )
  114. {
  115. if (_urlArray[a] == urlTip)
  116. {
  117. _urlArray.splice(a, 1);
  118. return;
  119. }
  120. }
  121. }
  122. private function CheckAdd(urlTip:String):void
  123. {
  124. for (var a:int = 0; a < _urlArray.length; a++ )
  125. {
  126. if (_urlArray[a] == urlTip)
  127. {
  128. return;
  129. }
  130. }
  131. _urlArray.push(urlTip);
  132. }
  133. }
  134. }