This commit is contained in:
2024-08-01 10:57:39 +08:00
parent 9aa9806b62
commit 6c5d417ad1
13 changed files with 119 additions and 330 deletions

View File

@@ -5,7 +5,7 @@
* @LastEditTime: 2022-08-17 12:36:18
*/
import { Vec3, _decorator ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from "cc";
import { Vec3, _decorator ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact,EPhysics2DDrawFlags,Label,Node} 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 { MonsterSpine } from "./MonsterSpine";
@@ -22,22 +22,65 @@ const { ccclass, property } = _decorator;
export class MonsterViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
hero_name : string = "hero";
camp: number = 1;
speed: number = 100;
ospeed: number = 100;
Tpos: Vec3 = v3(0,-60,0);
timer: number = 0;
/** 视图层逻辑代码分离演示 */
start () {
// 注册单个碰撞体的回调函数
let collider = this.getComponent(Collider2D);
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) {
// 只在两个碰撞体开始接触时被调用一次
// console.log('onBeginContact otherCollider.tag :',otherCollider,selfCollider);
if ( otherCollider.tag == BoxSet.HERO && selfCollider.tag == BoxSet.MONSTER) {
this.speed = 0
this.timer = 0.5
}
}
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体结束接触时被调用一次
// console.log('onEndContact');
}
// onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// // 每次将要处理碰撞体接触逻辑时被调用
// console.log('onPreSolve');
// }
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
if (otherCollider.tag === BoxSet.HERO && selfCollider.tag === BoxSet.MONSTER) {
// console.log('onPostSolve otherCollider.tag :'+otherCollider.tag);
// this.speed = 0;
// this.timer = 1;
}
}
onLoad() {
this.as = this.getComponent(MonsterSpine);
console.log('this.ent:',this);
console.log('hero load ent:',this);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
}
change_name(hero_name:string){
this.name=hero_name;
let label:any =this.node.getChildByName("top").getChildByName("lab_name")
label.getComponent(Label)!.string = hero_name;
}
update(dt: number){
if(this.timer > 0){
this.timer -= dt;
@@ -46,19 +89,30 @@ export class MonsterViewComp extends CCComp {
this.timer = 0;
}
}
if(this.node.position.x > -360){
this.move(dt);
if(this.camp == -1){
if(this.node.position.x < BoxSet.LETF_END){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
}else{
this.move(dt);
}
}
if(this.node.position.x < -300){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
if(this.camp == 1){
if(this.node.position.x > BoxSet.RIGHT_END){
console.log(this.node,this.ent)
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.node.destroy();
}else{
this.move(dt);
}
}
this.update_pos();
}
move(dt: number){
this.node.setPosition(this.node.position.x-dt*this.speed, this.node.position.y, this.node.position.z);
this.node.setPosition(this.node.position.x+dt*this.speed*this.camp, this.node.position.y, this.node.position.z);
}
update_pos(){
smc.monsters_in.forEach((element,index) => {