125 lines
3.1 KiB
TypeScript
Raw Normal View History

2023-10-23 18:56:01 +08:00
import { _decorator, sp } from "cc";
import GObject from "../GObject";
2023-10-24 02:32:06 +08:00
import { JNFrameInfo } from "../../../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
import GFSMBase from "../fsm/GFSMBase";
2023-10-24 19:12:25 +08:00
import GFSMBattle from "../fsm/base/GFSMBattle/GFSMBattle";
import { GFSMBattleAmin } from "../fsm/base/GFSMBattle/GFSMBattleAmin";
import { Vec2 } from "cc";
import { v2 } from "cc";
2023-10-25 02:31:51 +08:00
import { v3 } from "cc";
import { GTactical } from "../../entity/GTactical";
2023-10-23 18:56:01 +08:00
const { ccclass, property } = _decorator;
2023-10-25 02:31:51 +08:00
export enum GRoleAnimEvent{
Attack = "attack", //普通攻击
}
2023-10-23 18:56:01 +08:00
//角色基类
export default abstract class GRoleBase<T> extends GObject<T>{
@property(sp.Skeleton)
spine:sp.Skeleton;
2023-10-24 02:32:06 +08:00
//状态机
2023-10-24 19:12:25 +08:00
fsm:GFSMBattle;
2023-10-24 02:32:06 +08:00
2023-10-24 19:12:25 +08:00
//动画状态机
fsmAnim:GFSMBattleAmin;
//玩家是否镜像
_isMirror:boolean = false;
//玩家攻击范围
2023-10-25 02:31:51 +08:00
range:number = 100;
//移动速度
moveSpeed:number = 80;
//在阵容中的下标
tacticalIndex:number;
//阵容
tactical:GTactical;
//阵容位置
_tacticalPos:Vec2;
get tacticalPos(){ return this._tacticalPos}
set tacticalPos(value:Vec2){ this._tacticalPos = value}
//血量
blood:number = 100;
fullBlood:number = 100;
2023-10-24 19:12:25 +08:00
get isMirror(){
return this._isMirror;
}
set isMirror(value:boolean){
if(value){
GObject.SetMirror(this);
}else{
GObject.SetMirror(this,false);
}
this._isMirror = value;
}
onSyncLoad(){
2023-10-23 18:56:01 +08:00
if(!this.spine) this.spine = this.node.getComponent(sp.Skeleton);
//如果没有生成则直接销毁
2023-10-24 02:32:06 +08:00
if(!this.spine) {
this.node.removeFromParent();
return;
}
//创建角色状态机
this.fsm = this.fsmCreate();
2023-10-24 19:12:25 +08:00
//创建角色动画状态机
this.fsmAnim = this.fsmAnimCreate();
2023-10-25 02:31:51 +08:00
//监听攻击
this.fsmAnim.addEventListener(GRoleAnimEvent.Attack,this.onAttack.bind(this));
}
2023-10-24 02:32:06 +08:00
2023-10-25 02:31:51 +08:00
//攻击
onAttack(){
//敌人扣血
this.fsm.enemy.onHit();
2023-10-23 18:56:01 +08:00
}
2023-10-25 02:31:51 +08:00
//受击
onHit(){
this.blood--;
}
2023-10-24 02:32:06 +08:00
//创建一个状态机
2023-10-24 19:12:25 +08:00
protected abstract fsmCreate():GFSMBattle;
//创建一个动画状态机
protected abstract fsmAnimCreate():GFSMBattleAmin;
2023-10-24 02:32:06 +08:00
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T){
//更新状态机
2023-10-25 02:31:51 +08:00
this.fsm && this.fsm.onUpdate(dt / 1000);
this.fsmAnim && this.fsmAnim.onUpdate(dt / 1000);
2023-10-24 19:12:25 +08:00
}
2023-10-25 02:31:51 +08:00
//向目标点移动
onMoveTarget(target:Vec2,dt:number){
//获取两个坐标差值向量
let mins = this.v2World.subtract(target);
let normal = this.v2World.subtract(target).normalize();
if(Vec2.len(normal) >= Vec2.len(mins)){
this.node.setWorldPosition(Object.assign(v3(),target.clone()));
return true;
}else{
//移动
this.node.worldPosition = this.node.worldPosition.subtract(v3(normal.x*dt*this.moveSpeed,normal.y*dt*this.moveSpeed,0))
return false;
}
2023-10-23 18:56:01 +08:00
}
}