|
@@ -1,24 +1,22 @@
|
|
|
-import { _decorator, Node, Label, EventTouch, Vec3, UITransform, Prefab, Size, tween, easing} from 'cc';
|
|
|
+import { _decorator, Node, Label, EventTouch, Vec3, UITransform, Prefab, tween} from 'cc';
|
|
|
import { BaseExp } from '../core/base/BaseExp';
|
|
|
-import List from '../third/List';
|
|
|
import { autoBind } from '../extend/AutoBind';
|
|
|
import { Constants } from '../data/Constants';
|
|
|
-import { stateMgr } from '../core/manager/StateManager';
|
|
|
import { userIns } from '../data/UserData';
|
|
|
+import { Utils } from '../utils/Utils';
|
|
|
+import { uiMgr } from '../core/manager/UIManager';
|
|
|
+import { stateMgr } from '../core/manager/StateManager';
|
|
|
import { ITEM_TYPE, ResUtil } from '../utils/ResUtil';
|
|
|
-import MsgHints from '../utils/MsgHints';
|
|
|
import { PoolManager } from '../core/manager/PoolManager';
|
|
|
import { bundleMgr } from '../core/manager/BundleManager';
|
|
|
-import { Utils } from '../utils/Utils';
|
|
|
-import { uiMgr } from '../core/manager/UIManager';
|
|
|
import { audioMgr } from '../core/manager/AudioManager';
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
//扔飞镖
|
|
|
@ccclass('TossBoomerangUI')
|
|
|
export class TossBoomerangUI extends BaseExp {
|
|
|
- @autoBind({ type: Node, tooltip: "靶面" })
|
|
|
- public target_plate: Node;
|
|
|
+ @autoBind({ type: Node, tooltip: "靶面中的所有靶心" })
|
|
|
+ public content: Node;
|
|
|
@autoBind({ type: Node, tooltip: "飞镖按钮" })
|
|
|
public shoot_btn: Node;
|
|
|
@autoBind({ type: Label, tooltip: "金币文本" })
|
|
@@ -37,6 +35,9 @@ export class TossBoomerangUI extends BaseExp {
|
|
|
//注册动态变化值
|
|
|
stateMgr.registerUI(Constants.gold, this.gold_lable);
|
|
|
stateMgr.registerUI(Constants.diamond, this.diamond_lable);
|
|
|
+ this.content.children.forEach(child => {
|
|
|
+ child.on(Node.EventType.TOUCH_END, this.startToss.bind(this), this);
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
public show(...args: any[]){
|
|
@@ -64,12 +65,20 @@ export class TossBoomerangUI extends BaseExp {
|
|
|
/**
|
|
|
* 扔飞镖数据
|
|
|
*/
|
|
|
- public async startToss(){
|
|
|
+ public startToss(event:EventTouch = null,param:any = null){
|
|
|
if(this.isBoomeranging)return;
|
|
|
if(userIns.data.boomerang <= 0){
|
|
|
this.buyBoomerang();
|
|
|
return;
|
|
|
}
|
|
|
+ let target_plate:Node = null;
|
|
|
+ if(event){
|
|
|
+ target_plate = event.target;
|
|
|
+ }else{
|
|
|
+ //获取靶心
|
|
|
+ let r:number = Utils.getRandomInt(0,this.content.children.length);
|
|
|
+ target_plate = this.content.children[r];
|
|
|
+ }
|
|
|
audioMgr.playOneShot(Constants.audios.dart);
|
|
|
this.isBoomeranging = true;
|
|
|
userIns.data.boomerang -= 1;
|
|
@@ -92,75 +101,77 @@ export class TossBoomerangUI extends BaseExp {
|
|
|
selectedItem = userIns.boomerangTable[0];
|
|
|
}
|
|
|
//加载飞镖预制体
|
|
|
- const ret: any = await bundleMgr.loadAsset(Constants.bundleName.prefabs, `items/tossBoomerang`, Prefab);
|
|
|
- var boomerang:Node = PoolManager.getNode(ret,this.target_plate.parent);
|
|
|
- //获取靶面尺寸(假设靶面锚点在中心)
|
|
|
- const targetSize = this.target_plate.getComponent(UITransform)?.contentSize;
|
|
|
- //生成随机角度(0-360度)
|
|
|
- const angle = Math.random() * Math.PI * 2;
|
|
|
- //根据权重计算偏移比例(权重越大,offsetRatio越小) 获取靶面中心的世界坐标
|
|
|
- const targetWorldPos = this.target_plate.parent.getComponent(UITransform)
|
|
|
- .convertToWorldSpaceAR(this.target_plate.position);
|
|
|
- //转换为父节点坐标系下的坐标
|
|
|
- const targetCenter = this.target_plate.parent.getComponent(UITransform)
|
|
|
- .convertToNodeSpaceAR(targetWorldPos);
|
|
|
- //生成基于靶心坐标的随机偏移(权重越大越靠近中心)平方曲线增强中心聚集 缩小最大偏移范围
|
|
|
- const maxRadius = targetSize.width * 0.2;
|
|
|
- const offsetRatio = Math.pow(1 - (selectedItem.weight / totalWeight), 2);
|
|
|
- //最终落点计算(以靶心为基准)
|
|
|
- const targetPos = new Vec3(
|
|
|
- targetCenter.x + Math.cos(angle) * maxRadius * offsetRatio,
|
|
|
- targetCenter.y + Math.sin(angle) * maxRadius * offsetRatio,
|
|
|
- 0
|
|
|
- );
|
|
|
- const targetUITransform = this.target_plate.getComponent(UITransform);
|
|
|
- const radius = targetUITransform.contentSize.width / 2.0 * 1.5; // 半径取宽度的一半
|
|
|
- const randomAngle = Math.random() * Math.PI * 2; // 生成0-360度的随机角度
|
|
|
- //新起始点计算(靶面中心 + 半径 * 随机角度)
|
|
|
- const startPos = new Vec3(
|
|
|
- this.target_plate.position.x + radius * Math.cos(randomAngle),
|
|
|
- this.target_plate.position.y + radius * Math.sin(randomAngle),
|
|
|
- 0
|
|
|
- );
|
|
|
- //飞镖初始位置(从屏幕下方飞出)
|
|
|
- //let startPos: Vec3 = Utils.convertPosition(this.shoot_btn,this.target_plate.parent);
|
|
|
- boomerang.position = startPos.clone();
|
|
|
- let middePos = Utils.randomUIPointGenerator(this.target_plate,startPos,200);
|
|
|
- let pos = Utils.calculateParabolaCenter(startPos,targetPos);
|
|
|
- let controlPoint = new Vec3(middePos.x + pos.x,middePos.y,middePos.z)
|
|
|
- //贝塞尔轨迹计算
|
|
|
- const calculateTrajectory = (t: number, p0: Vec3, p1: Vec3, p2: Vec3) => {
|
|
|
- const u = 1 - t;
|
|
|
- return new Vec3(
|
|
|
- u*u*p0.x + 2*u*t*p1.x + t*t*p2.x,
|
|
|
- u*u*p0.y + 2*u*t*p1.y + t*t*p2.y,
|
|
|
+ ResUtil.loadRes(`items/tossBoomerang`).then((ret:Node)=>{
|
|
|
+ var boomerang:Node = PoolManager.getNode(ret,this.content);
|
|
|
+ //获取靶面尺寸(假设靶面锚点在中心)
|
|
|
+ const targetSize = target_plate.getComponent(UITransform)?.contentSize;
|
|
|
+ //生成随机角度(0-360度)
|
|
|
+ const angle = Math.random() * Math.PI * 2;
|
|
|
+ //根据权重计算偏移比例(权重越大,offsetRatio越小) 获取靶面中心的世界坐标
|
|
|
+ const targetWorldPos = target_plate.parent.getComponent(UITransform)
|
|
|
+ .convertToWorldSpaceAR(target_plate.position);
|
|
|
+ //转换为父节点坐标系下的坐标
|
|
|
+ const targetCenter = target_plate.parent.getComponent(UITransform)
|
|
|
+ .convertToNodeSpaceAR(targetWorldPos);
|
|
|
+ //生成基于靶心坐标的随机偏移(权重越大越靠近中心)平方曲线增强中心聚集 缩小最大偏移范围
|
|
|
+ const maxRadius = targetSize.width * 0.2;
|
|
|
+ const offsetRatio = Math.pow(1 - (selectedItem.weight / totalWeight), 2);
|
|
|
+ //最终落点计算(以靶心为基准)
|
|
|
+ const targetPos = new Vec3(
|
|
|
+ targetCenter.x + Math.cos(angle) * maxRadius * offsetRatio,
|
|
|
+ targetCenter.y + Math.sin(angle) * maxRadius * offsetRatio,
|
|
|
+ 0
|
|
|
+ );
|
|
|
+ const targetUITransform = target_plate.getComponent(UITransform);
|
|
|
+ const radius = targetUITransform.contentSize.width / 2.0 * 1.5; // 半径取宽度的一半
|
|
|
+ const randomAngle = Math.random() * Math.PI * 2; // 生成0-360度的随机角度
|
|
|
+ //新起始点计算(靶面中心 + 半径 * 随机角度)
|
|
|
+ const startPos = new Vec3(
|
|
|
+ target_plate.position.x + radius * Math.cos(randomAngle),
|
|
|
+ target_plate.position.y + radius * Math.sin(randomAngle),
|
|
|
0
|
|
|
);
|
|
|
- };
|
|
|
- tween(boomerang)
|
|
|
- .to(0.4, {}, {
|
|
|
- onUpdate: (_, ratio: number) => {//计算当前帧位置
|
|
|
- const currentPos = calculateTrajectory(
|
|
|
- ratio,
|
|
|
- startPos,
|
|
|
- controlPoint,
|
|
|
- targetPos
|
|
|
- );
|
|
|
- //更新位置和旋转
|
|
|
- boomerang.position = currentPos;
|
|
|
- boomerang.angle = Utils.getAngle(currentPos,targetPos)-90;
|
|
|
- },
|
|
|
- onComplete: () => {
|
|
|
+ //飞镖初始位置(从屏幕下方飞出)
|
|
|
+ //let startPos: Vec3 = Utils.convertPosition(this.shoot_btn,this.target_plate.parent);
|
|
|
+ boomerang.position = startPos.clone();
|
|
|
+ let middePos = Utils.randomUIPointGenerator(target_plate,startPos,200);
|
|
|
+ let pos = Utils.calculateParabolaCenter(startPos,targetPos);
|
|
|
+ let controlPoint = new Vec3(middePos.x + pos.x,middePos.y,middePos.z)
|
|
|
+ //贝塞尔轨迹计算
|
|
|
+ const calculateTrajectory = (t: number, p0: Vec3, p1: Vec3, p2: Vec3) => {
|
|
|
+ const u = 1 - t;
|
|
|
+ return new Vec3(
|
|
|
+ u*u*p0.x + 2*u*t*p1.x + t*t*p2.x,
|
|
|
+ u*u*p0.y + 2*u*t*p1.y + t*t*p2.y,
|
|
|
+ 0
|
|
|
+ );
|
|
|
+ };
|
|
|
+ tween(boomerang)
|
|
|
+ .to(0.8, {}, {
|
|
|
+ onUpdate: (_, ratio: number) => {//计算当前帧位置
|
|
|
+ const currentPos = calculateTrajectory(
|
|
|
+ ratio,
|
|
|
+ startPos,
|
|
|
+ controlPoint,
|
|
|
+ targetPos
|
|
|
+ );
|
|
|
+ //更新位置和旋转
|
|
|
+ boomerang.position = currentPos;
|
|
|
+ boomerang.angle = Utils.getAngle(currentPos,targetPos)-90;
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .call(()=>{
|
|
|
+ boomerang.position = targetPos;
|
|
|
+ })
|
|
|
+ .start();
|
|
|
+ this.scheduleOnce(() => {
|
|
|
if(boomerang.parent) {
|
|
|
- setTimeout(() => {
|
|
|
- PoolManager.putNode(boomerang);
|
|
|
- this.handleReward(selectedItem);
|
|
|
- this.isBoomeranging = false;
|
|
|
- },0.2);
|
|
|
+ PoolManager.putNode(boomerang);
|
|
|
+ this.handleReward(selectedItem);
|
|
|
+ this.isBoomeranging = false;
|
|
|
}
|
|
|
- }
|
|
|
- })
|
|
|
- .start();
|
|
|
+ },1)
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/**
|