diff --git a/assets/resources/game/heros/war.prefab b/assets/resources/game/heros/war.prefab index ff95cd56..3de7aba9 100644 --- a/assets/resources/game/heros/war.prefab +++ b/assets/resources/game/heros/war.prefab @@ -811,7 +811,7 @@ "tag": 0, "_group": 4, "_density": 1, - "_sensor": false, + "_sensor": true, "_friction": 0.2, "_restitution": 0, "_offset": { @@ -846,9 +846,9 @@ "bullet": true, "awakeOnLoad": true, "_group": 4, - "_type": 0, + "_type": 2, "_allowSleep": true, - "_gravityScale": 1, + "_gravityScale": 0, "_linearDamping": 0, "_angularDamping": 0, "_linearVelocity": { diff --git a/assets/resources/game/monster/monster.prefab b/assets/resources/game/monster/monster.prefab index 0156d59d..2c7e0c14 100644 --- a/assets/resources/game/monster/monster.prefab +++ b/assets/resources/game/monster/monster.prefab @@ -808,10 +808,10 @@ "__prefab": { "__id__": 39 }, - "tag": 1, + "tag": 0, "_group": 2, "_density": 1, - "_sensor": false, + "_sensor": true, "_friction": 0.2, "_restitution": 0, "_offset": { @@ -846,9 +846,9 @@ "bullet": true, "awakeOnLoad": true, "_group": 2, - "_type": 0, + "_type": 2, "_allowSleep": true, - "_gravityScale": 1, + "_gravityScale": 0, "_linearDamping": 0, "_angularDamping": 0, "_linearVelocity": { diff --git a/assets/script/game/common/GameCollision.ts b/assets/script/game/common/GameCollision.ts new file mode 100644 index 00000000..a7d7ebd7 --- /dev/null +++ b/assets/script/game/common/GameCollision.ts @@ -0,0 +1,86 @@ +/* + * @Author: dgflash + * @Date: 2022-03-29 17:08:08 + * @LastEditors: dgflash + * @LastEditTime: 2022-09-02 09:45:41 + */ +import { ccenum, Collider, Component, ICollisionEvent, ITriggerEvent, _decorator } from "cc"; + +const { ccclass, property } = _decorator; + +/** 碰撞物体类型 */ +export enum CollisionType { + /** 角色类 */ + Hero, + /** 飞弹类体*/ + Ballistic, + /** 墙体类 */ + Wall +} +ccenum(CollisionType); + +/** 碰撞器与触发器 */ +@ccclass('GameCollision') +export class GameCollision extends Component { + private Event_TriggerEnter: any = "onTriggerEnter"; + private Event_TriggerStay: any = "onTriggerStay"; + private Event_TriggerExit: any = "onTriggerExit"; + private Event_CollisionEnter: any = "onCollisionEnter"; + private Event_CollisionStay: any = "onCollisionStay"; + private Event_CollisionExit: any = "onCollisionExit"; + + protected collider: Collider = null!; + + @property({ type: CollisionType, tooltip: '碰撞物体类型' }) + type: CollisionType = CollisionType.Ballistic; + + onLoad() { + this.collider = this.getComponent(Collider)!; + if (this.collider.isTrigger) { + this.collider.on(this.Event_TriggerEnter, this.onTrigger, this); + this.collider.on(this.Event_TriggerStay, this.onTrigger, this); + this.collider.on(this.Event_TriggerExit, this.onTrigger, this); + } + else { + this.collider.on(this.Event_CollisionEnter, this.onCollision, this); + this.collider.on(this.Event_CollisionStay, this.onCollision, this); + this.collider.on(this.Event_CollisionExit, this.onCollision, this); + } + } + + private onTrigger(event: ITriggerEvent) { + switch (event.type) { + case this.Event_TriggerEnter: + this.onTriggerEnter(event); + break; + case this.Event_TriggerStay: + this.onTriggerStay(event); + break; + case this.Event_TriggerExit: + this.onTriggerExit(event); + break; + } + } + + protected onTriggerEnter(event: ITriggerEvent) { } + protected onTriggerStay(event: ITriggerEvent) { } + protected onTriggerExit(event: ITriggerEvent) { } + + private onCollision(event: ICollisionEvent) { + switch (event.type) { + case this.Event_CollisionEnter: + this.onCollisionEnter(event); + break; + case this.Event_CollisionStay: + this.onCollisionStay(event); + break; + case this.Event_CollisionExit: + this.onCollisionExit(event); + break; + } + } + + protected onCollisionEnter(event: ICollisionEvent) { } + protected onCollisionStay(event: ICollisionEvent) { } + protected onCollisionExit(event: ICollisionEvent) { } +} diff --git a/assets/script/game/common/GameCollision.ts.meta b/assets/script/game/common/GameCollision.ts.meta new file mode 100644 index 00000000..295f9857 --- /dev/null +++ b/assets/script/game/common/GameCollision.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "d331c466-f23e-4f9a-bce4-ae002d8d7871", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/script/game/common/config/BoxSet.ts b/assets/script/game/common/config/BoxSet.ts new file mode 100644 index 00000000..1acaa51f --- /dev/null +++ b/assets/script/game/common/config/BoxSet.ts @@ -0,0 +1,15 @@ +/* + * @Author: dgflash + * @Date: 2021-11-23 15:28:39 + * @LastEditors: dgflash + * @LastEditTime: 2022-01-26 16:42:00 + */ + +/** 碰撞分组 */ +export enum BoxSet { + DEFAULT = 0, + MONSTER = 2, + HERO = 4, + MONSTER_SKILL = 8, + HERO_SKILL = 16, +} \ No newline at end of file diff --git a/assets/script/game/common/config/BoxSet.ts.meta b/assets/script/game/common/config/BoxSet.ts.meta new file mode 100644 index 00000000..a5e5c9be --- /dev/null +++ b/assets/script/game/common/config/BoxSet.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "28d11009-6d68-462a-9880-8b31cf5975fd", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/script/game/common/config/PhysicsUtil.ts b/assets/script/game/common/config/PhysicsUtil.ts new file mode 100644 index 00000000..16e1dd6d --- /dev/null +++ b/assets/script/game/common/config/PhysicsUtil.ts @@ -0,0 +1,58 @@ +/* + * @Author: dgflash + * @Date: 2022-07-21 17:30:59 + * @LastEditors: dgflash + * @LastEditTime: 2022-09-02 14:40:28 + */ +import { Node } from "cc"; + +/** 物理分组数据 */ +export class GroupItem { + private _value: number; + /** 分组值 */ + get value(): number { + return this._value; + } + + private _name!: string; + /** 分组名 */ + get name(): string { + return this._name; + } + + /** 碰撞掩码 */ + get mask(): number { + return 1 << this._value; + } + + /** + * 构造函数 + * @param value 分组值 + * @param name 分组名 + */ + constructor(value: number, name: string) { + this._value = value; + this._name = name; + } +} + +/*** + * 为了方便使用,将编辑器中的物理分组定义到代码。如果编辑器中有修改,确保同步到这里。 + */ +export class PhysicsUtil { + /** 默认物理分组 */ + static DEFAULT = new GroupItem(0, 'DEFAULT'); + /** 能通过屏幕触摸中发出的射线检查到的游戏对象 */ + static MONSTER = new GroupItem(2, 'MONSTER'); + static HERO = new GroupItem(4, 'HERO'); + static MONSTER_SKILL = new GroupItem(8, 'MONSTER_SKILL'); + static HERO_SKILL = new GroupItem(16, 'HERO_SKILL'); + + static setNodeLayer(item: GroupItem, node: Node) { + node.layer = item.mask; + node.children.forEach(n => { + n.layer = item.mask; + PhysicsUtil.setNodeLayer(item, n); + }); + } +} \ No newline at end of file diff --git a/assets/script/game/common/config/PhysicsUtil.ts.meta b/assets/script/game/common/config/PhysicsUtil.ts.meta new file mode 100644 index 00000000..085a9603 --- /dev/null +++ b/assets/script/game/common/config/PhysicsUtil.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "ea3b7cf5-1be6-4436-a5a3-df3e1c913cb5", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/script/game/heros/Hero.ts b/assets/script/game/heros/Hero.ts index 6aedf780..10503af4 100644 --- a/assets/script/game/heros/Hero.ts +++ b/assets/script/game/heros/Hero.ts @@ -55,8 +55,7 @@ export class Hero extends ecs.Entity { }).start(); //移除全局列表 smc.heros.splice(0,1) - console.log(ecs.query(ecs.allOf(HeroViewComp)) - ) + // console.log(ecs.query(ecs.allOf(HeroViewComp))) } diff --git a/assets/script/game/heros/HeroViewComp.ts b/assets/script/game/heros/HeroViewComp.ts index 738b6afb..c9f94e24 100644 --- a/assets/script/game/heros/HeroViewComp.ts +++ b/assets/script/game/heros/HeroViewComp.ts @@ -5,11 +5,11 @@ * @LastEditTime: 2022-08-17 12:36:18 */ -import { Vec3, _decorator ,tween} from "cc"; +import { Vec3, _decorator ,Collider2D,Contact2DType,PhysicsSystem2D,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 { HeroSpine } from "./HeroSpine"; - +import {BoxSet} from "../common/config/BoxSet" const { ccclass, property } = _decorator; /** 角色显示组件 */ @@ -20,6 +20,38 @@ export class HeroViewComp extends CCComp { as: HeroSpine = null!; /** 角色控制器 */ + + start () { + // 注册单个碰撞体的回调函数 + // console.log('MonsterViewComp start'); + let collider = this.getComponent(Collider2D); + // console.log('hero collider',collider); + if (collider) { + collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); + } + + // 注册全局碰撞回调函数 + if (PhysicsSystem2D.instance) { + // console.log('PhysicsSystem2D.instance'); + PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); + } + } + onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) { + // 只在两个碰撞体开始接触时被调用一次 + // console.log('hero Contact,selfCollider',selfCollider); + switch (otherCollider.group) { + case BoxSet.MONSTER: + console.log('hero coolider MONSTER '); + break; + case BoxSet.MONSTER_SKILL: + console.log('hero coolider MONSTER skill'); + break; + default: + break; + } + + } + /** 视图层逻辑代码分离演示 */ onLoad() { this.as = this.getComponent(HeroSpine); diff --git a/assets/script/game/map/view/MapViewComp.ts b/assets/script/game/map/view/MapViewComp.ts index 8b4fbdcd..6b4592fd 100644 --- a/assets/script/game/map/view/MapViewComp.ts +++ b/assets/script/game/map/view/MapViewComp.ts @@ -29,7 +29,7 @@ export class MapViewComp extends CCComp { oops.message.on("monster_load", this.onMonsterLoaded, this); } private onMonsterLoaded(event: string, args: any) { - console.log('on_monster_load', args); + console.log('on_monster_load'); } reset(): void { diff --git a/assets/script/game/monster/BaseMonsterModel.ts b/assets/script/game/monster/BaseMonsterModel.ts new file mode 100644 index 00000000..9d5d6cdf --- /dev/null +++ b/assets/script/game/monster/BaseMonsterModel.ts @@ -0,0 +1,41 @@ +/* + * @Author: dgflash + * @Date: 2021-11-18 15:56:01 + * @LastEditors: dgflash + * @LastEditTime: 2022-08-17 13:43:25 + */ +import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS"; +import { Monster } from "./Monster"; +/** + * 角色属性数据 + */ +@ecs.register('BaseMonsterModel') +export class BaseMonsterModel extends ecs.Comp { + /** 角色编号 */ + hp: number = 100; + /** 角色名 */ + name: string = "base monster"; + /** */ + + + + reset() { + this.hp = 100; + this.name = ""; + } + +} +@ecs.register('Monster') +export class MonsterUpgradeSystem extends ecs.ComblockSystem implements ecs.IEntityEnterSystem { + filter(): ecs.IMatcher { + + return ecs.allOf(BaseMonsterModel); + } + + entityEnter(e: Monster): void { + let MonsterModel = e.MonsterModel; + MonsterModel.name = "base monster" + console.log("MonsterUpgradeSystem", e); + e.remove(BaseMonsterModel); + } +} \ No newline at end of file diff --git a/assets/script/game/monster/BaseMonsterModel.ts.meta b/assets/script/game/monster/BaseMonsterModel.ts.meta new file mode 100644 index 00000000..2b7e2665 --- /dev/null +++ b/assets/script/game/monster/BaseMonsterModel.ts.meta @@ -0,0 +1,9 @@ +{ + "ver": "4.0.23", + "importer": "typescript", + "imported": true, + "uuid": "52aacfb8-cb73-4ce1-be54-3c2a83e83408", + "files": [], + "subMetas": {}, + "userData": {} +} diff --git a/assets/script/game/monster/Monster.ts b/assets/script/game/monster/Monster.ts index 81bba770..7396d0fd 100644 --- a/assets/script/game/monster/Monster.ts +++ b/assets/script/game/monster/Monster.ts @@ -15,13 +15,11 @@ import { MonsterModelComp } from "./MonsterModelComp"; import { MonsterSpine } from "./MonsterSpine"; import { MonsterViewComp } from "./MonsterViewComp"; import { MoveToComp } from "../common/MoveTo"; -import { GameCollision } from "../../../../extensions/oops-plugin-framework/assets/module/common/GameCollision"; /** 角色实体 */ @ecs.register(`Monster`) export class Monster extends ecs.Entity { // 数据层 MonsterModel!: MonsterModelComp; - GameCollision: GameCollision =new GameCollision(); // 视图层 MonsterView!: MonsterViewComp; RoleMoveTo!: MoveToComp; // 移动 @@ -49,7 +47,7 @@ export class Monster extends ecs.Entity { var mv = node.getComponent(MonsterViewComp)!; mv.speed = speed; - console.log("speed:"+mv.speed) + // console.log("speed:"+mv.speed) mv.Tpos = v3(0,0,0); this.add(mv); // node.setScale(-1, 1, 1); diff --git a/assets/script/game/monster/MonsterModelComp.ts b/assets/script/game/monster/MonsterModelComp.ts index b7aceac4..f04a6812 100644 --- a/assets/script/game/monster/MonsterModelComp.ts +++ b/assets/script/game/monster/MonsterModelComp.ts @@ -17,13 +17,13 @@ export class MonsterModelComp extends ecs.Comp { /** 角色名 */ name: string = "monster"; /** speed */ - speed: number = 0; + // speed: number = 0; /** 动画名资源 */ anim: string = "monster"; reset() { this.id = -1; - this.speed = 0; + // this.speed = 0; this.name = ""; } } diff --git a/assets/script/game/monster/MonsterSpine.ts b/assets/script/game/monster/MonsterSpine.ts index f9f54890..fa97ee5e 100644 --- a/assets/script/game/monster/MonsterSpine.ts +++ b/assets/script/game/monster/MonsterSpine.ts @@ -36,7 +36,7 @@ export class MonsterSpine extends Component { /** 初始化动画 */ protected initAnimator() { this.spine = this.animator.getComponent(sp.Skeleton)!; - console.log("MonsterSpine initAnimator", this.spine); + // console.log("MonsterSpine initAnimator", this.spine); } diff --git a/assets/script/game/monster/MonsterViewComp.ts b/assets/script/game/monster/MonsterViewComp.ts index e2d222d3..4216bb28 100644 --- a/assets/script/game/monster/MonsterViewComp.ts +++ b/assets/script/game/monster/MonsterViewComp.ts @@ -11,6 +11,7 @@ import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/modu import { MonsterSpine } from "./MonsterSpine"; import { Monster } from "./Monster"; import { MonsterModelComp } from "./MonsterModelComp"; +import { BoxSet } from "../common/config/BoxSet"; const { ccclass, property } = _decorator; @@ -25,39 +26,33 @@ export class MonsterViewComp extends CCComp { /** 视图层逻辑代码分离演示 */ start () { // 注册单个碰撞体的回调函数 - console.log('MonsterViewComp start'); + // console.log('MonsterViewComp start'); let collider = this.getComponent(Collider2D); + // console.log('Monster collider',collider); 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); } // 注册全局碰撞回调函数 if (PhysicsSystem2D.instance) { - console.log('PhysicsSystem2D.instance'); + // console.log('PhysicsSystem2D.instance'); PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); - PhysicsSystem2D.instance.on(Contact2DType.END_CONTACT, this.onEndContact, this); - PhysicsSystem2D.instance.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this); - PhysicsSystem2D.instance.on(Contact2DType.POST_SOLVE, this.onPostSolve, this); } } onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) { // 只在两个碰撞体开始接触时被调用一次 - console.log('onBeginContact'); - } - 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) { - // 每次处理完碰撞体接触逻辑时被调用 - console.log('onPostSolve'); + // console.log('monster Contact,selfCollider',selfCollider); + + switch (otherCollider.group) { + case BoxSet.HERO: + console.log('monster coolider hero'); + break; + case BoxSet.HERO_SKILL: + console.log('monster coolider hero skill'); + break; + default: + break; + } } onLoad() { diff --git a/settings/v2/packages/project.json b/settings/v2/packages/project.json index ae2c8358..a91a218b 100644 --- a/settings/v2/packages/project.json +++ b/settings/v2/packages/project.json @@ -27,23 +27,23 @@ "collisionGroups": [ { "index": 1, - "name": "monster" + "name": "MONSTER" }, { "index": 2, - "name": "hero" + "name": "HERO" }, { "index": 3, - "name": "monster_skill" + "name": "MONSTER_SKILL" }, { "index": 4, - "name": "hero_skill" + "name": "HERO_SKILL" } ], "collisionMatrix": { - "0": 1, + "0": 0, "1": 20, "2": 10, "3": 4,