当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
1 Star 0 Fork 3

子麒/kele_wxapkg
关闭

forked from 虚幻小黑侠/kele_wxapkg
关闭
 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
AwardFlyEffect.ts 6.40 KB
一键复制 编辑 原始数据 按行查看 历史
fangmin 提交于 2020-01-09 16:45 . 添加效果类文件
class AwardFlyEffect extends egret.DisplayObjectContainer {
static flyUnitArr: FlyUnit[] = [];
static _targetPostion = {};
static register(id: number, position: { x: number, y: number }) {
this._targetPostion[id] = position;
}
static fly(start: { x: number, y: number }, data: any, end?: { x: number, y: number }, options?: any) {
let awards = data.awards;
if (awards && Array.isArray(awards)) {
for (let i = 0; i < awards.length; i++) {
let award = awards[i];
if (award.id == 1001) {
Game.data["frozenKey"]("coin", BigNum.add(Game.data.coin, award.cnt));
}
if (award.id == 1002) {
Game.data["frozenKey"]("diamond", Game.data.diamond + award.cnt);
}
AwardFlyEffect._fly(start, awards[i], end, options);
}
}
}
static _fly(start: { x: number, y: number }, awards: { id: number, cnt: number }, end?: { x: number, y: number }, options?: any) {
if (!end) {
end = this._targetPostion[awards.id];
}
if (!end) {
console.error("没有目标点...")
return;
}
if (!options) {
options = {};
}
let count = awards.cnt || 1;
let type = options.type || 1;
if (count <= 0) {
egret.error("飞行动画的参数不能小于0");
} else {
count = Math.min(count, 10);
let flyCompleteCount = 0;
let flyCount = count;
while (count--) {
let unit = AwardFlyEffect.flyUnitArr.pop() || new FlyUnit();
let source = awards.id == 1001 ? "main_coin_png" : "main_diamond_png";
unit.setData({ source});
let minAngle = options.minAngle || 30;
let maxAngle = options.maxAngle || 150;
let minDistance = options.minDistance || 50;
let maxDistance = options.maxDistance || 80;
let angle = (minAngle + Math.random() * (maxAngle - minAngle));
let distance = minDistance + Math.random() * (maxDistance - minDistance);
let offX = Math.cos(angle) * distance;
let offY = Math.sin(angle) * distance;
let startOff = new egret.Point(offX, offY);
unit.once("complete", () => {
flyCompleteCount++;
if (flyCompleteCount >= flyCount) {
if (awards.id == 1001) {
Game.data["unFrozenKey"]("coin");
Game.emit("fly_coin_complete");
}
if (awards.id == 1002) {
Game.data["unFrozenKey"]("diamond");
Game.emit("fly_diamond_complete");
}
Game.playSound("getGold");
flyCompleteCount = 0;
}
}, null)
unit.fly(start, end, startOff);
Game.stage.addChild(unit);
}
}
}
}
class FlyUnit extends egret.DisplayObjectContainer {
private image: eui.Image;
private p0_x;
private p0_y;
private p1_x;
private p1_y;
private p2_x;
private p2_y;
public constructor() {
super();
this.image = new eui.Image();
this.image.touchEnabled = false;
this.addChild(this.image);
}
setData(options) {
this.image.source = options.source;
}
public setPos(x: number, y: number) {
this.x = x;
this.y = y;
}
/**
* 飞到目标点
* @param start 开始
* @param end 结束
* @param startOff 开始偏移
*/
public fly(start: { x: number, y: number }, end: { x: number, y: number }, startOff?: { x: number, y: number }) {
this.setPos(start.x, start.y);
let time = MathUtil.between(150, 180);
if (startOff && (startOff.x != start.x || startOff.y != start.y)) {
let tween = egret.Tween.get(this);
let toX = start.x + startOff.x;
let toY = start.y + startOff.y;
tween.to({ x: toX, y: toY }, time, egret.Ease.quartIn).wait(100).call(this.fly1, this, [start, end, startOff]);
} else {
this.fly1(start, end, startOff)
}
}
private fly1(start: { x: number, y: number }, end: { x: number, y: number }, startOff?: { x: number, y: number }) {
let time = (1000 + Math.random() * 500);
this.p0_x = this.x;
this.p0_y = this.y;
let off = this.calcOff(start.x, start.y, end.x, end.y, start.x + startOff.x, start.y + startOff.y);
this.p1_x = off.x;
this.p1_y = off.y;
this.p2_x = end.x;
this.p2_y = end.y;
let tween = egret.Tween.get(this);
tween.to({ factor: 1 }, time, egret.Ease.quadIn).call(this.flyComplete, this);
}
private flyComplete() {
this.dispatchEventWith("complete");
this.release();
AwardFlyEffect.flyUnitArr.push(this);
}
private calcOff(x, y, tx, ty, mx, my) {
var p = [x - tx, y - ty];
var q = [mx - tx, my - ty];
let v = p[0] * q[1] - p[1] * q[0];
let c = [x + (tx - x) * 0.5, y + (ty - y) * 0.5];
let point = new egret.Point();
if (v > 0) {
point.x = Math.min(this.stage.stageWidth - 20, c[0] + Math.max((x - mx) * 3, 200));
point.y = c[1];
} else if (v < 0) {
point.x = Math.max(c[0] + Math.max((x - mx) * 3, -200), 20);
point.y = c[1];
} else {
point.x = tx + mx - x;
point.y = ty + my - y;
}
return point;
}
public get factor(): number {
return 0;
}
//计算方法参考 二次贝塞尔公式
//(1 - t)^2 P0 + 2 t (1 - t) P1 + t^2 P2
public set factor(value: number) {
let t = value;
let p0_x = this.p0_x;
let p0_y = this.p0_y;
let p1_x = this.p1_x;
let p1_y = this.p1_y;
let p2_x = this.p2_x;
let p2_y = this.p2_y;
this.x = (1 - t) * (1 - t) * p0_x + 2 * t * (1 - t) * p1_x + t * t * p2_x;
this.y = (1 - t) * (1 - t) * p0_y + 2 * t * (1 - t) * p1_y + t * t * p2_y;
}
public release() {
DisplayUtil.removeFromParent(this);
egret.Tween.removeTweens(this);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/wwyysoft/kele_wxapkg.git
[email protected]:wwyysoft/kele_wxapkg.git
wwyysoft
kele_wxapkg
kele_wxapkg
master

搜索帮助