123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- export namespace Utils {
- export function str2arr(str: string, char: string){
- return str.split(char);
- }
- export function createNode(parent: cc.Node, name: string = ''){
- let node = new cc.Node(name);
- parent.addChild(node);
- return node;
- }
- export function intDefault(data, def: number = 0){
- if(data == '') return 0;
- return Number(data);
- }
- // 角度转弧度
- export function radian(degree: number){
- return degree * (Math.PI/180);
- }
- export function degree(radian: number){
- return radian * (180/Math.PI);
- }
- export function getCpt<T extends cc.Component>(cpt: {new(): T}, node: cc.Node): T{
- let moveCpt = node.getComponent(cpt);
- if(!moveCpt) {
- moveCpt = node.addComponent(cpt);
- }
- return moveCpt;
- }
- export function addFullWidget(node: cc.Node){
- let widget = node.addComponent(cc.Widget);
- widget.isAlignBottom = true;
- widget.isAlignTop = true;
- widget.isAlignLeft = true;
- widget.isAlignRight = true;
- widget.left = 0;
- widget.right = 0;
- widget.top = 0;
- widget.bottom = 0;
- }
- export function strReplaceSeparator(str:string){
- return str.replace(/\;|\;/g, ',');
- }
- export function strToJson(str: string): any{
- let jsonStr = strReplaceSeparator(str)
- jsonStr = jsonStr.replace(/\]\[/g, '],[');
- return JSON.parse(jsonStr);
- }
- export function pos2vec2(pos: string): cc.Vec2 {
- let posJson = Utils.strToJson(pos);
- return cc.v2(Number(posJson[0]), -Number(posJson[1]));
- }
- export function getAniExtent(state: cc.AnimationState){
- // let cur = state.curves;
- let extent = 0;
- for (let index = 0; index < state.curves.length; index++) {
- const element = state.curves[index];
- if(element.prop == 'position') {
- let values = element.values as cc.Vec3[];
- for (let index = 1; index < values.length; index++) {
- extent += values[index].sub(values[index-1]).mag();
- }
- }
- if(element.prop == 'y' || element.prop == 'x') {
- let values = element.values as number[];
- for (let index = 1; index < values.length; index++) {
- extent += Math.abs(values[index] - values[index-1]);
- }
- }
- }
- return extent;
- }
- }
|