Commit d3680af2 authored by Seaborn Lee's avatar Seaborn Lee

feat: add star

parent cb466971
......@@ -11,6 +11,8 @@ cc.Class({
extends: cc.Component,
properties: {
collectedCount: 0,
layout: null,
bgHeight: {
set: function (bgHeight) {
this._bgHeight = bgHeight;
......@@ -25,143 +27,105 @@ cc.Class({
},
},
start() {},
onLoad() {
this.layout = cc.find("layout", this.node);
},
setStarCount() {
// let disH = 80;
// // let disScale = 1;
// // if (this._count > 5) {
// // disH = (400 / gArr.length);
// // disScale = 5 / gArr.length;
// // }
// for (let i = 0; i < this._count; i++) {
// const starBg = this.getSprNode("img/star_bg");
// starBg.y = -i * disH;
// this.node.addChild(starBg);
// }
this.layout.height = this._bgHeight - 100;
let length = this._count;
const layout = cc.find('layout', this.node);
layout.height = this._bgHeight - 100;
const paddingY = layout.getComponent(cc.Layout).spacingY;
const _starBase = this.createStarBase(this.layout);
for (let i = 0; i < length; i++) {
const starBase = this.getSprNode("img/star_bg");
starBase.name = `starBase_${i}`;
starBase.scale = this.between(0.5, (layout.height / length - paddingY) / starBase.height, 1);
starBase.parent = layout;
const starBase = cc.instantiate(_starBase);
starBase.parent = this.layout;
}
this.currentStarIdx = 0;
},
getSprNode(resName) {
const sf = cc.find(resName, this.node).getComponent(cc.Sprite).spriteFrame;
const node = new cc.Node();
node.addComponent(cc.Sprite).spriteFrame = sf;
return node;
createStarBase(layout) {
const paddingY = layout.getComponent(cc.Layout).spacingY;
const starBase = getSprNode("img/star_bg", this.node);
const star = getSprNode("img/star", this.node);
star.name = "Star";
const starBig = getSprNode("img/icon_bigstar", this.node);
starBig.name = "StarBig";
star.addChild(starBig);
star.active = false;
starBase.addChild(star);
starBase.scale = this.between(
0.5,
(layout.height / length - paddingY) / starBase.height,
1
);
return starBase;
},
bigStarArr: null,
initStar() {
const gArr = this.data.groupArr;
if (gArr.length <= 1) {
add() {
this.collectedCount++;
if (this.collectedCount > this._count) {
return;
}
this.cleanStar();
this.bigStarArr = [];
let baseX = this.canvas.width / 2 - 80 * this._mapScaleMin;
let baseY = this.canvas.height / 2 - 80 * this._mapScaleMin;
let disH = 80 * this._mapScaleMin;
let disScale = 1;
if (gArr.length > 5) {
disH = (400 / gArr.length) * this._mapScaleMin;
disScale = 5 / gArr.length;
}
for (let i = 0; i < gArr.length; i++) {
const starBg = getSprNode("star_bg");
this.canvas.addChild(starBg, 10);
starBg.x = baseX;
starBg.y = baseY - i * disH;
starBg.scale = disScale;
const starTop = getSprNode("star");
starBg.addChild(starTop);
starTop.active = false;
starBg.star = starTop;
const starBig = getSprNode("icon_bigstar");
starTop.addChild(starBig);
const starBase = this.layout.getChildren()[this.collectedCount - 1];
const star = starBase.getChildByName("Star");
const starBig = star.getChildByName("StarBig");
starBig.angle = -90;
starBg.starBig = starBig;
this.bigStarArr.push(starBg);
}
console.log("bigStarArr: ", this.bigStarArr);
},
cleanStar() {
if (this.bigStarArr && this.bigStarArr.length > 0) {
for (let i = 0; i < this.bigStarArr.length; i++) {
this.bigStarArr[i].removeFromParent();
}
}
},
currentStarIdx: null,
showStar() {
if (!this.currentStarIdx) {
this.currentStarIdx = 0;
}
const starBase = cc.find(`Canvas/layout/starBase_${this.currentStarIdx}`);
if (!starBase) {
return;
}
const star = starBase.getChildByName('Star');
const starBig = star.getChildByName('StarBig');
starBig.scale = 1;
star.active = true;
star.scaleX = 0.7 / starBase.scale;
star.scaleY = 1 / starBase.scale;
star.angle = 90;
const canvas = cc.find('Canvas');
const startPos = this.exchangeNodePos(star.parent, canvas, cc.v2(0, -canvas.height / 2));
const middlePos = this.exchangeNodePos(star.parent, canvas, cc.v2(0, -canvas.height / 4));
const canvas = cc.find("Canvas");
const startPos = this.exchangeNodePos(
star.parent,
canvas,
cc.v2(0, -canvas.height / 2)
);
const middlePos = this.exchangeNodePos(
star.parent,
canvas,
cc.v2(0, -canvas.height / 4)
);
star.x = startPos.x;
star.y = startPos.y - starBig.height;
console.log('middlePos = ' + JSON.stringify(middlePos));
console.log("middlePos = " + JSON.stringify(middlePos));
const time = 1;
cc.tween(star)
.to(0.3, { y: middlePos.y + 80 }, { easing: 'quadOut' })
.to(0.1, { y: middlePos.y + 40, scaleX: 1.2 / starBase.scale, scaleY: 0.8 / starBase.scale }, { easing: 'quadOut' })
.to(0.1, { y: middlePos.y, scaleX: 1 / starBase.scale, scaleY: 1 / starBase.scale }, { easing: 'quadOut' })
.to(0.3, { y: middlePos.y + 80 }, { easing: "quadOut" })
.to(
0.1,
{
y: middlePos.y + 40,
scaleX: 1.2 / starBase.scale,
scaleY: 0.8 / starBase.scale,
},
{ easing: "quadOut" }
)
.to(
0.1,
{
y: middlePos.y,
scaleX: 1 / starBase.scale,
scaleY: 1 / starBase.scale,
},
{ easing: "quadOut" }
)
.delay(0.1)
.to(0.8, { angle: -720, scale: 1 })
.start();
cc.tween(star)
.delay(0.6)
.to(0.8, { x: 0 }, { easing: 'quadIn' })
.start();
cc.tween(star)
.delay(0.6)
.to(0.8, { y: 0 }, { easing: 'quadOut' })
.start();
cc.tween(star).delay(0.6).to(0.8, { x: 0 }, { easing: "quadIn" }).start();
cc.tween(star).delay(0.6).to(0.8, { y: 0 }, { easing: "quadOut" }).start();
cc.tween(starBig)
.delay(0.6)
.to(0.8, { scale: 0 }, { easing: 'quadOut' })
.to(0.8, { scale: 0 }, { easing: "quadOut" })
.call(() => {
// this.checkGameEnd();
})
......@@ -170,13 +134,12 @@ cc.Class({
this.currentStarIdx++;
},
exchangeNodePos(targetNode, baseNode, basePos) {
return targetNode.convertToNodeSpaceAR(baseNode.convertToWorldSpaceAR(cc.v2(basePos.x, basePos.y)));
return targetNode.convertToNodeSpaceAR(
baseNode.convertToWorldSpaceAR(cc.v2(basePos.x, basePos.y))
);
},
between(a, b, c) {
return [a, b, c].sort()[1];
},
......
......@@ -291,6 +291,12 @@ cc.Class({
this.initStar();
setInterval(() => {
const starNode = cc.find("Canvas/starNode");
const script = starNode.getComponent("star");
script.add();
}, 2000);
// setTimeout(() => {
// this.showBigStar();
// }, 2000);
......
export function getPosByAngle(angle, len) {
const radian = angle * Math.PI / 180;
const radian = (angle * Math.PI) / 180;
const x = Math.sin(radian) * len;
const y = Math.cos(radian) * len;
return { x, y };
}
export function getAngleByPos(px, py, mx, my) {
const x = Math.abs(px - mx);
const y = Math.abs(py - my);
const z = Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
const cos = y / z;
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;
}
if (mx === px && my > py) {// 鼠标在y轴负方向上
if (mx === px && my > py) {
// 鼠标在y轴负方向上
angle = 180;
}
if (mx > px && my === py) {// 鼠标在x轴正方向上
if (mx > px && my === py) {
// 鼠标在x轴正方向上
angle = 90;
}
if (mx < px && my > py) {// 鼠标在第三象限
if (mx < px && my > py) {
// 鼠标在第三象限
angle = 180 + angle;
}
if (mx < px && my === py) {// 鼠标在x轴负方向
if (mx < px && my === py) {
// 鼠标在x轴负方向
angle = 270;
}
if (mx < px && my < py) {// 鼠标在第二象限
if (mx < px && my < py) {
// 鼠标在第二象限
angle = 360 - angle;
}
// console.log('angle: ', angle);
return angle;
}
export function exchangeNodePos(baseNode, targetNode) {
return baseNode.convertToNodeSpaceAR(targetNode._parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y)));
return baseNode.convertToNodeSpaceAR(
targetNode._parent.convertToWorldSpaceAR(cc.v2(targetNode.x, targetNode.y))
);
}
export function RandomInt(a, b = 0) {
......@@ -70,20 +74,26 @@ export function setSprNodeMaxLen(sprNode, maxW, maxH) {
sprNode.scale = Math.round(s * 1000) / 1000;
}
export function getSpriteFrimeByUrl(url, cb) {
cc.loader.load({url}, (err, img) => {
const spriteFrame = new cc.SpriteFrame(img)
cc.loader.load({ url }, (err, img) => {
const spriteFrame = new cc.SpriteFrame(img);
if (cb) {
cb(spriteFrame);
}
})
});
}
export function getSprNode(resName) {
const sf = cc.find('Canvas/res/img/' + resName).getComponent(cc.Sprite).spriteFrame;
export function getSprNode(resName, root) {
let resNode;
if (root) {
resNode = cc.find(resName, root);
} else {
resNode = cc.find("Canvas/res/img/" + resName);
}
const node = new cc.Node();
node.addComponent(cc.Sprite).spriteFrame = sf;
node.addComponent(cc.Sprite).spriteFrame = resNode.getComponent(
cc.Sprite
).spriteFrame;
return node;
}
......@@ -95,12 +105,10 @@ export function getSprNodeByUrl(url, cb) {
if (cb) {
cb(spr);
}
})
});
}
export function playAudio(audioClip, cb=null) {
export function playAudio(audioClip, cb = null) {
if (audioClip) {
const audioId = cc.audioEngine.playEffect(audioClip, false, 0.8);
if (cb) {
......@@ -113,8 +121,7 @@ export function playAudio(audioClip, cb=null) {
}
}
export function playAudioByUrl(audio_url, cb=null) {
export function playAudioByUrl(audio_url, cb = null) {
if (audio_url) {
cc.assetManager.loadRemote(audio_url, (err, audioClip) => {
playAudio(audioClip, cb);
......@@ -122,9 +129,6 @@ export function playAudioByUrl(audio_url, cb=null) {
}
}
export async function asyncDelay(time) {
return new Promise((resolve, reject) => {
try {
......@@ -134,7 +138,7 @@ export async function asyncDelay(time) {
} catch (e) {
reject(e);
}
})
});
}
export class FireworkSettings {
......@@ -145,7 +149,9 @@ export class FireworkSettings {
range; // 扩散范围
number; // 发射数量
scalseRange; // 缩放范围
constructor(baseNode, nodeList,
constructor(
baseNode,
nodeList,
pos = cc.v2(0, 0),
side = cc.v2(0, 100),
range = 50,
......@@ -168,15 +174,22 @@ export class FireworkSettings {
firework.pos,
firework.side,
firework.range,
firework.number,
firework.number
);
}
}
export async function showFireworks(fireworkSettings) {
const { baseNode, nodeList, pos, side, range, number, scalseRange } = fireworkSettings;
new Array(number).fill(' ').forEach(async (_, i) => {
const {
baseNode,
nodeList,
pos,
side,
range,
number,
scalseRange,
} = fireworkSettings;
new Array(number).fill(" ").forEach(async (_, i) => {
let rabbonNode = new cc.Node();
rabbonNode.parent = baseNode;
rabbonNode.x = pos.x;
......@@ -194,16 +207,19 @@ export async function showFireworks(fireworkSettings) {
const rate = Math.random();
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,
y: side.y * rate + Math.sin(angle) * range * rate
}, {
easing: 'quadIn'
});
y: side.y * rate + Math.sin(angle) * range * rate,
},
{
easing: "quadIn",
}
);
cc.tween(rabbonNode)
.by(8, { y: -2000 })
.start();
cc.tween(rabbonNode).by(8, { y: -2000 }).start();
cc.tween(rabbonNode)
.to(5, { scale: (Math.random() - 0.5) * scalseRange + 1 })
......@@ -236,11 +252,10 @@ export async function showFireworks(fireworkSettings) {
async function rabbonFall(node) {
const time = 1 + Math.random();
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);
}
export async function asyncTweenTo(node, duration, obj, ease = undefined) {
return new Promise((resolve, reject) => {
try {
......@@ -292,9 +307,9 @@ export function showTrebleFirework(baseNode, rabbonList) {
showFireworks(right);
}
export function delayCall(time, cb) {
return cc.tween({})
return cc
.tween({})
.delay(time)
.call(() => {
if (cb) {
......@@ -313,13 +328,11 @@ export function removeFromArr(arr, item) {
return false;
}
export function showBtnAnima(btn, cb=null) {
export function showBtnAnima(btn, cb = null) {
const baseS = btn.scale;
cc.tween(btn)
.to(0.05, {scale: 0.9 * baseS})
.to(0.05, {scale: 1 * baseS})
.to(0.05, { scale: 0.9 * baseS })
.to(0.05, { scale: 1 * baseS })
.call(() => {
if (cb) {
cb();
......@@ -332,21 +345,40 @@ export function jellyShake(node) {
const baseS = node.scale;
const time = 1;
cc.tween(node)
.to(time / 5 / 2, {scaleX: baseS * 0.8, scaleY: baseS * 1.2}, {easing: "sineInOut"})
.to(time / 5, {scaleX: baseS * 1.1, scaleY: baseS * 0.9}, {easing: "sineInOut"})
.to(time / 5, {scaleX: baseS * 0.95, scaleY: baseS * 1.15}, {easing: "sineInOut"})
.to(time / 5, {scaleX: baseS * 1.02, scaleY: baseS * 0.98}, {easing: "sineInOut"})
.to(time / 5, {scaleX: baseS * 1, scaleY: baseS * 1}, {easing: "sineInOut"})
.to(
time / 5 / 2,
{ scaleX: baseS * 0.8, scaleY: baseS * 1.2 },
{ easing: "sineInOut" }
)
.to(
time / 5,
{ scaleX: baseS * 1.1, scaleY: baseS * 0.9 },
{ easing: "sineInOut" }
)
.to(
time / 5,
{ scaleX: baseS * 0.95, scaleY: baseS * 1.15 },
{ easing: "sineInOut" }
)
.to(
time / 5,
{ scaleX: baseS * 1.02, scaleY: baseS * 0.98 },
{ easing: "sineInOut" }
)
.to(
time / 5,
{ scaleX: baseS * 1, scaleY: baseS * 1 },
{ easing: "sineInOut" }
)
.start();
}
export function onHomeworkFinish() {
const middleLayer = cc.find('middleLayer');
const middleLayer = cc.find("middleLayer");
if (middleLayer) {
const middleLayerComponent = middleLayer.getComponent('middleLayer');
if (middleLayerComponent.role == 'student') {
middleLayerComponent.onHomeworkFinish(() => { });
const middleLayerComponent = middleLayer.getComponent("middleLayer");
if (middleLayerComponent.role == "student") {
middleLayerComponent.onHomeworkFinish(() => {});
}
return;
}
......
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