Kziwws 1 nedēļu atpakaļ
vecāks
revīzija
917f75ea6c

+ 19 - 1
assets/scripts/items/base/BulletBase.ts

@@ -1,8 +1,9 @@
-import { _decorator, Component, js,Node, PhysicsRayResult } from 'cc';
+import { _decorator, Component, js,Node, PhysicsRayResult, Vec3 } from 'cc';
 import { GunBase } from './GunBase';
 import { PoolManager } from '../../core/manager/PoolManager';
 import { Enemy } from '../../game/Enemy';
 import { ResUtil } from '../../utils/ResUtil';
+import { Game } from '../../game/Game';
 
 const { ccclass, property } = _decorator;
 
@@ -46,6 +47,23 @@ export class BulletBase extends Component {
             });
     }
     
+    /**
+     * 计算子弹用时
+     */
+    public getBulletTime(): number{
+        let endWorldPos: Vec3 = Game.I.player.shootUI.getCrossHairPos();
+        const bulletPos: Vec3 = this.node.position.clone();
+        //计算3D空间距离 忽略Y轴
+        const horizontalDistance = Vec3.distance(
+            new Vec3(bulletPos.x, 0, bulletPos.z),
+            new Vec3(endWorldPos.x, 0, endWorldPos.z)
+        );
+        //计算时间
+        const minTime: number = 0.25;
+        const factor: number = 2;
+        return Math.max(horizontalDistance / (this.gunBase.data.bulletSpeed * factor), minTime);
+    }
+    
     /**
      * 自动回收子弹
      */

+ 16 - 14
assets/scripts/items/player/Bullet1.ts

@@ -1,11 +1,9 @@
-import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult, native} from 'cc';
-import { Gun1 } from './Gun1';
+import { _decorator, Vec3,Node, geometry, PhysicsSystem, PhysicsRayResult} from 'cc';
 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;
 
@@ -16,6 +14,8 @@ export class Bullet1 extends BulletBase {
     private isRemoveDead: boolean = false;
     //射击方向的位置
     private vector: Vec3 = null;
+    //计算子弹停留时间
+    private time: number = 0.6;
 
     /**
      * 初始化一个子弹
@@ -36,10 +36,10 @@ export class Bullet1 extends BulletBase {
      * @param v 射向的方向
      */
     public setBulletVector(v: Vec3){
+        this.time = this.getBulletTime();
         const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
         this.vector = v.clone().multiplyScalar(bulletSpeed);
         this.node.forward = v;
-        this.rayPreCheck(bulletSpeed / 60);
     }
 
     /**
@@ -65,31 +65,33 @@ export class Bullet1 extends BulletBase {
         if(this.vector) {
             const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
             this.node.worldPosition = this.node.worldPosition.add(moveDelta);
-            this.rayPreCheck(moveDelta.length());
+            //开始检测射击
+            this.scheduleOnce(this.rayPreCheck,this.time);
         }
     }
 
+        
     /**
      * 检测射击
      * @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) {
+    private rayPreCheck() {
+        let screenPos: Vec3 = Game.I.player.shootUI.getScreenCenterPos();
+        const r = geometry.Ray.create();
+        Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, r);
+        const hasHit = PhysicsSystem.instance.raycast(r, 0xffffffff, 100000, true);
+        const raycastResults:PhysicsRayResult[] = PhysicsSystem.instance.raycastResults;
+        if(hasHit && raycastResults.length > 0){
             //先查找 要检测的物体 对象上带args参数的就是优先检测的
             let targetResult: PhysicsRayResult | null = null;
-            for (const result of phy.raycastResults) {
+            for (const result of raycastResults) {
                 if (result.collider["args"]) {
                     targetResult = result;
                     break;
                 }
             }
-            targetResult = targetResult ?? phy.raycastResults[0];
-            //targetResult.collider.getComponent(RigidBody).applyForce(this.vector, targetResult.hitPoint); 
+            targetResult = targetResult ?? raycastResults[0];
             this.addHitImpact(targetResult);
             const args:any = targetResult.collider["args"];
             if(args){

+ 14 - 12
assets/scripts/items/player/Bullet2.ts

@@ -5,7 +5,6 @@ import { Enemy, EPartType } from '../../game/Enemy';
 import { Gun2 } from './Gun2';
 import { BulletBase } from '../base/BulletBase';
 import { GunBase } from '../base/GunBase';
-import MsgHints from '../../utils/MsgHints';
 import { Sundries } from '../../game/Sundries';
 const { ccclass, property } = _decorator;
 
@@ -16,6 +15,8 @@ export class Bullet2 extends BulletBase {
     private isRemoveDead: boolean = false;
     //射击方向的位置
     private vector: Vec3 = null;
+    //计算子弹停留时间
+    private time: number = 0.6;
 
     /**
      * 初始化一个子弹
@@ -36,10 +37,10 @@ export class Bullet2 extends BulletBase {
      * @param v 射向的方向
      */
     public setBulletVector(v: Vec3){
+        this.time = this.getBulletTime();
         const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
         this.vector = v.clone().multiplyScalar(bulletSpeed);
         this.node.forward = v;
-        this.rayPreCheck(bulletSpeed / 60);
     }
 
     /**
@@ -64,7 +65,8 @@ export class Bullet2 extends BulletBase {
         if(this.vector) {
             const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
             this.node.worldPosition = this.node.worldPosition.add(moveDelta);
-            this.rayPreCheck(moveDelta.length());
+            //开始检测射击
+            this.scheduleOnce(this.rayPreCheck,this.time);
         }
     }
 
@@ -73,22 +75,22 @@ export class Bullet2 extends BulletBase {
      * @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) {
+    private rayPreCheck() {
+        let screenPos: Vec3 = Game.I.player.shootUI.getScreenCenterPos();
+        const r = geometry.Ray.create();
+        Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, r);
+        const hasHit = PhysicsSystem.instance.raycast(r, 0xffffffff, 100000, true);
+        const raycastResults:PhysicsRayResult[] = PhysicsSystem.instance.raycastResults;
+        if(hasHit && raycastResults.length > 0){
             //先查找 要检测的物体 对象上带args参数的就是优先检测的
             let targetResult: PhysicsRayResult | null = null;
-            for (const result of phy.raycastResults) {
+            for (const result of raycastResults) {
                 if (result.collider["args"]) {
                     targetResult = result;
                     break;
                 }
             }
-            targetResult = targetResult ?? phy.raycastResults[0];
-            //targetResult.collider.getComponent(RigidBody).applyForce(this.vector, targetResult.hitPoint); 
+            targetResult = targetResult ?? raycastResults[0];
             this.addHitImpact(targetResult);
             const args:any = targetResult.collider["args"];
             if(args){

+ 15 - 14
assets/scripts/items/player/Bullet3.ts

@@ -15,10 +15,10 @@ export class Bullet3 extends BulletBase {
     private isRemoveDead: boolean = false;
     //射击方向的位置
     private vector: Vec3 = null;
+    //计算子弹停留时间
+    private time: number = 0.6;
+
 
-    /**
-     * 初始化一个子弹
-     */
     /**
      * 初始化一个子弹
      * @param gun 属于哪把枪
@@ -35,10 +35,10 @@ export class Bullet3 extends BulletBase {
      * @param v 射向的方向
      */
     public setBulletVector(v: Vec3){
+        this.time = this.getBulletTime();
         const bulletSpeed = this.gunBase.data.bulletSpeed / 3;
         this.vector = v.clone().multiplyScalar(bulletSpeed);
         this.node.forward = v;
-        this.rayPreCheck(bulletSpeed / 60);
     }
 
     /**
@@ -63,7 +63,8 @@ export class Bullet3 extends BulletBase {
         if(this.vector) {
             const moveDelta = this.vector.clone().multiplyScalar(deltaTime);
             this.node.worldPosition = this.node.worldPosition.add(moveDelta);
-            this.rayPreCheck(moveDelta.length());
+            //开始检测射击
+            this.scheduleOnce(this.rayPreCheck,this.time);
         }
     }
 
@@ -72,22 +73,22 @@ export class Bullet3 extends BulletBase {
      * @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) {
+    private rayPreCheck() {
+        let screenPos: Vec3 = Game.I.player.shootUI.getScreenCenterPos();
+        const r = geometry.Ray.create();
+        Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, r);
+        const hasHit = PhysicsSystem.instance.raycast(r, 0xffffffff, 100000, true);
+        const raycastResults:PhysicsRayResult[] = PhysicsSystem.instance.raycastResults;
+        if(hasHit && raycastResults.length > 0){
             //先查找 要检测的物体 对象上带args参数的就是优先检测的
             let targetResult: PhysicsRayResult | null = null;
-            for (const result of phy.raycastResults) {
+            for (const result of raycastResults) {
                 if (result.collider["args"]) {
                     targetResult = result;
                     break;
                 }
             }
-            targetResult = targetResult ?? phy.raycastResults[0];
-            //targetResult.collider.getComponent(RigidBody).applyForce(this.vector, targetResult.hitPoint); 
+            targetResult = targetResult ?? raycastResults[0];
             this.addHitImpact(targetResult);
             const args:any = targetResult.collider["args"];
             if(args){

+ 13 - 5
assets/scripts/ui/GunfightShootUI.ts

@@ -376,12 +376,10 @@ export class GunfightShootUI extends BaseExp {
      */
     public getCrossHairPos(){
         //获取屏幕准心位置(世界坐标)
-        const worldPos = this.crossHair.worldPosition.clone(); 
-        const camera2D: Camera = Game.I.canvas.getChildByName('Camera2D').getComponent(Camera);
-        let screenPos = camera2D.worldToScreen(worldPos);
+        const screenPos = this.getScreenCenterPos();
         //校准补偿准心的偏移量
-        screenPos.x -= 0;
-        screenPos.y += this._isScopeOpen ? 60 : 40;
+        //screenPos.x -= 0;
+        //screenPos.y += 0;
         //从摄像机发射通过准心的射线
         const ray = new geometry.Ray();
         Game.I.camera.screenPointToRay(screenPos.x, screenPos.y, ray);
@@ -393,6 +391,16 @@ export class GunfightShootUI extends BaseExp {
                 muzzlePos.z + ray.d.z * this.bulletDistance);
     }
 
+    /**
+     * 返回屏幕中心的世界坐标
+     */
+    public getScreenCenterPos(){
+       const worldPos = this.crossHair.worldPosition.clone(); 
+        const camera2D: Camera = Game.I.canvas.getChildByName('Camera2D').getComponent(Camera);
+        let screenPos = camera2D.worldToScreen(worldPos);
+        return screenPos;
+    }
+
     /**
      * 玩家血量低于30%一直闪烁、打一枪的时候闪一下
      * @param progress 血量进度