手动控制spine 动画

This commit is contained in:
2024-07-19 17:11:19 +08:00
parent f34018ac59
commit d80f16a0f7
22 changed files with 675 additions and 1221 deletions

View File

@@ -7,8 +7,6 @@
import { Color, Component, EventTouch, sp, Vec3, _decorator } from "cc";
import { LayerUtil } from "../../../../../extensions/oops-plugin-framework/assets/core/utils/LayerUtil";
import { smc } from "../../common/SingletonModuleComp";
import Charactor, { CharactorDirection, CharactorState } from "../../map/view/map/charactor/Charactor";
import { ICharactorClip } from "../../map/view/map/charactor/ICharactorClip";
import RoleSpineAnimator from "./RoleSpineAnimator";
const { ccclass, property } = _decorator;
@@ -17,55 +15,29 @@ const { ccclass, property } = _decorator;
* SPINE角色模型
*/
@ccclass('RoleSpine')
export class RoleSpine extends Component implements ICharactorClip {
export class RoleSpine extends Component {
@property({ type: RoleSpineAnimator, tooltip: '动画控制器' })
animator: RoleSpineAnimator = null!;
private spine!: sp.Skeleton;
private charactor!: Charactor;
onLoad() {
// 角色控制组件
// this.charactor = this.addComponent(Charactor)!;
this.initAnimator();
LayerUtil.setNodeLayer(LayerUtil.MAP, this.node);
this.idle();
}
/** 初始化动画 */
protected initAnimator() {
this.spine = this.animator.getComponent(sp.Skeleton)!;
this.spine.setSkin('war');
}
setPlayer(pos: Vec3) {
// var scene = smc.map.MapView.scene;
// this.node.parent = scene.entityLayer!.node!;
// this.charactor.clip = this;
// this.charactor.sceneMap = scene;
this.charactor.pos = pos;
this.charactor.updateZIndex();
}
setDirection(value: CharactorDirection): void {
if (value > 4) {
this.animator!.node.setScale(-1, 1, 1);
}
else {
this.animator!.node.setScale(1, 1, 1);
}
}
setState(value: CharactorState): void {
switch (value) {
case CharactorState.Idle:
this.idle();
break;
case CharactorState.Run:
this.walk();
break;
}
}
setSkin(value: string): void {
this.spine.setSkin(value);
}
@@ -89,10 +61,10 @@ export class RoleSpine extends Component implements ICharactorClip {
}
walk() {
this.animator!.setNumber("Speed", 1);
}
idle() {
this.animator!.setNumber("Speed", 0);
this.animator!.play("idle");
}
}

View File

@@ -4,9 +4,7 @@
* @LastEditors: dgflash
* @LastEditTime: 2022-08-04 15:26:38
*/
import { sp, _decorator } from "cc";
import AnimatorSpine from "../../../../../extensions/oops-plugin-framework/assets/libs/animator/AnimatorSpine";
import Charactor, { CharactorDirection } from "../../map/view/map/charactor/Charactor";
import { sp, _decorator ,Component} from "cc";
const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
@@ -16,24 +14,28 @@ const { ccclass, property, requireComponent, disallowMultiple } = _decorator;
@ccclass
@disallowMultiple
@requireComponent(sp.Skeleton)
export default class RoleSpineAnimator extends AnimatorSpine {
private charactor!: Charactor;
private dir: CharactorDirection = CharactorDirection.bottom;
export default class RoleSpineAnimator extends Component {
private animName: string = "idle";
private loop: boolean = true;
start() {
this.charactor = this.node.parent!.getComponent(Charactor)!;
super.start();
console.log("RoleSpineAnimator start");
this.playAnimation(this.animName, this.loop);
}
lateUpdate(dt: number) {
if (this.dir != this.charactor.direction) {
this.dir = this.charactor.direction;
this.playAnimation(this.animName, this.loop);
}
this.playAnimation(this.animName, this.loop);
}
play(animName: string, loop: boolean) {
if (animName) {
this.animName = animName;
this.loop = loop;
}
else {
}
}
/**
* 播放动画
* @override
@@ -44,29 +46,9 @@ export default class RoleSpineAnimator extends AnimatorSpine {
if (animName) {
this.animName = animName;
this.loop = loop;
// animName = `huaxian/${this.getDirection(this.charactor.direction)}${animName}`;
this._spine.setAnimation(0, animName, loop);
}
else {
this._spine.clearTrack(0);
}
}
private getDirection(dir: CharactorDirection): string {
let dirName = "";
if (dir == CharactorDirection.up) {
dirName = "back";
}
else if (dir == CharactorDirection.bottom) {
dirName = "positive";
}
else if (dir == CharactorDirection.left || dir == CharactorDirection.left_up || dir == CharactorDirection.left_bottom) {
dirName = "side";
}
else if (dir == CharactorDirection.right || dir == CharactorDirection.right_up || dir == CharactorDirection.right_bottom) {
dirName = "side";
}
return dirName;
}
}

View File

@@ -8,7 +8,6 @@
import { Vec3, _decorator } 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 Charactor from "../../map/view/map/charactor/Charactor";
import { RoleSpine } from "./RoleSpine";
const { ccclass, property } = _decorator;
@@ -20,12 +19,10 @@ export class RoleViewComp extends CCComp {
/** 角色动画 */
as: RoleSpine = null!;
/** 角色控制器 */
charactor: Charactor = null!;
/** 视图层逻辑代码分离演示 */
onLoad() {
this.as = this.getComponent(RoleSpine);
this.charactor = this.getComponent(Charactor);
}

View File

@@ -7,30 +7,18 @@
import { Component, v3, _decorator } from "cc";
import { smc } from "../../common/SingletonModuleComp";
import Charactor from "../../map/view/map/charactor/Charactor";
import MapRoadUtils from "../../map/view/map/road/MapRoadUtils";
import RoadNode from "../../map/view/map/road/RoadNode";
const { ccclass, property } = _decorator;
@ccclass('RoleViewOwn')
export class RoleViewOwn extends Component {
private charactor!: Charactor;
onLoad() {
this.charactor = this.getComponent(Charactor)!;
this.node.on(Charactor.NextRoadNode, this.onNextRoadNode, this);
}
private onNextRoadNode(rn: RoadNode) {
var key: string = rn.dx + "," + rn.dy;
var mv = smc.map.MapView;
var delivery = mv.deliverys.get(key);
if (delivery && mv.isTransfer == false) {
this.charactor.stop();
mv.transfer(delivery.toMapId, this.aStarToVec3(delivery.start));
}
}
private aStarToVec3(str: string) {
let array = str.split(",");