235 lines
6.1 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-12-13 19:33:57 +08:00
import { TB } from "../../../config/data/schema";
2023-11-30 02:20:57 +08:00
import GRoleValues, { GRoleAttackType } from "../values/GRoleValues";
import GAttributeBase from "../values/attribute/GAttributeBase";
2023-12-13 19:33:57 +08:00
import { GPetAminEnum } from "../anim/GPetAnim";
2023-12-18 18:48:42 +08:00
import NOV2DSimple from "../../../../../extensions/ngame/assets/ngame/util/collide/OV/NOV2DSimple";
import NOVBase from "../../../../../extensions/ngame/assets/ngame/util/collide/OV/NOVBase";
import { BoxCollider2D } from "cc";
import { Vec3 } from "cc";
2024-01-11 02:46:36 +08:00
import GAttribute from "../values/attribute/GAttribute";
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-11-16 19:10:19 +08:00
if(this.isDie){
//如果死亡则关闭状态机
//关闭状态机
this.fsm.close();
}
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[] = [];
2023-11-17 18:29:39 +08:00
//添加攻击回调
2023-10-31 01:52:46 +08:00
addAttackCallback(fun:Function){this.attackCallbacks.push(fun)};
2023-11-30 02:20:57 +08:00
//角色数值类
2023-12-05 01:43:43 +08:00
_values:GRoleValues;
get values(){
if(!this._values) this._values = new GRoleValues();
return this._values;
}
set values(value){
this._values = value;
}
2023-11-30 02:20:57 +08:00
2023-12-18 18:48:42 +08:00
//添加避障
ov:NOVBase;
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-12-18 18:48:42 +08:00
//避障
this.ov = new NOV2DSimple(this.node.getComponent(BoxCollider2D));
2023-10-24 19:12:25 +08:00
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-12-05 01:43:43 +08:00
// //创建数值类
// this.values = new GRoleValues();
2023-11-30 02:20:57 +08:00
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-12-12 18:54:31 +08:00
public init(role:TB.TbGRole){
console.log("初始化宠物",!!this.spine,!!(app.battleRes.getRoleSpine(role.id)));
this.spine.skeletonData = app.battleRes.getRoleSpine(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)){
2023-12-18 18:48:42 +08:00
this.setWorldPosition(Object.assign(v3(),target.clone()));
2023-10-25 02:31:51 +08:00
return true;
}else{
//移动
2023-12-19 03:08:16 +08:00
this.setWorldPosition(this.node.worldPosition.clone().subtract(v3(normal.x*dt*this.moveSpeed,normal.y*dt*this.moveSpeed,0)));
2023-10-25 02:31:51 +08:00
return false;
}
2023-10-23 18:56:01 +08:00
}
2023-12-18 18:48:42 +08:00
//设置世界坐标
setWorldPosition(worldPos:Vec3){
2023-12-19 03:08:16 +08:00
// if(worldPos = this.ov.moveTo(worldPos)){
2023-12-18 18:48:42 +08:00
this.node.setWorldPosition(worldPos);
2023-12-19 03:08:16 +08:00
// }
// return worldPos;
2023-12-18 18:48:42 +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-11-30 02:20:57 +08:00
//受击 伤害类型 伤害
onHit(type:GRoleAttackType,harm:number,enemy:GRoleBase<{}>){
2023-10-30 02:34:11 +08:00
// return;
2023-11-30 02:20:57 +08:00
harm = this.values.onUnderAttack(type,harm)
this.blood -= harm;
this.hitCallbacks.forEach(fun => fun(this,harm));
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-11-30 02:20:57 +08:00
//生效数值
2024-01-11 02:46:36 +08:00
onEffectiveValues(...values:GAttributeBase[]){
2023-11-30 02:20:57 +08:00
this.values.reset();
values.forEach(value => {
this.values.add(value);
})
this.values.update();
//赋值血量
this.blood = this.fullBlood = this.values.onBlood();
}
2024-01-11 02:46:36 +08:00
//生效属性
onEffectiveValue(data:GAttribute){
this.values.reset(data);
this.values.update();
//赋值血量
this.blood = this.fullBlood = this.values.onBlood();
}
2023-10-25 19:19:52 +08:00
2023-10-23 18:56:01 +08:00
}