class LoaderManager extends EventDispatcher
{
public var preAssetsAr:Array<Object> = [];
private var totalLoads:Int = 0;
private var currentLoads:Int = 0;
private var _busy:Bool = false;
private var curLoadingObj:Object;
inline public static var BITMAP_TYPE:String = "bmp";
inline public static var ATLAS_TYPE:String = "atl";
inline public static var AUDIO_TYPE:String = "aud";
inline public static var FONT_TYPE:String = "fnt";
inline public static var TEXT_TYPE:String = "txt";
private var allLoadExperienceDic:Map<String, Int>;
public function new()
{
super();
allLoadExperienceDic = new Map();
}
public function setLoadAssets(path:Array<String>, valTxt:String, type:String, progPersent:Array<Float> = null):Void
{
if (_busy == true)
{
throw new Error("YOU SHOU CLEAR ASSETS FIRS WITH startNewLoad()");
return;
}
var obj:Object = { "path":path, "valTxt": valTxt, "type":type, "progPersent":progPersent, "loaders":[]}
allLoadExperienceDic[obj.valTxt] = 0;
preAssetsAr.push(obj);
if (type == ATLAS_TYPE || type == FONT_TYPE)
{
totalLoads += 2;
}
else
{
totalLoads++;
}
obj = null;
}
public function startLoadAssets():Void
{
_busy = true;
var future_BMP:Future<BitmapData>;
var future_SND:Future<Sound>;
//var future_FNT:Future<Font>;
var future_TXT:Future<String>;
for (i in 0 ... preAssetsAr.length)
{
if (preAssetsAr[i].type == BITMAP_TYPE)
{
future_BMP = Assets.loadBitmapData(preAssetsAr[i].path[0], true, onLoaderComp);
preAssetsAr[i].loaders.push(future_BMP);
}
else if (preAssetsAr[i].type == AUDIO_TYPE)
{
future_SND = Assets.loadSound(preAssetsAr[i].path[0], true, onLoaderComp);
preAssetsAr[i].loaders.push(future_SND);
}
else if (preAssetsAr[i].type == FONT_TYPE)
{
future_BMP = Assets.loadBitmapData(preAssetsAr[i].path[0], true, onLoaderComp);
preAssetsAr[i].loaders.push(future_BMP);
future_TXT = Assets.loadText(preAssetsAr[i].path[1], onLoaderComp);
preAssetsAr[i].loaders.push(future_TXT);
}
else if (preAssetsAr[i].type == TEXT_TYPE)
{
future_TXT = Assets.loadText(preAssetsAr[i].path[0], onLoaderComp);
preAssetsAr[i].loaders.push(future_TXT);
}
else if (preAssetsAr[i].type == ATLAS_TYPE)
{
future_BMP = Assets.loadBitmapData(preAssetsAr[i].path[0], true, onLoaderComp);
preAssetsAr[i].loaders.push(future_BMP);
future_TXT = Assets.loadText(preAssetsAr[i].path[1], onLoaderComp);
preAssetsAr[i].loaders.push(future_TXT);
}
}
//trace(preAssetsAr);
}
private function onLoaderComp(res:Dynamic)
{
currentLoads++;
dispatchEvent(new AssetsLoaderEvents(AssetsLoaderEvents.CUR_ASSET_LOADED, {"currentLoads":currentLoads, "totalLoads":totalLoads}));
if (currentLoads == totalLoads)
{
dispatchEvent(new AssetsLoaderEvents(AssetsLoaderEvents.ALL_ASSETS_LOADED, {"ass":preAssetsAr}));
}
}
public function startNewLoad():Void
{
clearQue();
_busy = false;
}
public function clearQue()
{
for (i in 0 ... preAssetsAr.length)
{
preAssetsAr[i] = null;
}
preAssetsAr = [];
currentLoads = 0;
totalLoads = 0;
}
}
package assetsManager;
import openfl.events.Event;
import openfl.utils.Object;
/**
* ...
* @author George Chitaladze
*/
class AssetsLoaderEvents extends Event
{
inline public static var ALL_ASSETS_LOADED:String = "allAssetsLoaded";
inline public static var CUR_ASSET_LOADED:String = "curAssetLoaded";
inline public static var CUR_ASSET_PROGRESS_INFO:String = "curAssetProgressInfo";
inline public static var SEND_PROGRESS_TO_PRELOADER:String = "sendProgressToPreloader";
public var params:Object;
public function new(_type:String, _params:Object = null, _bubbles:Bool = false, _cancelable:Bool = false)
{
super(_type, _bubbles, _cancelable);
this.params = _params;
}
}
var loaderManager:LoaderManager = new LoaderManager();
loaderManager.addEventListener(AssetsLoaderEvents.ALL_ASSETS_LOADED, preAssetsLoaded);
loaderManager.setLoadAssets(["doubleSheet.png","doubleSheet.xml"], "doubleSheetAtl", LoaderManager.ATLAS_TYPE);
loaderManager.setLoadAssets(["audio/RegularWinSnd.mp3"], "RegularWinSnd", LoaderManager.AUDIO_TYPE);
loaderManager.startLoadAssets();
private function preAssetsLoaded(e:AssetsLoaderEvents):Void
{
var preAssetsAr:Array<Object> = e.params.ass;
haxe.Timer.delay(function()
{
for (i in 0 ... preAssetsAr.length)
{
if (preAssetsAr[i].type == LoaderManager.BITMAP_TYPE)
{
assetsManager.addTexture(preAssetsAr[i].valTxt, Texture.fromBitmapData(preAssetsAr[i].loaders[0].value, false));
preAssetsAr[i].loaders[0] = null;
}
else if (preAssetsAr[i].type == LoaderManager.AUDIO_TYPE)
{
assetsManager.addSound(preAssetsAr[i].valTxt, preAssetsAr[i].loaders[0].value);
preAssetsAr[i].loaders[0] = null;
}
else if (preAssetsAr[i].type == LoaderManager.FONT_TYPE)
{
TextField.registerBitmapFont(new BitmapFont(Texture.fromBitmapData(preAssetsAr[i].loaders[0].value, false),
Xml.parse(Std.string(preAssetsAr[i].loaders[1].value)).firstElement()), preAssetsAr[i].valTxt);
preAssetsAr[i].loaders[0] = null;
preAssetsAr[i].loaders[1] = null;
}
else if (preAssetsAr[i].type == LoaderManager.TEXT_TYPE)
{
}
else if (preAssetsAr[i].type == LoaderManager.ATLAS_TYPE)
{
assetsManager.addTextureAtlas(preAssetsAr[i].valTxt, new TextureAtlas(Texture.fromBitmapData(preAssetsAr[i].loaders[0].value, false),
Xml.parse(Std.string(preAssetsAr[i].loaders[1].value)).firstElement()));
preAssetsAr[i].loaders[0] = null;
preAssetsAr[i].loaders[1] = null;
}
}
loaderManager.startNewLoad();
preAssetsAr = null;
}, 0);
}