ddd
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
* @LastEditors: dgflash
|
||||
* @LastEditTime: 2022-08-04 15:43:04
|
||||
*/
|
||||
import { instantiate, Node, Prefab, Vec3 ,tween} from "cc";
|
||||
import { instantiate, Node, Prefab, Vec3 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact} from "cc";
|
||||
import { UICallbacks } from "../../../../extensions/oops-plugin-framework/assets/core/gui/layer/Defines";
|
||||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||||
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
|
||||
@@ -14,18 +14,22 @@ import { smc } from "../common/SingletonModuleComp";
|
||||
import { MonsterModelComp } from "./MonsterModelComp";
|
||||
import { MonsterSpine } from "./MonsterSpine";
|
||||
import { MonsterViewComp } from "./MonsterViewComp";
|
||||
import { MoveTo } from "../../../../extensions/oops-plugin-framework/assets/libs/animator-move/MoveTo";
|
||||
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; // 移动
|
||||
|
||||
protected init() {
|
||||
this.addComponents<ecs.Comp>(
|
||||
MonsterModelComp);
|
||||
|
||||
}
|
||||
|
||||
destroy(): void {
|
||||
@@ -34,10 +38,8 @@ export class Monster extends ecs.Entity {
|
||||
}
|
||||
|
||||
/** 加载角色 */
|
||||
load(pos: Vec3 = Vec3.ZERO) {
|
||||
|
||||
load(pos: Vec3 = Vec3.ZERO,speed:number = 100) {
|
||||
var path = "game/monster/monster";
|
||||
|
||||
var prefab: Prefab = oops.res.get(path, Prefab)!;
|
||||
var node = instantiate(prefab);
|
||||
var scene = smc.map.MapView.scene;
|
||||
@@ -46,14 +48,22 @@ export class Monster extends ecs.Entity {
|
||||
node.setPosition(pos)
|
||||
|
||||
var mv = node.getComponent(MonsterViewComp)!;
|
||||
mv.speed = speed;
|
||||
console.log("speed:"+mv.speed)
|
||||
mv.Tpos = v3(0,0,0);
|
||||
this.add(mv);
|
||||
// node.setScale(-1, 1, 1);
|
||||
tween(node).to(1, { position: new Vec3(0, -60, 0) }, {
|
||||
onComplete: () => {
|
||||
// this.as.walk();
|
||||
}
|
||||
}).start();
|
||||
|
||||
oops.message.dispatchEvent("monster_load",this)
|
||||
}
|
||||
|
||||
/** 移动(ECS System处理逻辑,分享功能独立的业务代码) */
|
||||
move(target: Vec3,speed:number = 100) {
|
||||
var move = this.get(MoveToComp) || this.add(MoveToComp);
|
||||
move.target = target;
|
||||
move.node = this.MonsterView.node;
|
||||
move.speed = speed;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -16,12 +16,14 @@ export class MonsterModelComp extends ecs.Comp {
|
||||
|
||||
/** 角色名 */
|
||||
name: string = "monster";
|
||||
|
||||
/** speed */
|
||||
speed: number = 0;
|
||||
/** 动画名资源 */
|
||||
anim: string = "monster";
|
||||
|
||||
reset() {
|
||||
this.id = -1;
|
||||
this.speed = 0;
|
||||
this.name = "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ export default class MonsterSpineAnimator extends Component {
|
||||
private spine!: sp.Skeleton;
|
||||
start() {
|
||||
this.spine = this.getComponent(sp.Skeleton)!;
|
||||
console.log("MonsterSpineAnimator start");
|
||||
// console.log("MonsterSpineAnimator start");
|
||||
this.playAnimation(this.animName, this.loop);
|
||||
}
|
||||
|
||||
@@ -44,9 +44,9 @@ export default class MonsterSpineAnimator extends Component {
|
||||
* @param loop 是否循环播放
|
||||
*/
|
||||
protected playAnimation(animName: string, loop: boolean) {
|
||||
console.log("MonsterSpineAnimator playAnimation");
|
||||
// console.log("MonsterSpineAnimator playAnimation");
|
||||
if (animName) {
|
||||
console.log("MonsterSpineAnimator playAnimation animName", animName);
|
||||
// console.log("MonsterSpineAnimator playAnimation animName", animName);
|
||||
this.animName = animName;
|
||||
this.loop = loop;
|
||||
this.spine.setAnimation(0, this.animName, this.loop);
|
||||
|
||||
@@ -5,10 +5,12 @@
|
||||
* @LastEditTime: 2022-08-17 12:36:18
|
||||
*/
|
||||
|
||||
import { Vec3, _decorator ,tween} from "cc";
|
||||
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";
|
||||
|
||||
const { ccclass, property } = _decorator;
|
||||
|
||||
@@ -18,16 +20,53 @@ const { ccclass, property } = _decorator;
|
||||
export class MonsterViewComp extends CCComp {
|
||||
/** 角色动画 */
|
||||
as: MonsterSpine = null!;
|
||||
/** 角色控制器 */
|
||||
|
||||
speed: number = 100;
|
||||
Tpos: Vec3 = v3(0,-60,0);
|
||||
/** 视图层逻辑代码分离演示 */
|
||||
onLoad() {
|
||||
this.as = this.getComponent(MonsterSpine);
|
||||
//移动到0,-60
|
||||
|
||||
start () {
|
||||
// 注册单个碰撞体的回调函数
|
||||
console.log('MonsterViewComp 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);
|
||||
}
|
||||
|
||||
// 注册全局碰撞回调函数
|
||||
if (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');
|
||||
}
|
||||
|
||||
|
||||
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();
|
||||
|
||||
Reference in New Issue
Block a user