技能运动基础修改完成,todo 完善各类技能实现

This commit is contained in:
2025-02-08 10:07:28 +08:00
parent c619b97aa4
commit 6e1c7ee715
12 changed files with 231 additions and 274 deletions

View File

@@ -1,50 +1,50 @@
import { _decorator, Component, Node, tween, v3, Vec3 } from 'cc';
import { SkillCom } from './SkillCom';
import { smc } from '../common/SingletonModuleComp';
const { ccclass, property } = _decorator;
// import { _decorator, Component, Node, tween, v3, Vec3 } from 'cc';
// import { SkillCom } from './SkillCom';
// import { smc } from '../common/SingletonModuleComp';
// const { ccclass, property } = _decorator;
@ccclass('BezCom')
export class BezCom extends Component {
start() {
let base =this.node.getComponent(SkillCom)
// if(this.node.parent.scale.x < 0){
// base.t_pos.x=base.t_pos.x*-1
// }
let s_pos = v3(this.node.position.x,this.node.position.y+35)
let c_pos = v3((base.t_pos.x+this.node.position.x)/2,this.node.position.y+200)
let e_pos = v3(base.t_pos.x,this.node.position.y+50)
let time =Math.abs(Math.abs(base.t_pos.x-this.node.position.x)/base.speed)
BezCom.bezierTo(this.node,time,s_pos,c_pos,e_pos,{
onComplete: (target?: object) => {
base.is_destroy=true
},
}).start();
}
// @ccclass('BezCom')
// export class BezCom extends Component {
// start() {
// let base =this.node.getComponent(SkillCom)
// // if(this.node.parent.scale.x < 0){
// // base.t_pos.x=base.t_pos.x*-1
// // }
// let s_pos = v3(this.node.position.x,this.node.position.y+35)
// let c_pos = v3((base.t_pos.x+this.node.position.x)/2,this.node.position.y+200)
// let e_pos = v3(base.t_pos.x,this.node.position.y+50)
// let time =Math.abs(Math.abs(base.t_pos.x-this.node.position.x)/base.speed)
// BezCom.bezierTo(this.node,time,s_pos,c_pos,e_pos,{
// onComplete: (target?: object) => {
// base.is_destroy=true
// },
// }).start();
// }
update(deltaTime: number) {
if(smc.mission.pause) return
}
// update(deltaTime: number) {
// if(smc.mission.pause) return
// }
public static bezierTo(target: any, duration: number, c1: Vec3, c2: Vec3, to: Vec3, opts: any) {
opts = opts || Object.create(null);
/*
* @desc 二阶贝塞尔
* @param {number} t 当前百分比
* @param {} p1 起点坐标
* @param {} cp 控制点
* @param {} p2 终点坐标
* @returns {any}
*/
let twoBezier = (t:number, p1: Vec3, cp: Vec3, p2: Vec3) => {
let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
return v3(x, y, 0);
};
opts.onUpdate = (arg: Vec3, ratio: number) => {
target.position = twoBezier(ratio, c1, c2, to);
};
return tween(target).to(duration, {}, opts);
}
}
// public static bezierTo(target: any, duration: number, c1: Vec3, c2: Vec3, to: Vec3, opts: any) {
// opts = opts || Object.create(null);
// /*
// * @desc 二阶贝塞尔
// * @param {number} t 当前百分比
// * @param {} p1 起点坐标
// * @param {} cp 控制点
// * @param {} p2 终点坐标
// * @returns {any}
// */
// let twoBezier = (t:number, p1: Vec3, cp: Vec3, p2: Vec3) => {
// let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
// let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
// return v3(x, y, 0);
// };
// opts.onUpdate = (arg: Vec3, ratio: number) => {
// target.position = twoBezier(ratio, c1, c2, to);
// };
// return tween(target).to(duration, {}, opts);
// }
// }

View File

@@ -39,22 +39,22 @@ export class Skill extends ecs.Entity {
// 设置节点属性
node.parent = parent;
node.setPosition(startPos);
console.log("加载预制体:",startPos,targetPos)
// 添加技能组件
const skillComp = node.getComponent(SkillCom);
this.add(skillComp);
// 初始化技能参数
const skillComp = node.getComponent(SkillCom); // 初始化技能参数
skillComp.animType = config.AnimType;
skillComp.endType = config.endType;
skillComp.speed = config.speed;
skillComp.duration = config.in;
skillComp.startPos = startPos.clone();
skillComp.targetPos = targetPos.clone();
skillComp.inTime = config.in;
skillComp.startPos = v3(startPos.x,BoxSet.GAME_LINE+35,0)
skillComp.targetPos = v3(targetPos.x,BoxSet.GAME_LINE+50,0)
skillComp.prefabName = config.sp_name;
skillComp.group = group;
// 初始化动画名称
skillComp.animName = config.animName; // 从配置获取动画名称
this.add(skillComp);
}
}

View File

@@ -1,13 +1,11 @@
import { _decorator,Collider2D ,Contact2DType,v3,IPhysics2DContact,Vec3, tween, math} from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { BoxSet } from "../common/config/BoxSet";
import { smc } from "../common/SingletonModuleComp";
import { Timer } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { GameEvent } from "../common/config/GameEvent";
import { SkillSet } from "../common/config/SkillSet";
import { AnimType, endType } from "../common/config/SkillSet";
import { EndAnmCom } from './EndAnmCom';
const { ccclass, property } = _decorator;
@@ -22,7 +20,6 @@ export class SkillCom extends CCComp {
speed:number = 200;
scale:number = 1;
angle:number = 0;
t_pos:Vec3 = v3(0,0,0); // 目标增量
is_destroy:boolean = false;
enemys:any = [];
animType: number = 0; // 运动类型
@@ -55,45 +52,39 @@ export class SkillCom extends CCComp {
case AnimType.fixed:
this.startFixedMove();
break;
case AnimType.fixedStart:
this.startFixedStartMove();
break;
case AnimType.fixedEnd:
this.startFixedEndMove();
break;
}
}
private startLinearMove() {
if (!this.targetPos) return;
const duration = Vec3.distance(this.node.position, this.targetPos) / this.speed;
const duration = Vec3.distance(this.startPos, this.targetPos) / this.speed;
tween(this.node)
.to(duration, { position: this.targetPos })
.call(() => {
if (this.endType === endType.distanceEnd) {
this.is_destroy = true;
}
this.is_destroy = true;
})
.start();
}
private startBezierMove() {
if (!this.targetPos) return;
const startPos = this.node.position;
const endPos = this.targetPos;
const controlPos = v3(
(startPos.x + endPos.x) / 2,
Math.max(startPos.y, endPos.y) + 200
);
const duration = Vec3.distance(startPos, endPos) / this.speed;
tween(this.node)
.to(duration, {}, {
onUpdate: (target, ratio) => {
const pos = this.twoBezier(ratio, startPos, controlPos, endPos);
this.node.setPosition(pos);
}
})
.call(() => this.is_destroy = true)
.start();
let s_pos = v3(this.startPos.x,this.startPos.y)
let c_pos = v3((this.targetPos.x+this.startPos.x)/2,this.startPos.y+150)
let e_pos = v3(this.targetPos.x,this.targetPos.y)
let time =Math.abs(Math.abs(this.targetPos.x-this.startPos.x)/this.speed)
console.log("开始贝塞尔运动=>time:"+time,"s_pos:"+s_pos,"c_pos:"+c_pos,"e_pos:"+e_pos)
SkillCom.bezierTo(this.node,time,s_pos,c_pos,e_pos,{
onComplete: (target?: object) => {
this.is_destroy=true
},
}).start();
}
private startFixedMove() {
@@ -102,15 +93,66 @@ export class SkillCom extends CCComp {
.delay(this.inTime)
.call(() => this.is_destroy = true)
.start();
} else if (this.endType === endType.animationEnd) {
if (!this.node.getComponent(EndAnmCom)) {
this.node.addComponent(EndAnmCom);
}
}
}
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 startFixedStartMove() {
console.log("开始固定起点运动")
this.node.position = this.startPos;
if (this.endType === endType.timeEnd) {
console.log("开始固定起点运动=>time:"+this.inTime)
tween(this.node)
.delay(this.inTime)
.call(() => this.is_destroy = true)
.start();
} else if (this.endType === endType.animationEnd) {
if (!this.node.getComponent(EndAnmCom)) {
this.node.addComponent(EndAnmCom);
}
}
}
private startFixedEndMove() {
console.log("开始固定终点运动")
this.node.position = this.targetPos;
if (this.endType === endType.timeEnd) {
console.log("开始固定终点运动=>time:"+this.inTime)
tween(this.node)
.delay(this.inTime)
.call(() => this.is_destroy = true)
.start();
} else if (this.endType === endType.animationEnd) {
if (!this.node.getComponent(EndAnmCom)) {
this.node.addComponent(EndAnmCom);
}
}
}
public static bezierTo(target: any, duration: number, c1: Vec3, c2: Vec3, to: Vec3, opts: any) {
opts = opts || Object.create(null);
/*
* @desc 二阶贝塞尔
* @param {number} t 当前百分比
* @param {} p1 起点坐标
* @param {} cp 控制点
* @param {} p2 终点坐标
* @returns {any}
*/
let twoBezier = (t:number, p1: Vec3, cp: Vec3, p2: Vec3) => {
let x = (1 - t) * (1 - t) * p1.x + 2 * t * (1 - t) * cp.x + t * t * p2.x;
let y = (1 - t) * (1 - t) * p1.y + 2 * t * (1 - t) * cp.y + t * t * p2.y;
return v3(x, y, 0);
};
opts.onUpdate = (arg: Vec3, ratio: number) => {
target.position = twoBezier(ratio, c1, c2, to);
};
return tween(target).to(duration, {}, opts);
}
to_console(value:any,value2:any=null,value3:any=null){
console.log("["+this.s_name+this.s_uuid+"]:",value,value2,value3)
}

View File

@@ -1,143 +0,0 @@
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;
}
}
}

View File

@@ -1,9 +0,0 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "02c5af20-5daa-4384-bcc4-c4c73d827ab7",
"files": [],
"subMetas": {},
"userData": {}
}