12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult, RigidBody } from 'cc';
- import { PoolManager } from '../../core/manager/PoolManager';
- import { Game } from '../../game/Game';
- import { BulletBase } from '../base/BulletBase';
- import { GunBase } from '../base/GunBase';
- import { Gun10 } from './Gun10';
- const { ccclass, property } = _decorator;
- //1001敌人所使用的枪大兵手枪 OR 1007将军手枪所用的子弹
- @ccclass('Bullet10')
- export class Bullet10 extends BulletBase {
- //是否必死 必死的子弹不会回收 要自己控制
- private isRemoveDead: boolean = false;
- //射击方向的位置
- private vector: Vec3 = null;
- /**
- * 初始化一个子弹
- */
- /**
- * 初始化一个子弹
- * @param gun 属于哪把枪
- * @param isRemoveDead 是否不受控制的子弹自己管理
- */
- public init(gun:GunBase,isRemoveDead:boolean = false){
- this.gunBase = gun;
- this.isDead = false;
- this.isRemoveDead = isRemoveDead;
- }
- /**
- * 设置子弹开始位置和射击方向
- * @param v 射向的方向
- */
- public setBulletVector(v: Vec3){
- const bulletSpeed = this.gunBase.data.bulletSpeed;
- this.vector = v.clone().multiplyScalar(bulletSpeed);
- this.node.forward = v;
- this.rayPreCheck(bulletSpeed / 60);
- }
- /**
- * 射击速度发生改变
- */
- public speedChange() {
- const bulletSpeed = this.gunBase.data.bulletSpeed;
- if(this.vector) {
- this.vector = this.vector.normalize().multiplyScalar(bulletSpeed);
- }
- }
- /**
- * 实时帧更新
- */
- public update(deltaTime: number) {
- if(Game.I.isGameOver
- || Game.I.isPause
- ||this.isRemoveDead) {
- this.isDead = true;
- PoolManager.putNode(this.node);
- return;
- }
- if(this.vector) {
- const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
- this.node.worldPosition = this.node.worldPosition.add(moveDelta);
- this.rayPreCheck(moveDelta.length());
- }
- }
- /**
- * 检测射击
- * @param b
- * @param len 检测
- */
- private rayPreCheck(len: number) {
- const p = this.node.worldPosition;
- const ray = geometry.Ray.create(p.x, p.y, p.z, this.vector.x, this.vector.y, this.vector.z);
- const phy:PhysicsSystem = PhysicsSystem.instance;
- const isHit = phy.raycast(ray, 0xffffff, len);
- if (isHit && phy.raycastResults.length > 0) {
- let result:PhysicsRayResult = phy.raycastResults[0];
- if(result.collider.getComponent(RigidBody)) {
- result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
- }
- let name:string = result.collider.node.name;
- if(name == Game.I.player.head.name){//敌人打玩家爆头
- Game.I.player.subHP(this.gunBase.data.attack,this.gunBase.data);
- }else if(name == Game.I.player.body.name){//敌人打玩家身体
- Game.I.player.subHP(this.gunBase.data.attack,this.gunBase.data);
- }
- //自动回收了子弹
- this.autoRecycle();
- }
- }
- }
|