[add] first

This commit is contained in:
2023-02-22 09:50:51 +08:00
commit 6260e95bc8
9053 changed files with 4492644 additions and 0 deletions

View File

@@ -0,0 +1,224 @@
import { _decorator, JsonAsset, Prefab, TextAsset, SpriteFrame, AudioClip, v3, Vec3 } from 'cc';
import { Singleton } from '../pattern/singleton';
import { Res } from './res';
import { ILoadMsg } from '../../logic/ui/ui-loading';
import { Msg } from '../msg/msg';
import { FxAutoRemove } from '../effect/fx-auto-remove';
import { loadTextures } from './res-texture';
const { ccclass, property } = _decorator;
@ccclass('ResCache')
export class ResCache extends Singleton {
private _json: { [name: string]: JsonAsset } = {};
private _prefab: { [name: string]: Prefab } = {};
private _txt: { [name: string]: TextAsset } = {};
private _sprite: { [name: string]: SpriteFrame } = {};
private _sound: { [name: string]: AudioClip } = {};
private _callback: Function | undefined;
msg: ILoadMsg | undefined;
public async load (callback: Function) {
this._callback = callback;
Msg.on('msg_check_res_cache_end', this.checkEnd.bind(this));
Res.loadJson('data/data-res-cache', async (err, asset) => {
if (err) {
console.error('Load cache res error:', err);
return;
}
this.msg = {
id: 1,
action: 'load cache',
current: 'resource',
wait_count: 1,
count: 1
}
if (!asset || !asset.json) {
console.error('resource cache data is null', asset)
return;
}
const jsonPrefab = asset.json['prefab'];
if (jsonPrefab) ResCache.Instance.loadPrefab(jsonPrefab);
ResCache.Instance.loadJson(asset.json['json']);
ResCache.Instance.loadSprite(asset.json['sprite']);
ResCache.Instance.loadSound(asset.json['sound']);
Msg.emit('msg_loading', this.msg);
});
}
public addLoad () {
this.msg!.wait_count++;
this.msg!.count++;
//console.log('add load count:', this.msg?.count, 'wait count:', this.msg?.wait_count,);
}
public removeLoad () {
this.msg!.wait_count--;
//console.log('remove load wait count:', this.msg?.wait_count, ' count:', this.msg?.count);
}
public getJson (name: string) {
const ret = this._json[name];
if (ret) {
return ret;
} else {
console.error('Res cache not find json res:', name);
return null;
}
}
public getPrefab (name: string) {
const ret = this._prefab[name];
if (ret) {
return ret;
} else {
console.error('Res cache not find prefab res:', name);
return undefined;
}
}
public getTxt (name: string) {
const ret = this._txt[name];
if (ret) {
return ret;
} else {
console.error('Res cache not find text res:', name);
}
}
public getSprite (name: string) {
const ret = this._sprite[name];
if (ret !== undefined) {
return ret;
} else {
console.error('Res cache not find sprite res:', name);
}
}
public getSound (name: string) {
const ret = this._sound[name];
if (ret !== undefined) {
return ret;
} else {
console.error('Res cache not find sound res:', name);
}
}
public setJson (asset: any[]) {
asset.forEach(element => {
this._json[element.name] = element;
});
}
public preloadFxShader (prefab: Prefab) {
if (prefab.name.includes('fx_')) {
const preload = Res.inst(prefab, undefined, Vec3.ZERO);
preload.addComponent(FxAutoRemove);
}
}
public setPrefab (asset: any[]) {
asset.forEach(element => {
this.preloadFxShader(element);
this._prefab[element.name] = element;
});
}
public setText (asset: any[]) {
asset.forEach(element => {
this._txt[element.name] = element;
});
}
public setSprite (asset: any[]) {
asset.forEach(element => {
this._sprite[element.name] = element;
});
}
public setSound (asset: any[]) {
asset.forEach(element => {
this._sound[element.name] = element;
});
}
public loadJson (paths: string[]) {
paths.forEach(element => {
this.addLoad();
Res.loadDirJson(element, (err, asset) => {
if (asset) {
ResCache.Instance.setJson(asset);
this.removeLoad();
}
});
});
}
public loadPrefab (paths: string[]) {
paths.forEach(element => {
this.addLoad();
Res.loadDirPrefab(element, (err, asset) => {
if (asset) {
ResCache.Instance.setPrefab(asset);
this.removeLoad();
}
});
});
}
public loadText (paths: string[]) {
paths.forEach(element => {
this.addLoad();
Res.loadDirText(element, (err, asset) => {
if (asset) {
ResCache.Instance.setText(asset);
this.removeLoad();
}
})
});
}
public loadSprite (paths: string[]) {
paths.forEach(element => {
this.addLoad();
Res.loadDirSprite(element, (err, asset) => {
if (asset) {
ResCache.Instance.setSprite(asset);
this.removeLoad();
}
})
});
}
public loadSound (paths: string[]) {
paths.forEach(element => {
this.addLoad();
Res.loadDirSound(element, (err, asset) => {
if (asset) {
ResCache.Instance.setSprite(asset);
this.removeLoad();
}
})
});
}
public checkEnd (): void {
if (this._callback) {
if (Res.count <= 0) {
this._callback();
this._callback = undefined;
}
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "4a88625e-6c68-4351-9f40-f2023a99c8ce",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,73 @@
/*
Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
import { _decorator, Component, Node } from 'cc';
import { Msg } from '../msg/msg';
import { ILoadMsg } from '../../logic/ui/ui-loading';
import { Level } from '../../logic/level/level';
const { ccclass, property } = _decorator;
@ccclass('ResDestroy')
export class ResDestroy extends Component {
isDestroy = false;
msg:ILoadMsg | undefined;
start() {
Msg.bind('msg_destroy_res', ()=>{
this.isDestroy = true;
const length = this.node.children.length - 1;
this.msg = {
id:0,
action:'destroy',
current:' objects node. ',
wait_count:length,
count:length,
}
Msg.emit('msg_loading', this.msg);
if ((globalThis as any).ppSettings) {
(globalThis as any).ppSettings.passVersion++;
console.log('msg_destroy_res: passVersion - ' + (globalThis as any).ppSettings.passVersion)
}
}, this);
}
update(deltaTime: number) {
if(this.isDestroy) {
const length = this.node.children.length - 1;
this.msg!.wait_count = length;
//this.msg!.current = this.node.children[length].name;
if(length <= -1) {
this.isDestroy = false;
console.log('res is destroy');
return;
}
this.node.children[length].destroy();
}
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "d0393dcf-89ad-4adc-a21b-512eaf3e707c",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,20 @@
import { _decorator, Component, Node, JsonAsset, Prefab } from 'cc';
import { ResCache } from './res-cache';
const { ccclass, property } = _decorator;
@ccclass('ResEditorCache')
export class ResEditorCache extends Component {
@property(JsonAsset)
jsonAssets: JsonAsset[] | null = [];
@property(Prefab)
prefabs: Prefab[] = [];
init() {
ResCache.Instance.setJson(this.jsonAssets);
ResCache.Instance.setPrefab(this.prefabs);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "e457eca2-9a9d-4178-842a-8a2f37412fec",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,157 @@
import { _decorator, Component, Node, Prefab, Pool } from 'cc';
import { Msg } from '../msg/msg';
import { Singleton } from '../pattern/singleton';
import { Res } from './res';
import { ResCache } from './res-cache';
const { ccclass, property } = _decorator;
@ccclass('ResPool')
export class ResPool extends Singleton {
_root:Node = null;
_pool = new Map<string, pool>();
public init() {
}
public initPool(root:Node) {
this._root = root;
Msg.on('pool_recycle', this.pool_recycle.bind(this));
}
pool_recycle() {
this._pool.forEach(element => {
element.recycle();
});
}
public pop(name:string) {
if (!this._pool.has(name)) {
this.newPool(name);
}
return this._pool.get(name).pop();
}
public push(obj:Node) {
console.log(obj.name);
this._pool.get(obj.name).push(obj);
}
public pushByName(name:string, obj:Node) {
console.log(name, obj);
this._pool.get(name).push(obj);
}
public newPool(name:string) {
var newpool = new pool(name, this._root);
this._pool.set(name, newpool);
}
}
export class pool {
_index:number = 0;
_items:Node[] = [];
_state:number[] = [];
_max:number = 0;
_realMax:number = 0;
_name:string = '';
_prefab:Prefab = null;
_root:Node = null;
public constructor(name:string, root:Node) {
this.IncreaseSize();
this._name = name;
this._prefab = ResCache.Instance.getPrefab(this._name);
this._root = root;
}
recycle() {
for(let i = 0; i < this._max; i++) {
this._state[i] = 1;
this._items[i].active = false;
this._items[i].setPosition(10000, 10000, 10000);
}
}
public pop():Node {
for(let i = 0; i < this._max; i++) {
var state = this._state[this._index];
if (state === 1) {
var n = this._items[this._index];
this._state[this._index] = 2;
n.active = true;
this.next();
return n;
}
this.next();
}
// Not find res.
// Create new one.
return this.newRes();
}
public next() {
this._index++;
if (this._index >= this._max) this._index = 0;
}
public push(obj:Node) {
obj.active = false;
var poolindex = obj['pool_index'];
this._state[poolindex] = 1;
}
public newRes() {
// Check & Increase pool size
this.checkAndIncreaseSize();
// Inst new node.
const newNode = Res.inst(this._prefab, this._root);
this._index = this._max;
this._max++;
if (newNode['pool_index'] !== undefined) {
console.log('pool create error.');
}
newNode['pool_index'] = this._index;
this._items[this._index] = newNode;
this._state[this._index] = 2;
return newnode;
}
public checkAndIncreaseSize() {
// Check the size is full.
// Judge last items is not null.
var last = this._items.length - 1;
if (this._items[last] === null)
return;
// Increase size.
this.IncreaseSize();
}
public IncreaseSize() {
this._realMax += 8;
this._items.push(null, null, null, null, null, null, null, null);
this._state.push(0, 0, 0, 0, 0, 0, 0, 0)
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "b49eb5c9-6ae3-4bee-9899-e2152f7b6e7f",
"files": [],
"subMetas": {},
"userData": {}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "0aad6cb2-4a0e-490e-bcb2-d0b327f64fef",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,139 @@
import { _decorator, resources, Node, Asset, error, Constructor, Prefab, instantiate, TextAsset, JsonAsset, Texture2D, EffectAsset, AudioClip, AnimationClip, ImageAsset, SpriteFrame, SpriteAtlas, Mesh, Material, Skeleton, SceneAsset, Vec3, director } from 'cc';
import { Msg } from '../msg/msg';
export class Res {
public static count: number = 0;
public static load<T extends Asset>(path: string, type: Constructor<T> | null, cb?: (err: Error | null, asset?: T | null)=>void) {
this.count++;
resources.load(path, type, function(err, res){
if (err){
error(path,err.message || err);
Msg.emit('msg_res_error');
}
if (cb) {
cb(err, res);
}
Res.count--;
Msg.emit('msg_check_res_cache_end');
});
}
public static loadJson(path: string, cb?: (err: Error | null, asset?: JsonAsset | null)=>void) {
this.load(path, JsonAsset, cb);
}
public static loadTxt(path: string, cb?: (err: Error | null, asset?: TextAsset | null)=>void) {
this.load(path, TextAsset, cb);
}
public static loadPrefab(path: string, cb?: (err: Error | null, asset?: Prefab | null)=>void) {
this.load(path, Prefab, cb);
}
public static loadTex2D(path: string, cb?: (err: Error | null, asset?: Texture2D | null)=>void) {
this.load(path, Texture2D, cb);
}
public static loadImage(path: string, cb?: (err: Error | null, asset?: ImageAsset | null)=>void) {
this.load(path, ImageAsset, cb);
}
public static loadSprite(path: string, cb?: (err: Error | null, asset?: SpriteFrame | null)=>void) {
this.load(path, SpriteFrame, cb);
}
public static loadSpriteAtlas(path: string, cb?: (err: Error | null, asset?: SpriteAtlas | null)=>void) {
this.load(path, SpriteAtlas, cb);
}
public static loadEffect(path: string, cb?: (err: Error | null, asset?: EffectAsset | null)=>void) {
this.load(path, EffectAsset, cb);
}
public static loadAudio(path: string, cb?: (err: Error | null, asset?: AudioClip | null)=>void) {
this.load(path, AudioClip, cb);
}
public static loadAnimationClip(path: string, cb?: (err: Error | null, asset?: AnimationClip | null)=>void) {
this.load(path, AnimationClip, cb);
}
public static loadMesh(path: string, cb?: (err: Error | null, asset?: Mesh | null)=>void) {
this.load(path, Mesh, cb);
}
public static loadMateiral(path: string, cb?: (err: Error | null, asset?: Material | null)=>void) {
this.load(path, Material, cb);
}
public static loadSkeleton(path: string, cb?: (err: Error | null, asset?: Skeleton | null)=>void) {
this.load(path, Skeleton, cb);
}
public static loadScene(path: string, cb?: (err: Error | null, asset?: SceneAsset | null)=>void) {
this.load(path, SceneAsset, cb);
}
public static inst(asset: Prefab, root:Node | undefined = undefined, pos:Vec3 = Vec3.ZERO) : Node {
const instObj = instantiate(asset);
if (root === undefined) {
director.getScene()?.addChild(instObj);
}else{
instObj.setParent(root);
}
instObj.setPosition(pos);
instObj.setScale(Vec3.ONE);
return instObj;
}
public static instNode(node:Node, root:Node | undefined = undefined, pos:Vec3 = Vec3.ZERO) : Node {
const instObj = instantiate(node);
if (root === undefined) {
director.getScene()?.addChild(instObj);
}else{
instObj.setParent(root);
}
instObj.setPosition(pos);
instObj.setScale(Vec3.ONE);
return instObj;
}
public static loadDir<T extends Asset>(path: string, type: Constructor<T> | null, cb?: (err: Error | null, asset?: T[] | null)=>void) {
this.count++;
resources.loadDir(path, type, function(err, res){
if (err){
error(err.message || err);
Msg.emit('msg_res_error');
}
if (cb) {
cb(err, res);
}
Res.count--;
Msg.emit('msg_check_res_cache_end');
});
}
public static loadDirJson(path:string, cb?: (err: Error | null, asset?: JsonAsset[] | null)=>void) {
this.loadDir(path, JsonAsset, cb);
}
public static loadDirPrefab(path:string, cb?: (err: Error | null, asset?: Prefab[] | null)=>void) {
this.loadDir(path, Prefab, cb);
}
public static loadDirText(path:string, cb?: (err: Error | null, asset?: TextAsset[] | null)=>void) {
this.loadDir(path, TextAsset, cb);
}
public static loadDirSprite(path:string, cb?: (err: Error | null, asset?: SpriteFrame[] | null)=>void) {
this.loadDir(path, SpriteFrame, cb);
}
public static loadDirSound(path:string, cb?: (err:Error | null, asset?: AudioClip[] | null)=>void) {
this.loadDir(path, AudioClip, cb);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "746d9d21-41cd-4743-9986-0429d0dbe7ca",
"files": [],
"subMetas": {},
"userData": {}
}