原来是全局碰撞惹的祸

This commit is contained in:
2024-07-31 10:48:42 +08:00
parent df8b52264e
commit 9aa9806b62
38 changed files with 1640 additions and 313 deletions

View File

@@ -8,10 +8,12 @@
/** 碰撞分组 */
export enum BoxSet {
DEFAULT = 0,
MONSTER = 2,
HERO = 4,
MONSTER_SKILL = 8,
HERO_SKILL = 16,
MONSTER = 90,
HERO = 10,
MONSTER_SKILL = 91,
HERO_SKILL = 11,
BOX_WIDTH = 64,
BOX_HEIGHT = 64
BOX_HEIGHT = 64,
LETF_END = -400,
RIGHT_END = 400
}

View File

@@ -4,7 +4,9 @@ import { CCComp } from "../../../../../extensions/oops-plugin-framework/assets/m
import { smc } from "../../common/SingletonModuleComp";
import { Role } from "../../role/Role";
import { Niu } from "../../monster/niu/Niu";
import { BoxSet } from "../../common/config/BoxSet";
import { Hero } from "../../heros/Hero";
import { Monster } from "../../monster/Monster";
// import MapRoadUtils from "./map/road/MapRoadUtils";
import { MapViewScene } from "./MapViewScene";
import { Timer } from "../../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
@@ -15,8 +17,7 @@ const { ccclass, property } = _decorator;
@ecs.register('MapView', false)
export class MapViewComp extends CCComp {
scene: MapViewScene = null!;
@property(Prefab)
boss: Prefab = null!;
/** 是否正在转场 */
@@ -55,17 +56,32 @@ export class MapViewComp extends CCComp {
/** 添加玩家 */
private addHero() {
this.scene.node.active = true
if (smc.monsters.length>0){
let monster = ecs.getEntity<Niu>(Niu);
monster.load(v3(360,-60),smc.monsters[0].speed);
this.addMonster(smc.monsters[0].name,smc.monsters[0].speed)
smc.monsters.splice(0,1)
}
// if(smc.heros.length>0) {
// this.addMonster(smc.heros[0].name,100)
// }
}
private addMonster(name:string = "niu",speed:number = 100) {
let monster = null
switch (name) {
case "niu":
monster = ecs.getEntity<Niu>(Niu);
break;
case "hero":
monster = ecs.getEntity<Monster>(Monster);
break;
default:
break;
}
monster.load(v3(BoxSet.RIGHT_END,-60),speed);
}
private getRandomInt(min: number, max: number): number {
min = Math.ceil(min);
max = Math.floor(max);

View File

@@ -64,7 +64,7 @@ export class MonsterViewComp extends CCComp {
smc.monsters_in.forEach((element,index) => {
if(element.eid == this.ent.eid){
console.log("index:"+index)
// console.log("index:"+index)
element.pos_x = this.node.position.x;
}
});

View File

@@ -39,7 +39,7 @@ export class Niu extends ecs.Entity {
var node = instantiate(prefab);
var scene = smc.map.MapView.scene;
node.parent = scene.entityLayer!.node!;
var as = node.getComponent(MonsterSpine);
// var as = node.getComponent(MonsterSpine);
node.setPosition(pos)
var mv = node.getComponent(NiuViewComp)!;

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} 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";
@@ -23,18 +23,63 @@ export class NiuViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
speed: number = 100;
ospeed: number = 100;
@property
private _ospeed: number = 100;
@property
set ospeed(val: number) {
this._ospeed = val;
}
get ospeed() {
return this._ospeed;
}
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 || otherCollider.tag == BoxSet.MONSTER) {
console.log('onBeginContact otherCollider.tag :',otherCollider,selfCollider);
this.speed = 0;
this.timer = 1;
}
}
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 || otherCollider.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);
this.ospeed= this.speed;
console.log('this.ospeed:'+this.ospeed);
PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
EPhysics2DDrawFlags.Pair |
EPhysics2DDrawFlags.CenterOfMass |
EPhysics2DDrawFlags.Joint |
EPhysics2DDrawFlags.Shape;
}
update(dt: number){
@@ -45,10 +90,10 @@ export class NiuViewComp extends CCComp {
this.timer = 0;
}
}
if(this.node.position.x > -360){
if(this.node.position.x > BoxSet.LETF_END){
this.move(dt);
}
if(this.node.position.x < -300){
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();
@@ -63,7 +108,7 @@ export class NiuViewComp extends CCComp {
smc.monsters_in.forEach((element,index) => {
if(element.eid == this.ent.eid){
console.log("index:"+index)
// console.log("index:"+index)
element.pos_x = this.node.position.x;
}
});