浏览代码

中心点 适配

woso_javan 1 月之前
父节点
当前提交
5e4f677536

+ 28 - 0
assets/module_aliens/Script/Components/EnemyComponent.ts

@@ -1,4 +1,6 @@
 import { _decorator, CCBoolean, CCFloat, Collider, Component, isValid, ITriggerEvent, Node, PhysicsSystem, tween, Tween, Vec3 } from 'cc';
+import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
+import { GameEvent } from '../Enum/GameEvent';
 
 const { ccclass, property } = _decorator;
 
@@ -22,14 +24,40 @@ export class EnemyComponent extends Component {
     start() {
         this.status = EnemyStatus.LIFE;
         this.tigger = this.node.getComponent(Collider)!;
+        this.registerEvent();
+    }
+
+    private registerEvent() {
         this.tigger.on('onTriggerEnter', this.onTriggerEnter, this);
+        EventDispatcher.instance.on(GameEvent.EVENT_CAMERA_SHOOT_ENEMY, this.onShootEnemy, this);
+    }
+
+    private unRegisterEvent() {
+        this.tigger.off('onTriggerEnter', this.onTriggerEnter, this);
     }
 
     protected onTriggerEnter(event: ITriggerEvent): void {
     }
 
+    private onShootEnemy(hitNode: Node){
+        // 检查是否是当前怪物节点被击中
+        if(this.node === hitNode && this.status === EnemyStatus.LIFE) {
+            this.status = EnemyStatus.DIE;
+            
+            // 播放死亡动画或效果
+            tween(this.node)
+                .to(0.3, { scale: new Vec3(0, 0, 0) })
+                .call(() => {
+                    // 销毁怪物节点
+                    this.node.destroy();
+                })
+                .start();
+        }
+    }
+
     protected onDestroy(): void {
         Tween.stopAllByTarget(this.node);
+        this.unRegisterEvent();
     }
 }
 

+ 3 - 0
assets/module_aliens/Script/Enum/GameEvent.ts

@@ -18,6 +18,9 @@ export class GameEvent {
     /** 击中文本提示测试*/
     static readonly EVENT_CAMERA_SHOOT_TEXT = 'EVENT_CAMERA_SHOOT_TEXT';
 
+    /** 击中了怪物 外星人*/
+    static readonly EVENT_CAMERA_SHOOT_ENEMY = 'EVENT_CAMERA_SHOOT_ENEMY';
+
     /** 检测游戏是否结束*/
     static readonly EVENT_CHECK_GAME_OVER = 'EVENT_CHECK_GAME_OVER';
 

+ 10 - 8
assets/module_aliens/Script/LevelAction.ts

@@ -83,11 +83,14 @@ export class LevelAction extends Component {
     }
 
     private onShoot(){
-        // 从屏幕中心发射射线
-        const ray = new geometry.Ray();
-        this.camera.screenPointToRay(view.getVisibleSize().width * 0.5, 
-                                   view.getVisibleSize().height * 0.5, 
-                                   ray);
+       // 获取正确的屏幕中心坐标
+       const screenCenter = view.getVisibleSize();
+       const screenX = screenCenter.width * 0.5 * view.getScaleX();
+       const screenY = screenCenter.height * 0.5 * view.getScaleY();
+
+       // 从屏幕中心发射射线
+       const ray = new geometry.Ray();
+       this.camera.screenPointToRay(screenX, screenY, ray);
         
         // 射线检测参数
         const mask = 0xffffffff;
@@ -96,18 +99,17 @@ export class LevelAction extends Component {
         
         // 执行射线检测
         const hasHit = PhysicsSystem.instance.raycast(ray, mask, maxDistance, queryTrigger);
-        console.log(`射线检测结果: ${hasHit}`);
         
         if (hasHit) {
             const results = PhysicsSystem.instance.raycastResults;
             for (let i = 0; i < results.length; i++) {
                 const item = results[i];
                 const hitNode = item.collider.node;
-                console.log(`碰撞物体${i}: ${hitNode.name}`, 
-                          `距离: ${item.distance.toFixed(2)}`);
+                console.log(`碰撞物体${i}: ${hitNode.name} 距离: ${item.distance.toFixed(2)}`);
 
                 if(hitNode.getComponent(EnemyComponent)){
                     EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_TEXT);
+                    EventDispatcher.instance.emit(GameEvent.EVENT_CAMERA_SHOOT_ENEMY,hitNode);
                 }
             }
         }

+ 2 - 2
profiles/v2/packages/scene.json

@@ -1214,8 +1214,8 @@
     },
     "19323c5d-5d36-438a-86ee-8288c690e5b0": {
       "position": {
-        "x": 59.766666666666666,
-        "y": 127.23333333333335,
+        "x": 58.95166666666667,
+        "y": 125.49833333333333,
         "z": 5000
       },
       "rotation": {