Files
heros/assets/script/game/skills/SkillCom.ts
2024-09-12 13:46:42 +08:00

207 lines
8.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { _decorator,Collider2D ,Contact2DType,v3,IPhysics2DContact,Vec3, tween} 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 { MoveToComp } from "../common/ecs/position/MoveTo";
import { MonModelComp } from "../mon/MonModelComp";
import { HeroViewComp } from "../hero/HeroViewComp";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('SkillCom')
@ecs.register('SkillView', false)
export class SkillCom extends CCComp {
/** 视图层逻辑代码分离演示 */
// start() {
// // var entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
// // this.on(ModuleEvent.Cmd, this.onHandler, this);
// }
speed:number = 200;
y_speed:number = 0;
x_speed:number = 0;
dis:number = 80;
scale:number = 1;
atk:number = 10;
atk_count:number = 0;
angle:number = 0;
t_pos:Vec3 = null; // 目标增量
is_destroy:boolean = false;
box_group:number = 0;
box_tag:number=0;
type:number = 1;
time:Timer = new Timer(0.01);
run_type:number = 0; // 0直线1贝塞尔曲线 2 不动
in_time:number = 1.5; // 不动技能持续时间
start() {
// console.log("skill start parent :",this.node.parent)
// console.log("skill start t position :",this.t_pos)
this.node.active=true
this.node.angle = this.angle;
let collider = this.getComponent(Collider2D);
collider.group = this.box_group;
// console.log("skill group",this.box_group)
collider.tag = this.box_tag;
collider.sensor = true;
if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
// collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
if(this.t_pos){
this.x_speed=Math.cos(this.angle * Math.PI / 180) * this.speed-30*this.scale;
this.y_speed=Math.sin(this.angle * Math.PI / 180) * this.speed;
}else{
this.x_speed=this.speed
this.y_speed=0
}
if(this.run_type == 1){
let s_pos = v3(this.node.position.x,this.node.position.y)
let c_pos = v3(this.node.position.x,this.node.position.y)
let e_pos = v3(this.node.position.x+this.dis*this.scale,BoxSet.GAME_LINE-50)
SkillCom.bezierTo(this.node,2,s_pos,c_pos,e_pos,{onUpdate: (target: Vec3, ratio: number) => {
}
}).start();
}
if(this.run_type == 2){
tween(this.node).to( this.in_time,
{ position: new Vec3(this.node.position.x,this.node.position.y) },
{
onComplete: (target?: object) => {
this.is_destroy=true
},
}
).start();
}
if(this.run_type == 0){
if(this.t_pos){
let l_x=Math.cos(this.angle * Math.PI / 180) * 50;
let l_y=Math.sin(this.angle * Math.PI / 180) * 50;
let squaredDistance = this.t_pos.x * this.t_pos.x + this.t_pos.y * this.t_pos.y;
let distance = Math.sqrt(squaredDistance);
let time = distance / this.speed-0.2;
//通过欧拉角 延长 目标点 增量
// this.t_pos.x=Math.cos(this.angle * Math.PI / 180) * this.dis;
// this.t_pos.y=Math.sin(this.angle * Math.PI / 180) * this.dis;
let e_pos=v3(this.node.position.x+l_x+this.t_pos.x,this.node.position.y+this.t_pos.y)
tween(this.node).to( 0.7,{ angle:this.angle,position: e_pos},
{
onUpdate: (target: Vec3, ratio: number) => { // onUpdate 接受当前缓动的进度
// 将缓动系统计算出的结果赋予 node 的位置
},
onComplete: (target?: object) => {
this.is_destroy=true
},
}
).start();
}else{
tween(this.node).to( this.dis/this.speed,
{ position: new Vec3(this.node.position.x+this.scale*this.dis,this.node.position.y) },
{
onComplete: (target?: object) => {
this.is_destroy=true
},
}
).start();
}
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
if(otherCollider.group != selfCollider.group&&otherCollider.tag ==0){
this.atk_count+=1
// console.log("skill onBeginContact",selfCollider.group,otherCollider.group)
if(this.type==1 ){
this.is_destroy=true
}
}
}
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// console.log("skill post contact")
// switch (selfCollider.group) {
// case BoxSet.HERO:
// switch (otherCollider.group){
// case BoxSet.MONSTER:
// case BoxSet.DEFAULT:
// setTimeout(() => {
// this.toDestroy()
// }, 10);
// break
// }
// break;
// case BoxSet.MONSTER:
// switch (otherCollider.group){
// case BoxSet.PLAYER:
// case BoxSet.HERO:
// case BoxSet.DEFAULT:
// setTimeout(() => {
// this.toDestroy()
// }, 10);
// break
// }
// }
}
update(deltaTime: number) {
// this.node.setScale(v3(this.scale,this.node.scale.y,this.node.scale.z))
// this.move(deltaTime)
this.toDestroy()
// this.t_move(deltaTime)
if (this.node.position.x > 400||this.node.position.x < -400||this.node.position.y > 1000||this.node.position.y < -100) {
this.is_destroy=true
}
}
t_move(dt: number){
if( this.run_type!=0){
return
}
this.node.setPosition(v3(this.node.position.x+this.x_speed*dt*this.scale,this.node.position.y+this.y_speed*dt))
}
line_move(dt: number) {
if( this.run_type!=0 && this.t_pos){
return
}
this.node.setPosition(v3(this.node.position.x+dt*this.speed*this.scale,this.node.position.y))
}
bezier(t:number){
}
toDestroy() {
if(this.is_destroy){
if(this.node.isValid) this.ent.destroy()
}
}
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.is_destroy=false
this.node.destroy();
}
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);
}
}