Files
heros/assets/script/game/skills/SkillCom.ts
2024-08-11 16:50:42 +08:00

102 lines
3.5 KiB
TypeScript

import { _decorator,Collider2D ,Contact2DType,v3,IPhysics2DContact} 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";
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 = 600;
range:number = 80;
scale:number = 1;
atk:number = 10;
is_destroy:boolean = false;
start() {
let collider = this.getComponent(Collider2D);
if (collider) {
// collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
}
}
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
switch (selfCollider.group) {
case BoxSet.HERO_SKILL:
switch (otherCollider.group){
case BoxSet.MONSTER:
if(this.is_destroy){
return
}else{
this.is_destroy = true;
this.toDestroy();
}
// this.speed = 0;
// this.timer = 1;
// console.log("speed:"+this.speed+" | timer:"+this.timer);
break;
}
break;
case BoxSet.MONSTER_SKILL:
switch (otherCollider.group){
case BoxSet.HERO:
if(this.is_destroy){
return
}else{
this.is_destroy = true;
this.toDestroy();
}
// console.log('monster skill',selfCollider,otherCollider);
// this.reset()
break;
}
}
}
change_collider_group(group:number){
let collider = this.getComponent(Collider2D);
collider.group = group;
}
update(deltaTime: number) {
this.node.setScale(v3(this.scale,this.node.scale.y,this.node.scale.z))
this.node.setPosition(v3(this.node.position.x+deltaTime*this.speed*this.scale,this.node.position.y,this.node.position.z))
if(Math.abs(this.node.position.x) > this.range)
{
if(this.is_destroy){
return
}else{
this.is_destroy = true;
this.toDestroy()
}
}
}
/** 全局消息逻辑处理 */
// private onHandler(event: string, args: any) {
// switch (event) {
// case ModuleEvent.Cmd:
// break;
// }
// }
toDestroy() {
setTimeout(() => {
this.ent.destroy()
}, 15);
}
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.is_destroy=false
this.node.destroy();
}
}