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"; 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; angle:number = 0; t_pos:Vec3 = null; is_destroy:boolean = false; box_group:number = 0; box_tag:number=0; start() { 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.END_CONTACT, this.onEndContact, this); collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this); } if(this.t_pos){ //通过欧拉角 延长 目标点位置 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; tween(this.node).to( 1,{ angle:this.angle,position: this.t_pos}, { onUpdate: (target: Vec3, ratio: number) => { // onUpdate 接受当前缓动的进度 if(target.y <= 200 ){ this.toDestroy() } // 将缓动系统计算出的结果赋予 node 的位置 }, onComplete: (target?: object) => { this.toDestroy() }, } ).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.toDestroy() }, } ).start(); } } onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) { if(otherCollider.group != selfCollider.group){ console.log("skill end contact",selfCollider.group,otherCollider.group) setTimeout(() => { this.toDestroy() }, 10); } } 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) } move(dt: number) { // this.node.setPosition(v3(this.node.position.x+dt*this.x_speed*this.scale,this.node.position.y+this.y_speed,this.node.position.z)) } check_to_destroy(){ if(!this.is_destroy){ this.is_destroy = true; this.toDestroy(); } } toDestroy() { if(!this.is_destroy){ this.is_destroy = true; if(this.node.isValid) this.ent.destroy() } } /** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */ reset() { this.is_destroy=false this.node.destroy(); } }