/** 发射子弹 */
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;
}
Cocos Creator2.x发射子弹计算角度核心逻辑
最新推荐文章于 2024-02-25 12:36:09 发布