// Learn TypeScript: // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html // Learn Attribute: // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html import Utils from "../../../framework/utils/utils"; import { ROLE_STATE } from "../../utrl/gameEnum"; import GameObject from "../gameObject"; import PlayerObject from "./playerObject"; const { ccclass, property } = cc._decorator; enum state { standy = 0, walk = 1, hit = 2, die = 3, } @ccclass export default class dog_obj extends GameObject { spine: any; followNode: any; curState: state; // LIFE-CYCLE CALLBACKS: // onLoad () {} start() { this.spine = this.node.$spine.getComponent(sp.Skeleton); Utils.loadSpineBunble(this.spine, 'dog/dog', () => { this.spine.setAnimation(0, 'standy1', true); this.curState = state.standy; this.setSkin('dog'); }); } setFollowTarget(node) { this.followNode = node; } setSkin(name) { this.spine.setSkin(name); } follow(target) { if (!target || !cc.isValid(target) || target.getComponent(PlayerObject).isDie) { if (this.node && cc.isValid(this.node)) { this.node.destroy(); } return; } this.node.scaleX = target.getComponent(PlayerObject).player_view.node.scaleX; let len = target.position.sub(this.node.position).mag(); let curState = this.curState; if (curState == state.standy && len > 80) { this.moveToTarget(target); } else if (curState == state.walk && len > 50) { this.moveToTarget(target); } else { this.curState = state.standy; if (this.spine.animation !== 'standy1') { this.node.getComponent(cc.RigidBody).linearVelocity = cc.v2(); this.spine.setAnimation(0, 'standy1', true) } } } /**向目标移动 * @param target 目标 */ private moveToTarget(target: cc.Node) { this.curState = state.walk; let v_sub = target.position.sub(this.node.position).normalize(); let rad = Math.acos(v_sub.x); if (v_sub.y < 0) { rad = Math.PI * 2 - rad; } this.setLinearVelocity(rad); if (this.spine.animation !== 'walk1') { this.spine.setAnimation(0, 'walk1', true) } } /**设置刚体线性速度 * @param rad 弧度角 */ private setLinearVelocity(rad: number) { this.node.getComponent(cc.RigidBody).linearVelocity = cc.v2(Math.cos(rad), Math.sin(rad)).mul(280); } update(dt) { this.follow(this.followNode) } }