dd
This commit is contained in:
86
assets/script/game/common/GameCollision.ts
Normal file
86
assets/script/game/common/GameCollision.ts
Normal 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) { }
|
||||
}
|
||||
9
assets/script/game/common/GameCollision.ts.meta
Normal file
9
assets/script/game/common/GameCollision.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "d331c466-f23e-4f9a-bce4-ae002d8d7871",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
15
assets/script/game/common/config/BoxSet.ts
Normal file
15
assets/script/game/common/config/BoxSet.ts
Normal 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,
|
||||
}
|
||||
9
assets/script/game/common/config/BoxSet.ts.meta
Normal file
9
assets/script/game/common/config/BoxSet.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "28d11009-6d68-462a-9880-8b31cf5975fd",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
58
assets/script/game/common/config/PhysicsUtil.ts
Normal file
58
assets/script/game/common/config/PhysicsUtil.ts
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
9
assets/script/game/common/config/PhysicsUtil.ts.meta
Normal file
9
assets/script/game/common/config/PhysicsUtil.ts.meta
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "4.0.23",
|
||||
"importer": "typescript",
|
||||
"imported": true,
|
||||
"uuid": "ea3b7cf5-1be6-4436-a5a3-df3e1c913cb5",
|
||||
"files": [],
|
||||
"subMetas": {},
|
||||
"userData": {}
|
||||
}
|
||||
Reference in New Issue
Block a user