| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442 |
- import { _decorator, Component, Node, Vec3, find, instantiate, Prefab, UITransform, resources, sp, CircleCollider2D } from 'cc';
- import { BulletTrajectory } from './BulletTrajectory';
- const { ccclass, property } = _decorator;
- /**
- * 命中效果控制器
- * 负责处理可叠加的命中效果
- */
- export interface HitEffectConfig {
- type: 'normal_damage' | 'pierce_damage' | 'explosion' | 'ground_burn' | 'ricochet_damage';
- priority: number; // 优先级,数字越小优先级越高
- params: any; // 效果参数
- }
- export interface ExplosionParams {
- damage: number;
- radius: number;
- delay: number;
- }
- export interface PierceParams {
- damage: number;
- pierceCount: number;
- }
- export interface BurnParams {
- damage: number;
- duration: number;
- radius: number;
- tickInterval: number;
- }
- export interface RicochetParams {
- damage: number;
- ricochetCount: number;
- ricochetAngle: number;
- }
- export interface HitResult {
- shouldDestroy: boolean; // 是否应该销毁子弹
- shouldContinue: boolean; // 是否应该继续飞行
- shouldRicochet: boolean; // 是否应该弹射
- damageDealt: number; // 造成的伤害
- }
- @ccclass('BulletHitEffect')
- export class BulletHitEffect extends Component {
- private hitEffects: HitEffectConfig[] = [];
- private hitCount: number = 0;
- private pierceCount: number = 0;
- private ricochetCount: number = 0;
- private activeBurnAreas: Node[] = [];
-
- // 默认特效路径(从WeaponBullet传入)
- private defaultHitEffectPath: string = '';
- private defaultTrailEffectPath: string = '';
- private defaultBurnEffectPath: string = '';
- public setDefaultEffects(hit: string | null, trail?: string | null, burn?: string | null) {
- if (hit) this.defaultHitEffectPath = hit;
- if (trail) this.defaultTrailEffectPath = trail;
- if (burn) this.defaultBurnEffectPath = burn;
- }
-
- /**
- * 初始化命中效果
- */
- public init(effects: HitEffectConfig[]) {
- // 按优先级排序
- this.hitEffects = [...effects].sort((a, b) => a.priority - b.priority);
- }
-
- /**
- * 处理命中事件
- */
- public processHit(hitNode: Node, contactPos: Vec3): HitResult {
- this.hitCount++;
-
- const result: HitResult = {
- shouldDestroy: false,
- shouldContinue: false,
- shouldRicochet: false,
- damageDealt: 0
- };
-
- // 按优先级处理所有效果
- for (const effect of this.hitEffects) {
- const effectResult = this.processEffect(effect, hitNode, contactPos);
-
- // 累积伤害
- result.damageDealt += effectResult.damageDealt;
-
- // 处理控制逻辑(OR逻辑,任何一个效果要求的行为都会执行)
- if (effectResult.shouldDestroy) result.shouldDestroy = true;
- if (effectResult.shouldContinue) result.shouldContinue = true;
- if (effectResult.shouldRicochet) result.shouldRicochet = true;
- }
-
- // 逻辑优先级:销毁 > 弹射 > 继续
- if (result.shouldDestroy) {
- result.shouldContinue = false;
- result.shouldRicochet = false;
- } else if (result.shouldRicochet) {
- result.shouldContinue = false;
- }
-
- return result;
- }
-
- /**
- * 处理单个效果
- */
- private processEffect(effect: HitEffectConfig, hitNode: Node, contactPos: Vec3): HitResult {
- const result: HitResult = {
- shouldDestroy: false,
- shouldContinue: false,
- shouldRicochet: false,
- damageDealt: 0
- };
-
- switch (effect.type) {
- case 'normal_damage':
- result.damageDealt = this.processNormalDamage(effect.params, hitNode);
- result.shouldDestroy = true;
- break;
-
- case 'pierce_damage':
- result.damageDealt = this.processPierceDamage(effect.params as PierceParams, hitNode);
- break;
-
- case 'explosion':
- result.damageDealt = this.processExplosion(effect.params as ExplosionParams, contactPos);
- result.shouldDestroy = true;
- break;
-
- case 'ground_burn':
- this.processGroundBurn(effect.params as BurnParams, contactPos);
- break;
-
- case 'ricochet_damage':
- result.damageDealt = this.processRicochetDamage(effect.params as RicochetParams, hitNode);
- result.shouldRicochet = this.ricochetCount < (effect.params as RicochetParams).ricochetCount;
- break;
- }
-
- return result;
- }
-
- /**
- * 处理普通伤害
- */
- private processNormalDamage(params: any, hitNode: Node): number {
- const damage = params.damage || 0;
- this.damageEnemy(hitNode, damage);
- this.spawnHitEffect(hitNode.worldPosition);
- return damage;
- }
-
- /**
- * 处理穿透伤害
- */
- private processPierceDamage(params: PierceParams, hitNode: Node): number {
- const damage = params.damage || 0;
- this.damageEnemy(hitNode, damage);
- this.spawnHitEffect(hitNode.worldPosition);
- this.pierceCount++;
- return damage;
- }
-
- /**
- * 处理爆炸效果
- */
- private processExplosion(params: ExplosionParams, position: Vec3): number {
- const scheduleExplosion = () => {
- // 生成爆炸特效
- this.spawnExplosionEffect(position);
-
- // 对范围内敌人造成伤害
- const damage = this.damageEnemiesInRadius(position, params.radius, params.damage);
- return damage;
- };
-
- if (params.delay > 0) {
- this.scheduleOnce(scheduleExplosion, params.delay);
- return 0; // 延迟爆炸,当前不造成伤害
- } else {
- return scheduleExplosion();
- }
- }
-
- /**
- * 处理地面灼烧效果
- */
- private processGroundBurn(params: BurnParams, position: Vec3) {
- // 创建灼烧区域节点
- const burnArea = new Node('BurnArea');
- const gameArea = find('Canvas/GameLevelUI/GameArea');
- if (gameArea) {
- gameArea.addChild(burnArea);
-
- // 设置位置
- const localPos = gameArea.getComponent(UITransform)?.convertToNodeSpaceAR(position) || new Vec3();
- burnArea.position = localPos;
-
- // 添加碰撞体用于检测范围
- const collider = burnArea.addComponent(CircleCollider2D);
- collider.radius = params.radius;
- collider.sensor = true; // 设为传感器
-
- // 生成灼烧特效
- this.spawnBurnEffect(burnArea);
-
- // 定时造成伤害
- let remainingTime = params.duration;
- const damageTimer = () => {
- if (remainingTime <= 0 || !burnArea.isValid) {
- burnArea.destroy();
- return;
- }
-
- // 对范围内敌人造成伤害
- this.damageEnemiesInRadius(position, params.radius, params.damage);
-
- remainingTime -= params.tickInterval;
- this.scheduleOnce(damageTimer, params.tickInterval);
- };
-
- this.scheduleOnce(damageTimer, params.tickInterval);
- this.activeBurnAreas.push(burnArea);
- }
- }
-
- /**
- * 处理弹射伤害
- */
- private processRicochetDamage(params: RicochetParams, hitNode: Node): number {
- const damage = params.damage || 0;
- this.damageEnemy(hitNode, damage);
-
- if (this.ricochetCount < params.ricochetCount) {
- this.ricochetCount++;
-
- // 计算弹射方向
- this.calculateRicochetDirection(params.ricochetAngle);
- }
-
- return damage;
- }
-
- /**
- * 计算弹射方向
- */
- private calculateRicochetDirection(maxAngle: number) {
- const trajectory = this.getComponent(BulletTrajectory);
- if (!trajectory) return;
-
- // 获取当前速度方向
- const currentVel = trajectory.getCurrentVelocity();
-
- // 随机弹射角度
- const angleRad = (Math.random() - 0.5) * maxAngle * Math.PI / 180;
- const cos = Math.cos(angleRad);
- const sin = Math.sin(angleRad);
-
- // 应用旋转
- const newDirection = new Vec3(
- currentVel.x * cos - currentVel.y * sin,
- currentVel.x * sin + currentVel.y * cos,
- 0
- ).normalize();
-
- // 使用弹道组件的changeDirection方法
- trajectory.changeDirection(newDirection);
- }
-
- /**
- * 对单个敌人造成伤害
- */
- private damageEnemy(enemyNode: Node, damage: number) {
- if (!this.isEnemyNode(enemyNode)) return;
-
- // 尝试调用敌人的受伤方法
- const enemyInstance = enemyNode.getComponent('EnemyInstance') as any;
- if (enemyInstance) {
- if (typeof enemyInstance.takeDamage === 'function') {
- enemyInstance.takeDamage(damage);
- return;
- }
- if (typeof enemyInstance.health === 'number') {
- enemyInstance.health -= damage;
- if (enemyInstance.health <= 0) {
- enemyNode.destroy();
- }
- return;
- }
- }
-
- // 备用方案:通过EnemyController
- const enemyController = find('Canvas/GameLevelUI/EnemyController')?.getComponent('EnemyController') as any;
- if (enemyController && typeof enemyController.damageEnemy === 'function') {
- enemyController.damageEnemy(enemyNode, damage);
- }
- }
-
- /**
- * 对范围内敌人造成伤害
- */
- private damageEnemiesInRadius(center: Vec3, radius: number, damage: number): number {
- const enemyContainer = find('Canvas/GameLevelUI/enemyContainer');
- if (!enemyContainer) return 0;
-
- let totalDamage = 0;
- const enemies = enemyContainer.children.filter(child =>
- child.active && this.isEnemyNode(child)
- );
-
- for (const enemy of enemies) {
- const distance = Vec3.distance(center, enemy.worldPosition);
- if (distance <= radius) {
- this.damageEnemy(enemy, damage);
- totalDamage += damage;
- }
- }
-
- return totalDamage;
- }
-
- /**
- * 判断是否为敌人节点
- */
- private isEnemyNode(node: Node): boolean {
- const name = node.name.toLowerCase();
- return name.includes('enemy') ||
- name.includes('敌人') ||
- node.getComponent('EnemyInstance') !== null;
- }
-
- /**
- * 生成爆炸特效
- */
- private spawnExplosionEffect(position: Vec3) {
- const path = this.defaultHitEffectPath || 'Animation/tx/tx0004';
- this.spawnEffect(path, position, false);
- }
-
- /**
- * 生成灼烧特效
- */
- private spawnBurnEffect(parent: Node) {
- const path = this.defaultBurnEffectPath || this.defaultTrailEffectPath || 'Animation/tx/tx0006';
- this.spawnEffect(path, new Vec3(), true, parent);
- }
-
- /**
- * 生成特效
- */
- private spawnEffect(path: string, worldPos: Vec3, loop = false, parent?: Node) {
- if (!path) return;
- const spawnWithData = (skData: sp.SkeletonData) => {
- const effectNode = new Node('Effect');
- const skeletonComp: sp.Skeleton = effectNode.addComponent(sp.Skeleton);
- skeletonComp.skeletonData = skData;
- skeletonComp.setAnimation(0, 'animation', loop);
- const targetParent = parent || find('Canvas/GameLevelUI/GameArea') || find('Canvas');
- if (targetParent) {
- targetParent.addChild(effectNode);
- if (parent) {
- effectNode.setPosition(0, 0, 0);
- } else {
- const parentTrans = targetParent.getComponent(UITransform);
- if (parentTrans) {
- const localPos = parentTrans.convertToNodeSpaceAR(worldPos);
- effectNode.position = localPos;
- }
- }
- }
- if (!loop) {
- skeletonComp.setCompleteListener(() => {
- effectNode.destroy();
- });
- }
- };
- // 先尝试直接加载给定路径
- resources.load(path, sp.SkeletonData, (err, skData: sp.SkeletonData) => {
- if (err) {
- console.warn('加载特效失败:', path, err);
- return;
- }
- spawnWithData(skData);
- });
- }
-
- /**
- * 清理资源
- */
- onDestroy() {
- // 清理激活的灼烧区域
- for (const burnArea of this.activeBurnAreas) {
- if (burnArea && burnArea.isValid) {
- burnArea.destroy();
- }
- }
- this.activeBurnAreas = [];
- }
-
- /**
- * 获取命中统计
- */
- public getHitStats() {
- return {
- hitCount: this.hitCount,
- pierceCount: this.pierceCount,
- ricochetCount: this.ricochetCount
- };
- }
-
- /**
- * 验证配置
- */
- public static validateConfig(effects: HitEffectConfig[]): boolean {
- if (!Array.isArray(effects) || effects.length === 0) return false;
-
- for (const effect of effects) {
- if (!effect.type || effect.priority < 0) return false;
- if (!effect.params) return false;
- }
-
- return true;
- }
-
- private spawnHitEffect(worldPos: Vec3) {
- const path = this.defaultHitEffectPath;
- if (path) {
- this.spawnEffect(path, worldPos, false);
- }
- }
- }
|