Cocos解耦移动和发射模块
目标玩家受到摇杆A控制移动和方向发射受到摇杆B负责方向和发射//玩家模块ccclass(Player) export class Player extends Component { //玩家速度 Speed:number 500; //玩家方向 property(Vec3) PlayerDir:Vec3; //虚拟摇杆 property(Node) Joystick:Node null; start() { } update(deltaTime: number) { //更新位置 this.PlayerDir this.Joystick.getComponent(Joystick).dir.normalize(); let newPos this.node.position.clone().add(this.PlayerDir.clone().multiplyScalar(this.Speed*deltaTime)); this.node.setPosition(newPos); //视角跟随移动方向 this.node.angle this.Joystick.getComponent(Joystick).calculateAngle() - 90; } }//发射模块import { _decorator, Component, instantiate, Node, Prefab, Vec3 } from cc; import { Joystick } from ../UI/Joystick; import { Bullet } from ../Game/Bullet; const { ccclass, property } _decorator; ccclass(Shell) export class Shell extends Component { // 第二个摇杆射击方向 property(Node) fireJoystick: Node null; // 发射点 property(Node) launchPoint: Node null; // 关联玩家节点用来跟着走 property(Node) player: Node null; // 输出给子弹的方向 public fireDir: Vec3 new Vec3(); // 子弹相关 //子弹预制体 property(Prefab) BulletPrefab: Prefab null; property(Node) LaunchPoint: Node null; //发射频率 property LaunchCD: number 0.2; //计时器 private LaunchTimer: number 0; //子弹存储 property(Node) BulletStorage:Node null; update(deltaTime: number) { // // 核心外壳 跟随玩家位置旋转跟随第二个摇杆 if (this.player) { this.node.setWorldPosition(this.player.worldPosition); } if (!this.fireJoystick) return; const dir this.fireJoystick.getComponent(Joystick).dir; if (dir.length() 0.1) return; // 外壳朝向射击方向 this.fireDir dir.normalize(); //外壳方向摇杆方向 const angle Math.atan2(dir.y, dir.x) * 180 / Math.PI - 90; this.node.angle angle; // this.LaunchTimer deltaTime; if(this.LaunchTimer this.LaunchCD){ //发射 this.Launch(); //重置计时器 this.LaunchTimer 0; } } Launch(){ //前置检查--预制体和父节点是否正常 if (!this.BulletPrefab || !this.LaunchPoint) { console.warn(请配置子弹预制体和发射点); return; } //实例化子弹,挂载父节点 const bullet instantiate(this.BulletPrefab); //设置父节点 bullet.parent this.BulletStorage; //设置子弹的世界位置 发射点位置 bullet.setWorldPosition(this.LaunchPoint.worldPosition); // 关键给子弹设置飞行方向 // const bulletComp bullet.getComponent(Bullet); if (bulletComp) { bulletComp.setDirection(this.fireDir); } } }//子弹模块import { _decorator, Component, Node, Vec3, instantiate, Prefab, Quat, CCObject, Game } from cc; import { Joystick } from ../UI/Joystick; import { GameManager } from ./GameManager; const { ccclass, property } _decorator; ccclass(Bullet) export class Bullet extends Component { // 子弹飞行方向 public BulletDir: Vec3 new Vec3(1,1,0); // 子弹速度 speed: number 300; // 子弹生命周期 lifeTime: number 5; // 已存活时间 private _aliveTime: number 0; //游戏管理器 property(Node) GameManagerNode:Nodenull; update(deltaTime: number) { // 累加存活时间--正常 this._aliveTime deltaTime; // 超过生命周期则销毁--正常 if (this._aliveTime this.lifeTime) { this.node.destroy(); return; } // 计算子弹移动--正常 const moveStep this.BulletDir.clone().multiplyScalar(this.speed * deltaTime); const newPos this.node.position.clone().add(moveStep); this.node.setPosition(newPos); // 朝方向移动 this.node.translate(this.BulletDir.clone().multiplyScalar(this.speed * deltaTime)); } // 设置飞行方向由外壳调用 setDirection(direction: Vec3) { this.BulletDir.set(direction); } }//摇杆import { v3, Vec3 } from cc; import { v2 } from cc; import { EventTouch } from cc; import { _decorator, Component, Node, NodeEventType, UITransform, Vec2 } from cc; const { ccclass, property } _decorator; ccclass(Joystick) export class Joystick extends Component { public static ins: Joystick null; property({ type: Node, displayName: 摇杆bg }) node_dotBg: Node null; property({ type: Node, displayName: 摇杆中间点 }) node_dot: Node null; /** node_dotBg的UITransform组件*/ UITf_dot: UITransform null; /** 摇杆移动的最大半径 */ maxLength: number 0; /** 方向 */ private _dir: Vec3 new Vec3(0, 0); public get dir(): Vec3 { return this._dir; } public set dir(value: Vec3) { this._dir value; } /** 角度 */ roleAngle: number 0; onLoad() { if (Joystick.ins null) { Joystick.ins this; } this.init(); } init() { this.UITf_dot this.node_dotBg.getComponent(UITransform); this.maxLength this.node_dotBg.getComponent(UITransform).width / 2; this.node_dotBg.on(NodeEventType.TOUCH_START, this.onTouchMove, this); this.node_dotBg.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this); this.node_dotBg.on(NodeEventType.TOUCH_END, this.onTouchEnd, this); this.node_dotBg.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this); } /** 触摸移动 */ private onTouchMove(event: EventTouch) { // 获取世界坐标 let worldPos event.getUILocation(); // 摇杆点是dotBg的子节点所以要转换成dotBg的局部坐标 let localPos this.UITf_dot.convertToNodeSpaceAR(v3(worldPos.x, worldPos.y, 0)); let length localPos.length(); if (length 0) { // 只计算方向 this.dir.x localPos.x / length; this.dir.y localPos.y / length; // 计算最外一圈的x,y位置 if (length this.maxLength) { localPos.x this.maxLength * this.dir.x; localPos.y this.maxLength * this.dir.y; } this.node_dot.setPosition(localPos); } } /** 触摸结束 */ private onTouchEnd(event: NodeEventType) { this.dir v3(0, 0,0); this.node_dot.setPosition(0, 0, 0); } /** 求角度 */ public calculateAngle() { if (this.dir.x 0 this.dir.y 0) return this.roleAngle; // 计算单位向量相对于正右方向的角度以弧度表示 let angleRad Math.atan2(this.dir.y, this.dir.x); // 将弧度转换为角度以度数表示 this.roleAngle angleRad * 180 / Math.PI; return this.roleAngle; } }
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2451879.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!