DESKTOP-5RP3AKU\Jisol 6ebed0b45e 重构继承关系
2023-11-01 02:01:35 +08:00

171 lines
4.3 KiB
TypeScript

import { _decorator, sp } from "cc";
import GObject, { GTowards } from "../GObject";
import { JNFrameInfo } from "../../../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
import { Vec2 } from "cc";
import { v3 } from "cc";
import { TableGRole } from "../../../../resources/config/ts/TableGRole";
import { TableGRoleSkill } from "../../../../resources/config/ts/TableGRoleSkill";
import { GSkill, GSkillBase, GSkillState } from "../../skill/GSkill";
import JNSkeleton from "../../../../../extensions/ngame/assets/ngame/sync/frame/game/spine/JNFrameSkeleton";
import { GFSMAnimBase } from "../fsm/GFSMAnimBase";
import GFSMBase from "../fsm/GFSMBase";
import { app } from "../../../App";
const { ccclass, property } = _decorator;
export enum GRoleAnimEvent{
Attack = "attack", //普通攻击
}
//角色基类
export default abstract class GRoleBase<T> extends GObject<T>{
@property(JNSkeleton)
spine:JNSkeleton;
//角色
role:TableGRole;
//状态机
fsm:GFSMBase;
//动画状态机
fsmAnim:GFSMAnimBase;
//玩家攻击范围
range:number = 100;
//移动速度
moveSpeed:number = 80;
//血量
blood:number = 100;
fullBlood:number = 100;
//是否死亡
_isDie:boolean = false;
get isDie(){ return this._isDie}
set isDie(value:boolean){
this._isDie = value;
}
//受击回调
hitCallbacks:Function[] = [];
//添加受击回调
addHitCallback(fun:Function){this.hitCallbacks.push(fun)};
//攻击回调
attackCallbacks:Function[] = [];
//添加受击回调
addAttackCallback(fun:Function){this.attackCallbacks.push(fun)};
get():this{
if(this.isDie) return null;
return this;
}
onSyncLoad(){
if(!this.spine) this.spine = this.node.getComponent(JNSkeleton);
//如果没有生成则直接销毁
if(!this.spine) {
this.node.destroy();
return;
}
//创建角色状态机
this.fsm = this.fsmCreate();
//创建角色动画状态机
this.fsmAnim = this.fsmAnimCreate();
}
//初始化
protected init(role:TableGRole){
if(this.spine)
this.spine.skeletonData = app.role.skData[role.id];
}
//创建一个状态机
protected abstract fsmCreate():GFSMBase;
//创建一个动画状态机
protected abstract fsmAnimCreate():GFSMAnimBase;
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T){
//更新状态机
this.fsm && this.fsm.onUpdate(dt / 1000);
this.fsmAnim && this.fsmAnim.onUpdate(dt / 1000);
}
//向目标点移动
onMoveTarget(target:Vec2,dt:number){
//获取两个坐标差值向量
let mins = this.v2World.subtract(target);
let normal = this.v2World.subtract(target).normalize();
//设置朝向
if(normal.x != 0){
if(normal.x < 0){
this.setTowards(GTowards.RIGHT)
}else{
this.setTowards(GTowards.LEFT)
}
}
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;
}
}
//朝向目标
onTowardsTarget(role:GRoleBase<{}>){
//获取两个坐标差值向量
let normal = this.v2World.subtract(role.v2World).normalize();
//设置朝向
if(normal.x != 0){
if(normal.x < 0){
this.setTowards(GTowards.RIGHT)
}else{
this.setTowards(GTowards.LEFT)
}
}
}
//受击
onHit(){
// return;
this.blood -= 10;
this.hitCallbacks.forEach(fun => fun());
//检测是否死亡
if(this.blood <= 0){
//关闭状态机
this.fsm.close();
//设置死亡
this.isDie = true;
}
}
onDebugHit(){
this.blood -= 10;
//检测是否死亡
if(this.blood <= 0){
//关闭状态机
this.fsm.close();
//设置死亡
this.isDie = true;
}
}
}