204 lines
5.3 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 GRole from "../../entity/GRole";
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-28 18:50:06 +08:00
import { GRoleType } from "./GRoleType";
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-26 03:06:44 +08:00
//角色
role:GRole
2023-10-28 18:50:06 +08:00
//角色类型
type:GRoleType;
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-28 18:50:06 +08:00
//能量
energy:number = 0;
fullEnergy:number = 100;
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
get():GRoleBase<T>{
if(this.isDie) return null;
return this;
2023-10-24 19:12:25 +08:00
}
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;
}
2023-10-27 19:17:47 +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
//设置角色
bind(role:GRole){
if(this.spine)
this.spine.skeletonData = app.role.skData[role.id];
2023-10-27 02:38:08 +08:00
this.range = role.range; //设置攻击范围
2023-10-26 03:06:44 +08:00
this.role = role;
}
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-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-25 19:19:52 +08:00
//攻击
onAttack(){
//敌人扣血
2023-10-27 02:38:08 +08:00
let info = TableGRoleAttack.getConfig(this.role.id);
(new GAttack[info.attackWay]()).attack(this,info);
2023-10-28 18:50:06 +08:00
//每一次攻击 则 增加能量条
this.energy += 10;
2023-10-25 19:19:52 +08:00
}
//受击
onHit(){
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(){
console.log("onFly");
let vWorld = this.node.worldPosition;
2023-10-28 18:50:06 +08:00
let vEndWorld = this.getWorldBackLen(v2(1000,500));
2023-10-28 02:51:05 +08:00
this.JTween(vWorld)
2023-10-28 18:50:06 +08:00
.to({x:vEndWorld.x},800)
2023-10-25 19:19:52 +08:00
.onUpdate(pos => this.node.worldPosition = vWorld)
.start();
2023-10-28 02:51:05 +08:00
this.JTween(vWorld)
2023-10-28 18:50:06 +08:00
.to({y:vEndWorld.y},800)
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-25 19:19:52 +08:00
2023-10-23 18:56:01 +08:00
}