Commit 181499e2 authored by 范雪寒's avatar 范雪寒

feat: initUtils_NGT_02

parent 50583886
...@@ -7,4 +7,5 @@ function initGlobal_NGT_02() { ...@@ -7,4 +7,5 @@ function initGlobal_NGT_02() {
initStorageMgr_NGT_02(); initStorageMgr_NGT_02();
initResMgr_NGT_02(); initResMgr_NGT_02();
initSndMgr_NGT_02(); initSndMgr_NGT_02();
initUtils_NGT_02();
} }
\ No newline at end of file
g.utils = { function initUtils_NGT_02() {
g.utils = {
// 范围随机 // 范围随机
randFromTo: function (_min, _max) { randFromTo: function (_min, _max) {
var min = parseFloat(_min); var min = parseFloat(_min);
var max = parseFloat(_max); var max = parseFloat(_max);
return (min + Math.random() * (max - min)); return min + Math.random() * (max - min);
}, },
// 范围随机一个整数 // 范围随机一个整数
// 例:1~3,返回的可能值为:1、2、3 // 例:1~3,返回的可能值为:1、2、3
...@@ -15,7 +16,7 @@ g.utils = { ...@@ -15,7 +16,7 @@ g.utils = {
deepCopy: function (src) { deepCopy: function (src) {
var cpy = src instanceof Array ? [] : {}; var cpy = src instanceof Array ? [] : {};
for (var i in src) { for (var i in src) {
cpy[i] = typeof src[i] === 'object' ? this.deepCopy(src[i]) : src[i]; cpy[i] = typeof src[i] === "object" ? this.deepCopy(src[i]) : src[i];
} }
return cpy; return cpy;
}, },
...@@ -23,11 +24,14 @@ g.utils = { ...@@ -23,11 +24,14 @@ g.utils = {
//生成唯一标识 //生成唯一标识
generateUUID: function () { generateUUID: function () {
var d = new Date().getTime(); var d = new Date().getTime();
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
var r = (d + Math.random() * 16) % 16 | 0; var r = (d + Math.random() * 16) % 16 | 0;
d = Math.floor(d / 16); d = Math.floor(d / 16);
return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
}); }
);
return uuid; return uuid;
}, },
...@@ -36,68 +40,74 @@ g.utils = { ...@@ -36,68 +40,74 @@ g.utils = {
if (index > -1) { if (index > -1) {
this.splice(index, 1); this.splice(index, 1);
} }
} },
} };
export function getPosByAngle(angle, len) {
const radian = angle * Math.PI / 180; export function getPosByAngle(angle, len) {
const radian = (angle * Math.PI) / 180;
const x = Math.sin(radian) * len; const x = Math.sin(radian) * len;
const y = Math.cos(radian) * len; const y = Math.cos(radian) * len;
return { x, y }; return { x, y };
}
} export function getAngleByPos(px, py, mx, my) {
export function getAngleByPos(px, py, mx, my) {
const x = Math.abs(px - mx); const x = Math.abs(px - mx);
const y = Math.abs(py - my); const y = Math.abs(py - my);
const z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2)); const z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
const cos = y / z; const cos = y / z;
const radina = Math.acos(cos); // 用反三角函数求弧度 const radina = Math.acos(cos); // 用反三角函数求弧度
let angle = Math.floor(180 / (Math.PI / radina) * 100) / 100; // 将弧度转换成角度 let angle = Math.floor((180 / (Math.PI / radina)) * 100) / 100; // 将弧度转换成角度
if (mx > px && my > py) {// 鼠标在第四象限 if (mx > px && my > py) {
// 鼠标在第四象限
angle = 180 - angle; angle = 180 - angle;
} }
if (mx === px && my > py) {// 鼠标在y轴负方向上 if (mx === px && my > py) {
// 鼠标在y轴负方向上
angle = 180; angle = 180;
} }
if (mx > px && my === py) {// 鼠标在x轴正方向上 if (mx > px && my === py) {
// 鼠标在x轴正方向上
angle = 90; angle = 90;
} }
if (mx < px && my > py) {// 鼠标在第三象限 if (mx < px && my > py) {
// 鼠标在第三象限
angle = 180 + angle; angle = 180 + angle;
} }
if (mx < px && my === py) {// 鼠标在x轴负方向 if (mx < px && my === py) {
// 鼠标在x轴负方向
angle = 270; angle = 270;
} }
if (mx < px && my < py) {// 鼠标在第二象限 if (mx < px && my < py) {
// 鼠标在第二象限
angle = 360 - angle; angle = 360 - angle;
} }
// console.log('angle: ', angle); // console.log('angle: ', angle);
return angle; return angle;
}
} export function exchangeNodePos(baseNode, targetNode) {
return baseNode.convertToNodeSpaceAR(
export function exchangeNodePos(baseNode, targetNode) { targetNode._parent.convertToWorldSpaceAR(
return baseNode.convertToNodeSpaceAR(targetNode._parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y))); cc.v2(targetNode.x, targetNode.y)
} )
);
}
export function RandomInt(a, b = 0) { export function RandomInt(a, b = 0) {
let max = Math.max(a, b); let max = Math.max(a, b);
let min = Math.min(a, b); let min = Math.min(a, b);
return Math.floor(Math.random() * (max - min) + min); return Math.floor(Math.random() * (max - min) + min);
} }
export function Between(a, b, c) { export function Between(a, b, c) {
return [a, b, c].sort((a, b) => a - b)[1]; return [a, b, c].sort((a, b) => a - b)[1];
} }
export function randomSortByArr(arr) { export function randomSortByArr(arr) {
const newArr = []; const newArr = [];
const tmpArr = arr.concat(); const tmpArr = arr.concat();
while (tmpArr.length > 0) { while (tmpArr.length > 0) {
...@@ -106,9 +116,9 @@ export function randomSortByArr(arr) { ...@@ -106,9 +116,9 @@ export function randomSortByArr(arr) {
tmpArr.splice(randomIndex, 1); tmpArr.splice(randomIndex, 1);
} }
return newArr; return newArr;
} }
export async function asyncTweenTo(node, duration, obj, ease = undefined) { export async function asyncTweenTo(node, duration, obj, ease = undefined) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.to(duration, obj, ease) .to(duration, obj, ease)
...@@ -117,9 +127,9 @@ export async function asyncTweenTo(node, duration, obj, ease = undefined) { ...@@ -117,9 +127,9 @@ export async function asyncTweenTo(node, duration, obj, ease = undefined) {
}) })
.start(); .start();
}); });
} }
export async function asyncTweenBy(node, duration, obj, ease = undefined) { export async function asyncTweenBy(node, duration, obj, ease = undefined) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.by(duration, obj, ease) .by(duration, obj, ease)
...@@ -128,28 +138,36 @@ export async function asyncTweenBy(node, duration, obj, ease = undefined) { ...@@ -128,28 +138,36 @@ export async function asyncTweenBy(node, duration, obj, ease = undefined) {
}) })
.start(); .start();
}); });
} }
export async function asyncPlayDragonBoneAnimation(node, animationName, time = 1, onFrameEvent) { export async function asyncPlayDragonBoneAnimation(
node,
animationName,
time = 1,
onFrameEvent
) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
node.getComponent(dragonBones.ArmatureDisplay) node
.getComponent(dragonBones.ArmatureDisplay)
.once(dragonBones.EventObject.COMPLETE, () => { .once(dragonBones.EventObject.COMPLETE, () => {
resolve(); resolve();
}); });
node.getComponent(dragonBones.ArmatureDisplay) node
.getComponent(dragonBones.ArmatureDisplay)
.on(dragonBones.EventObject.FRAME_EVENT, ({ name }) => { .on(dragonBones.EventObject.FRAME_EVENT, ({ name }) => {
if (onFrameEvent && typeof (onFrameEvent) == 'function') { if (onFrameEvent && typeof onFrameEvent == "function") {
onFrameEvent(name); onFrameEvent(name);
} }
}); });
node.getComponent(dragonBones.ArmatureDisplay) node
.getComponent(dragonBones.ArmatureDisplay)
.playAnimation(animationName, time); .playAnimation(animationName, time);
}); });
} }
export async function asyncPlayEffectByUrl(url, loop = false) { export async function asyncPlayEffectByUrl(url, loop = false) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.assetManager.loadRemote(url, (err, clip) => { cc.assetManager.loadRemote(url, (err, clip) => {
console.log(clip); console.log(clip);
...@@ -157,9 +175,9 @@ export async function asyncPlayEffectByUrl(url, loop = false) { ...@@ -157,9 +175,9 @@ export async function asyncPlayEffectByUrl(url, loop = false) {
resolve(); resolve();
}); });
}); });
} }
export async function jelly(node) { export async function jelly(node) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
cc.tween(node) cc.tween(node)
.to(0.1, { scaleX: 0.9, scaleY: 1.1 }) .to(0.1, { scaleX: 0.9, scaleY: 1.1 })
...@@ -168,19 +186,25 @@ export async function jelly(node) { ...@@ -168,19 +186,25 @@ export async function jelly(node) {
.call(resolve) .call(resolve)
.start(); .start();
}); });
} }
export async function asyncDelay(time) { export async function asyncDelay(time) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
resolve(); resolve();
}, time * 1000); }, time * 1000);
}) });
} }
export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side = cc.v2(0, 100), range = 50, number = 100) {
new Array(number).fill(' ').forEach(async (_, i) => {
export async function showFireworks(
baseNode,
nodeList,
pos = cc.v2(0, 0),
side = cc.v2(0, 100),
range = 50,
number = 100
) {
new Array(number).fill(" ").forEach(async (_, i) => {
let rabbonNode = new cc.Node(); let rabbonNode = new cc.Node();
rabbonNode.parent = baseNode; rabbonNode.parent = baseNode;
rabbonNode.x = pos.x; rabbonNode.x = pos.x;
...@@ -197,16 +221,19 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side ...@@ -197,16 +221,19 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side
const rate = Math.random(); const rate = Math.random();
const angle = Math.PI * (Math.random() * 2 - 1); const angle = Math.PI * (Math.random() * 2 - 1);
await asyncTweenBy(rabbonNode, 0.3, { await asyncTweenBy(
rabbonNode,
0.3,
{
x: side.x * rate + Math.cos(angle) * range * rate, x: side.x * rate + Math.cos(angle) * range * rate,
y: side.y * rate + Math.sin(angle) * range * rate y: side.y * rate + Math.sin(angle) * range * rate,
}, { },
easing: 'quadIn' {
}); easing: "quadIn",
}
);
cc.tween(rabbonNode) cc.tween(rabbonNode).by(8, { y: -2000 }).start();
.by(8, { y: -2000 })
.start();
rabbonFall(rabbonNode); rabbonFall(rabbonNode);
...@@ -230,11 +257,12 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side ...@@ -230,11 +257,12 @@ export async function showFireworks(baseNode, nodeList, pos = cc.v2(0, 0), side
}) })
.start(); .start();
}); });
} }
async function rabbonFall(node) { async function rabbonFall(node) {
const time = 1 + Math.random(); const time = 1 + Math.random();
const offsetX = RandomInt(-200, 200) * time; const offsetX = RandomInt(-200, 200) * time;
await asyncTweenBy(node, time, { x: offsetX, angle: offsetX * 60 / 200 }); await asyncTweenBy(node, time, { x: offsetX, angle: (offsetX * 60) / 200 });
rabbonFall(node); rabbonFall(node);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment