| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- import { _decorator, Component, Node, Vec2, Vec3, find, RigidBody2D, Collider2D, Contact2DType, IPhysics2DContact } from 'cc';
- const { ccclass, property } = _decorator;
- @ccclass('BulletController')
- export class BulletController extends Component {
- @property
- public speed: number = 300;
- @property
- public damage: number = 10;
- @property
- public lifetime: number = 5;
- private targetEnemy: Node = null;
- private rigidBody: RigidBody2D = null;
- private lifeTimer: number = 0;
- private initialDirection: Vec2 = null; // 存储初始方向
- start() {
- // 获取刚体组件
- this.rigidBody = this.node.getChildByName('Icon').getComponent(RigidBody2D);
-
- // 设置子弹生命周期
- this.lifeTimer = this.lifetime;
-
- // 查找最近的敌人
- this.findNearestEnemy();
-
- // 如果没有找到敌人,设置随机方向
- if (!this.initialDirection) {
- const angle = Math.random() * Math.PI * 2;
- this.initialDirection = new Vec2(
- Math.cos(angle),
- Math.sin(angle)
- );
- console.log('未找到敌人目标,设置随机方向');
- }
-
- // 设置初始速度
- this.updateVelocity();
-
- // 注册碰撞事件
- const collider = this.node.getChildByName('Icon').getComponent(Collider2D);
- if (collider) {
- collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
- }
- }
- // 查找最近的敌人
- findNearestEnemy() {
- // 直接查找enemyContainer节点
- const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
- if (!enemyContainer) {
- console.warn('找不到enemyContainer节点');
- return;
- }
-
- // 获取enemyContainer下的所有子节点(敌人)
- const enemies = enemyContainer.children;
- if (!enemies || enemies.length === 0) {
- console.log('没有可用的敌人目标');
- return;
- }
-
- console.log(`在enemyContainer中找到 ${enemies.length} 个敌人`);
-
- // 找到最近的敌人
- let nearestEnemy = null;
- let minDistance = Number.MAX_VALUE;
-
- for (const enemy of enemies) {
- if (!enemy || !enemy.isValid) continue;
-
- const distance = Vec3.distance(this.node.worldPosition, enemy.worldPosition);
- if (distance < minDistance) {
- minDistance = distance;
- nearestEnemy = enemy;
- }
- }
-
- if (nearestEnemy) {
- this.targetEnemy = nearestEnemy;
-
- // 计算朝向敌人的方向,并保存为初始方向
- const bulletPos = this.node.worldPosition;
- const enemyPos = this.targetEnemy.worldPosition;
-
- this.initialDirection = new Vec2(
- enemyPos.x - bulletPos.x,
- enemyPos.y - bulletPos.y
- );
- this.initialDirection.normalize();
-
- console.log('子弹锁定敌人目标,设置初始方向');
- }
- }
- // 更新子弹速度方向
- updateVelocity() {
- if (!this.rigidBody) return;
-
- // 使用初始方向,不追踪敌人
- if (this.initialDirection) {
- // 设置速度
- this.rigidBody.linearVelocity = new Vec2(
- this.initialDirection.x * this.speed,
- this.initialDirection.y * this.speed
- );
- }
- }
- // 碰撞检测
- onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
- // 检查是否碰到敌人
- if (otherCollider.node.name === 'Enemy') {
- console.log('子弹击中敌人');
-
- // 获取EnemyController
- const enemyController = find('Canvas/GameLevelUI/EnemyController');
- if (enemyController) {
- const enemyControllerComp = enemyController.getComponent('EnemyController');
- if (enemyControllerComp) {
- // 对敌人造成伤害
- (enemyControllerComp as any).damageEnemy(otherCollider.node, this.damage);
- }
- }
-
- // 销毁子弹
- this.destroyBullet();
- }
- // 检查是否碰到墙体或其他物体
- else if (otherCollider.node.name !== 'Ball') { // 不与小球碰撞
- this.destroyBullet();
- }
- }
- // 销毁子弹
- destroyBullet() {
- if (this.node && this.node.isValid) {
- this.node.destroy();
- }
- }
- update(dt: number) {
- // 更新生命周期
- this.lifeTimer -= dt;
- if (this.lifeTimer <= 0) {
- this.destroyBullet();
- return;
- }
-
- // 注意:不再每帧更新速度方向,子弹将沿初始方向直线移动
- }
- }
|