302 lines
8.0 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 GFSMBattle from "../fsm/base/GFSMBattle/GFSMBattle";
2023-10-25 19:19:52 +08:00
import { GFSMBattleAmin, GFSMBattleAminEnum } from "../fsm/base/GFSMBattle/GFSMBattleAmin";
2023-10-24 19:12:25 +08:00
import { Vec2 } from "cc";
2023-10-25 02:31:51 +08:00
import { v3 } from "cc";
import { GTactical } from "../../entity/GTactical";
2023-10-25 19:19:52 +08:00
import { JEasing, JTween } from "../../../../../extensions/ngame/assets/ngame/sync/frame/game/tween/JNFrameTween";
import { v2 } from "cc";
2023-10-26 03:06:44 +08:00
import { app } from "../../../App";
2023-10-27 02:38:08 +08:00
import { TableGRoleAttack } from "../../../../resources/config/ts/TableGRoleAttack";
import { GAttack, GAttackBase } from "../attack/GAttack";
2023-10-30 02:34:11 +08:00
import { TableGRole } from "../../../../resources/config/ts/TableGRole";
import { TableGRoleSkill } from "../../../../resources/config/ts/TableGRoleSkill";
2023-10-30 18:53:21 +08:00
import { GSkill, GSkillBase, GSkillState } from "../../skill/GSkill";
2023-10-30 02:34:11 +08:00
import JNSkeleton from "../../../../../extensions/ngame/assets/ngame/sync/frame/game/spine/JNFrameSkeleton";
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-10-30 02:34:11 +08:00
role:TableGRole
//角色技能
skills:GSkillBase[] = [];
2023-10-26 03:06:44 +08:00
2023-10-28 18:50:06 +08:00
//角色类型
2023-10-30 02:34:11 +08:00
type:number;
2023-10-28 18:50:06 +08:00
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;
//玩家攻击范围
2023-10-25 02:31:51 +08:00
range:number = 100;
//移动速度
moveSpeed:number = 80;
//在阵容中的下标
tacticalIndex:number;
//阵容
2023-10-26 03:06:44 +08:00
_tactical:GTactical;
get tactical(){return this._tactical};
set tactical(value:GTactical){
this.setTowards(value.towards);
this._tactical = value;
}
2023-10-25 02:31:51 +08:00
//阵容位置
_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
2023-10-25 19:19:52 +08:00
//是否死亡
_isDie:boolean = false;
get isDie(){ return this._isDie}
set isDie(value:boolean){
this._isDie = value;
//设置死亡状态
this.fsmAnim.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-10-25 19:19:52 +08:00
get():GRoleBase<T>{
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-30 02:34:11 +08:00
// this.spine.debugBones = true;
2023-10-26 03:06:44 +08:00
this.bind(this.role);
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
//监听攻击
this.fsmAnim.addEventListener(GRoleAnimEvent.Attack,this.onAttack.bind(this));
2023-10-25 19:19:52 +08:00
//监听死亡击飞
this.fsmAnim.addStartListener(GFSMBattleAminEnum.Fly,this.onFly.bind(this));
2023-10-25 02:31:51 +08:00
}
2023-10-24 02:32:06 +08:00
2023-10-26 03:06:44 +08:00
//设置角色
2023-10-30 02:34:11 +08:00
bind(role:TableGRole){
2023-10-26 03:06:44 +08:00
if(this.spine)
this.spine.skeletonData = app.role.skData[role.id];
2023-10-30 02:34:11 +08:00
this.range = role.roleAttackRange; //设置攻击范围
this.role = role; //设置角色
// 设置技能
this.skills = role.roleSkillIds.map(skillId => {
let info = TableGRoleSkill.getConfig(skillId);
return (new GSkill[info.skillController]()).bind(this,info);
})
2023-10-26 03:06:44 +08:00
}
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-26 03:06:44 +08:00
2023-10-24 02:32:06 +08:00
//更新状态机
2023-10-25 02:31:51 +08:00
this.fsm && this.fsm.onUpdate(dt / 1000);
this.fsmAnim && this.fsmAnim.onUpdate(dt / 1000);
2023-10-26 03:06:44 +08:00
2023-10-30 18:53:21 +08:00
//更新技能
this.skills.forEach(skill => skill.onUpdate(dt));
2023-10-30 02:34:11 +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
//攻击
onAttack(){
2023-10-31 01:52:46 +08:00
if(!this.fsm.enemy) return;
2023-10-25 19:19:52 +08:00
//敌人扣血
2023-10-27 02:38:08 +08:00
let info = TableGRoleAttack.getConfig(this.role.id);
(new GAttack[info.attackWay]()).attack(this,info);
2023-10-31 01:52:46 +08:00
this.attackCallbacks.forEach(fun => fun());
2023-10-30 02:34:11 +08:00
}
//释放技能 每一次只能释放一次
onSkill():boolean{
for (const item of this.skills) {
if(item.isRelease()){
//如果可以释放则释放
item.release();
return true;
}
}
return false;
2023-10-28 18:50:06 +08:00
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;
}
}
//击飞
onFly(){
2023-10-30 18:53:21 +08:00
if(this.nId == 1)
console.log("onFly");
2023-10-25 19:19:52 +08:00
let vWorld = this.node.worldPosition;
2023-10-31 02:34:12 +08:00
let vEndWorld = this.getWorldBackLen(v2(1500,500));
2023-10-28 02:51:05 +08:00
this.JTween(vWorld)
2023-10-31 02:34:12 +08:00
.to({x:vEndWorld.x},1000)
2023-10-30 18:53:21 +08:00
.onUpdate(pos => this.node.worldPosition = pos)
2023-10-25 19:19:52 +08:00
.start();
2023-10-28 02:51:05 +08:00
this.JTween(vWorld)
2023-10-31 02:34:12 +08:00
.to({y:vEndWorld.y},1000)
2023-10-25 19:19:52 +08:00
.easing(JEasing.Circular.Out)
.onUpdate(pos => this.node.worldPosition = vWorld)
.start();
}
2023-10-26 03:06:44 +08:00
//恢复阵容朝向
onRecoverTacticalTowards(){
this.setTowards(this.tactical.towards);
}
2023-10-30 02:34:11 +08:00
//过滤敌人
filterEnemy(roles:GRoleBase<{}>[] = []){
return roles.filter(role => role.type != this.type);
}
2023-10-30 18:53:21 +08:00
//判断是否可以释放技能
isReleaseSkill():boolean{
for (const skill of this.skills) {
if(skill.isRelease()){
return true;
}
}
return false;
}
//释放技能
onReleaseSkill():boolean{
for (const skill of this.skills) {
if(skill.isRelease()){
skill.release();
return true;
}
}
return false;
}
//是否正在释放技能
isReleasingSkill():boolean {
for (const skill of this.skills) {
if(skill.state() == GSkillState.Releasing){
return true;
}
}
return false;
}
2023-10-25 19:19:52 +08:00
2023-10-23 18:56:01 +08:00
}