1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import {EVENT_TYPE, GAME_OBJECT_TYPE} from "../../utrl/gameEnum";
- import gameEventManager from "../../utrl/gameEventManager";
- import GameObject from "../gameObject";
- import PlayerObject from "../player/playerObject";
- const {ccclass, property} = cc._decorator;
- /**子弹实体类 */
- @ccclass
- export default class BulletObject extends GameObject {
- /**子弹开始位置 */
- private start_pos: cc.Vec3 = null;
- /**方向速度 */
- private v: cc.Vec2 = null;
- /**子弹伤害 */
- damage = 0;
- /**所属玩家 */
- from_p: PlayerObject = null;
- /**飞行距离上限 */
- max_dis = 600;
- /**子弹速度 */
- private speed = 3000;
- /**初始化完成 */
- private inited = false;
- /**初始化
- * @param start_pos 子弹开始位置
- * @param rad 弧度角
- * @param damage 子弹伤害
- * @param from_p 所属玩家
- */
- destroyCall: Function = null;
- init(start_pos: cc.Vec3, rad: number, damage: number, from_p: PlayerObject) {
- this.start_pos = start_pos;
- this.v = cc.v2(Math.cos(rad), Math.sin(rad)).mul(this.speed);
- this.node.setPosition(start_pos.add(cc.v3(this.v).mul(50 / this.speed)));
- this.damage = damage;
- this.from_p = from_p;
- this.node.angle = rad / Math.PI * 180;
- this.inited = true;
- this.type = GAME_OBJECT_TYPE.bullet;
- }
- /**运行
- * @param dt 每帧时间
- */
- run(dt: number) {
- if (!this.inited) {
- return;
- }
- this.node.setPosition(this.node.position.add(cc.v3(this.v.mul(dt))));
- let dis = this.node.position.sub(this.start_pos).mag();
- if (dis > this.max_dis) {
- this.node.destroy();
- return;
- }
- }
- update(dt) {
- if (!this.inited) {
- return;
- }
- this.node.setPosition(this.node.position.add(cc.v3(this.v.mul(dt))));
- let dis = this.node.position.sub(this.start_pos).mag();
- if (dis > this.max_dis) {
- gameEventManager.emit(EVENT_TYPE.RECOVER_PLAYER_BULLET);
- this.node.destroy();
- return;
- }
- }
- onDestroy() {
- this.destroyCall && this.destroyCall();
- }
- /**销毁 */
- die() {
- this.node.destroy();
- }
- }
|