69 lines
2.4 KiB
TypeScript
69 lines
2.4 KiB
TypeScript
/*
|
|
* @Author: dgflash
|
|
* @Date: 2021-11-18 17:42:59
|
|
* @LastEditors: dgflash
|
|
* @LastEditTime: 2022-08-17 12:36:18
|
|
*/
|
|
|
|
import { Vec3, _decorator ,tween, v3,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 { MonsterSpine } from "./MonsterSpine";
|
|
import { Monster } from "./Monster";
|
|
import { MonsterModelComp } from "./MonsterModelComp";
|
|
import { BoxSet } from "../common/config/BoxSet";
|
|
|
|
const { ccclass, property } = _decorator;
|
|
|
|
/** 角色显示组件 */
|
|
@ccclass('MonsterViewComp') // 定义为 Cocos Creator 组件
|
|
@ecs.register('MonsterView', false) // 定义为 ECS 组件
|
|
export class MonsterViewComp extends CCComp {
|
|
/** 角色动画 */
|
|
as: MonsterSpine = null!;
|
|
speed: number = 100;
|
|
Tpos: Vec3 = v3(0,-60,0);
|
|
/** 视图层逻辑代码分离演示 */
|
|
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);
|
|
}
|
|
|
|
// 注册全局碰撞回调函数
|
|
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('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() {
|
|
this.as = this.getComponent(MonsterSpine);
|
|
}
|
|
|
|
update(dt: number){
|
|
this.node.setPosition(this.node.position.x-dt*this.speed, this.node.position.y, this.node.position.z);
|
|
}
|
|
|
|
reset() {
|
|
this.node.destroy();
|
|
}
|
|
} |