This commit is contained in:
董刚
2022-03-09 14:30:43 +08:00
parent 32c783df19
commit 88c9fee97a
10 changed files with 148 additions and 68 deletions

View File

@@ -8,8 +8,8 @@
"__id__": 1
},
"optimizationPolicy": 0,
"asyncLoadAssets": false,
"persistent": false
"persistent": false,
"asyncLoadAssets": false
},
{
"__type__": "cc.Node",
@@ -214,7 +214,6 @@
"a": 255
},
"loop": false,
"_premultipliedAlpha": false,
"_timeScale": 1,
"_useTint": false,
"_preCacheMode": 0,
@@ -223,6 +222,7 @@
"_debugBones": false,
"_debugSlots": false,
"_skeletonData": null,
"_premultipliedAlpha": false,
"defaultSkin": "",
"defaultAnimation": "",
"_sockets": [],
@@ -234,7 +234,7 @@
"fileId": "75PJPRcM5F2ai9GB8vX0Ni"
},
{
"__type__": "100adZTIqRBJqNLcy81+N4b",
"__type__": "7bdcbRIIHlC34xmnaLT+Ef/",
"_name": "",
"_objFlags": 0,
"node": {
@@ -254,7 +254,7 @@
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "908ZWdysNJzqq4N39wqa7Q"
"fileId": "cfSE2lA5ROKpxObYgShpZb"
},
{
"__type__": "cc.PrefabInfo",

View File

@@ -2,92 +2,62 @@
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-03-08 14:57:40
* @LastEditTime: 2022-03-09 14:29:43
*/
import { assetManager, EventTouch, Node, sp, UITransform, v3, _decorator } from "cc";
import { resLoader } from "../../../core/common/loader/ResLoader";
import { sp, _decorator } from "cc";
import { ecs } from "../../../core/libs/ECS";
import { oops } from "../../../core/Oops";
import { config } from "../../common/config/Config";
import { CCComp } from "../../common/ecs/CCComp";
import { RoleModelComp } from "../model/RoleModelComp";
import { Role } from "../Role";
import { RoleEvent } from "../RoleEvent";
import { RoleViewAnimator } from "./RoleViewAnimator";
import { RoleViewAnimator } from "./component/RoleViewAnimator";
import { RoleViewController } from "./component/RoleViewController";
import { RoleViewLoader } from "./component/RoleViewLoader";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
/** 角色显示组件 - 管理业务功能方法的定义与模块之间交互处理 */
@ccclass('RoleViewComp')
@ecs.register('RoleView', false)
export class RoleViewComp extends CCComp {
@property({ type: sp.Skeleton, tooltip: '角色动画' })
spine: sp.Skeleton | null = null;
spine: sp.Skeleton = null!;
/** --- 演示显示层业务逻辑分离 --- */
/** 资源加载管理 */
loader: RoleViewLoader = null!;
/** 动画状态机 */
animator: RoleViewAnimator = null!;
private path: string = '';
/** 角色控制器 */
controller: RoleViewController = null!;
onLoad() {
this.node.active = false;
this.animator = this.spine!.getComponent(RoleViewAnimator)!;
this.animator.role = this.ent as Role;
this.on(RoleEvent.ChangeJob, this.onHandler, this);
}
/** 全局事件处理器 */
/** 全局事件处理器 - 模块之间解耦合 */
private onHandler(event: string, args: any) {
switch (event) {
case RoleEvent.ChangeJob:
this.changeJob();
// 切换职业动画 - 演示业务层通过事件控制视图层逻辑,避免两层代码直接偶合
this.animator.refresh();
break;
}
}
/** 演示业务层通过事件控制视图层逻辑,避免两层代码直接偶合 */
private changeJob() {
// 切换职业动画
this.animator.refresh();
}
load() {
var name = "model1";
this.path = config.game.getRolePath(name);
resLoader.load(this.path, sp.SkeletonData, (err: Error | null, sd: sp.SkeletonData) => {
if (err) {
console.error(`动画名为【${this.path}】的角色资源不存在`);
return;
}
this.loader = this.node.addComponent(RoleViewLoader);
this.loader.load("model1");
this.spine!.skeletonData = sd;
// this.spine!.skeletonData.addRef();
this.animator = this.spine.getComponent(RoleViewAnimator)!;
this.animator.role = this.ent as Role;
this.node.active = true;
// 移动控制
oops.gui.root.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
});
}
private onTouchEnd(event: EventTouch) {
// 注角色移动控制代码在RPG类游戏中应该设计到地图模块监听触摸事件。因为测试代码只有一个角色为了简少DEMO代码量只表达程序设计思想
var role = this.ent.get(RoleModelComp).ent as Role;
var uit = this.node.parent!.getComponent(UITransform)!;
var x = event.getUILocation().x - uit.contentSize.width / 2;
var y = event.getUILocation().y - uit.contentSize.height / 2;
role.move(v3(x, y));
if (x < role.RoleView.node.position.x)
role.RoleView.animator.left();
else
role.RoleView.animator.right();
this.controller = this.node.addComponent(RoleViewController);
this.controller.role = this.ent as Role;
}
reset() {
// this.spine!.skeletonData.decRef();
this.node.destroy();
}
}

View File

@@ -2,7 +2,7 @@
* @Author: dgflash
* @Date: 2022-01-26 16:07:58
* @LastEditors: dgflash
* @LastEditTime: 2022-01-26 16:07:58
* @LastEditTime: 2022-03-09 14:13:25
*/
/*
* @Author: dgflash

View File

@@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "b3bb384b-5a96-4e00-a5f8-29bd621bdf99",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@@ -2,17 +2,17 @@
* @Author: dgflash
* @Date: 2021-12-29 11:33:59
* @LastEditors: dgflash
* @LastEditTime: 2022-01-29 11:51:59
* @LastEditTime: 2022-03-09 14:11:48
*/
import { sp, _decorator } from "cc";
import AnimatorSpine from "../../../core/libs/animator/AnimatorSpine";
import { AnimatorStateLogic } from "../../../core/libs/animator/core/AnimatorStateLogic";
import { RoleAnimatorType, WeaponName } from "../model/RoleEnum";
import { Role } from "../Role";
import { AnimationEventHandler } from "./animator/AnimationEventHandler";
import { RoleStateAttack } from "./animator/RoleStateAttack";
import { RoleStateDead } from "./animator/RoleStateDead";
import { RoleStateHit } from "./animator/RoleStateHit";
import AnimatorSpine from "../../../../core/libs/animator/AnimatorSpine";
import { AnimatorStateLogic } from "../../../../core/libs/animator/core/AnimatorStateLogic";
import { RoleAnimatorType, WeaponName } from "../../model/RoleEnum";
import { Role } from "../../Role";
import { AnimationEventHandler } from "../animator/AnimationEventHandler";
import { RoleStateAttack } from "../animator/RoleStateAttack";
import { RoleStateDead } from "../animator/RoleStateDead";
import { RoleStateHit } from "../animator/RoleStateHit";
const { ccclass, property, requireComponent, disallowMultiple } = _decorator;

View File

@@ -2,7 +2,7 @@
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "100ad653-22a4-4126-a34b-732f35f8de1b",
"uuid": "7bdcb448-2079-42df-8c66-9da2d3f847ff",
"files": [],
"subMetas": {},
"userData": {}

View File

@@ -0,0 +1,40 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-03-09 14:19:41
*/
import { Component, EventTouch, Node, UITransform, v3, _decorator } from "cc";
import { oops } from "../../../../core/Oops";
import { Role } from "../../Role";
const { ccclass, property } = _decorator;
/** 角色资源加载 */
@ccclass('RoleViewController')
export class RoleViewController extends Component {
/** 角色对象 */
role: Role = null!;
onLoad() {
oops.gui.root.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
}
private onTouchEnd(event: EventTouch) {
// 注角色移动控制代码在RPG类游戏中应该设计到地图模块监听触摸事件。因为测试代码只有一个角色为了简少DEMO代码量只表达程序设计思想
var uit = this.node.parent!.getComponent(UITransform)!;
var x = event.getUILocation().x - uit.contentSize.width / 2;
var y = event.getUILocation().y - uit.contentSize.height / 2;
this.role.move(v3(x, y));
if (x < this.role.RoleView.node.position.x)
this.role.RoleView.animator.left();
else
this.role.RoleView.animator.right();
}
onDestroy() {
oops.gui.root.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "3bec4297-2ea2-432e-8f94-da9d967bac8f",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@@ -0,0 +1,40 @@
/*
* @Author: dgflash
* @Date: 2021-11-18 17:42:59
* @LastEditors: dgflash
* @LastEditTime: 2022-03-09 14:11:19
*/
import { Component, sp, _decorator } from "cc";
import { resLoader } from "../../../../core/common/loader/ResLoader";
import { config } from "../../../common/config/Config";
import { RoleViewComp } from "../RoleViewComp";
const { ccclass, property } = _decorator;
/** 角色资源加载 */
@ccclass('RoleViewLoader')
export class RoleViewLoader extends Component {
spine: sp.Skeleton = null!;
load(name: string) {
this.node.active = false;
this.spine = this.getComponent(RoleViewComp)!.spine!;
var path = config.game.getRolePath(name);
resLoader.load(path, sp.SkeletonData, (err: Error | null, sd: sp.SkeletonData) => {
if (err) {
console.error(`动画名为【${path}】的角色资源不存在`);
return;
}
this.spine.skeletonData = sd;
this.spine.skeletonData.addRef();
this.node.active = true;
});
}
onDestroy() {
this.spine.skeletonData.decRef();
}
}

View File

@@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "51984b80-850d-456b-8949-abdf4705f9bc",
"files": [],
"subMetas": {},
"userData": {}
}