169 lines
4.2 KiB
TypeScript
Raw Normal View History

2023-10-23 18:56:01 +08:00
import { _decorator, sp } from "cc";
2023-10-26 03:06:44 +08:00
import GObject, { GTowards } from "../GObject";
2023-10-24 02:32:06 +08:00
import { JNFrameInfo } from "../../../../../extensions/ngame/assets/ngame/sync/frame/JNSyncFrame";
2023-10-24 19:12:25 +08:00
import { Vec2 } from "cc";
2023-10-25 02:31:51 +08:00
import { v3 } from "cc";
2023-10-30 02:34:11 +08:00
import JNSkeleton from "../../../../../extensions/ngame/assets/ngame/sync/frame/game/spine/JNFrameSkeleton";
2023-11-01 02:01:35 +08:00
import { GFSMAnimBase } from "../fsm/GFSMAnimBase";
import GFSMBase from "../fsm/GFSMBase";
import { app } from "../../../App";
2023-11-06 02:25:02 +08:00
import { TB } from "../../../../resources/config/data/schema";
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>{
2023-10-30 02:34:11 +08:00
@property(JNSkeleton)
spine:JNSkeleton;
2023-10-23 18:56:01 +08:00
2023-10-26 03:06:44 +08:00
//角色
2023-11-06 02:25:02 +08:00
role:TB.TbGRole;
2023-10-28 18:50:06 +08:00
2023-10-24 02:32:06 +08:00
//状态机
2023-11-01 02:01:35 +08:00
fsm:GFSMBase;
2023-10-24 02:32:06 +08:00
2023-10-24 19:12:25 +08:00
//动画状态机
2023-11-01 02:01:35 +08:00
fsmAnim:GFSMAnimBase;
2023-10-24 19:12:25 +08:00
//玩家攻击范围
2023-10-25 02:31:51 +08:00
range:number = 100;
//移动速度
moveSpeed:number = 80;
//血量
blood:number = 100;
fullBlood:number = 100;
2023-10-24 19:12:25 +08:00
2023-10-25 19:19:52 +08:00
//是否死亡
_isDie:boolean = false;
get isDie(){ return this._isDie}
set isDie(value:boolean){
this._isDie = value;
2023-10-24 19:12:25 +08:00
}
2023-10-25 19:19:52 +08:00
2023-10-31 01:52:46 +08:00
//受击回调
hitCallbacks:Function[] = [];
//添加受击回调
addHitCallback(fun:Function){this.hitCallbacks.push(fun)};
//攻击回调
attackCallbacks:Function[] = [];
//添加受击回调
addAttackCallback(fun:Function){this.attackCallbacks.push(fun)};
2023-11-01 02:01:35 +08:00
get():this{
2023-10-25 19:19:52 +08:00
if(this.isDie) return null;
return this;
2023-10-24 19:12:25 +08:00
}
onSyncLoad(){
2023-10-30 02:34:11 +08:00
if(!this.spine) this.spine = this.node.getComponent(JNSkeleton);
2023-10-23 18:56:01 +08:00
//如果没有生成则直接销毁
2023-10-24 02:32:06 +08:00
if(!this.spine) {
2023-10-30 02:34:11 +08:00
this.node.destroy();
2023-10-24 02:32:06 +08:00
return;
}
2023-10-27 19:17:47 +08:00
2023-10-24 02:32:06 +08:00
//创建角色状态机
this.fsm = this.fsmCreate();
2023-10-24 19:12:25 +08:00
//创建角色动画状态机
this.fsmAnim = this.fsmAnimCreate();
2023-10-25 02:31:51 +08:00
}
2023-10-24 02:32:06 +08:00
2023-11-01 02:01:35 +08:00
//初始化
2023-11-06 02:25:02 +08:00
protected init(role:TB.TbGRole){
2023-10-26 03:06:44 +08:00
if(this.spine)
2023-11-06 02:25:02 +08:00
this.spine.skeletonData = app.battleRes.roleSpine[role.id];
2023-10-26 03:06:44 +08:00
}
2023-10-24 02:32:06 +08:00
//创建一个状态机
2023-11-01 02:01:35 +08:00
protected abstract fsmCreate():GFSMBase;
2023-10-24 19:12:25 +08:00
//创建一个动画状态机
2023-11-01 02:01:35 +08:00
protected abstract fsmAnimCreate():GFSMAnimBase;
2023-10-24 02:32:06 +08:00
onSyncUpdate(dt: number,frame:JNFrameInfo, input?: T){
2023-10-26 03:06:44 +08:00
2023-10-24 02:32:06 +08:00
//更新状态机
this.fsm && this.fsm.onUpdate(dt / 1000,frame);
this.fsmAnim && this.fsmAnim.onUpdate(dt / 1000,frame);
2023-10-26 03:06:44 +08:00
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();
2023-10-26 03:06:44 +08:00
//设置朝向
if(normal.x != 0){
if(normal.x < 0){
this.setTowards(GTowards.RIGHT)
}else{
this.setTowards(GTowards.LEFT)
}
}
2023-10-25 02:31:51 +08:00
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
}
2023-10-30 02:34:11 +08:00
//朝向目标
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)
}
}
}
2023-10-25 19:19:52 +08:00
//受击
onHit(){
2023-10-30 02:34:11 +08:00
// return;
this.blood -= 10;
2023-10-31 01:52:46 +08:00
this.hitCallbacks.forEach(fun => fun());
2023-10-30 02:34:11 +08:00
//检测是否死亡
if(this.blood <= 0){
//关闭状态机
this.fsm.close();
//设置死亡
this.isDie = true;
}
}
onDebugHit(){
2023-10-28 18:50:06 +08:00
this.blood -= 10;
2023-10-25 19:19:52 +08:00
//检测是否死亡
if(this.blood <= 0){
//关闭状态机
this.fsm.close();
//设置死亡
this.isDie = true;
}
}
2023-10-23 18:56:01 +08:00
}