123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- import { _decorator, Label,sp, UITransform, Vec2, Vec3 ,Node, isValid, tween} from 'cc';
- import { ObjectUtil } from '../../../../framework/util/ObjectUtil';
- import { BattleNodeBase } from './BattleNodeBase';
- import { HeroData } from '../data/HeroData';
- import { AudioID } from '../../../../framework/config/AudioConf';
- import { Framework } from '../../../../framework/Framework';
- import { UptypeConf } from '../conf/UptypeConf';
- import { BattleEventManager } from '../base/BattleEventManager';
- import { BattleEventHeroAction, BattleEventTarget, HeroActionType } from '../base/BattleEventUtil';
- import { BattleUtil } from '../data/BattleUtil';
- const { ccclass, property } = _decorator;
- //英雄基类
- @ccclass('Hero')
- export class Hero extends BattleNodeBase {
- @property({ type: sp.Skeleton, tooltip: '骨骼动画' })
- modelSpine: sp.Skeleton = null;
- @property({ type: Label, tooltip: '等级' })
- levelLabel: Label = null;
- @property({ type: Node, tooltip: '枪 左右' })
- gunNode1: Node = null;
- @property({ type: Node, tooltip: '枪 下' })
- gunNode2: Node = null;
- @property({ type: Node, tooltip: '枪 上' })
- gunNode3: Node = null;
-
- @property({ type: UITransform, tooltip: '点击框' })
- clickBox: UITransform = null;
- //所属格子 -1表示暂无
- private _posIndex = -1;
-
- private _bAttackAnimation: boolean = false;
- attackNode = null;
- //
- attackBone1 = null;
- attackBone2 = null;
- attackBone3 = null;
- attackBone4 = null;
- attackAngle1 = 0;
- attackAngle2 = 0;
- attackAngle3 = 0;
- attackAngle4 = 0;
- attackSlot = null;
- typeID: number = -1;
- private _raceID: number = -1;
- level: number = 1;
- radius = 1; //半径
- battleEventManager: BattleEventManager;
- private _nextAnimName: string = "idle";
- set posIndex(posIndx:number){
- this._posIndex = posIndx;
- }
- get posIndex(){
- return this._posIndex;
- }
- get raceID(){
- return this._raceID;
- }
- protected onLoad(): void {
- this.modelSpine.setStartListener(this.actionStart.bind(this))
- this.modelSpine.setCompleteListener(this.actionComplete.bind(this))
- this.modelSpine.setEventListener(this.actionCallback.bind(this))
- this.battleEventManager = BattleEventManager.instance
- let bone1 = this.modelSpine.findBone("gun1")
- if(bone1){
- this.attackBone1= bone1
- let rot = this.attackBone1.rotation
- this.attackAngle1 = rot
- console.log("rot",rot)
- }
- let bone2 = this.modelSpine.findBone("gun2")
- if(bone2){
- this.attackBone2 = bone2
- let rot = this.attackBone2.rotation
- this.attackAngle2 = rot
- console.log("rot",rot)
- }
- let bone3 = this.modelSpine.findBone("gun3")
- if(bone3){
- this.attackBone3 = bone3
- let rot = this.attackBone3.rotation
- this.attackAngle3 = rot
- console.log("rot",rot)
- }
- let bone4 = this.modelSpine.findBone("gun1")
- if(bone4){
- this.attackBone4 = bone4
- let rot = this.attackBone4.rotation
- this.attackAngle4 = rot
- console.log("rot",rot)
- }
- // this.clickBox.setContentSize(this.modelSpine.getComponent(UITransform).contentSize)
- }
- start() {
-
- }
- resetData(heroData:HeroData) {
- this.typeID = heroData.typeID;
- this._raceID = heroData.raceID;
- this.level = heroData.level;
- this.radius = heroData.attackRadius;
- this.levelLabel.string = heroData.level.toString();
- this.node.active = true;
- this.modelSpine.node.active = true;
- this.modelSpine.timeScale = 1;
- this.stand()
- // let id: string = heroData.heroID;
- }
- clearData() {
- this.node.active = false;
- this.modelSpine.node.active = false;
- this._posIndex = -1;
- this.typeID = -1;
- this.level = 0;
- this._isLock = false;
- }
- update(deltaTime: number) {
- super.update(deltaTime);
- if(this._bAttackAnimation && isValid(this.attackNode)){
- let pos = this.attackNode.position;
- let rotationAngleDegrees = this.getRotationAngle(this.node,pos)
- this.attackBone1.rotation = 0
- this.attackBone2.rotation = 0
- this.attackBone3.rotation = 0
- this.attackBone4.rotation = 0
- let curAnimName = this.modelSpine.animation
- if(rotationAngleDegrees<67.5 || rotationAngleDegrees > 292.5){
- let rotationAngle = this.getRotationAngle(this.gunNode1,pos)
- this.attackBone1.rotation = (rotationAngle+this.attackAngle1)
- this._nextAnimName = "atk_right"
- }
- else if(rotationAngleDegrees < 112.5){
- let rotationAngle = this.getRotationAngle(this.gunNode3,pos)
- this.attackBone3.rotation = rotationAngle+this.attackAngle3-90
- this._nextAnimName = "atk_up"
- }
- else if(rotationAngleDegrees < 247.5){
- let rotationAngle = this.getRotationAngle(this.gunNode1,pos)
- this.attackBone4.rotation = 180-rotationAngle+this.attackAngle4
- this._nextAnimName = "atk_left"
- }
- else if(rotationAngleDegrees < 292.5){
- let rotationAngle = this.getRotationAngle(this.gunNode2,pos)
- this.attackBone2.rotation = rotationAngle+this.attackAngle2-270
- this._nextAnimName = "atk_down"
- }
- if(!curAnimName.includes("atk_") && this._nextAnimName.includes("atk_")){
- this.modelSpine.setAnimation(0,this._nextAnimName, true);
- let conf = UptypeConf.data
- let speedScale = conf.HeroSpeedColArr[this.level]
- this.modelSpine.timeScale = speedScale;
- }
- }
-
- }
- //屏幕坐标
- bTouch(pos: Vec2){
- return this.node.getComponent(UITransform).hitTest(pos,0);
- }
- get bAttackAnimation(): boolean{
- return this._bAttackAnimation;
- }
- //通过目标坐标 来确定朝向
- attack(attackNode:Node){
- if(!this.modelSpine.node.active) return;
-
-
- this.attackNode = attackNode;
-
- // if(this._bAttackAnimation) return;
- this._bAttackAnimation = true;
- }
- stand() {
- if(!this.modelSpine.node.active) return;
- this._bAttackAnimation = false;
- // this.gunNode.angle = 0;
- // this.gunNode.active = true;
- this.attackBone1.rotation = 0
- this.attackBone2.rotation = 0
- this.attackBone3.rotation = 0
- this.attackBone4.rotation = 0
- // this._attackCallback = null;
- let animationName = this.modelSpine.animation
- if(animationName.includes("idle")) return;
- switch(animationName){
- case "atk_right":
- this._nextAnimName = "atk_right";
- this.modelSpine.setAnimation(0, 'idle_right', true);
- break;
- case "atk_left":
- this._nextAnimName = "atk_left";
- this.modelSpine.setAnimation(0, 'idle_left', true);
- break;
- case "atk_up":
- this._nextAnimName = "atk_up";
- this.modelSpine.setAnimation(0, 'idle_up', true);
- break;
- case "atk_down":
- this._nextAnimName = "atk_down";
- this.modelSpine.setAnimation(0, 'idle_down', true);
- break;
- default:
- this._nextAnimName = "idk_right";
- this.modelSpine.setAnimation(0, 'idle_right', true);
- break;
- }
- this.modelSpine.timeScale = 1;
- // this.modelSpine.setAnimation(0, 'idle_up', true);
- // if(this.modelSpine.node.scale.x < 0)
- // this.modelSpine.node.scale = new Vec3(-this.modelSpine.node.scale.x,this.modelSpine.node.scale.y,this.modelSpine.node.scale.z);
- }
- levelUp() {
- this.level++;
- this.levelLabel.string = this.level.toString();
- }
- actionCallback(trackEntry: sp.spine.TrackEntry,ev: sp.spine.Event){
- console.log("动作回调:",trackEntry.animation.name,ev)
- if (trackEntry.animation.name && trackEntry.animation.name.includes("atk")) {
- // if (this._attackCallback) {
- // this._attackCallback(trackEntry);
- // }
- // console.log("攻击动作完成:",trackEntry.animation.name)
- if(this.posIndex > BattleUtil.PosID_Init){
- let eventData:BattleEventHeroAction = {
- action: HeroActionType.Normal,
- posIndex: this._posIndex - BattleUtil.BagListSize
- }
- this.battleEventManager.fireEvent(BattleEventTarget.HeroAction,eventData)
- }
- }
- }
- actionStart(trackEntry:sp.spine.TrackEntry){
- // console.log("动作完成:",trackEntry.animation.name,this._posID)
- if (trackEntry.animation.name && trackEntry.animation.name.includes("atk_")) {
- Framework.audio.playEffect(AudioID.Tututu);
-
- }
- }
- actionComplete(trackEntry:sp.spine.TrackEntry){
- // console.log("动作完成:",trackEntry.animation.name,this._posID)
- if (trackEntry.animation.name && trackEntry.animation.name.includes("atk_")) {
- if( this._nextAnimName.includes("atk_")){
- this.modelSpine.setAnimation(0,this._nextAnimName, true);
- let conf = UptypeConf.data
- let speedScale = conf.HeroSpeedColArr[this.level]
- this.modelSpine.timeScale = speedScale;
- }
- // this._bAttackAnimation = false;
- // // this.gunNode.angle = 0;
- // // this.gunNode.active = false;
-
- // this.attackBone.rotation = 0
- // this._attackCallback = null;
- }
- }
- //
- flyTo(pos:Vec3, callback:Function){
- if(this._isLock){ return false; }
- this.isLock = true;
- let length = Vec3.distance(this.node.position,pos);
- if(length > 300){
- length = 300;
- }
- tween(this.node).to(length/500,{position:pos}).call(()=>{
- this.isLock = false;
- if(callback) callback();
- }).start();
-
- }
- hitTest(pos:Vec2){
- return this.clickBox.hitTest(pos) || this.node.getComponent(UITransform).hitTest(pos);
- }
- //根据目标位置获取旋转角度
- getRotationAngle(srcNode:Node,targetPos:Vec2){
- // 获取子节点的世界坐标
- let worldPosition = this.node.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
- // 将子节点的世界坐标转换为父节点的局部坐标
- let localPosition = this.node.parent.getComponent(UITransform).convertToNodeSpaceAR(worldPosition);
- // 计算从p0到p1的向量
- let deltaX: number = targetPos.x - localPosition.x;
- let deltaY: number = targetPos.y - localPosition.y;
- // 计算旋转角度(弧度)
- let rotationAngleRadians: number = Math.atan2(deltaY, deltaX);
- // 将旋转角度转换为度数
- let rotationAngleDegrees: number = (rotationAngleRadians * (180 / Math.PI)+360)%360;
- return rotationAngleDegrees;
- }
-
- }
|