GS.ZipAssetLoader = function(audioContext) {
this.audioContext = audioContext,
this.objLoader = new THREE.OBJLoader,
this.mapLoader = new GS.MapLoader,
this.fontLoader = new GS.FontLoader,
this.fontLoader.fontPath = "",
this.zipPath = "assets.zip",
this.path = {},
this.path[GS.AssetTypes.Texture] = "textures/",
this.path[GS.AssetTypes.CubeTexture] = "textures/",
this.path[GS.AssetTypes.UIWidget] = "textures/widgets/",
this.path[GS.AssetTypes.Mesh] = "meshes/",
this.path[GS.AssetTypes.Sound] = "sounds/",
this.path[GS.AssetTypes.Map] = "maps/",
this.path[GS.AssetTypes.Script] = "scripts/",
this.path[GS.AssetTypes.MusicTrack] = "music/",
this.reset()
}
GS.ZipAssetLoader.prototype = {
reset: function() {
this.loaded = !1,
this.assetsToLoad = 0,
this.queue = [],
this.assets = {},
this.assets[GS.AssetTypes.Texture] = {},
this.assets[GS.AssetTypes.CubeTexture] = {},
this.assets[GS.AssetTypes.UIWidget] = {},
this.assets[GS.AssetTypes.Mesh] = {},
this.assets[GS.AssetTypes.Sound] = {},
this.assets[GS.AssetTypes.Map] = {},
this.assets[GS.AssetTypes.Script] = {},
this.assets[GS.AssetTypes.MusicTrack] = {}
},
init: function() {
for (var j in GS.AssetTypes) {
var assetDict = GS.Assets[GS.AssetTypes[j]];
for (var i in assetDict)
this.add(i, assetDict[i].filename, GS.AssetTypes[j])
}
},
add: function(name, filename, type) {
this.queue.push({
name: name,
filename: filename,
type: type
}),
this.assetsToLoad++
},
loadZip: function() {
var that = this
, xhr = new XMLHttpRequest;
xhr.open("GET", this.zipPath, !0),
xhr.responseType = "arraybuffer",
xhr.onreadystatechange = function(e) {
4 == this.readyState && 200 == this.status && (that.zip = new JSZip(this.response),
setTimeout(function() {
that._load()
}, 0))
}
,
xhr.onprogress = function(e) {
that.zipOnProgress(e)
}
,
xhr.send()
},
_load: function() {
var that = this;
this.totalAssets = this.queue.length + 1,
this.assetsToLoad++,
this.fontLoader.load("hudFont", GS.CustomFontFile, function() {
that.checkIfFullyLoaded()
});
for (var asset; this.queue.length > 0; )
asset = this.queue.pop(),
asset.name in this.assets[asset.type] ? this.assetsToLoad-- : this.loadAsset(asset)
},
load: function() {
this.loadZip()
},
loadAsset: function(asset) {
switch (asset.type) {
case GS.AssetTypes.Texture:
this.loadTexture(asset.name, asset.filename);
break;
case GS.AssetTypes.CubeTexture:
this.loadCubeTexture(asset.name, asset.filename);
break;
case GS.AssetTypes.UIWidget:
this.loadUIWidget(asset.name, asset.filename);
break;
case GS.AssetTypes.Mesh:
this.loadMesh(asset.name, asset.filename);
break;
case GS.AssetTypes.Sound:
this.loadSound(asset.name, asset.filename, GS.AssetTypes.Sound);
break;
case GS.AssetTypes.Map:
this.loadMap(asset.name, asset.filename);
break;
case GS.AssetTypes.Script:
this.loadScript(asset.name, asset.filename);
break;
case GS.AssetTypes.MusicTrack:
this.loadSound(asset.name, asset.filename, GS.AssetTypes.MusicTrack)
}
},
getImageFromZip: function(path) {
var buffer = this.zip.file(path).asArrayBuffer()
, blob = new Blob([buffer],{
type: "image/png"
})
, urlCreator = window.URL || window.webkitURL
, imageUrl = urlCreator.createObjectURL(blob)
, img = new Image;
return img.src = imageUrl,
img
},
getTextureFromZip: function(path) {
var img = this.getImageFromZip(path)
, tex = new THREE.Texture(img);
return tex.needsUpdate = !0,
tex
},
loadTexture: function(name, filename) {
var path = this.path[GS.AssetTypes.Texture] + filename;
this.assets[GS.AssetTypes.Texture][name] = this.getTextureFromZip(path),
this.checkIfFullyLoaded()
},
loadCubeTexture: function(name, folder) {
var path = this.path[GS.AssetTypes.CubeTexture] + folder + "/"
, tex = new THREE.Texture;
tex.image = [],
tex.flipY = !1;
for (var i = 0; i < GS.CubeTextureNames.length; i++)
tex.image[i] = this.getImageFromZip(path + GS.CubeTextureNames[i] + GS.CubeTextureExtension);
tex.needsUpdate = !0,
this.assets[GS.AssetTypes.CubeTexture][name] = tex,
this.checkIfFullyLoaded()
},
loadUIWidget: function(name, filename) {
var path = this.path[GS.AssetTypes.UIWidget] + filename;
this.assets[GS.AssetTypes.UIWidget][name] = this.getImageFromZip(path),
this.checkIfFullyLoaded()
},
loadMesh: function(name, filename) {
var path = this.path[GS.AssetTypes.Mesh] + filename
, text = this.zip.file(path).asText();
this.assets[GS.AssetTypes.Mesh][name] = this.objLoader.parse(text).children[0],
this.checkIfFullyLoaded()
},
loadSound: function(name, filename, assetType) {
var that = this
, path = this.path[assetType] + filename
, encodedBuffer = this.zip.file(path).asArrayBuffer();
this.audioContext.decodeAudioData(encodedBuffer, function(buffer) {
that.assets[assetType][name] = buffer,
that.checkIfFullyLoaded()
})
},
loadMap: function(name, filename) {
var path = this.path[GS.AssetTypes.Map] + filename;
"testMap" !== name ? this.assets[GS.AssetTypes.Map][name] = this.zip.file(path).asText() : GAME.isTestMap() && (this.assets[GS.AssetTypes.Map][name] = this.mapLoader.loadTestMap()),
this.checkIfFullyLoaded()
},
loadScript: function(name, filename) {
var path = this.path[GS.AssetTypes.Script] + filename
, scriptTag = document.createElement("script");
scriptTag.text = this.zip.file(path).asText(),
document.body.appendChild(scriptTag),
this.checkIfFullyLoaded()
},
zipOnProgress: function(e) {
var percentLoaded = e.loaded / e.total * 50;
this.dispatchEvent({
type: "progress",
percentLoaded: percentLoaded.toFixed(0)
})
},
checkIfFullyLoaded: function() {
this.assetsToLoad--;
var percentLoaded = 100 * (.5 + (this.totalAssets - this.assetsToLoad) / this.totalAssets * .5);
this.dispatchEvent({
type: "progress",
percentLoaded: percentLoaded.toFixed(0)
}),
0 === this.assetsToLoad && (this.loaded = !0,
this.dispatchEvent({
type: "load",
percentLoaded: 100,
assets: this.assets
}))
}
}

zip loader的更多相关文章

  1. USB Loader使用心得之游戏名称、简介、背景音乐

    我在<WAD独立安装版USB Loader的下载和安装>(链接:http://www.cnblogs.com/duxiuxing/p/4255124.html)开头提到:“任何版本的USB ...

  2. DOL版USB Loader的下载和运行

    下载 在Wii上面玩硬盘版游戏,自然少不了USB Loader,相关教程和下载资源在网上一搜就有. 我在官网(地址:http://gwht.wikidot.com/usb-loader)上找到了一个下 ...

  3. Couldn't load libPassword from loader:NDK开发中C文件编译成cpu对应的so类库时,找不到类库报错的原因之一

    LogCat输出: 03-03 12:42:32.665: E/AndroidRuntime(32432): FATAL EXCEPTION: main03-03 12:42:32.665: E/An ...

  4. java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader

    eclipse tomcat报错:org.apache.catalina.loader.DevLoader java.lang.ClassNotFoundException: org.apache.c ...

  5. Taxonomy of class loader problems encountered when using Jakarta Commons Logging(转)

    Acknowledgments I would like to thank Jacob Kjome for reviewing early drafts of this document. His c ...

  6. IOS开发创建开发证书及发布App应用(八)——使用Application Loader工具上传应用

    8.使用Application Loader工具上传应用 继续第七步在iTunes所创建的应用,打开应用,如下图 点击详情按钮进去之后,单击右上角Ready to Upload Binary按钮,如下 ...

  7. MySQL5.7以上Zip版官方安装文档(选译)

    前言 在windows上安装Zip版MySQL(选译) 学习mysql的朋友们会发现5.7+版本的mysql变得比以前难安装了许多(当然我们可以选择installer版本,但是这样总感觉对学习mysq ...

  8. Cordova打包vue项目生成Apk (解决cordova build android抛出的zip问题)

    最近对vue前端框架情有独钟.但研究了一下怎么把vue项目打包成android apk来玩玩. 首先讲一下创建vue2.x项目.其实在之前的文章中都有写过,有兴趣的同学可以去看看.http://www ...

  9. Windows下的PHP 5.3.x安装 Zend Guard Loader

    PHP5.3之后不再使用Zend Optimizer而是由Zend Guard Loader替换,而Zend Guard Loader安装比前者方便了很多,只有一个dll: 址:http://down ...

随机推荐

  1. mysql JOIN关键字 语法

    mysql JOIN关键字 语法 作用:用于根据两个或多个表中的列之间的关系,从这些表中查询数据.大理石量具 说明:数据库中的表可通过键将彼此联系起来.主键(Primary Key)是一个列,在这个列 ...

  2. Codeforces 878A - Short Program(位运算)

    原题链接:http://codeforces.com/problemset/problem/878/A 题意:给出n个位运算操作, 化简这些操作, 使化简后的操作次数不多于5步. 思路:我们可以对二进 ...

  3. I - Rake It In

    题目链接:https://nanti.jisuanke.com/t/A1538 题意:给一个4*4的方阵,k个回合,a和b轮流选一个2*2的矩阵和,a要使和最大,b要使和最小,选完后2*2矩阵要逆时针 ...

  4. 树莓派安装配置teamviewer host

    过程: 下载teamviewer,直接到teamviewer的官网上直接下载 下载完后,安装teamviewerw sudo dpkg -i 下载的文件的路径+文件名 安装完后会提示存在依赖问题,修复 ...

  5. [CSP-S模拟测试]:集合论(模拟)

    题目传送门(内部题73) 输入格式 输入文件$jihe.in$ 第一行一个整数$m$,表示操作的次数. 接下来$m$行,每行描述一个操作. 每行的开始都是一个数字,$1,2,3,4$依次代表$unio ...

  6. 手机H5设计尺寸

    手机型号 导航栏和状态栏的高度 宽度 高度 可视区域高度 iPhone 4 (4, 4S) 64px 320px 480px 416px iPhone 5 (5c, 5s) 64px 320px 56 ...

  7. C#中查找或结束程序域中的主、子进程

    有时候,我们的程序需要启动一些子进程,如嵌入的图形程序. 当启动一个进程后,获得这个进程信息Process,然后其内部在某个时刻启动了一个子进程,这个时候就涉及程序域和进程树的概念.当我们通过非正常操 ...

  8. ASP 解析json

    第一个方法是使用 JScript : <script language="jscript" runat="server"> Array.protot ...

  9. leetcode 39. 组合总和(python)

    给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的数字可以无限制重复被选 ...

  10. Django路由小知识

    from django.urls import path,re_path from app01 import views urlpatterns = [ re_path(r'^articles/200 ...