Files
heros/assets/script/game/hero/HeroViewComp.ts

376 lines
12 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Vec3, _decorator , v3,Collider2D,Contact2DType,Label ,Node,Prefab,instantiate,ProgressBar, Component, Material, Sprite, math, clamp, Game, tween, Color} from "cc";
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { HeroSpine } from "./HeroSpine";
import { HeroModelComp } from "./HeroModelComp";
import { BoxSet, GameSet } from "../common/config/BoxSet";
import { smc } from "../common/SingletonModuleComp";
import { Skill } from "../skills/Skill";
import { Timer } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
import { SkillSet, TargetGroup, TargetType } from "../common/config/SkillSet";
import { RandomManager } from "../../../../extensions/oops-plugin-framework/assets/core/common/random/RandomManager";
import { HeroSet } from "../common/config/heroSet";
import { BuffComp } from "./BuffComp";
import { MonModelComp } from "./MonModelComp";
import { getMonsterDrops, MonsterType } from "../common/config/RewardSet";
import { HeroSkillsComp } from "../skill/heroSkillsComp";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('HeroViewComp') // 定义为 Cocos Creator 组件
@ecs.register('HeroView', false) // 定义为 ECS 组件
export class HeroViewComp extends CCComp {
BUFFCOMP:BuffComp=null!
enemy_pos:Vec3=null!
// enemy:any=null!;
as: HeroSpine = null!
anm_timer:Timer = new Timer(0.3);
anm_name="idle"
status:String = "idle"
hero_uuid:number = 1001;
hero_name : string = "hero";
lv:number =1;
slv:number =1;
scale: number = 1; /** 角色阵营 1hero -1 :mon */
type: number = 0; /**角色类型 0近战-需要贴身 1远程-保持距离 2辅助 */
fac:number=0; //阵营 0hero 1monster
box_group:number = BoxSet.HERO;
atk_range:number = 150;
is_dead:boolean = false; //是否摧毁
is_stop:boolean = false;
is_atking:boolean = false;
is_boss:boolean = false;
is_big_boss:boolean = false;
hp: number = 100; /** 血量 */
hp_max: number = 100; /** 最大血量 */
hp_speed: number = 0; //每秒回复量
pw: number = 0; // 当前能量值
pwm: number = 100; // 最大能量值
pws: number = 1; //能量回复速度每0.1秒回复量
apw:number=0;
uapw:number=0;
cpw:number=0;
dopw:number=0;
dpw:number=0;
pwt:Timer = new Timer(1); //计时器
akr:number=0; //攻击触发机率
uar:number=0; //受伤触发机率
dgr:number=0; //闪避触发机率
crr:number=0; //暴击触发机率
akc:number=0; //攻击次数触发
uac:number=0; //受伤次数触发
dgc:number=0; //闪避次数触发
crc:number=0; //暴击次数触发
aexp:number=0; //攻击经验
uaexp:number=0; //受伤经验
cexp:number=0; //暴击经验 */
doexp:number=0; //闪避经验 */
dexp:number=0; //死亡经验 */
ap: number = 10; /**攻击力 */
ap_buff: number = 0;
ap_buffs:any = [];
// atk_speed: number = 1;
cd: number = 1.3; /**攻击速度 攻击间隔 */
dis: number = 80;
at: number = 0; /** 冷却时间 */
def: number = 0; //防御
vun: number = 0; //易伤
crit: number = 0; //暴击率
crit_add: number = 0;//暴击伤害加成
dodge: number = 10; //闪避率
shield:number = 0; //护盾,免伤1次减1
speed: number = 100; /** 角色移动速度 */
ospeed: number = 100; /** 角色初始速度 */
atk_count: number = 0;
atked_count: number = 0;
dodge_count: number = 0;
crit_count: number = 0;
stop_cd: number = 0; /*停止倒计时*/
yun_cd: number = 0; //眩晕倒计时
ice_cd: number = 0; //冰冻倒计时
dir_y:number = 0;
speek_time:number = 0;
onLoad() {
this.as = this.getComponent(HeroSpine);
} /** 视图层逻辑代码分离演示 */
start () {
this.as.idle()
this.BUFFCOMP=this.node.getComponent(BuffComp);
/** 方向 */
this.node.setScale(this.scale,1);
this.node.getChildByName("top").setScale(this.scale,1);
if(this.is_boss){
this.node.setScale(this.node.scale.x*1.5,this.node.scale.y*1.5);
this.node.getChildByName("top").getChildByName("sboss").active = true;
}
/* 显示角色血量 */
this.node.getChildByName("top").getChildByName("hp").active = true;
}
update(dt: number){
if(!smc.mission.play||smc.mission.pause) return
if(this.is_dead) {
if(!this.in_grave()) this.to_grave()
return
}
if (this.pwt.update(dt)) {
this.pw+=this.pws
}
this.hp_show()
if(this.ice_cd > 0){
this.ice_cd -=dt;
return
}
if(this.yun_cd > 0){
this.yun_cd -=dt;
return
}
this.at += dt;
this.in_stop(dt);
}
get isActive() {
return this.ent.has(HeroViewComp) && this.node?.isValid;
}
hp_show(){
let hp_progress= this.hp/this.hp_max;
this.node.getChildByName("top").getChildByName("hp").getComponent(ProgressBar)!.progress = hp_progress;
if(this.is_boss) return
this.node.getChildByName("top").getChildByName("hp").active = true;
// if(this.hp == this.hp_max){
// this.node.getChildByName("top").getChildByName("hp").active = false;
// } else{
// this.node.getChildByName("top").getChildByName("hp").active = true;
// }
}
//移动
//状态切换
status_change(type:string){
this.status=type
if(type == "idle"){
this.as.idle()
// this.as.change_default("idle")
}
if(type == "move"){
this.as.move()
// this.as.change_default("move")
}
}
exp_add(exp:number=0){
if(this.box_group==BoxSet.HERO){
smc.vmdata.mission.exp +=exp
}
if(this.box_group==BoxSet.MONSTER){
smc.vmdata.mission.mexp +=exp
}
}
power_add(p:number){
this.pw+= p
}
add_shield(shield:number){
this.shield =shield
if(this.shield>0) this.BUFFCOMP.show_shield(true)
}
// add_cd(cd: number){
// this.cd += this.cd*((100-cd)/100);
// this.BUFFCOMP.buff_get("cd")
// }
/**
* 增加英雄的攻击AP
* @param ap 要增加的攻击。
*/
add_ap(ap: number){
this.ap += Math.floor(ap);
this.BUFFCOMP.buff_get("ap")
}
de_ap(ap: number){
this.ap -= Math.floor(ap);
this.BUFFCOMP.buff_get("deap")
}
add_def(def: number){
this.def += Math.floor(def);
this.BUFFCOMP.buff_get("def")
}
de_def(def: number){
this.def -= Math.floor(def);
this.BUFFCOMP.buff_get("dedef")
}
/**
* 增加英雄的暴击率。
* @param crit 要增加的暴击率。
*/
add_crit(critRate: number) {
this.crit += Math.floor(critRate);
this.BUFFCOMP.buff_get("crit");
}
de_crit(critRate: number) {
this.crit += Math.floor(critRate);
this.BUFFCOMP.buff_get("decrit");
}
/**
* 增加英雄的闪避率。
* @param dodge 要增加的闪避率。
*/
add_dodge(dodgeRate: number) {
this.dodge += Math.floor(dodgeRate);
this.BUFFCOMP.buff_get("dodge");
}
de_dodge(dodgeRate: number) {
this.dodge += Math.floor(dodgeRate);
this.BUFFCOMP.buff_get("dedodge");
}
add_hp_max(hprate: number=0){
this.BUFFCOMP.buff_get("hp")
this.hp_max += Math.floor(hprate/100*this.hp_max) ;
this.add_hp2(hprate)
}
de_hp_max(hprate: number=0){
this.BUFFCOMP.buff_get("dehp")
this.hp_max -= Math.floor(hprate/100*this.hp_max) ;
}
add_hp(hp: number = 0) {
this.BUFFCOMP.heathed();
this.hp+=Math.floor(hp);
if(this.hp > this.hp_max){
this.hp = this.hp_max;
}
this.BUFFCOMP.tooltip(2,hp.toFixed(0));
}
add_hp2(hprate: number=0){
this.hp += Math.floor(hprate/100*this.hp_max) ;
}
/** 静止时间 */
in_stop (dt: number) {
if(this.stop_cd > 0){
this.stop_cd -= dt;
if(this.stop_cd <= 0){
this.stop_cd = 0;
this.is_atking = false;
}
}
}
dead(){
this.BUFFCOMP.dead()
this.exp_add(this.dexp)
this.power_add(this.dpw)
this.to_drop()
}
//掉落物品
to_drop(){
let Drops = getMonsterDrops(1, MonsterType.Normal, 1.2);
if(this.is_boss) Drops =getMonsterDrops(1, MonsterType.Elite, 1.2);
if(this.is_big_boss) Drops =getMonsterDrops(1, MonsterType.Boss, 1.2);
// console.log("掉落物品:",Drops);
//根据掉落类型和uuid 写入用户数据文件SingletonModuleComp =smc英雄碎片对应smc.heros[uuid].num,smc.heros[uuid].x1 记录本局掉落的数量,也需要添加对应值
//金币uuid=9001 对应smc.vmdata.gold.num,宝箱uuid1003,对应smc.vmdata.box.num smc.itmes[9001].x1,smc.itmes[1003].x1 记录本局
for (let i = 0; i < Drops.length; i++) {
const drop = Drops[i];
if(drop.type == 1 ){ //英雄碎片
smc.heros[drop.uuid].num += drop.num
smc.heros[drop.uuid].x1 += drop.num
}
if(drop.type==2){
if(drop.uuid == 9001){
smc.vmdata.gold.num+=drop.num
smc.items[9001].x1+=drop.num
}
if(drop.uuid == 1003){
smc.vmdata.box.num+=drop.num
smc.items[1003].x1+=drop.num
}
}
}
}
//进入墓地
to_grave(){
let pos =v3(-1000,this.node.position.y,this.node.position.z)
if(this.fac==1) pos.x=1000
this.node.setPosition(pos)
}
//是否在墓地
in_grave(){
return this.node.position.y <= -900;
}
to_alive(){
let pos =v3(HeroSet.StartPos[this.type],this.node.position.y,this.node.position.z)
this.node.setPosition(pos)
this.revive()
}
to_console(value:any,value2:any=null,value3:any=null){
console.log("["+this.scale+this.hero_name+']'+value,value2,value3)
}
reset() {
this.is_dead = false;
this.node.destroy();
}
playSkillEffect(skill_id:number) {
let skill = SkillSet[skill_id]
switch(skill.act){
case "max":
this.as.max()
this.BUFFCOMP.max_show(skill.fname)
this.BUFFCOMP.tooltip(3,skill.name,skill_id)
break
case "atk":
this.as.atk()
break
}
}
public revive() {
this.hp = this.hp_max;
const skills = this.ent.get(HeroSkillsComp);
skills.resetAllCooldowns();
}
/** 显示伤害数字 */
showDamage(damage: number, isCrit: boolean) {
this.BUFFCOMP.in_atked()
// this.as.atked();
this.atked_count++;
this.exp_add(this.uaexp)
this.power_add(this.uapw)
if(isCrit){
this.BUFFCOMP.tooltip(4,damage.toFixed(0),damage)
console.log("暴击伤害:"+damage)
}else{
this.BUFFCOMP.tooltip(1,damage.toFixed(0),damage)
console.log("普通伤害:"+damage)
}
}
}