123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- // 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)
- }
- }
|