[add] Engine
This commit is contained in:
127
assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts
Normal file
127
assets/Script/Engine/Utils/CCExtensions/ArrayExtension.ts
Normal file
@@ -0,0 +1,127 @@
|
||||
declare interface Array<T> {
|
||||
/**
|
||||
* 移除一個值並且回傳
|
||||
* @param index
|
||||
*/
|
||||
ExRemoveAt(index: number): T;
|
||||
/**
|
||||
* 移除全部值(注意. 參考的也會被清空)
|
||||
* @example
|
||||
*
|
||||
* let bar: number[] = [1, 2, 3];
|
||||
* let bar2: number[] = bar;
|
||||
* bar.Clear();
|
||||
* console.log(bar, bar2);
|
||||
*
|
||||
* // {
|
||||
* // "bar": [],
|
||||
* // "bar2": []
|
||||
* // }
|
||||
*/
|
||||
Clear(): void;
|
||||
/**
|
||||
* 物件陣列排序,asc&key陣列長度請一樣
|
||||
* PS. boolean 帶false是先true在false
|
||||
* @link JavaScript Object 排序 http://www.eion.com.tw/Blogger/?Pid=1170#:~:text=JavaScript%20Object%20排序
|
||||
* @param asc 是否升序排列(小到大)
|
||||
* @param key 需排序的key(優先順序左到右)(沒有就放空)
|
||||
*/
|
||||
ObjectSort(asc?: boolean[], key?: string[]): any[];
|
||||
/**
|
||||
* 設計給Array<cc.Component.EventHandler>forHoldButton使用
|
||||
* Add a non persistent listener to the UnityEvent.
|
||||
* @param call Callback function.
|
||||
*/
|
||||
AddListener(call: Function): void;
|
||||
}
|
||||
|
||||
Array.prototype.ExRemoveAt || Object.defineProperty(Array.prototype, "ExRemoveAt", {
|
||||
enumerable: false,
|
||||
value: function (index: number): any {
|
||||
let item: any = this.splice(index, 1);
|
||||
return item[0];
|
||||
}
|
||||
});
|
||||
|
||||
Array.prototype.Clear || Object.defineProperty(Array.prototype, "Clear", {
|
||||
enumerable: false,
|
||||
value: function (): void {
|
||||
this.length = 0;
|
||||
|
||||
// let foo: number[] = [1, 2, 3];
|
||||
// let bar: number[] = [1, 2, 3];
|
||||
// let foo2: number[] = foo;
|
||||
// let bar2: number[] = bar;
|
||||
// foo = [];
|
||||
// bar.length = 0;
|
||||
// console.log(foo, bar, foo2, bar2);
|
||||
|
||||
// {
|
||||
// "foo": [],
|
||||
// "bar": [],
|
||||
// "foo2": [
|
||||
// 1,
|
||||
// 2,
|
||||
// 3
|
||||
// ],
|
||||
// "bar2": []
|
||||
// }
|
||||
}
|
||||
});
|
||||
|
||||
Array.prototype.ObjectSort || Object.defineProperty(Array.prototype, "ObjectSort", {
|
||||
enumerable: false,
|
||||
/**
|
||||
* @param asc 是否升序排列(小到大)
|
||||
* @param key 需排序的key(優先順序左到右)(沒有就放空)
|
||||
*/
|
||||
value: function (asc: boolean[] = [true], key?: string[]): any[] {
|
||||
if (this.length === 0) {
|
||||
return this;
|
||||
} else if (!key || key.length === 0) {
|
||||
console.error(`ObjectSort key error`);
|
||||
return this;
|
||||
} else if (asc.length !== key.length) {
|
||||
console.error(`ObjectSort key asc error asc.length: ${asc.length}, key.length: ${key.length}`);
|
||||
return this;
|
||||
}
|
||||
for (let i: number = 0; i < key.length; i++) {
|
||||
const keyname: string = key[i];
|
||||
if (this[0][keyname] === undefined) {
|
||||
console.error(`ObjectSort has not key[${i}]: ${keyname}`);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
let count: number = key ? key.length : 1;
|
||||
let arr: any[];
|
||||
for (let i: number = count - 1; i >= 0; i--) {
|
||||
arr = this.sort(function (a: any, b: any): 1 | -1 {
|
||||
let mya: any = a;
|
||||
let myb: any = b;
|
||||
if (key) {
|
||||
mya = a[key[i]];
|
||||
myb = b[key[i]];
|
||||
}
|
||||
|
||||
// 加個等於數字相同不要再去排序到
|
||||
if (asc[i]) {
|
||||
return mya >= myb ? 1 : -1;
|
||||
} else {
|
||||
return mya <= myb ? 1 : -1;
|
||||
}
|
||||
});
|
||||
}
|
||||
return arr;
|
||||
}
|
||||
});
|
||||
|
||||
Array.prototype.AddListener || Object.defineProperty(Array.prototype, "AddListener", {
|
||||
enumerable: false,
|
||||
value: function (call: Function): void {
|
||||
let EventHandler: cc.Component.EventHandler = new cc.Component.EventHandler();
|
||||
EventHandler.target = <any>"Callback";
|
||||
EventHandler.component = "Callback";
|
||||
EventHandler.handler = <any>call;
|
||||
this.push(EventHandler);
|
||||
}
|
||||
});
|
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"uuid": "02aa6cd7-21a1-4c22-bcbe-296bb938badd",
|
||||
"importer": "typescript",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
429
assets/Script/Engine/Utils/CCExtensions/CCExtension.ts
Normal file
429
assets/Script/Engine/Utils/CCExtensions/CCExtension.ts
Normal file
@@ -0,0 +1,429 @@
|
||||
declare namespace cc {
|
||||
|
||||
export interface Node {
|
||||
/**
|
||||
* 設定世界座標
|
||||
* @param worldPoint
|
||||
*/
|
||||
SetWorldPosition(worldPoint: cc.Vec2 | cc.Vec3): void;
|
||||
/**
|
||||
* 取得世界座標
|
||||
*/
|
||||
GetWorldPosition(): cc.Vec2;
|
||||
/**
|
||||
* 取得目標在自己區域的相對座標
|
||||
* @param target
|
||||
*/
|
||||
GetTargetInMyLocalPosition(target: cc.Node): cc.Vec2;
|
||||
/**
|
||||
* 取得目標距離
|
||||
* @param target
|
||||
*/
|
||||
GetTargetDistance(target: cc.Node): number;
|
||||
/**
|
||||
* 設定長寬
|
||||
* @param size
|
||||
*/
|
||||
SetSizeDelta(size: cc.Vec2);
|
||||
/**
|
||||
* 取得長寬
|
||||
*/
|
||||
GetSizeDelta(): cc.Vec2;
|
||||
/**
|
||||
* 增加一個子物件
|
||||
* @param childObj
|
||||
*/
|
||||
ExAddChild(childObj: cc.Prefab | cc.Node, childActive?: boolean): cc.Node;
|
||||
|
||||
/**設定層級為最低層 */
|
||||
ExSetLowestOrder(): void;
|
||||
|
||||
/**設定層級為最高層 */
|
||||
ExSetHighestOrder(): void;
|
||||
|
||||
/**設定層級,比目標OBJ再低一層 */
|
||||
ExSetOrderUnderTheObj(obj: cc.Node, isNew?: boolean): number;
|
||||
|
||||
/**設定層級,比目標OBJ再高一層 */
|
||||
ExSetOrderOverTheObj(obj: cc.Node, isNew?: boolean): number;
|
||||
/**位置維持在原位 */
|
||||
ExSetParent(parentObj: cc.Node): void;
|
||||
ExSetGray(showGray: boolean): void;
|
||||
|
||||
/** 通過觀察目標來設置 rotation */
|
||||
ExLookAt(targetPos: cc.Vec3): void;
|
||||
|
||||
/**
|
||||
* !#zh
|
||||
* 当前节点的自身激活状态。<br/>
|
||||
* 值得注意的是,一个节点的父节点如果不被激活,那么即使它自身设为激活,它仍然无法激活。<br/>
|
||||
* 如果你想检查节点在场景中实际的激活状态可以使用 {{#crossLink "Node/activeInHierarchy:property"}}{{/crossLink}}。
|
||||
* @param value 当前节点的自身激活状态
|
||||
*/
|
||||
SetActive(value: boolean): void;
|
||||
}
|
||||
|
||||
// export interface ActionInterval {
|
||||
// step(dt: number): void;
|
||||
// }
|
||||
|
||||
export interface Tween {
|
||||
/**
|
||||
* 獲取持續時間
|
||||
* @example let duration = tween.duration();
|
||||
*/
|
||||
duration(): number;
|
||||
/**
|
||||
* 獲取已經進行的時間
|
||||
* @example let elapsed = tween.elapsed();
|
||||
*/
|
||||
elapsed(): number;
|
||||
/**
|
||||
* 跳轉到指定時間
|
||||
* @param time 時間(秒)
|
||||
* @example tween.goto(2);
|
||||
*/
|
||||
goto(time: number): void;
|
||||
}
|
||||
}
|
||||
|
||||
cc.Node.prototype.SetWorldPosition || Object.defineProperty(cc.Node.prototype, 'SetWorldPosition', {
|
||||
enumerable: false,
|
||||
value: function (cocosWorldPos: cc.Vec2 | cc.Vec3) {
|
||||
// let cocosWorldPos = new cc.Vec2(unityWorldPos.x + 711, unityWorldPos.y + 400);
|
||||
this.setPosition(this.parent.convertToNodeSpaceAR(cocosWorldPos));
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.GetWorldPosition || Object.defineProperty(cc.Node.prototype, 'GetWorldPosition', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
let cocosWorldPos = this.parent.convertToWorldSpaceAR(this.position);
|
||||
// let unityWorldPos = new cc.Vec2(cocosWorldPos.x - 711, cocosWorldPos.y - 400);
|
||||
return cocosWorldPos;
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.GetTargetInMyLocalPosition || Object.defineProperty(cc.Node.prototype, 'GetTargetInMyLocalPosition', {
|
||||
enumerable: false,
|
||||
value: function (target: cc.Node): cc.Vec2 {
|
||||
let selfPos: cc.Vec2 = this.GetWorldPosition();
|
||||
let targetPos: cc.Vec2 = target.GetWorldPosition();
|
||||
let diff: cc.Vec2 = targetPos.sub(selfPos);
|
||||
let newPos: cc.Vec2 = this.getPosition().add(diff);
|
||||
return newPos;
|
||||
}
|
||||
});
|
||||
cc.Node.prototype.GetTargetDistance || Object.defineProperty(cc.Node.prototype, 'GetTargetDistance', {
|
||||
enumerable: false,
|
||||
value: function (target: cc.Node): number {
|
||||
let vector: cc.Vec2 = target.GetWorldPosition().sub(this.GetWorldPosition());
|
||||
let distance: number = vector.mag();
|
||||
return distance;
|
||||
}
|
||||
});
|
||||
cc.Node.prototype.SetSizeDelta || Object.defineProperty(cc.Node.prototype, 'SetSizeDelta', {
|
||||
enumerable: false,
|
||||
value: function (size: cc.Vec2) {
|
||||
this.setContentSize(size.x, size.y);
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.GetSizeDelta || Object.defineProperty(cc.Node.prototype, 'GetSizeDelta', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
let size: cc.Size = this.GetSizeDelta();
|
||||
return new cc.Vec2(size.width, size.width);
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExAddChild || Object.defineProperty(cc.Node.prototype, 'ExAddChild', {
|
||||
enumerable: false,
|
||||
value: function (childObj: cc.Prefab | cc.Node, childActive: boolean = true) {
|
||||
let gameObj = null;
|
||||
if (childObj instanceof cc.Prefab) {
|
||||
gameObj = cc.instantiate(childObj);
|
||||
}
|
||||
else {
|
||||
gameObj = cc.instantiate(childObj);
|
||||
}
|
||||
gameObj.active = childActive ? true : childActive;
|
||||
gameObj.parent = this;
|
||||
return gameObj;
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetLowestOrder || Object.defineProperty(cc.Node.prototype, 'ExSetLowestOrder', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
this.setSiblingIndex(0);
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetHighestOrder || Object.defineProperty(cc.Node.prototype, 'ExSetHighestOrder', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
this.setSiblingIndex(Number.MAX_VALUE);
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetOrderUnderTheObj || Object.defineProperty(cc.Node.prototype, 'ExSetOrderUnderTheObj', {
|
||||
enumerable: false,
|
||||
value: function (obj: cc.Node, isNew?: boolean) {
|
||||
|
||||
let newIndex: number;
|
||||
let objIndex = obj.getSiblingIndex();
|
||||
|
||||
// 如果是新創的元件
|
||||
if (isNew) {
|
||||
newIndex = objIndex;
|
||||
}
|
||||
// 如果是已經在場景上的元件
|
||||
else {
|
||||
let myIndex = this.getSiblingIndex();
|
||||
|
||||
// 如果一開始就在它下面
|
||||
if (myIndex < objIndex) {
|
||||
newIndex = objIndex - 1;
|
||||
}
|
||||
else {
|
||||
newIndex = objIndex;
|
||||
}
|
||||
}
|
||||
this.setSiblingIndex(newIndex);
|
||||
return newIndex;
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetOrderOverTheObj || Object.defineProperty(cc.Node.prototype, 'ExSetOrderOverTheObj', {
|
||||
enumerable: false,
|
||||
value: function (obj: cc.Node, isNew?: boolean) {
|
||||
let newIndex: number;
|
||||
let objIndex = obj.getSiblingIndex();
|
||||
|
||||
// 如果是新創的元件
|
||||
if (isNew) {
|
||||
newIndex = objIndex + 1;
|
||||
}
|
||||
// 如果是已經在場景上的元件
|
||||
else {
|
||||
let myIndex = this.getSiblingIndex();
|
||||
|
||||
// 如果一開始就在它下面
|
||||
if (myIndex < objIndex) {
|
||||
newIndex = objIndex;
|
||||
}
|
||||
else {
|
||||
newIndex = objIndex + 1;
|
||||
}
|
||||
}
|
||||
this.setSiblingIndex(newIndex);
|
||||
return newIndex;
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetParent || Object.defineProperty(cc.Node.prototype, 'ExSetParent', {
|
||||
enumerable: false,
|
||||
value: function (parentObj: cc.Node) {
|
||||
let oriPos = this.GetWorldPosition();
|
||||
this.setParent(parentObj);
|
||||
this.SetWorldPosition(oriPos);
|
||||
}
|
||||
})
|
||||
cc.Node.prototype.ExSetGray || Object.defineProperty(cc.Node.prototype, 'ExSetGray', {
|
||||
enumerable: false,
|
||||
value: function (showGray: boolean): void {
|
||||
let btn: cc.Button = this.getComponent(cc.Button);
|
||||
if (btn) {
|
||||
btn.interactable = !showGray;
|
||||
}
|
||||
let material: cc.Material = cc.Material.createWithBuiltin(showGray ? cc.Material.BUILTIN_NAME.GRAY_SPRITE.toString() : cc.Material.BUILTIN_NAME.SPRITE.toString(), 0);
|
||||
!showGray && material.define("USE_TEXTURE", true, 0);
|
||||
let spriteComs: any[] = this.getComponentsInChildren(cc.Sprite).concat(this.getComponentsInChildren(cc.Label));
|
||||
for (let sprite of spriteComs) {
|
||||
sprite.setMaterial(0, material);
|
||||
}
|
||||
|
||||
// 先使用createWithBuiltin,如果材質球一直Create沒被刪除,會在修改。
|
||||
// let material: cc.Material = cc.Material.getBuiltinMaterial(showGray ? cc.Material.BUILTIN_NAME.GRAY_SPRITE.toString() : cc.Material.BUILTIN_NAME.SPRITE.toString());
|
||||
// for (let sprite of spriteComs) {
|
||||
// if (showGray) {
|
||||
// sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-gray-sprite'));
|
||||
// }
|
||||
// else {
|
||||
// sprite.setMaterial(0, cc.Material.getBuiltinMaterial('2d-sprite'));
|
||||
// }
|
||||
// }
|
||||
},
|
||||
});
|
||||
cc.Node.prototype.ExLookAt || Object.defineProperty(cc.Node.prototype, "ExLookAt", {
|
||||
enumerable: false,
|
||||
value: function (targetPos: cc.Vec3): void {
|
||||
let TargetX: number = targetPos.x;
|
||||
let TargetY: number = targetPos.y;
|
||||
let SelfX: number = this.x;
|
||||
let SelfY: number = this.y;
|
||||
let r1: number = Math.atan2(TargetX - SelfX, TargetY - SelfY);
|
||||
let angle: number = (180 * r1 / Math.PI);
|
||||
this.angle = -angle;
|
||||
},
|
||||
});
|
||||
cc.Node.prototype.SetActive || Object.defineProperty(cc.Node.prototype, "SetActive", {
|
||||
enumerable: false,
|
||||
value: function (value: boolean): void {
|
||||
this.active = value;
|
||||
},
|
||||
});
|
||||
// cc.Node.prototype.SetWorldPosition = function (cocosWorldPos: cc.Vec2): void {
|
||||
// // let cocosWorldPos = new cc.Vec2(unityWorldPos.x + 711, unityWorldPos.y + 400);
|
||||
// this.setPosition(this.parent.convertToNodeSpaceAR(cocosWorldPos));
|
||||
// }
|
||||
// cc.Node.prototype.GetWorldPosition = function (): cc.Vec2 {
|
||||
// let cocosWorldPos = this.parent.convertToWorldSpaceAR(this.position);
|
||||
// // let unityWorldPos = new cc.Vec2(cocosWorldPos.x - 711, cocosWorldPos.y - 400);
|
||||
// return cocosWorldPos;
|
||||
// }
|
||||
|
||||
// cc.Node.prototype.SetSizeDelta = function (size: cc.Vec2) {
|
||||
// this.setContentSize(size.x, size.y);
|
||||
// }
|
||||
// cc.Node.prototype.GetSizeDelta = function (): cc.Vec2 {
|
||||
// let size: cc.Size = this.GetSizeDelta();
|
||||
// return new cc.Vec2(size.width, size.width);
|
||||
// }
|
||||
|
||||
// cc.Node.prototype.ExAddChild = function (childObj: cc.Prefab | cc.Node): cc.Node {
|
||||
|
||||
// let gameObj = null;
|
||||
// if (childObj instanceof cc.Prefab) {
|
||||
// gameObj = cc.instantiate(childObj);
|
||||
// }
|
||||
// else {
|
||||
// gameObj = cc.instantiate(childObj);
|
||||
// }
|
||||
// gameObj.parent = this;
|
||||
// return gameObj;
|
||||
// }
|
||||
|
||||
// cc.Node.prototype.ExSetLowestOrder = function (): void {
|
||||
// this.setSiblingIndex(0);
|
||||
// }
|
||||
// cc.Node.prototype.ExSetHighestOrder = function (): void {
|
||||
// this.setSiblingIndex(Number.MAX_VALUE);
|
||||
// }
|
||||
|
||||
|
||||
// cc.ActionInterval.prototype.step || Object.defineProperty(cc.ActionInterval.prototype, "step", {
|
||||
// enumerable: false,
|
||||
// value: function (dt: number): void {
|
||||
// if (this.paused) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// if (this._firstTick && !this._goto) {
|
||||
// this._firstTick = false;
|
||||
// this._elapsed = 0;
|
||||
// } else {
|
||||
// this._elapsed += dt;
|
||||
// }
|
||||
|
||||
// let t: number = this._elapsed / (this._duration > 0.0000001192092896 ? this._duration : 0.0000001192092896);
|
||||
// t = (1 > t ? t : 1);
|
||||
// this.update(t > 0 ? t : 0);
|
||||
|
||||
// // Compatible with repeat class, Discard after can be deleted (this._repeatMethod)
|
||||
// if (this._repeatMethod && this._timesForRepeat > 1 && this.isDone()) {
|
||||
// if (!this._repeatForever) {
|
||||
// this._timesForRepeat--;
|
||||
// }
|
||||
// this.startWithTarget(this.target);
|
||||
// this.step(this._elapsed - this._duration);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// /**
|
||||
// * 暂停
|
||||
// * @example tween.pause();
|
||||
// */
|
||||
// cc.Tween.prototype.pause = function () {
|
||||
// this._finalAction.paused = true;
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * 恢复
|
||||
// * @example tween.resume();
|
||||
// */
|
||||
// cc.Tween.prototype.resume = function () {
|
||||
// this._finalAction.paused = false;
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * 倍速播放
|
||||
// * @param speed 倍速
|
||||
// * @example tween.speed(2);
|
||||
// */
|
||||
// cc.Tween.prototype.speed = function (speed) {
|
||||
// this._finalAction._speedMethod = true;
|
||||
// this._finalAction._speed = speed;
|
||||
// }
|
||||
|
||||
cc.Tween.prototype.duration || Object.defineProperty(cc.Tween.prototype, "duration", {
|
||||
enumerable: false,
|
||||
value: function (): number {
|
||||
// let duration: number = this._finalAction._duration;
|
||||
let duration: number = 0;
|
||||
for (let i: number = 0; i < this["_actions"].length - 1; i++) {
|
||||
const action: any = this["_actions"][i];
|
||||
duration += action._duration;
|
||||
}
|
||||
// duration += ((cc.game.getFrameRate() / 3) / cc.game.getFrameRate());
|
||||
duration *= 1.3;
|
||||
return duration;
|
||||
}
|
||||
});
|
||||
|
||||
cc.Tween.prototype.elapsed || Object.defineProperty(cc.Tween.prototype, "elapsed", {
|
||||
enumerable: false,
|
||||
value: function (): number {
|
||||
return this._finalAction._elapsed;
|
||||
}
|
||||
});
|
||||
|
||||
cc.Tween.prototype.goto || Object.defineProperty(cc.Tween.prototype, "goto", {
|
||||
enumerable: false,
|
||||
/**
|
||||
* @param time 時間(秒)
|
||||
*/
|
||||
value: function (time: number): void {
|
||||
this._finalAction._goto = true;
|
||||
this._finalAction._elapsed = time;
|
||||
}
|
||||
});
|
||||
|
||||
// cc.ActionManager.prototype.pauseByTag = function (tag, pause) {
|
||||
// if (tag === cc.Action.TAG_INVALID) {
|
||||
// cc.logID(1002);
|
||||
// }
|
||||
|
||||
// let hashTargets = this._hashTargets;
|
||||
// for (let name in hashTargets) {
|
||||
// let element = hashTargets[name];
|
||||
// for (let i = 0, l = element.actions.length; i < l; ++i) {
|
||||
// let action = element.actions[i];
|
||||
// if (action && action.getTag() === tag) {
|
||||
// action.paused = pause;
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 根据标签暂停动作
|
||||
// * @param tag tween的标签
|
||||
// * @example cc.Tween.pauseByTag(100);
|
||||
// */
|
||||
// cc.Tween.pauseByTag = function (tag) {
|
||||
// cc.director.getActionManager().pauseByTag(tag, true);
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * 根据标签恢复动作
|
||||
// * @param tag tween的标签
|
||||
// * @example cc.Tween.resumeByTag(100);
|
||||
// */
|
||||
// cc.Tween.resumeByTag = function (tag) {
|
||||
// cc.director.getActionManager().pauseByTag(tag, false);
|
||||
// }
|
10
assets/Script/Engine/Utils/CCExtensions/CCExtension.ts.meta
Normal file
10
assets/Script/Engine/Utils/CCExtensions/CCExtension.ts.meta
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"uuid": "b373f805-9297-4af5-8ea6-0a250649b5b0",
|
||||
"importer": "typescript",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
189
assets/Script/Engine/Utils/CCExtensions/NumberExtension.ts
Normal file
189
assets/Script/Engine/Utils/CCExtensions/NumberExtension.ts
Normal file
@@ -0,0 +1,189 @@
|
||||
|
||||
declare interface Number {
|
||||
|
||||
/**
|
||||
* 金額每三位數(千)加逗號, 並且補到小數點第2位
|
||||
* 輸出 41,038,560.00
|
||||
* @param precision 補到小數點第幾位
|
||||
* @param isPadZero 是否要補零
|
||||
* */
|
||||
ExFormatNumberWithComma(precision?: number, isPadZero?: boolean): string;
|
||||
/**
|
||||
* 基本4位數(9,999-999B-T)
|
||||
* */
|
||||
ExTransferToBMK(precision?: number,offset?: number): string;
|
||||
/**
|
||||
* 數字轉字串, 頭補0
|
||||
* @param size
|
||||
*/
|
||||
Pad(size: number): string;
|
||||
/**
|
||||
* 四捨五入到小數點第X位 (同server計算規則)
|
||||
* @param precision
|
||||
*/
|
||||
ExToNumRoundDecimal(precision: number): number;
|
||||
/**
|
||||
* 無條件捨去到小數點第X位
|
||||
* @param precision
|
||||
*/
|
||||
ExToNumFloorDecimal(precision: number): number;
|
||||
/**
|
||||
* 無條件捨去強制保留X位小數,如:2,會在2後面補上00.即2.00
|
||||
* @param precision 補到小數點第幾位
|
||||
* @param isPadZero 是否要補零
|
||||
*/
|
||||
ExToStringFloorDecimal(precision: number, isPadZero?: boolean): string;
|
||||
/**
|
||||
* 取整數)
|
||||
*/
|
||||
ExToInt():number;
|
||||
/**
|
||||
* 小數轉整數(支援科學符號)
|
||||
*/
|
||||
Float2Fixed():number;
|
||||
/**
|
||||
* 數字長度(支援科學符號)
|
||||
*/
|
||||
DigitLength():number;
|
||||
|
||||
target: number;
|
||||
|
||||
|
||||
}
|
||||
|
||||
Number.prototype.ExFormatNumberWithComma || Object.defineProperty(Number.prototype, 'ExFormatNumberWithComma', {
|
||||
enumerable: false,
|
||||
value: function (precision: number = 2, isPadZero: boolean = true) {
|
||||
|
||||
// let arr = String(this).split('.');
|
||||
let arr = this.ExToStringFloorDecimal(precision, isPadZero).split('.');
|
||||
let num = arr[0], result = '';
|
||||
while (num.length > 3) {
|
||||
result = ',' + num.slice(-3) + result;
|
||||
num = num.slice(0, num.length - 3);
|
||||
}
|
||||
if (num.length > 0) result = num + result;
|
||||
return arr[1] ? result + '.' + arr[1] : result;
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Number.prototype.ExTransferToBMK || Object.defineProperty(Number.prototype, 'ExTransferToBMK', {
|
||||
enumerable: false,
|
||||
value: function (precision: number=2,offset: number = 0) {
|
||||
/**千 */
|
||||
let MONEY_1K: number = 1000;
|
||||
/**萬 */
|
||||
// let MONEY_10K: number = 10000;
|
||||
/**十萬 */
|
||||
// let MONEY_100K: number = 100000;
|
||||
/**百萬 */
|
||||
let MONEY_1M: number = 1000000;
|
||||
/**千萬 */
|
||||
// let MONEY_10M: number = 10000000;
|
||||
/**億 */
|
||||
// let MONEY_100M: number = 100000000;
|
||||
/**十億 */
|
||||
let MONEY_1B: number = 1000000000;
|
||||
/**百億 */
|
||||
// let MONEY_10B: number = 10000000000;
|
||||
/**千億 */
|
||||
// let MONEY_100B: number = 100000000000;
|
||||
/**兆 */
|
||||
// let MONEY_1T: number = 1000000000000;
|
||||
offset = Math.pow(10, offset);
|
||||
// if (this >= MONEY_1T * offset) {
|
||||
// //(3)1,000T
|
||||
// //1T~
|
||||
// return (~~(this / MONEY_1T)).ExFormatNumberWithComma(0) + "T";
|
||||
// }
|
||||
if (this >= MONEY_1B * offset) {
|
||||
//1,000B~900,000B
|
||||
//1B~900B
|
||||
return (this / MONEY_1B).ExFormatNumberWithComma(3, false) + "B";
|
||||
}
|
||||
else if (this >= MONEY_1M * offset) {
|
||||
//1,000M~900,000M
|
||||
//1M~900M
|
||||
return (this / MONEY_1M).ExFormatNumberWithComma(3, false) + "M";
|
||||
}
|
||||
else if (this >= MONEY_1K * offset) {
|
||||
//1,000K~900,000K
|
||||
//1K~90K
|
||||
return (this / MONEY_1K).ExFormatNumberWithComma(3, false) + "K";
|
||||
}
|
||||
else {
|
||||
//0~9,000,000
|
||||
//0~9,000
|
||||
return this.ExFormatNumberWithComma(precision);
|
||||
}
|
||||
}
|
||||
})
|
||||
Number.prototype.Pad || Object.defineProperty(Number.prototype, 'Pad', {
|
||||
enumerable: false,
|
||||
value: function (size: number) {
|
||||
let s = this + "";
|
||||
while (s.length < size) s = "0" + s;
|
||||
return s;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToNumRoundDecimal || Object.defineProperty(Number.prototype, 'ExToNumRoundDecimal', {
|
||||
enumerable: false,
|
||||
value: function (precision: number) {
|
||||
return Math.round(Math.round(this * Math.pow(10, (precision || 0) + 1)) / 10) / Math.pow(10, (precision || 0));
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToInt || Object.defineProperty(Number.prototype, 'ExToInt',{
|
||||
enumerable: false,
|
||||
value: function (){
|
||||
return ~~this;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToNumFloorDecimal || Object.defineProperty(Number.prototype, 'ExToNumFloorDecimal', {
|
||||
enumerable: false,
|
||||
value: function (precision: number) {
|
||||
let str = this.toPrecision(12);
|
||||
let dotPos = str.indexOf('.');
|
||||
return dotPos == -1 ? this : +`${str.substr(0, dotPos + 1 + precision)}`;
|
||||
}
|
||||
})
|
||||
Number.prototype.ExToStringFloorDecimal || Object.defineProperty(Number.prototype, 'ExToStringFloorDecimal', {
|
||||
enumerable: false,
|
||||
value: function (precision: number, isPadZero: boolean = true) {
|
||||
// 取小數點第X位
|
||||
let f = this.ExToNumFloorDecimal(precision);
|
||||
let s = f.toString();
|
||||
// 補0
|
||||
if (isPadZero) {
|
||||
let rs = s.indexOf('.');
|
||||
if (rs < 0) {
|
||||
rs = s.length;
|
||||
s += '.';
|
||||
}
|
||||
while (s.length <= rs + precision) {
|
||||
s += '0';
|
||||
}
|
||||
}
|
||||
return s;
|
||||
}
|
||||
})
|
||||
Number.prototype.Float2Fixed || Object.defineProperty(Number.prototype, 'Float2Fixed', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
if (this.toString().indexOf('e') === -1) {
|
||||
return Number(this.toString().replace('.', ''));
|
||||
}
|
||||
const dLen = this.DigitLength();
|
||||
return dLen > 0 ? +parseFloat((this * Math.pow(10, dLen)).toPrecision(12)) : this;
|
||||
}
|
||||
})
|
||||
Number.prototype.DigitLength || Object.defineProperty(Number.prototype, 'DigitLength', {
|
||||
enumerable: false,
|
||||
value: function () {
|
||||
const eSplit = this.toString().split(/[eE]/);
|
||||
const len = (eSplit[0].split('.')[1] || '').length - (+(eSplit[1] || 0));
|
||||
return len > 0 ? len : 0;
|
||||
}
|
||||
})
|
||||
|
||||
|
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ver": "1.1.0",
|
||||
"uuid": "788e7381-bee6-4b74-addb-c4aa4c4ff4e3",
|
||||
"importer": "typescript",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
Reference in New Issue
Block a user