86 lines
4.0 KiB
TypeScript
86 lines
4.0 KiB
TypeScript
import { _decorator,Contact2DType,Collider2D ,IPhysics2DContact,v3, v2,Vec3} 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 { HeroViewComp } from "./HeroViewComp";
|
|
import { smc } from "../common/SingletonModuleComp";
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/** 视图层对象 */
|
|
@ccclass('BoxRangComp')
|
|
@ecs.register('BoxRang', false)
|
|
export class BoxRangComp extends CCComp {
|
|
Hero_node: any=null!;
|
|
HeroViewComp:HeroViewComp = null!;
|
|
box_group:number = BoxSet.HERO;
|
|
box_tag:number = BoxSet.ATK_RANGE;
|
|
offset_x:number = 300;
|
|
atk_range:number = 150;
|
|
/** 视图层逻辑代码分离演示 */
|
|
start() {
|
|
let collider = this.getComponent(Collider2D);
|
|
collider.group = this.box_group;
|
|
collider.tag = this.box_tag;
|
|
// collider.offset = v2(300*this.Hero_node.MonView.scale,collider.offset.y);
|
|
this.Hero_node = this.node.parent;
|
|
this.HeroViewComp=this.Hero_node.getComponent(HeroViewComp);
|
|
// console.log("range box",this.HeroViewComp);
|
|
if (collider) {
|
|
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
|
collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
|
|
collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
|
|
collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
|
|
}
|
|
}
|
|
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
|
|
// if(selfCollider.group != otherCollider.group&&otherCollider.tag == 0){
|
|
// console.log(this.node.name+"onBeginContact: seft:"+selfCollider.group+"|other:"+otherCollider.group+"| tag: seft:"+selfCollider.tag+"|other:"+otherCollider.tag);
|
|
// this.HeroViewComp.is_atking = true;
|
|
// if(Math.abs(otherCollider.node.position.x-selfCollider.node.position.x) < 120){
|
|
// this.HeroViewComp.stop_cd = 0.1
|
|
// }
|
|
// }
|
|
}
|
|
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
|
|
if(selfCollider.group != otherCollider.group&&otherCollider.tag == 0 ){
|
|
// console.log(this.node.name+"onEndContact: seft:"+selfCollider.group+"|other:"+otherCollider.group+"| tag: seft:"+selfCollider.tag+"|other:"+otherCollider.tag);
|
|
this.HeroViewComp.is_atking = false;
|
|
this.HeroViewComp.enemy = null;
|
|
this.HeroViewComp.as.change_default("move");
|
|
this.HeroViewComp.as.move();
|
|
}
|
|
}
|
|
onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
if(selfCollider.group != otherCollider.group&&otherCollider.tag == 0){
|
|
let scene =smc.map.MapView.scene.mapLayer!.node!
|
|
let other_pos = otherCollider.node.getWorldPosition() ;
|
|
let self_pos = this.node.getWorldPosition();
|
|
// console.log("onPreSolve:",self_pos,other_pos);
|
|
if(this.HeroViewComp.enemy==null){
|
|
this.HeroViewComp.enemy = otherCollider.node;
|
|
}else{
|
|
if(this.HeroViewComp.enemy.isValid==false){
|
|
this.HeroViewComp.enemy = otherCollider.node;
|
|
}
|
|
}
|
|
if(Math.abs(other_pos.x-self_pos.x) < this.HeroViewComp.atk_dis){
|
|
this.HeroViewComp.is_atking = true;
|
|
this.HeroViewComp.stop_cd = 0.1
|
|
this.HeroViewComp.as.change_default("idle");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
|
|
|
|
|
|
|
|
}
|
|
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
|
|
reset() {
|
|
this.node.destroy();
|
|
}
|
|
} |