feat: 构建版本加载

This commit is contained in:
AncTe committed 2024-05-03 20:44:43 +08:00
1 parent 684fba1475
commit 4d5f14e8a5
14 files changed
+444 -391

No files matched your search

+145 -14
View File
@@ -26,6 +26,29 @@ interface ResourceMap {
zip: ZipResource;
}
interface CompressedLoadListItem {
type: keyof ResourceType;
name: string;
usage: string;
}
type CompressedLoadList = Record<string, CompressedLoadListItem[]>;
const types: Record<keyof ResourceType, JSZip.OutputType> = {
text: 'string',
buffer: 'arraybuffer',
image: 'blob',
material: 'blob',
audio: 'arraybuffer',
json: 'string',
zip: 'arraybuffer'
};
const base = import.meta.env.DEV ? '/' : '';
function toURL(uri: string) {
return import.meta.env.DEV ? uri : `${import.meta.env.BASE_URL}${uri}`;
}
export abstract class Resource<T = any> extends Disposable<string> {
type = 'none';
@@ -92,7 +115,7 @@ export class ImageResource extends Resource<HTMLImageElement> {
}
resolveURI(): string {
return `/${findURL(this.uri)}`;
return toURL(`${base}${findURL(this.uri)}`);
}
}
@@ -107,7 +130,7 @@ export class MaterialResource extends ImageResource {
}
override resolveURI(): string {
return `/project/materials/${findURL(this.uri)}`;
return toURL(`${base}project/materials/${findURL(this.uri)}`);
}
}
@@ -136,7 +159,7 @@ export class TextResource extends Resource<string> {
}
resolveURI(): string {
return `/${findURL(this.uri)}`;
return toURL(`${base}${findURL(this.uri)}`);
}
}
@@ -164,7 +187,7 @@ export class BufferResource extends Resource<ArrayBuffer> {
}
resolveURI(): string {
return `/${findURL(this.uri)}`;
return toURL(`${base}${findURL(this.uri)}`);
}
}
@@ -190,7 +213,7 @@ export class JSONResource<T = any> extends Resource<T> {
}
resolveURI(): string {
return `/${findURL(this.uri)}`;
return toURL(`${base}${findURL(this.uri)}`);
}
}
@@ -217,7 +240,7 @@ export class AudioResource extends Resource<HTMLAudioElement> {
}
resolveURI(): string {
return `/project/bgms/${findURL(this.uri)}`;
return toURL(`${base}project/bgms/${findURL(this.uri)}`);
}
}
@@ -228,7 +251,7 @@ export class ZipResource extends Resource<JSZip> {
* 注意后缀名不要是zip,不然有的浏览器会触发下载,而不是加载
*/
constructor(uri: string) {
super(uri);
super(uri, 'zip');
this.type = 'zip';
}
@@ -249,7 +272,7 @@ export class ZipResource extends Resource<JSZip> {
}
resolveURI(): string {
return `/${findURL(this.uri)}`;
return toURL(`${base}${findURL(this.uri)}`);
}
}
@@ -289,7 +312,7 @@ interface LoadEvent<T extends keyof ResourceType> extends EmitableEvent {
now: number,
total: number
) => void;
load: (resource: ResourceMap[T]) => void;
load: (resource: ResourceMap[T]) => void | Promise<void>;
loadStart: (resource: ResourceMap[T]) => void;
}
@@ -371,14 +394,14 @@ export class LoadTask<
);
});
this.emit('loadStart', this.resource);
load.then(() => {
return load.then(async value => {
// @ts-ignore
LoadTask.loadedTaskList.add(this);
this.loaded = totalByte;
LoadTask.loadedTask++;
this.emit('load', this.resource);
await Promise.all(this.emit('load', this.resource));
return value;
});
return load;
}
/**
@@ -493,7 +516,7 @@ export function loadDefaultResource() {
});
});
});
// tilseset
// tileset
data.main.tilesets.forEach(v => {
const res = LoadTask.add('image', `image/project/tilesets/${v}`);
res.once('load', res => {
@@ -563,4 +586,112 @@ export function loadDefaultResource() {
}
}
export function loadCompressedResource() {}
export async function loadCompressedResource() {
const data = await axios.get(toURL('loadList.json'), {
responseType: 'text'
});
const list: CompressedLoadList = JSON.parse(data.data);
const d = data_a1e2fb4a_e986_4524_b0da_9b7ba7c0874d;
// 对于bgm,直接按照原来的方式加载即可
d.main.bgms.forEach(v => {
const res = LoadTask.add('audio', `audio/${v}`);
Mota.r(() => {
res.once('loadStart', res => {
Mota.require('var', 'bgm').add(`bgms.${v}`, res.resource!);
});
});
});
// 对于区域内容,按照zip格式进行加载,然后解压处理
const autotiles: Partial<Record<AllIdsOf<'autotile'>, HTMLImageElement>> =
{};
const materialImages = core.materials.slice() as SelectKey<
MaterialImages,
HTMLImageElement
>[];
materialImages.push('keyboard');
const weathers: (keyof Weather)[] = ['fog', 'cloud', 'sun'];
Object.entries(list).forEach(v => {
const [uri, list] = v;
const res = LoadTask.add('zip', `zip/${uri}`);
res.once('load', resource => {
const res = resource.resource;
if (!res) return;
list.forEach(async v => {
const { type, name, usage } = v;
const asyncType = types[type];
const value = await res
.file(`${type}/${name}`)
?.async(asyncType);
if (!value) return;
// 图片类型的资源
if (type === 'image') {
const img = value as Blob;
const image = new Image();
image.src = URL.createObjectURL(img);
image.addEventListener('load', () => {
image.setAttribute('_width', image.width.toString());
image.setAttribute('_height', image.height.toString());
});
// 图片
if (usage === 'image') {
core.material.images.images[name as ImageIds] = image;
} else if (usage === 'tileset') {
// 额外素材
core.material.images.tilesets[name] = image;
} else if (usage === 'autotile') {
// 自动元件
autotiles[name.slice(0, -4) as AllIdsOf<'autotile'>] =
image;
const loading = Mota.require('var', 'loading');
loading.addAutotileLoaded();
loading.onAutotileLoaded(autotiles);
core.material.images.autotile[
name.slice(0, -4) as AllIdsOf<'autotile'>
] = image;
}
} else if (type === 'material') {
const img = value as Blob;
const image = new Image();
image.src = URL.createObjectURL(img);
image.addEventListener('load', () => {
image.setAttribute('_width', image.width.toString());
image.setAttribute('_height', image.height.toString());
});
// material
if (materialImages.some(v => name === v + '.png')) {
// @ts-ignore
core.material.images[
name.slice(0, -4) as SelectKey<
MaterialImages,
HTMLImageElement
>
] = image;
} else if (weathers.some(v => name === v + '.png')) {
// @ts-ignore
core.animateFrame.weather[v] = image;
} else {
}
}
if (usage === 'font') {
const font = value as ArrayBuffer;
document.fonts.add(new FontFace(name.slice(0, -4), font));
} else if (usage === 'sound') {
const sound = value as ArrayBuffer;
Mota.require('var', 'sound').add(`sounds.${name}`, sound);
} else if (usage === 'animate') {
const ani = value as string;
core.material.animates[name.slice(0, -8) as AnimationIds] =
core.loader._loadAnimate(ani);
}
});
});
});
}
+1 -1
View File
@@ -394,7 +394,7 @@ function handleAudioSetting<T extends number | boolean>(
) {
if (key === 'bgmEnabled') {
bgm.disable = !n;
core.checkBgm();
if (core.isPlaying()) core.checkBgm();
} else if (key === 'bgmVolume') {
bgm.volume = (n as number) / 100;
} else if (key === 'soundEnabled') {
+2
View File
@@ -101,6 +101,8 @@ type MaterialImages = {
keyboard: HTMLImageElement;
hero: HTMLImageElement;
icons: HTMLImageElement;
};
interface Material {
+4 -2
View File
@@ -10,11 +10,13 @@
@touchstart="touchStart(one.id)"
>
<span class="danmaku-info">
<like-filled
<span
class="danmaku-like-icon"
:liked="likedMap[one.id] < 0"
@click="postLike(one)"
/>
>
<like-filled />
</span>
<span class="danmaku-like-num">{{
Math.abs(likedMap[one.id])
}}</span>
+16 -8
View File
@@ -7,21 +7,27 @@
@click="openTool('css')"
>CSS</span
>
<font-colors-outlined
<span
class="danmaku-tool"
:open="fillOpened"
@click="openTool('fillColor')"
/>
<highlight-outlined
>
<font-colors-outlined />
</span>
<span
class="danmaku-tool"
:open="strokeOpened"
@click="openTool('strokeColor')"
/>
<meh-outlined
>
<highlight-outlined />
</span>
<span
class="danmaku-tool"
:open="iconOpened"
@click="openTool('icon')"
/>
>
<meh-outlined />
</span>
<div id="danmaku-input-div">
<a-input
id="danmaku-input-input"
@@ -33,11 +39,13 @@
@pressEnter="inputEnter()"
/>
</div>
<send-outlined
<span
class="danmaku-tool danmaku-post"
:posting="posting"
@click="send()"
/>
>
<send-outlined />
</span>
</div>
<Transition name="danmaku">
<div v-if="cssOpened" id="danmaku-css">
+28 -24
View File
@@ -26,7 +26,11 @@
<script lang="ts" setup>
import { onMounted, ref } from 'vue';
import { loadDefaultResource, LoadTask } from '@/core/common/resource';
import {
loadCompressedResource,
loadDefaultResource,
LoadTask
} from '@/core/common/resource';
import { GameUi } from '@/core/main/custom/ui';
import { formatSize } from '@/plugin/utils';
import { logger } from '@/core/common/logger';
@@ -48,32 +52,32 @@ const totalTask = ref(0);
let loadDiv: HTMLDivElement;
loadDefaultResource();
onMounted(async () => {
if (import.meta.env.DEV) loadDefaultResource();
else await loadCompressedResource();
LoadTask.onProgress(() => {
const loadingNum = [...LoadTask.taskList].filter(v => v.loading).length;
LoadTask.onProgress(() => {
const loadingNum = [...LoadTask.taskList].filter(v => v.loading).length;
loadedByte.value = LoadTask.loadedByte;
loadedPercent.value = parseFloat(
((LoadTask.loadedByte / LoadTask.totalByte) * 100).toFixed(2)
);
loading.value = loadingNum;
loaded.value = LoadTask.loadedTask;
totalByte.value = LoadTask.totalByte;
totalTask.value = LoadTask.totalTask;
});
loadedByte.value = LoadTask.loadedByte;
loadedPercent.value = parseFloat(
((LoadTask.loadedByte / LoadTask.totalByte) * 100).toFixed(2)
);
loading.value = loadingNum;
loaded.value = LoadTask.loadedTask;
totalByte.value = LoadTask.totalByte;
totalTask.value = LoadTask.totalTask;
});
LoadTask.load().then(async () => {
core.loader._loadMaterials_afterLoad();
core._afterLoadResources(props.callback);
logger.log(`Resource load end.`);
loadDiv.style.opacity = '0';
await sleep(1000);
fixedUi.close(props.num);
fixedUi.open('start');
});
onMounted(() => {
LoadTask.load().then(async () => {
core.loader._loadMaterials_afterLoad();
core._afterLoadResources(props.callback);
logger.log(`Resource load end.`);
loadDiv.style.opacity = '0';
await sleep(1000);
fixedUi.close(props.num);
fixedUi.open('start');
});
loadDiv = document.getElementById('load') as HTMLDivElement;
});
</script>
+3 -1
View File
@@ -1,7 +1,7 @@
<template>
<div id="start">
<div id="start-div">
<img id="background" src="/project/images/bg.jpg" />
<img id="background" :src="bg.src" />
<div id="start-main">
<div id="title">人类:开天辟地</div>
<div id="settings">
@@ -80,6 +80,8 @@ const props = defineProps<{
ui: GameUi;
}>();
const bg = core.material.images.images['bg.jpg'];
let startdiv: HTMLDivElement;
let start: HTMLDivElement;
let main: HTMLDivElement;
+9 -24
View File
@@ -20,10 +20,7 @@
>
<span id="status-lv">{{ lvName }}</span>
<div id="status-skill" class="status-item">
<img
src="/project/images/skill.png"
class="status-icon"
/>
<img :src="imgs['skill.png'].src" class="status-icon" />
<span>{{ skill }}</span>
<span
v-if="has(spring)"
@@ -33,7 +30,7 @@
>
</div>
<div id="status-hp" class="status-item">
<img src="/project/images/hp.png" class="status-icon" />
<img :src="imgs['hp.png'].src" class="status-icon" />
<span class="status-item-bold">{{
format(hero.hp!)
}}</span>
@@ -50,10 +47,7 @@
>
</div>
<div id="status-atk" class="status-item">
<img
src="/project/images/atk.png"
class="status-icon"
/>
<img :src="imgs['atk.png'].src" class="status-icon" />
<span class="status-item-bold">{{
format(hero.atk!)
}}</span>
@@ -64,28 +58,19 @@
>
</div>
<div id="status-def" class="status-item status-item-bold">
<img
src="/project/images/def.png"
class="status-icon"
/>
<img :src="imgs['def.png'].src" class="status-icon" />
<span>{{ format(hero.def!) }}</span>
</div>
<div id="status-mdef" class="status-item status-item-bold">
<img src="/project/images/IQ.png" class="status-icon" />
<img :src="imgs['IQ.png'].src" class="status-icon" />
<span>{{ format(hero.mdef!) }}</span>
</div>
<div id="status-money" class="status-item status-item-bold">
<img
src="/project/images/money.png"
class="status-icon"
/>
<img :src="imgs['money.png'].src" class="status-icon" />
<span>{{ format(hero.money!) }}</span>
</div>
<div id="status-exp" class="status-item status-item-bold">
<img
src="/project/images/exp.png"
class="status-icon"
/>
<img :src="imgs['exp.png'].src" class="status-icon" />
<span>{{ format(up) }}</span>
</div>
<div id="status-key" class="status-item status-item-bold">
@@ -143,8 +128,8 @@ import { has } from '@/plugin/utils';
let main: HTMLDivElement;
const items = core.flags.statusBarItems;
const icons = core.statusBar.icons;
const imgs = core.material.images.images;
const skillTree = Mota.Plugin.require('skillTree_g');
const width = ref(