123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult} from 'cc';
- import { Gun1 } from './Gun1';
- import { Game } from '../../game/Game';
- import { GunBase } from '../base/GunBase';
- import { BulletBase } from '../base/BulletBase';
- import { Enemy, EPartType } from '../../game/Enemy';
- import { PoolManager } from '../../core/manager/PoolManager';
- import MsgHints from '../../utils/MsgHints';
- import { Sundries } from '../../game/Sundries';
- const { ccclass, property } = _decorator;
- //玩家所用子弹 98k m24 awm BarrettM82A1 重狙子弹
- @ccclass('Bullet1')
- export class Bullet1 extends BulletBase {
- //是否必死 必死的子弹不会回收 要自己控制
- private isRemoveDead: boolean = false;
- //射击方向的位置
- private vector: Vec3 = null;
- /**
- * 初始化一个子弹
- */
- /**
- * 初始化一个子弹
- * @param gun 属于哪把枪
- * @param isRemoveDead 是否不受控制的子弹自己管理
- */
- public init(gun:GunBase,isRemoveDead:boolean = true){
- this.gunBase = gun;
- this.isDead = false;
- this.isRemoveDead = isRemoveDead;
- }
- /**
- * 设置子弹开始位置和射击方向
- * @param v 射向的方向
- */
- public setBulletVector(v: Vec3){
- const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
- 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.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];
- this.addImpact(result);
- let attack: number = this.gunBase.data.attack;
- const name: string = result.collider.node.name;
- //查询是否打到的是敌人
- const enemyNode: Node = this.findEnemyNode(result.collider.node,Enemy);
- if(enemyNode){
- let e: Enemy = enemyNode.getComponent(Enemy);
- //爆头击杀
- let isHeadShot:boolean = false;
- if(name == EPartType.head){
- Game.I.player.headShotNum += 1;
- attack = Game.I.player.pData.headshotDmgMul * attack;
- isHeadShot = true;
- }
- //打到了盾兵上的盾
- if(name == EPartType.shield && e.shieldHp > 0){
- e.shieldHp -= attack;
- console.log(" 打击到盾上了 " + e.shieldHp);
- return;
- }else{
- console.log(" 扣敌人的血了 " + attack);
- }
- //坦克是单独的碰撞体
- if(e.isTank()){
- if(name == EPartType.tank){
- e.subHP(attack,this.gunBase.data,false);
- }
- }else{
- e.subHP(attack,this.gunBase.data,isHeadShot);
- }
- }else{//查询是否打的是杂物
- const sundrieNode: Node = this.findEnemyNode(result.collider.node,Sundries);
- if(sundrieNode){
- let sundrie: Sundries = sundrieNode.getComponent(Sundries);
- sundrie.subHP(attack,this.gunBase.data);
- }
- }
- //自动回收了子弹
- this.autoRecycle();
- }
- }
- /**
- * 加入射击中的效果
- * @param e 碰撞到的结果
- */
- private addImpact(e:PhysicsRayResult) {
- let impact: Node = PoolManager.getNode((this.gunBase as unknown as Gun1).impact);
- impact.worldPosition = e.hitPoint.add(e.hitNormal.multiplyScalar(0.01));
- impact.forward = e.hitNormal.multiplyScalar(-1);
- impact.scale = this.node.scale;
- impact.setParent(e.collider.node,true);
- }
- }
- /*MsgHints.show(`打中了: ${result.collider.name}`);
- if(result.collider.getComponent(RigidBody)) {
- result.collider.getComponent(RigidBody).applyForce(this.vector, result.hitPoint);
- }*/
|