225 lines
5.5 KiB
TypeScript
225 lines
5.5 KiB
TypeScript
import {
|
|
Component,
|
|
IVec2Like,
|
|
IVec3Like,
|
|
Label,
|
|
Node,
|
|
NodeSpace,
|
|
Vec2,
|
|
Vec3,
|
|
randomRangeInt,
|
|
toDegree,
|
|
toRadian,
|
|
} from 'cc';
|
|
import ObjectPool from '../Pool/ObjectPool';
|
|
|
|
declare module 'cc' {
|
|
interface Component {
|
|
setNodeActive(isActive: boolean): void;
|
|
}
|
|
|
|
interface Node {
|
|
setActive(isActive: boolean): void;
|
|
setPositionX(x: number): void;
|
|
setPositionY(y: number): void;
|
|
setPositionZ(z: number): void;
|
|
setWorldPositionX(x: number): void;
|
|
setWorldPositionY(y: number): void;
|
|
setWorldPositionZ(z: number): void;
|
|
translateX(x: number, ns?: NodeSpace): void;
|
|
translateY(y: number, ns?: NodeSpace): void;
|
|
translateZ(z: number, ns?: NodeSpace): void;
|
|
setScaleX(x: number): void;
|
|
setScaleY(y: number): void;
|
|
setScaleZ(z: number): void;
|
|
releaseToPool(): void;
|
|
}
|
|
|
|
interface Vec2 {
|
|
toVec3(): Vec3;
|
|
getPositionOnCircle(angle: number, radius?: number): Vec2;
|
|
getAngleOnCircle(center: IVec2Like): number;
|
|
}
|
|
|
|
interface Vec3 {
|
|
toVec2(): Vec2;
|
|
getPositionOnCircle(angle: number, radius?: number): Vec3;
|
|
getAngleOnCircle(center: IVec3Like): number;
|
|
}
|
|
|
|
interface Label {
|
|
setString(value: string | number);
|
|
}
|
|
}
|
|
|
|
declare global {
|
|
interface String {
|
|
jsonParse(): any;
|
|
isNullOrWhiteSpace(): boolean;
|
|
}
|
|
|
|
interface Array<T> {
|
|
getRandom(): T;
|
|
getRandom(weights: number[]): T;
|
|
}
|
|
}
|
|
|
|
//#region COMPONENT
|
|
|
|
Component.prototype.setNodeActive = function (isActive: boolean) {
|
|
this.node.setActive(isActive);
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region NODE
|
|
|
|
Node.prototype.setActive = function (isActive: boolean) {
|
|
this.active = isActive;
|
|
};
|
|
|
|
Node.prototype.setPositionX = function (x: number): void {
|
|
this.position = new Vec3(x, this.position.y, this.position.z);
|
|
};
|
|
|
|
Node.prototype.setPositionY = function (y: number): void {
|
|
this.position = new Vec3(this.position.x, y, this.position.z);
|
|
};
|
|
|
|
Node.prototype.setPositionZ = function (z: number): void {
|
|
this.position = new Vec3(this.position.x, this.position.y, z);
|
|
};
|
|
|
|
Node.prototype.setWorldPositionX = function (x: number): void {
|
|
this.worldPosition = new Vec3(x, this.worldPosition.y, this.worldPosition.z);
|
|
};
|
|
|
|
Node.prototype.setWorldPositionY = function (y: number): void {
|
|
this.worldPosition = new Vec3(this.worldPosition.x, y, this.worldPosition.z);
|
|
};
|
|
|
|
Node.prototype.setWorldPositionZ = function (z: number): void {
|
|
this.worldPosition = new Vec3(this.worldPosition.x, this.worldPosition.y, z);
|
|
};
|
|
|
|
Node.prototype.translateX = function (x: number, ns?: NodeSpace): void {
|
|
this.translate(new Vec3(x, 0), ns);
|
|
};
|
|
|
|
Node.prototype.translateY = function (y: number, ns?: NodeSpace): void {
|
|
this.translate(new Vec3(0, y), ns);
|
|
};
|
|
|
|
Node.prototype.translateZ = function (z: number, ns?: NodeSpace): void {
|
|
this.translate(new Vec3(0, 0, z), ns);
|
|
};
|
|
|
|
Node.prototype.setScaleX = function (x: number): void {
|
|
this.scale = new Vec3(x, this.scale.y, this.scale.z);
|
|
};
|
|
|
|
Node.prototype.setScaleY = function (y: number): void {
|
|
this.scale = new Vec3(this.scale.x, y, this.scale.z);
|
|
};
|
|
|
|
Node.prototype.setScaleZ = function (z: number): void {
|
|
this.scale = new Vec3(this.scale.x, this.scale.y, z);
|
|
};
|
|
|
|
Node.prototype.releaseToPool = function () {
|
|
ObjectPool.release(this);
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region VEC2
|
|
|
|
Vec2.prototype.toVec3 = function () {
|
|
return new Vec3(this.x, this.y);
|
|
};
|
|
|
|
Vec2.prototype.getPositionOnCircle = function (angle: number, radius?: number) {
|
|
const rad = toRadian(angle);
|
|
const r = radius || 1;
|
|
const x = this.x + r * -Math.cos(rad);
|
|
const y = this.y + r * Math.sin(rad);
|
|
return new Vec2(x, y);
|
|
};
|
|
|
|
Vec2.prototype.getAngleOnCircle = function (center: IVec2Like): number {
|
|
const diffX = this.x - center.x;
|
|
const diffY = this.y - center.y;
|
|
const angle = Math.atan2(diffY, diffX);
|
|
return toDegree(angle) - 90;
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region VEC3
|
|
|
|
Vec3.prototype.toVec2 = function () {
|
|
return new Vec2(this.x, this.y);
|
|
};
|
|
|
|
Vec3.prototype.getPositionOnCircle = function (angle: number, radius?: number) {
|
|
const rad = toRadian(angle);
|
|
const r = radius || 1;
|
|
const x = this.x + r * -Math.cos(rad);
|
|
const y = this.y + r * Math.sin(rad);
|
|
return new Vec3(x, y, this.z);
|
|
};
|
|
|
|
Vec3.prototype.getAngleOnCircle = function (center: IVec3Like): number {
|
|
const diffX = this.x - center.x;
|
|
const diffY = this.y - center.y;
|
|
const angle = Math.atan2(diffY, diffX);
|
|
return toDegree(angle) - 90;
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region LABEL
|
|
|
|
Label.prototype.setString = function (value: string | number) {
|
|
if (typeof value == 'number') {
|
|
this.string = value.toString();
|
|
return;
|
|
}
|
|
this.string = value;
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region STRING
|
|
|
|
String.prototype.jsonParse = function () {
|
|
try {
|
|
return JSON.parse(this);
|
|
} catch (error) {
|
|
console.log(error);
|
|
return false;
|
|
}
|
|
};
|
|
|
|
String.prototype.isNullOrWhiteSpace = function () {
|
|
return !this || !this.trim();
|
|
};
|
|
|
|
//#endregion
|
|
|
|
//#region ARRAY
|
|
|
|
Array.prototype.getRandom = function (weights?: number[]) {
|
|
if (weights) {
|
|
let weightsClone = [...weights];
|
|
const totalWeight = weightsClone.reduce((a, b) => a + b, 0);
|
|
let random = Math.random() * totalWeight;
|
|
const item = this.find((_, i) => (random -= weightsClone[i]) <= 0);
|
|
return item;
|
|
} else {
|
|
return this[randomRangeInt(0, this.length)];
|
|
}
|
|
};
|
|
|
|
//#endregion
|