Cocos Creator2.x发射子弹计算角度核心逻辑


    /** 发射子弹 */
    private sendAmmo(wpos: cc.Vec2) {
        let is_wang = this.checkIsWang();
        if (is_wang) {
            // 是网的话重置击杀数
            this.kill_num = 0;
            this.updateKillLight();
        }
        let item_type = is_wang ? ResConfig.EPrefab.wang_item : ResConfig.EPrefab.fort_ammo;
        let node = UIItemCreater.createItem(item_type);
        node.setPosition(cc.Vec2.ZERO);
        node.angle = 0;
        this.ui.setWPosition(node, this.fort_ammo_pos);
        node.setParent(this.ammo_parent);
        let t_pos = node.convertToNodeSpaceAR(wpos);
        t_pos.normalizeSelf();
        t_pos.mulSelf(1000);

        let red = ColorHelper.hexToRgbColor("#FF3F3F");
        let white = ColorHelper.hexToRgbColor("#FFFFFF");
        let is_collider = false;
        //绑定碰撞监听
        let onCollisionEnter = (other: cc.PolygonCollider, self: cc.PolygonCollider) => {
            // 不是网并且已经碰撞了就不执行了
            if (!is_wang && is_collider) return;
            is_collider = true;
            if (other) {
                let other_node = other.node;
                // 抓到鱼发放奖励金币
                let reward_call = () => {
                    this.ui.playCoinAnim(
                        this.view,
                        this.coin_icon,
                        this.ui.getWPos(other_node),
                        ResConfig.EPrefab.coin,
                        null,
                        10,
                        null,
                        () => {
                            this.playerModel.setEnergy(this.getFishRewardCoin(other.tag));
                            UIItemCreater.recycleItem(
                                ResConfig.EPrefab![other_node.name],
                                other_node
                            );
                        }
                    );
                };
                //鱼闪红光
                this.ui.setColor(other_node, red);
                let is_kill = false;
                if (is_wang) {
                    //网不销毁一直抓
                    is_kill = true;
                } else {
                    //概率抓鱼
                    // if (Utils.randomHitRate((this.playerModel.fish_cost_coin / 10) * Utils.random(1, 2))) {
                    if (Utils.randomHitRate(this.playerModel.fish_cost_coin / 10)) {
                        this.kill_num++;
                        this.updateKillLight();
                        is_kill = true;
                    }
                }
                if (is_kill) {
                    cc.Tween.stopAllByTarget(other_node);
                    other_node.active = false;
                    // this.ui.setComponentEnabled(other_node, cc.PolygonCollider, false);
                }
                this.scheduleOnce(() => {
                    this.ui.setColor(other_node, white);
                    if (is_kill) reward_call();
                }, 0.2);
                if (!is_wang) {
                    //子弹销毁
                    cc.Tween.stopAllByTarget(node);
                    UIItemCreater.recycleItem(ResConfig.EPrefab.fort_ammo, node);
                }
            }
        };
        let com = node.getComponent(PolygonColliderComUtil);
        if (!com) com = node.addComponent(PolygonColliderComUtil);
        com.reCallback(onCollisionEnter);
        //开移动
        cc.tween(node)
            .to(1, { position: cc.v3(t_pos) })
            .call(() => {
                if (cc.isValid(node)) UIItemCreater.recycleItem(item_type, node);
            })
            .start();

        // 计算角度
        var angle = Math.atan2(t_pos.y - this.node.y, t_pos.x - this.node.x) * (180 / Math.PI);
        // 设置节点旋转角度
        node.angle = angle - 90;
        this.fort.angle = angle - 90;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值