This commit is contained in:
2024-07-29 11:05:17 +08:00
parent f131de6489
commit 5fa8794c09
18 changed files with 304 additions and 44 deletions

View File

@@ -811,7 +811,7 @@
"tag": 0, "tag": 0,
"_group": 4, "_group": 4,
"_density": 1, "_density": 1,
"_sensor": false, "_sensor": true,
"_friction": 0.2, "_friction": 0.2,
"_restitution": 0, "_restitution": 0,
"_offset": { "_offset": {
@@ -846,9 +846,9 @@
"bullet": true, "bullet": true,
"awakeOnLoad": true, "awakeOnLoad": true,
"_group": 4, "_group": 4,
"_type": 0, "_type": 2,
"_allowSleep": true, "_allowSleep": true,
"_gravityScale": 1, "_gravityScale": 0,
"_linearDamping": 0, "_linearDamping": 0,
"_angularDamping": 0, "_angularDamping": 0,
"_linearVelocity": { "_linearVelocity": {

View File

@@ -808,10 +808,10 @@
"__prefab": { "__prefab": {
"__id__": 39 "__id__": 39
}, },
"tag": 1, "tag": 0,
"_group": 2, "_group": 2,
"_density": 1, "_density": 1,
"_sensor": false, "_sensor": true,
"_friction": 0.2, "_friction": 0.2,
"_restitution": 0, "_restitution": 0,
"_offset": { "_offset": {
@@ -846,9 +846,9 @@
"bullet": true, "bullet": true,
"awakeOnLoad": true, "awakeOnLoad": true,
"_group": 2, "_group": 2,
"_type": 0, "_type": 2,
"_allowSleep": true, "_allowSleep": true,
"_gravityScale": 1, "_gravityScale": 0,
"_linearDamping": 0, "_linearDamping": 0,
"_angularDamping": 0, "_angularDamping": 0,
"_linearVelocity": { "_linearVelocity": {

View File

@@ -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) { }
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "d331c466-f23e-4f9a-bce4-ae002d8d7871",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -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,
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "28d11009-6d68-462a-9880-8b31cf5975fd",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -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);
});
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "ea3b7cf5-1be6-4436-a5a3-df3e1c913cb5",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -55,8 +55,7 @@ export class Hero extends ecs.Entity {
}).start(); }).start();
//移除全局列表 //移除全局列表
smc.heros.splice(0,1) smc.heros.splice(0,1)
console.log(ecs.query(ecs.allOf(HeroViewComp)) // console.log(ecs.query(ecs.allOf(HeroViewComp)))
)
} }

View File

@@ -5,11 +5,11 @@
* @LastEditTime: 2022-08-17 12:36:18 * @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 { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp"; import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { HeroSpine } from "./HeroSpine"; import { HeroSpine } from "./HeroSpine";
import {BoxSet} from "../common/config/BoxSet"
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
/** 角色显示组件 */ /** 角色显示组件 */
@@ -20,6 +20,38 @@ export class HeroViewComp extends CCComp {
as: HeroSpine = null!; 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() { onLoad() {
this.as = this.getComponent(HeroSpine); this.as = this.getComponent(HeroSpine);

View File

@@ -29,7 +29,7 @@ export class MapViewComp extends CCComp {
oops.message.on("monster_load", this.onMonsterLoaded, this); oops.message.on("monster_load", this.onMonsterLoaded, this);
} }
private onMonsterLoaded(event: string, args: any) { private onMonsterLoaded(event: string, args: any) {
console.log('on_monster_load', args); console.log('on_monster_load');
} }
reset(): void { reset(): void {

View File

@@ -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);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "52aacfb8-cb73-4ce1-be54-3c2a83e83408",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -15,13 +15,11 @@ import { MonsterModelComp } from "./MonsterModelComp";
import { MonsterSpine } from "./MonsterSpine"; import { MonsterSpine } from "./MonsterSpine";
import { MonsterViewComp } from "./MonsterViewComp"; import { MonsterViewComp } from "./MonsterViewComp";
import { MoveToComp } from "../common/MoveTo"; import { MoveToComp } from "../common/MoveTo";
import { GameCollision } from "../../../../extensions/oops-plugin-framework/assets/module/common/GameCollision";
/** 角色实体 */ /** 角色实体 */
@ecs.register(`Monster`) @ecs.register(`Monster`)
export class Monster extends ecs.Entity { export class Monster extends ecs.Entity {
// 数据层 // 数据层
MonsterModel!: MonsterModelComp; MonsterModel!: MonsterModelComp;
GameCollision: GameCollision =new GameCollision();
// 视图层 // 视图层
MonsterView!: MonsterViewComp; MonsterView!: MonsterViewComp;
RoleMoveTo!: MoveToComp; // 移动 RoleMoveTo!: MoveToComp; // 移动
@@ -49,7 +47,7 @@ export class Monster extends ecs.Entity {
var mv = node.getComponent(MonsterViewComp)!; var mv = node.getComponent(MonsterViewComp)!;
mv.speed = speed; mv.speed = speed;
console.log("speed:"+mv.speed) // console.log("speed:"+mv.speed)
mv.Tpos = v3(0,0,0); mv.Tpos = v3(0,0,0);
this.add(mv); this.add(mv);
// node.setScale(-1, 1, 1); // node.setScale(-1, 1, 1);

View File

@@ -17,13 +17,13 @@ export class MonsterModelComp extends ecs.Comp {
/** 角色名 */ /** 角色名 */
name: string = "monster"; name: string = "monster";
/** speed */ /** speed */
speed: number = 0; // speed: number = 0;
/** 动画名资源 */ /** 动画名资源 */
anim: string = "monster"; anim: string = "monster";
reset() { reset() {
this.id = -1; this.id = -1;
this.speed = 0; // this.speed = 0;
this.name = ""; this.name = "";
} }
} }

View File

@@ -36,7 +36,7 @@ export class MonsterSpine extends Component {
/** 初始化动画 */ /** 初始化动画 */
protected initAnimator() { protected initAnimator() {
this.spine = this.animator.getComponent(sp.Skeleton)!; this.spine = this.animator.getComponent(sp.Skeleton)!;
console.log("MonsterSpine initAnimator", this.spine); // console.log("MonsterSpine initAnimator", this.spine);
} }

View File

@@ -11,6 +11,7 @@ import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/modu
import { MonsterSpine } from "./MonsterSpine"; import { MonsterSpine } from "./MonsterSpine";
import { Monster } from "./Monster"; import { Monster } from "./Monster";
import { MonsterModelComp } from "./MonsterModelComp"; import { MonsterModelComp } from "./MonsterModelComp";
import { BoxSet } from "../common/config/BoxSet";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@@ -25,39 +26,33 @@ export class MonsterViewComp extends CCComp {
/** 视图层逻辑代码分离演示 */ /** 视图层逻辑代码分离演示 */
start () { start () {
// 注册单个碰撞体的回调函数 // 注册单个碰撞体的回调函数
console.log('MonsterViewComp start'); // console.log('MonsterViewComp start');
let collider = this.getComponent(Collider2D); let collider = this.getComponent(Collider2D);
// console.log('Monster collider',collider);
if (collider) { if (collider) {
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); 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) { if (PhysicsSystem2D.instance) {
console.log('PhysicsSystem2D.instance'); // console.log('PhysicsSystem2D.instance');
PhysicsSystem2D.instance.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); 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) { onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体开始接触时被调用一次 // 只在两个碰撞体开始接触时被调用一次
console.log('onBeginContact'); // 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;
} }
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');
} }
onLoad() { onLoad() {

View File

@@ -27,23 +27,23 @@
"collisionGroups": [ "collisionGroups": [
{ {
"index": 1, "index": 1,
"name": "monster" "name": "MONSTER"
}, },
{ {
"index": 2, "index": 2,
"name": "hero" "name": "HERO"
}, },
{ {
"index": 3, "index": 3,
"name": "monster_skill" "name": "MONSTER_SKILL"
}, },
{ {
"index": 4, "index": 4,
"name": "hero_skill" "name": "HERO_SKILL"
} }
], ],
"collisionMatrix": { "collisionMatrix": {
"0": 1, "0": 0,
"1": 20, "1": 20,
"2": 10, "2": 10,
"3": 4, "3": 4,