技能组件修改,由skillcom统一负责动画,只负责动画
This commit is contained in:
143
assets/script/game/skills/SkillSystem.ts
Normal file
143
assets/script/game/skills/SkillSystem.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import { _decorator, Vec3, v3, tween } from "cc";
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||||
import { GameEvent } from "../common/config/GameEvent";
|
||||
import { SkillCom } from "./SkillCom";
|
||||
import { SkillSet, AnimType, endType } from "../common/config/SkillSet";
|
||||
import { Animation, sp } from "cc";
|
||||
|
||||
/** 技能运动系统 */
|
||||
@ecs.register('SkillSystem')
|
||||
export class SkillSystem extends ecs.ComblockSystem implements ecs.ISystemUpdate {
|
||||
|
||||
filter(): ecs.IMatcher {
|
||||
return ecs.allOf(SkillCom);
|
||||
}
|
||||
|
||||
|
||||
|
||||
update(entity: ecs.Entity) {
|
||||
const skill = entity.get(SkillCom);
|
||||
if (!skill || skill.is_destroy) return;
|
||||
|
||||
this.updateSkillMovement(skill);
|
||||
this.checkDestroy(skill);
|
||||
}
|
||||
|
||||
private updateSkillMovement(skill: SkillCom) {
|
||||
switch(skill.animType) {
|
||||
case AnimType.linear:
|
||||
this.linearMove(skill);
|
||||
break;
|
||||
case AnimType.parabolic:
|
||||
this.bezierMove(skill);
|
||||
break;
|
||||
case AnimType.fixed:
|
||||
this.fixedMove(skill);
|
||||
break;
|
||||
default:
|
||||
console.warn(`未知运动类型: ${skill.animType}`);
|
||||
}
|
||||
}
|
||||
|
||||
private checkDestroy(skill: SkillCom) {
|
||||
let shouldDestroy = false;
|
||||
|
||||
switch(skill.endType) {
|
||||
case endType.animationEnd:
|
||||
// 同时检测普通动画和Spine动画
|
||||
const anim = skill.node.getComponent(Animation);
|
||||
const spine = skill.node.getComponentInChildren(sp.Skeleton);
|
||||
|
||||
if (anim) {
|
||||
const state = anim.getState(skill.animName);
|
||||
shouldDestroy = state?.isPlaying === false;
|
||||
}
|
||||
else if (spine) {
|
||||
shouldDestroy = spine.animation === 'end';
|
||||
}
|
||||
break;
|
||||
case endType.timeEnd:
|
||||
skill.duration -= this.dt;
|
||||
shouldDestroy = skill.duration <= 0;
|
||||
break;
|
||||
case endType.distanceEnd:
|
||||
shouldDestroy = skill.targetPos &&
|
||||
Vec3.distance(skill.node.position, skill.targetPos) < 10;
|
||||
break;
|
||||
}
|
||||
|
||||
if (shouldDestroy) {
|
||||
skill.is_destroy = true;
|
||||
skill.node.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
private linearMove(skill: SkillCom) {
|
||||
if (!skill.targetPos) return;
|
||||
|
||||
// 计算移动方向
|
||||
const dir = skill.targetPos.clone().subtract(skill.node.position).normalize();
|
||||
|
||||
// 计算新位置
|
||||
const newPos = skill.node.position.clone().add(
|
||||
dir.multiplyScalar(skill.speed * this.dt)
|
||||
);
|
||||
|
||||
// 更新位置和角度
|
||||
skill.node.setPosition(newPos);
|
||||
|
||||
// 自动处理距离销毁
|
||||
if (skill.endType === endType.distanceEnd) {
|
||||
const remaining = Vec3.distance(newPos, skill.targetPos);
|
||||
if (remaining < 10) {
|
||||
skill.is_destroy = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bezierMove(skill: SkillCom) {
|
||||
if (!skill.targetPos) return;
|
||||
|
||||
// 计算控制点(拱顶位置)
|
||||
const startPos = skill.startPos;
|
||||
const endPos = skill.targetPos;
|
||||
const controlHeight = Math.max(startPos.y, endPos.y) + 200;
|
||||
const controlPos = v3(
|
||||
(startPos.x + endPos.x) / 2,
|
||||
controlHeight
|
||||
);
|
||||
|
||||
// 计算当前进度
|
||||
skill.duration += this.dt * skill.speed;
|
||||
const t = skill.duration / skill.inTime;
|
||||
|
||||
// 使用二阶贝塞尔曲线公式计算位置
|
||||
const newPos = this.twoBezier(t, startPos, controlPos, endPos);
|
||||
|
||||
skill.node.setPosition(newPos);
|
||||
|
||||
// 自动结束判断
|
||||
if (t >= 1) {
|
||||
skill.is_destroy = true;
|
||||
}
|
||||
}
|
||||
|
||||
private twoBezier(t: number, p1: Vec3, cp: Vec3, p2: Vec3): Vec3 {
|
||||
const x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
|
||||
const y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
|
||||
return v3(x, y, 0);
|
||||
}
|
||||
|
||||
private fixedMove(skill: SkillCom) {
|
||||
// 仅处理时间结束逻辑
|
||||
skill.duration += this.dt * skill.speed;
|
||||
|
||||
// 示例:在固定位置播放动画
|
||||
if (skill.endType === endType.timeEnd && skill.duration >= skill.inTime) {
|
||||
skill.is_destroy = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user