zip loader
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的更多相关文章
- USB Loader使用心得之游戏名称、简介、背景音乐
我在<WAD独立安装版USB Loader的下载和安装>(链接:http://www.cnblogs.com/duxiuxing/p/4255124.html)开头提到:“任何版本的USB ...
- DOL版USB Loader的下载和运行
下载 在Wii上面玩硬盘版游戏,自然少不了USB Loader,相关教程和下载资源在网上一搜就有. 我在官网(地址:http://gwht.wikidot.com/usb-loader)上找到了一个下 ...
- 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 ...
- java.lang.ClassNotFoundException: org.apache.catalina.loader.DevLoader
eclipse tomcat报错:org.apache.catalina.loader.DevLoader java.lang.ClassNotFoundException: org.apache.c ...
- 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 ...
- IOS开发创建开发证书及发布App应用(八)——使用Application Loader工具上传应用
8.使用Application Loader工具上传应用 继续第七步在iTunes所创建的应用,打开应用,如下图 点击详情按钮进去之后,单击右上角Ready to Upload Binary按钮,如下 ...
- MySQL5.7以上Zip版官方安装文档(选译)
前言 在windows上安装Zip版MySQL(选译) 学习mysql的朋友们会发现5.7+版本的mysql变得比以前难安装了许多(当然我们可以选择installer版本,但是这样总感觉对学习mysq ...
- Cordova打包vue项目生成Apk (解决cordova build android抛出的zip问题)
最近对vue前端框架情有独钟.但研究了一下怎么把vue项目打包成android apk来玩玩. 首先讲一下创建vue2.x项目.其实在之前的文章中都有写过,有兴趣的同学可以去看看.http://www ...
- Windows下的PHP 5.3.x安装 Zend Guard Loader
PHP5.3之后不再使用Zend Optimizer而是由Zend Guard Loader替换,而Zend Guard Loader安装比前者方便了很多,只有一个dll: 址:http://down ...
随机推荐
- SQL Server性能调优--优化建议(二)
序言 优化建议 库表的合理设计对项目后期的响应时间和吞吐量起到至关重要的地位,它直接影响到了业务所需处理的sql语句的复杂程度,为提高数据库的性能,更多的把逻辑主外键.级联删除.减少check约束.给 ...
- 三、angular7登录请求和路由带参传递
在 app.module.ts 中引入 HttpClientModule 并注入 import {HttpClientModule} from '@angular/common/http'; impo ...
- 支持向量机(一)----总述(点到平面的距离,Lagrange函数,Lagrange对偶)
概述:在对支持向量机的学习和使用过程中,遇到了许许多多的问题,通过查阅各种资料,也是逐一攻克了遇到的问题.感悟颇多,写此博文的目的是想以一个学习者的身份从一个刚接触支持向量机的角度去记录模型推到过程中 ...
- SpringCloud 教程 (四) docker部署spring cloud项目
一.docker简介 Docker是一个开源的引擎,可以轻松的为任何应用创建一个轻量级的.可移植的.自给自足的容器.开发者在笔记本上编译测试通过的容器可以批量地在生产环境中部署,包括VMs(虚拟机). ...
- 170830-关于JdbcTemplate的知识点
1.Spring提供的一个操作数据库的技术JdbcTemplate,是对Jdbc的封装.语法风格非常接近DBUtils. JdbcTemplate可以直接操作数据库,加快效率,而且学这个JdbcTem ...
- 大数据笔记(十九)——数据采集引擎Sqoop和Flume安装测试详解
一.Sqoop数据采集引擎 采集关系型数据库中的数据 用在离线计算的应用中 强调:批量 (1)数据交换引擎: RDBMS <---> Sqoop <---> HDFS.HBas ...
- 阿里云服务器+ubantu+nodejs 服务器基本配置流程
所有步骤在MAC 电脑环境下操作 一.配置环境 1.连接到远程服务器 1.购买阿里云ECS服务器,我选用的 ubantu 14.0.4 (64位),购买的时候输入的密码记录下来,没有设置的话可以随后在 ...
- 11年Macbook Air 安装win10 经验
11年macbook air安装win10折腾了好久,安装成功经验记录下. 1.使用BootCamp v6.0.6133 2.启动时U盘里面有两个引导Window和EFI,选择Windows启动.EF ...
- (二)Maven之坐标和依赖
目录 坐标 依赖 目录 坐标 引言: 坐标是依赖管理的基础,是构建的唯一标识. 组成元素: 使用groupId.artifactId.version.packaging.classifier标签即可定 ...
- struts2 基础
框架(frameWork):某一种应用的半成品 struts2: 表现层 处理与页面进行交互的相关功能 hibernate: 持久层 负责业务逻辑数据的持久化 spring: 业务层 负责复杂的业 ...