mirror of
https://gitee.com/devil_root/snake.git
synced 2025-10-10 00:56:28 +00:00
the demo
This commit is contained in:
12
assets/Scripts/core/audio.meta
Normal file
12
assets/Scripts/core/audio.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "83df4394-0767-4beb-b866-a462e2cdc935",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/conf.meta
Normal file
12
assets/Scripts/core/conf.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "208bfb92-0ad4-43a2-aee6-f6ac8ade12f1",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/data.meta
Normal file
12
assets/Scripts/core/data.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "52f9fe41-4b8f-4a28-a615-9738f52a1974",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/event.meta
Normal file
12
assets/Scripts/core/event.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "c447163d-4e7c-4993-ae73-1daf18176e15",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
139
assets/Scripts/core/event/EventManager.ts
Normal file
139
assets/Scripts/core/event/EventManager.ts
Normal file
@@ -0,0 +1,139 @@
|
||||
import ArrayUtil from "../utils/ArrayUtil";
|
||||
|
||||
interface EventInfo {
|
||||
func: Function;
|
||||
target: any;
|
||||
}
|
||||
|
||||
class EventInfoList {
|
||||
infos: EventInfo[] = null;
|
||||
constructor() {
|
||||
this.infos = [];
|
||||
}
|
||||
|
||||
has(cb: Function, target: any) {
|
||||
if (this.infos.length <= 0) {
|
||||
return false;
|
||||
}
|
||||
for (let i = 0; i < this.infos.length; i++) {
|
||||
if (this.infos[i].func === cb && this.infos[i].target === target) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* 添加事件
|
||||
* @param cb 回调
|
||||
* @param target 目标
|
||||
*/
|
||||
add(cb: Function, target: any) {
|
||||
if (this.has(cb, target)) {
|
||||
console.log('event repeat--------')
|
||||
return;
|
||||
}
|
||||
let info = Object.create(null);
|
||||
info.func = cb;
|
||||
info.target = target;
|
||||
|
||||
this.infos.push(info);
|
||||
}
|
||||
|
||||
/**移除指定 */
|
||||
remove(cb: Function, target: any) {
|
||||
|
||||
if (this.infos.length <= 0) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < this.infos.length; i++) {
|
||||
if (this.infos[i].func === cb && this.infos[i].target === target) {
|
||||
ArrayUtil.fastRemoveAt(this.infos, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
/**移除目标所有 */
|
||||
removeByTarget(target: any) {
|
||||
if (this.infos.length <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < this.infos.length; i++) {
|
||||
if (this.infos[i].target === target) {
|
||||
ArrayUtil.fastRemoveAt(this.infos, i);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**移除所有 */
|
||||
removeAll() {
|
||||
this.infos.forEach(info => {
|
||||
info.func = null;
|
||||
info.target = null;
|
||||
})
|
||||
this.infos = [];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export class EventManager {
|
||||
private _evtMap: { [k: string]: EventInfoList } = Object.create(null);
|
||||
on(evtType: string, cb: Function, target?: any) {
|
||||
let list = this._evtMap[evtType];
|
||||
if (!list) {
|
||||
list = new EventInfoList();
|
||||
this._evtMap[evtType] = list;
|
||||
}
|
||||
list.add(cb, target);
|
||||
}
|
||||
|
||||
emit(evtType: string, ...params: any) {
|
||||
let list = this._evtMap[evtType];
|
||||
if (!list || list.infos.length <= 0) {
|
||||
return;
|
||||
}
|
||||
for (let i = 0; i < list.infos.length; i++) {
|
||||
let info = list.infos[i];
|
||||
if (info.func) {
|
||||
info.func.apply(info.target, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**移除 指定*/
|
||||
off(evtType: string, cb: Function, target?: any) {
|
||||
let list = this._evtMap[evtType];
|
||||
if (!list) return;
|
||||
list.remove(cb, target);
|
||||
}
|
||||
|
||||
/**事件名或目标移除 */
|
||||
offByKeyOrTarget(key: string | Object) {
|
||||
let list: EventInfoList = null;
|
||||
if (typeof key == 'string') {
|
||||
list = this._evtMap[key];
|
||||
if (list) {
|
||||
list.removeAll();
|
||||
}
|
||||
} else {
|
||||
|
||||
for (let k in this._evtMap) {
|
||||
list = this._evtMap[k];
|
||||
list.removeByTarget(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* [1] Class member could be defined like this.
|
||||
* [2] Use `property` decorator if your want the member to be serializable.
|
||||
* [3] Your initialization goes here.
|
||||
* [4] Your update function goes here.
|
||||
*
|
||||
* Learn more about scripting: https://docs.cocos.com/creator/3.4/manual/zh/scripting/
|
||||
* Learn more about CCClass: https://docs.cocos.com/creator/3.4/manual/zh/scripting/ccclass.html
|
||||
* Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.4/manual/zh/scripting/life-cycle-callbacks.html
|
||||
*/
|
11
assets/Scripts/core/event/EventManager.ts.meta
Normal file
11
assets/Scripts/core/event/EventManager.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "8069a6da-1c33-4e0f-bd20-cbe42cdf93c0",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
12
assets/Scripts/core/gui.meta
Normal file
12
assets/Scripts/core/gui.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "a60ec33d-05ca-4c72-af63-b9114ccf5f19",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/libs.meta
Normal file
12
assets/Scripts/core/libs.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "592a89e6-ccc0-4774-9ee7-9ae076b12106",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/libs/encrypt.meta
Normal file
12
assets/Scripts/core/libs/encrypt.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "51ab9e9b-4ad5-4d65-aa54-46a3cdddf83d",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/resLoad.meta
Normal file
12
assets/Scripts/core/resLoad.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "c84048a4-78cc-469b-b4de-3eacd1548372",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
12
assets/Scripts/core/utils.meta
Normal file
12
assets/Scripts/core/utils.meta
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"importer": "directory",
|
||||
"imported": true,
|
||||
"uuid": "226f498a-54ae-42df-a53b-444ed7d701dc",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"compressionType": {},
|
||||
"isRemoteBundle": {}
|
||||
}
|
||||
}
|
120
assets/Scripts/core/utils/ArrayUtil.ts
Normal file
120
assets/Scripts/core/utils/ArrayUtil.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* @Author: dgflash
|
||||
* @Date: 2021-08-11 16:41:12
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-01-24 15:00:52
|
||||
*/
|
||||
/**
|
||||
* 数组工具
|
||||
*/
|
||||
export default class ArrayUtil {
|
||||
/** 去重 */
|
||||
public static noRepeated(arr: any[]) {
|
||||
var res = [arr[0]];
|
||||
for (var i = 1; i < arr.length; i++) {
|
||||
var repeat = false;
|
||||
for (var j = 0; j < res.length; j++) {
|
||||
if (arr[i] == res[j]) {
|
||||
repeat = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!repeat) {
|
||||
res.push(arr[i]);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制二维数组
|
||||
* @param array 目标数组
|
||||
*/
|
||||
public static copy2DArray(array: any[][]): any[][] {
|
||||
let newArray: any[][] = [];
|
||||
for (let i = 0; i < array.length; i++) {
|
||||
newArray.push(array[i].concat());
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fisher-Yates Shuffle 随机置乱算法
|
||||
* @param array 目标数组
|
||||
*/
|
||||
public static fisherYatesShuffle(array: any[]): any[] {
|
||||
let count = array.length;
|
||||
while (count) {
|
||||
let index = Math.floor(Math.random() * count--);
|
||||
let temp = array[count];
|
||||
array[count] = array[index];
|
||||
array[index] = temp;
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/**
|
||||
* 混淆数组
|
||||
* @param array 目标数组
|
||||
*/
|
||||
public static confound(array: []): any[] {
|
||||
let result = array.slice().sort(() => Math.random() - .5);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数组扁平化
|
||||
* @param array 目标数组
|
||||
*/
|
||||
public static flattening(array: any[]) {
|
||||
for (; array.some(v => Array.isArray(v));) { // 判断 array 中是否有数组
|
||||
array = [].concat.apply([], array); // 压扁数组
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
/** 删除数组中指定项 */
|
||||
public static removeItem(array: any[], item: any) {
|
||||
var temp = array.concat();
|
||||
for (let i = 0; i < temp.length; i++) {
|
||||
const value = temp[i];
|
||||
if (item == value) {
|
||||
array.splice(i, 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并数组
|
||||
* @param array1 目标数组1
|
||||
* @param array2 目标数组2
|
||||
*/
|
||||
public static combineArrays(array1: any[], array2: any[]): any[] {
|
||||
let newArray = [...array1, ...array2];
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取随机数组成员
|
||||
* @param array 目标数组
|
||||
*/
|
||||
public static getRandomValueInArray(array: any[]): any {
|
||||
let newArray = array[Math.floor(Math.random() * array.length)];
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 快速移除指定位置数组元素
|
||||
* @param array
|
||||
* @param idx
|
||||
* @returns
|
||||
*/
|
||||
public static fastRemoveAt(array: any[],idx:number){
|
||||
let len = array.length-1; //1
|
||||
if(idx > len) return;
|
||||
array[idx] = array[len];
|
||||
array.length = len;
|
||||
}
|
||||
}
|
11
assets/Scripts/core/utils/ArrayUtil.ts.meta
Normal file
11
assets/Scripts/core/utils/ArrayUtil.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "b65cfca1-1091-485a-adbf-aadd39753a50",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
116
assets/Scripts/core/utils/MathUtil.ts
Normal file
116
assets/Scripts/core/utils/MathUtil.ts
Normal file
@@ -0,0 +1,116 @@
|
||||
export class MathUtil {
|
||||
/**
|
||||
* 角度转弧度
|
||||
*/
|
||||
public static deg2Rad: number = Math.PI / 180;
|
||||
|
||||
/**
|
||||
* 弧度转角度
|
||||
*/
|
||||
public static rad2Deg: number = 180 / Math.PI;
|
||||
|
||||
/**
|
||||
* 获得随机方向
|
||||
*/
|
||||
public static sign(x: number) {
|
||||
if (x > 0) {
|
||||
return 1;
|
||||
}
|
||||
if (x < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** 随时间变化进度值 */
|
||||
public static progress(start: number, end: number, t: number) {
|
||||
return start + (end - start) * t;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插值
|
||||
* @param numStart
|
||||
* @param numEnd
|
||||
* @param t
|
||||
*/
|
||||
public static lerp(numStart: number, numEnd: number, t: number): number {
|
||||
if (t > 1) {
|
||||
t = 1;
|
||||
}
|
||||
else if (t < 0) {
|
||||
t = 0
|
||||
}
|
||||
|
||||
return numStart * (1 - t) + (numEnd * t);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param angle1 角度插值
|
||||
* @param angle2
|
||||
* @param t
|
||||
*/
|
||||
public static lerpAngle(current: number, target: number, t: number): number {
|
||||
current %= 360;
|
||||
target %= 360;
|
||||
|
||||
var dAngle: number = target - current;
|
||||
|
||||
if (dAngle > 180) {
|
||||
target = current - (360 - dAngle);
|
||||
}
|
||||
else if (dAngle < -180) {
|
||||
target = current + (360 + dAngle);
|
||||
}
|
||||
|
||||
return (MathUtil.lerp(current, target, t) % 360 + 360) % 360;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按一定的速度从一个角度转向令一个角度
|
||||
* @param current
|
||||
* @param target
|
||||
* @param speed
|
||||
*/
|
||||
public static angleTowards(current: number, target: number, speed: number): number {
|
||||
current %= 360;
|
||||
target %= 360;
|
||||
|
||||
var dAngle: number = target - current;
|
||||
|
||||
if (dAngle > 180) {
|
||||
target = current - (360 - dAngle);
|
||||
}
|
||||
else if (dAngle < -180) {
|
||||
target = current + (360 + dAngle);
|
||||
}
|
||||
|
||||
var dir = target - current;
|
||||
|
||||
if (speed > Math.abs(dir)) {
|
||||
return target;
|
||||
}
|
||||
|
||||
return ((current + speed * Math.sign(dir)) % 360 + 360) % 360;
|
||||
}
|
||||
|
||||
public static clamp(value: number, minLimit: number, maxLimit: number) {
|
||||
if (value < minLimit) {
|
||||
return minLimit;
|
||||
}
|
||||
|
||||
if (value > maxLimit) {
|
||||
return maxLimit;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得一个值的概率
|
||||
* @param value
|
||||
*/
|
||||
public static probability(value: number) {
|
||||
return Math.random() < value;
|
||||
}
|
||||
}
|
11
assets/Scripts/core/utils/MathUtil.ts.meta
Normal file
11
assets/Scripts/core/utils/MathUtil.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "2df4c13d-eb96-4b3c-bf18-ba3367cc6022",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
231
assets/Scripts/core/utils/Util.ts
Normal file
231
assets/Scripts/core/utils/Util.ts
Normal file
@@ -0,0 +1,231 @@
|
||||
// Learn TypeScript:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
|
||||
// Learn Attribute:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
|
||||
// Learn life-cycle callbacks:
|
||||
// - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||
|
||||
import { v2, v3, Vec2, Vec3 } from "cc";
|
||||
|
||||
|
||||
export default class Util {
|
||||
|
||||
|
||||
static simpleV2(value, out?: Vec2) {
|
||||
if (out) {
|
||||
out.set(value, value)
|
||||
return out
|
||||
}
|
||||
return v2(value, value)
|
||||
}
|
||||
|
||||
static simpleV3(value, out?: Vec3) {
|
||||
if (out) {
|
||||
out.set(value, value, value)
|
||||
return out
|
||||
}
|
||||
return v3(value, value, value)
|
||||
}
|
||||
|
||||
static v2t3(v2Data: Vec2, out?: Vec3) {
|
||||
if (!out) {
|
||||
return v3(v2Data.x, v2Data.y, 1)
|
||||
} else {
|
||||
out.x = v2Data.x
|
||||
out.y = v2Data.y
|
||||
out.z = 1
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
static v3t2(v3Data: Vec3, out?: Vec2) {
|
||||
if (!out) {
|
||||
return v2(v3Data.x, v3Data.y)
|
||||
} else {
|
||||
out.x = v3Data.x
|
||||
out.y = v3Data.y
|
||||
return out
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* return min - max 之间的数,包括min和max
|
||||
* @param min
|
||||
* @param max
|
||||
*/
|
||||
static randomNumber(min: number, max: number) {
|
||||
if (min == max) {
|
||||
return min
|
||||
}
|
||||
return Math.floor(Math.random() * (max - min + 1) + min)
|
||||
}
|
||||
|
||||
static findArrNullIdx(arr: any[]) {
|
||||
if (arr.length == 0) return 0;
|
||||
for (let i = 0; i < arr.length; i++) {
|
||||
if (!arr[i]) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
//分割number字符串 返回number 数组
|
||||
public static splitNumber(str: number | string, Separator: string = ',') {
|
||||
if (!str) return [];
|
||||
if (typeof str == 'number') {
|
||||
return [str];
|
||||
}
|
||||
|
||||
return str.split(Separator).map((s_num, idx) => {
|
||||
return Number(s_num);
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @param weights 权重数组 随机一个
|
||||
* @returns
|
||||
*/
|
||||
static weightRandomIdx(weights: number[]) {
|
||||
if (weights.length <= 1) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let tw: number = 0;
|
||||
for (let i = 0; i < weights.length; i++) {
|
||||
tw += weights[i];
|
||||
}
|
||||
let rw = Math.random() * tw;
|
||||
let sw: number = 0, ew: number = 0;
|
||||
|
||||
for (let i = 0; i < weights.length; i++) {
|
||||
ew = sw + weights[i];
|
||||
if (sw < rw && rw <= ew) {
|
||||
return i;
|
||||
}
|
||||
sw = ew;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**小数去0 */
|
||||
public static numMoveZoro(num: string) {
|
||||
if (num.indexOf('.') < 0) {
|
||||
return num;
|
||||
}
|
||||
num = num.replace(/0+?$/g, '')
|
||||
if (num[num.length - 1] == '.') {
|
||||
num = num.replace(/[.$]/, '');
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param num 秒 格式化 h:f:s
|
||||
*/
|
||||
public static secondFormat(num: number) {
|
||||
let str = '';
|
||||
let h: number = Math.floor(num / 3600)
|
||||
num -= h * 3600;
|
||||
let f: number = Math.floor(num / 60)
|
||||
num -= f * 60;
|
||||
num = Math.floor(num)
|
||||
|
||||
// str += (h<10?'0' + h:h);
|
||||
|
||||
// str += ':';
|
||||
str += (f < 10 ? '0' + f : f);
|
||||
|
||||
str += ':';
|
||||
str += (num < 10 ? '0' + num : num);
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**日期展示 */
|
||||
public static getDateStr(time: number, format: number = 1) {
|
||||
|
||||
let date = new Date(time);
|
||||
let y = date.getFullYear(), m = date.getMonth() + 1,
|
||||
d = date.getDate(), h = date.getHours(), mn = date.getMinutes();
|
||||
|
||||
let mnStr = '' + mn;
|
||||
if (mn < 10) mnStr = '0' + mn;
|
||||
if (format == 1) {
|
||||
return `${y}/${m}/${d} ${h}:${mnStr}`;
|
||||
} else {
|
||||
return `${y}年${m}月${d}日 ${h}:${mnStr}`;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
static generatorCallBack(len: number, callBack: Function, ...params: any) {
|
||||
function* gen() {
|
||||
for (let i = 0; i < len; i++) {
|
||||
yield callBack(i, ...params)
|
||||
}
|
||||
}
|
||||
return this.exeGenerator(gen(), 10);
|
||||
}
|
||||
|
||||
static exeGenerator(generator: Generator, duration: number) {
|
||||
return new Promise((resolve, reject) => {
|
||||
let gen = generator
|
||||
let execute = () => {
|
||||
let startTime = new Date().getTime()
|
||||
for (let iter = gen.next(); ; iter = gen.next()) {
|
||||
if (iter == null || iter.done) {
|
||||
resolve(null)
|
||||
return
|
||||
}
|
||||
if (new Date().getTime() - startTime > duration) {
|
||||
setTimeout(() => {
|
||||
execute()
|
||||
}, duration)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
execute()
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 拷贝对象
|
||||
* @param src_obj 源对象
|
||||
* @param dst_obj 目标对象
|
||||
*/
|
||||
static copyObj(src_obj: any, dst_obj: any) {
|
||||
if (typeof dst_obj === "object") {
|
||||
for (var key in dst_obj) {
|
||||
if (typeof dst_obj[key] === "object") {
|
||||
src_obj[key] != null && Util.copyObj(src_obj[key], dst_obj[key]);
|
||||
} else if (typeof dst_obj[key] != "function") {
|
||||
src_obj[key] != null && (dst_obj[key] = src_obj[key]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log("can not copy the value type");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 克隆对象
|
||||
* @param obj 源对象
|
||||
* @returns 新的对象
|
||||
*/
|
||||
static cloneObj(obj: any) {
|
||||
var new_obj;
|
||||
if (obj != null && (obj.constructor === Object || obj.constructor === Array)) {
|
||||
new_obj = new obj.constructor();
|
||||
for (var key in obj) {
|
||||
new_obj[key] = Util.cloneObj(obj[key]);
|
||||
}
|
||||
} else {
|
||||
new_obj = obj;
|
||||
}
|
||||
return new_obj;
|
||||
}
|
||||
}
|
11
assets/Scripts/core/utils/Util.ts.meta
Normal file
11
assets/Scripts/core/utils/Util.ts.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "75224168-75d8-4cf6-bad8-d73031b24457",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {
|
||||
"simulateGlobals": []
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user