610 lines
20 KiB
TypeScript
610 lines
20 KiB
TypeScript
/*
|
||
* @Author: dgflash
|
||
* @Date: 2021-11-18 17:42:59
|
||
* @LastEditors: dgflash
|
||
* @LastEditTime: 2022-08-17 12:36:18
|
||
*/
|
||
|
||
import { Vec3, _decorator , v3,Collider2D,Contact2DType,Label,RigidBody2D ,Node,Prefab,instantiate,ProgressBar, Component, Material, Sprite, math} 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 { Hero } from "./Hero";
|
||
import { HeroModelComp } from "./HeroModelComp";
|
||
import { BoxSet, GameSet } from "../common/config/BoxSet";
|
||
import { smc } from "../common/SingletonModuleComp";
|
||
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
|
||
import { Skill } from "../skills/Skill";
|
||
import { Timer } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
|
||
import { SkillCom } from "../skills/SkillCom";
|
||
import { SkillSet } from "../common/config/SkillSet";
|
||
import { Tooltip } from "../skills/Tooltip";
|
||
import { RandomManager } from "../../../../extensions/oops-plugin-framework/assets/core/common/random/RandomManager";
|
||
import { TimerManager } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/TimerManager";
|
||
import { HeroSet } from "../common/config/heroSet";
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/** 角色显示组件 */
|
||
@ccclass('HeroViewComp') // 定义为 Cocos Creator 组件
|
||
@ecs.register('HeroView', false) // 定义为 ECS 组件
|
||
export class HeroViewComp extends CCComp {
|
||
|
||
enemy_pos:Vec3=null!;
|
||
enemy:any=null!;
|
||
|
||
as: HeroSpine = null!;
|
||
anm_timer:Timer = new Timer(2);
|
||
anm_name="idle"
|
||
status:String = "idle"
|
||
|
||
hero_uuid:number = 1001;
|
||
hero_name : string = "hero";
|
||
hero_type:number = 1;
|
||
level:number =1;
|
||
scale: number = 1; /** 角色阵营 1:hero -1 :mon */
|
||
type: number = 1; /**角色类型 1近战 2 远程 3 辅助 */
|
||
|
||
box_group:number = BoxSet.HERO;
|
||
atk_range:number = 150;
|
||
private timer:Timer = new Timer(1); //计时器
|
||
|
||
|
||
is_dead:boolean = false; //是否摧毁
|
||
is_stop:boolean = false;
|
||
is_atking:boolean = false;
|
||
|
||
hp: number = 100; /** 血量 */
|
||
hp_max: number = 100; /** 最大血量 */
|
||
hp_speed: number = 0; //每秒回复量
|
||
|
||
power: number = 0; /**能量**/
|
||
power_max: number = 1200; /** 能量最大值 */
|
||
power_speed: number = 1; //能量回复速度每0.1秒回复量
|
||
|
||
sk1:number = 9001;
|
||
sk2:number = 1001;
|
||
sk3:number = 1001;
|
||
sk4:number = 1001;
|
||
sk5:number = 1001;
|
||
|
||
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 = 1.5; //暴击伤害比率
|
||
crit_rate: number = 0;//暴击伤害加成
|
||
dodge: number = 10; //闪避率
|
||
|
||
shield:number = 0; //护盾,免伤1次减1
|
||
speed: number = 100; /** 角色移动速度 */
|
||
ospeed: number = 100; /** 角色初始速度 */
|
||
|
||
Tpos: Vec3 = v3(0,-60,0);
|
||
stop_cd: number = 0.5; /*停止倒计时*/
|
||
|
||
dir_y:number = 0;
|
||
speek_time:number = 0;
|
||
|
||
onLoad() {
|
||
this.as = this.getComponent(HeroSpine);
|
||
// this.BoxRang = this.node.getChildByName("range_box");
|
||
// this.BoxRang = this.node.getChildByName("range_box");
|
||
|
||
} /** 视图层逻辑代码分离演示 */
|
||
|
||
start () {
|
||
this.as.idle()
|
||
/** 方向 */
|
||
this.node.setScale(this.scale,1);
|
||
this.node.getChildByName("top").setScale(this.scale,1);
|
||
/** 显示角色血量 */
|
||
this.node.getChildByName("top").getChildByName("hp").active = true;
|
||
|
||
// this.node.getChildByName("shielded").active = false;
|
||
// this.node.getChildByName("top").setScale(this.scale,1);
|
||
// this.node.getChildByName("atk").setScale(this.scale,1);
|
||
// this.node.getChildByName("atk").getComponent(Label).string = this.ap.toString();
|
||
// this.node.getChildByName("hp_max").setScale(this.scale,1);
|
||
// this.node.getChildByName("hp_max").getComponent(Label).string=this.hp_max.toString();
|
||
// this.BoxRang.getComponent(BoxRangComp).box_group = this.box_group;
|
||
// this.BoxRang.getComponent(BoxRangComp).atk_range = this.ap_range
|
||
// this.BoxRang.getComponent(BoxRangComp).offset_x = 300;
|
||
// console.log("monseter ",this.BoxRang);
|
||
// console.log("monseter ",this.BoxRang);
|
||
|
||
// 注册单个碰撞体的回调函数
|
||
|
||
let collider = this.getComponent(Collider2D);
|
||
collider.group = this.box_group;
|
||
console.log("hero collider ",this.scale,collider);
|
||
if (collider) {
|
||
collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
|
||
// collider.on(Contact2DType.END_CONTACT, this.onEndContact, this);
|
||
collider.on(Contact2DType.PRE_SOLVE, this.onPreSolve, this);
|
||
// collider.on(Contact2DType.POST_SOLVE, this.onPostSolve, this);
|
||
}
|
||
|
||
|
||
}
|
||
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
|
||
if(otherCollider.tag==BoxSet.SKILL_TAG &&selfCollider.tag!=BoxSet.SKILL_TAG){
|
||
if(selfCollider.group != otherCollider.group){
|
||
let skill = otherCollider.node.getComponent(SkillCom)!;
|
||
// console.log('onPostSolve',skill);
|
||
this.in_atked();
|
||
if(this.hp <= 0 ){
|
||
return
|
||
}
|
||
if(this.shield > 0){
|
||
this.shield -= 1
|
||
if(this.shield <= 0){
|
||
this.shield = 0
|
||
this.node.getChildByName("shielded").active = false
|
||
}
|
||
return
|
||
}
|
||
let dodge = RandomManager.instance.getRandomInt(0,100,3)
|
||
if(dodge < this.dodge){
|
||
//todo 闪避跳字
|
||
console.log("闪避了",dodge,this.dodge);
|
||
return
|
||
}
|
||
let l_hp=(skill.atk-this.def)*-1
|
||
if(l_hp >= 0){
|
||
l_hp=-1 //最低1点伤害
|
||
}
|
||
this.hp_change(l_hp);
|
||
}
|
||
}
|
||
|
||
}
|
||
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
|
||
}
|
||
onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
// if(selfCollider.group == otherCollider.group&&selfCollider.tag==otherCollider.tag){
|
||
// if(otherCollider.node.name=="role") return
|
||
// let self_x = selfCollider.node.position.x;
|
||
// let other_x = otherCollider.node.position.x;
|
||
// if(otherCollider.node.getComponent(HeroViewComp).type == this.type && self_x < other_x && Math.abs(other_x-self_x) < 15 ){
|
||
// this.stop_cd = 0.1;
|
||
// }
|
||
// // if(selfCollider.node.position.y < otherCollider.node.position.y){
|
||
// // if(selfCollider.node.getSiblingIndex() < otherCollider.node.getSiblingIndex()){
|
||
|
||
// // selfCollider.node.setSiblingIndex(otherCollider.node.getSiblingIndex()+1)
|
||
// // // console.log("onPreSolve b:"+selfCollider.node.uuid+":"+selfCollider.node.getSiblingIndex()+"/"+otherCollider.node.uuid+":"+otherCollider.node.getSiblingIndex());
|
||
// // }
|
||
|
||
// // }
|
||
// }
|
||
if(selfCollider.group != otherCollider.group&&otherCollider.tag == 0){
|
||
this.stop_cd = 0.1;
|
||
this.is_atking=true
|
||
}
|
||
}
|
||
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
}
|
||
update(dt: number){
|
||
if(!smc.vm_data.mission.play||smc.vm_data.pause){
|
||
let pos =v3(this.scale*-9999,this.node.position.y,this.node.position.z)
|
||
this.node.setPosition(pos)
|
||
return
|
||
}
|
||
if(this.is_dead){
|
||
let pos =v3(this.scale*-9999,this.node.position.y,this.node.position.z)
|
||
this.node.setPosition(pos)
|
||
return
|
||
}
|
||
if (this.timer.update(dt)) {
|
||
this.power_change(this.power_speed)
|
||
}
|
||
// if(this.anm_timer.update(dt)) {
|
||
// this.change_anm()
|
||
// }
|
||
this.check_atk_buffs(dt)
|
||
this.in_stop(dt);
|
||
this.at += dt;
|
||
this.check_enemy_alive()
|
||
this.in_atk(dt);
|
||
// this.hp_show()
|
||
this.move(dt);
|
||
|
||
}
|
||
|
||
|
||
|
||
change_anm(){
|
||
if (this.anm_name == "atk") {
|
||
this.as.atk()
|
||
this.anm_name = "move"
|
||
}
|
||
if (this.anm_name == "move") {
|
||
this.as.move()
|
||
this.anm_name = "atk"
|
||
}
|
||
if (this.anm_name == "idle") {
|
||
this.as.idle()
|
||
this.anm_name = "move"
|
||
}
|
||
}
|
||
|
||
check_enemy_alive(){
|
||
let dir = 320
|
||
this.enemy = v3(720,this.node.position.y)
|
||
if(this.box_group == BoxSet.MONSTER){
|
||
for (let i = 0; i < smc.hero_pos.length; i++) {
|
||
let ho:any = smc.hero_pos[i];
|
||
let x=Math.abs(ho.x-this.node.position.x)
|
||
if(x < dir){
|
||
dir = x
|
||
this.enemy = ho
|
||
}
|
||
}
|
||
}
|
||
if(this.box_group == BoxSet.HERO){
|
||
for (let i = 0; i < smc.enemy_pos.length; i++) {
|
||
let mon:any = smc.enemy_pos[i];
|
||
let x=Math.abs(mon.x-this.node.position.x)
|
||
if(x < dir){
|
||
dir = x
|
||
this.enemy = mon
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
if(dir < this.dis){
|
||
this.is_atking=true
|
||
this.stop_cd = 0.1
|
||
}else{
|
||
this.is_atking=false
|
||
}
|
||
}
|
||
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")
|
||
}
|
||
}
|
||
move(dt: number){
|
||
if(this.stop_cd > 0){
|
||
this.status_change("idle")
|
||
return
|
||
}
|
||
if (this.node.position.x >= 360 && this.scale==1) {
|
||
return;
|
||
}
|
||
if(this.scale===-1&&this.node.position.x <= -360){
|
||
return;
|
||
}
|
||
this.status_change("move")
|
||
|
||
|
||
// if(this.enemy){
|
||
// return
|
||
// }
|
||
this.node.setPosition(this.node.position.x+dt*this.speed*this.scale, this.node.position.y+dt*this.dir_y, this.node.position.z);
|
||
}
|
||
|
||
hp_show(){
|
||
if(this.hp == this.hp_max){
|
||
this.node.getChildByName("top").getChildByName("hp").active = false;
|
||
} else{
|
||
this.node.getChildByName("top").getChildByName("hp").active = true;
|
||
}
|
||
}
|
||
|
||
power_change(power: number){
|
||
this.power += power;
|
||
if(this.power >= this.power_max){
|
||
this.as.atk()
|
||
// this.to_speek(smc.skills[this.sk2].name)
|
||
this.scheduleOnce(()=>{
|
||
this.handle_skill(this.sk2);
|
||
},0.5)
|
||
this.power = 0
|
||
}
|
||
// let power_progress= this.power/this.power_max;
|
||
// this.node.getChildByName("top").getChildByName("power").getComponent(ProgressBar)!.progress = power_progress;
|
||
}
|
||
skill_pos(){
|
||
return v3(0,35)
|
||
}
|
||
get_enemy_pos(){
|
||
let pos =this.skill_pos()
|
||
let t_pos:Vec3 = v3(720,0)
|
||
if(this.enemy){
|
||
t_pos = v3(this.enemy.x-this.node.position.x,this.enemy.y-this.node.position.y)
|
||
}
|
||
return {pos,t_pos}
|
||
}
|
||
get_hero_pos(hero:any){
|
||
let pos =this.skill_pos()
|
||
let t_pos:Vec3 = v3(720,0)
|
||
if(!hero.node.isValid){
|
||
return
|
||
}else{
|
||
t_pos = v3(hero.node.position.x-this.node.position.x,hero.node.position.y-(this.node.position.y+pos.y))
|
||
}
|
||
|
||
return {pos,t_pos}
|
||
}
|
||
shoot_enemy(sk1:number,y:number=0,x:number=0){
|
||
// console.log("mon shoot_enemy");
|
||
let skill = ecs.getEntity<Skill>(Skill);
|
||
let increase_atk=Math.floor(this.ap*smc.vm_data.mission.hero.atk)
|
||
let atk = smc.skills[sk1].atk+this.ap_buff+increase_atk;
|
||
let {pos,t_pos}=this.get_enemy_pos()
|
||
|
||
pos.y=pos.y + y
|
||
pos.x=pos.x + x
|
||
skill.load(pos,this.box_group,this.node,sk1,atk,t_pos);
|
||
console.log(this.scale+this.hero_name+"使用技能:"+sk1);
|
||
// this.tooltip(3,smc.skills[sk1].name,this.sk1);
|
||
}
|
||
to_add_buff(hero:any,s_uuid:number){
|
||
let skill = ecs.getEntity<Skill>(Skill);
|
||
let atk = smc.skills[s_uuid].atk+this.ap;
|
||
let {pos,t_pos}=this.get_hero_pos(hero)
|
||
skill.load(pos,this.box_group,this.node,this.sk2,atk,t_pos);
|
||
if(smc.skills[s_uuid].hp > 0){ //buff加血
|
||
let increase_hp=Math.floor(smc.skills[s_uuid].hp*this.ap)
|
||
hero.add_hp(increase_hp)
|
||
}
|
||
if(smc.skills[s_uuid].atk > 0){ //buff加攻击
|
||
let increase_atk=Math.floor(smc.skills[s_uuid].atk*this.ap)
|
||
hero.add_atk(increase_atk,smc.skills[s_uuid].bsd)
|
||
}
|
||
if(smc.skills[s_uuid].shield > 0){ //buff护盾
|
||
hero.add_shield(smc.skills[s_uuid].shield)
|
||
}
|
||
}
|
||
|
||
push_least_buff(skill:number){
|
||
let heros:any = ecs.query(ecs.allOf(HeroModelComp));
|
||
let least_hp:number=0
|
||
let t_hero:any= null
|
||
if (heros.length > 0) {
|
||
if(smc.skills[skill].type==92){ //随机添加buff
|
||
|
||
let i = RandomManager.instance.getRandomInt(0,heros.length-1,3)
|
||
while(!heros[i].HeroView){
|
||
i = RandomManager.instance.getRandomInt(0,heros.length-1,3)
|
||
if(heros[i].HeroView){
|
||
break
|
||
}
|
||
}
|
||
this.to_add_buff(heros[i].HeroView,skill)
|
||
}else{
|
||
for (let i = 0; i < heros.length; i++) {
|
||
if(!heros[i].HeroView) continue
|
||
let hero = heros[i].HeroView;
|
||
if(smc.skills[skill].type==91){ //血量最少单体
|
||
if((hero.hp_max-hero.hp) > least_hp){
|
||
least_hp = (hero.hp_max-hero.hp)
|
||
t_hero = hero
|
||
}
|
||
}else{ //群体
|
||
this.to_add_buff(hero,skill)
|
||
}
|
||
|
||
}
|
||
if(t_hero){ //血量最少单体
|
||
|
||
this.to_add_buff(t_hero,skill)
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
//使用max_skill
|
||
handle_skill(skill:number){
|
||
this.node.getChildByName("max").active=true
|
||
this.scheduleOnce(()=>{
|
||
this.node.getChildByName("max").active=false
|
||
},0.8)
|
||
switch (smc.skills[skill].tg) {
|
||
case 0: //自己
|
||
this.to_add_buff(this.node.getComponent(HeroViewComp),skill)
|
||
break;
|
||
case 1: //伙伴
|
||
this.push_least_buff(skill)
|
||
break;
|
||
case 2: //自己和伙伴
|
||
this.to_add_buff(this.node.getComponent(HeroViewComp),skill)
|
||
this.push_least_buff(skill)
|
||
break;
|
||
case 3: //敌人
|
||
this.shoot_enemy(skill)
|
||
break;
|
||
case 4: //敌人和自己
|
||
this.to_add_buff(this.node.getComponent(HeroViewComp),skill)
|
||
this.shoot_enemy(skill)
|
||
break;
|
||
}
|
||
}
|
||
to_speek(words:string,time:number=0.5){
|
||
this.speek_time=0.5
|
||
this.node.getChildByName("tooltip").active=true
|
||
this.node.getChildByName("tooltip").getChildByName("words").getComponent(Label)!.string = words
|
||
}
|
||
in_speek(dt: number){
|
||
if(this.speek_time <= 0){
|
||
return;
|
||
}
|
||
this.speek_time -= dt;
|
||
if(this.speek_time <= 0){
|
||
this.speek_time = 0;
|
||
this.node.getChildByName("tooltip").getChildByName("words").getComponent(Label)!.string = "";
|
||
this.node.getChildByName("tooltip").active=false;
|
||
}
|
||
}
|
||
|
||
|
||
in_atk(dt: number) {
|
||
if(this.at >= this.cd){
|
||
if(this.is_atking){
|
||
this.at = 0;
|
||
console.log("cd:"+this.cd);
|
||
this.as.atk();
|
||
this.scheduleOnce(()=>{
|
||
this.shoot_enemy(this.sk1)
|
||
},0.4)
|
||
}
|
||
}
|
||
}
|
||
|
||
heathed(){
|
||
this.node.getChildByName("heathed").active=true
|
||
|
||
}
|
||
hp_max_add(){
|
||
console.log("hp_max_add 动画");
|
||
}
|
||
add_hp(hp: number=0){
|
||
this.heathed();
|
||
this.hp+=hp;
|
||
if(this.hp > this.hp_max){
|
||
this.hp = this.hp_max;
|
||
}
|
||
this.tooltip(2,hp.toString());
|
||
let hp_progress= this.hp/this.hp_max;
|
||
this.node.getChildByName("top").getChildByName("hp").getComponent(ProgressBar)!.progress = hp_progress;
|
||
}
|
||
add_hp_max(hp: number=0){
|
||
this.hp_max_add()
|
||
this.hp += hp;
|
||
this.hp_max += hp;
|
||
let hp_progress= this.hp/this.hp_max;
|
||
this.node.getChildByName("top").getChildByName("hp").getComponent(ProgressBar)!.progress = hp_progress;
|
||
}
|
||
hp_change(hp: number){
|
||
if(this.is_dead){
|
||
return;
|
||
}
|
||
|
||
this.hp += hp;
|
||
this.tooltip(1,hp.toString(),250);
|
||
if(this.hp > this.hp_max){
|
||
this.hp = this.hp_max;
|
||
}
|
||
let hp_progress= this.hp/this.hp_max;
|
||
this.node.getChildByName("top").getChildByName("hp").getComponent(ProgressBar)!.progress = hp_progress;
|
||
if(this.hp <= 0){
|
||
this.dead();
|
||
this.is_dead = true;
|
||
// setTimeout(() => {
|
||
// this.ent.destroy();
|
||
// }, 15);
|
||
}
|
||
}
|
||
add_atk(atk: number,time:number=0){
|
||
if(time > 0){
|
||
this.ap_buff=0
|
||
let buff={atk:atk,time:time}
|
||
this.ap_buffs.push(buff);
|
||
this.ap_buffs.forEach((element: { atk: number; }) => {
|
||
this.ap_buff += element.atk
|
||
});
|
||
}else{
|
||
this.ap += atk;
|
||
}
|
||
// console.log(this.ap_buffs)
|
||
}
|
||
|
||
check_atk_buffs(dt: number){
|
||
for(let i=0;i<this.ap_buffs.length;i++){
|
||
let buff=this.ap_buffs[i];
|
||
buff.time -= dt;
|
||
if(buff.time <= 0){
|
||
this.ap_buff -= buff.atk
|
||
this.ap_buffs.splice(i,1);
|
||
}
|
||
}
|
||
if(this.ap_buffs.length <= 0){
|
||
this.ap_buff = 0
|
||
|
||
}else{
|
||
|
||
}
|
||
}
|
||
|
||
|
||
buff_icon_change(icon:string,value:boolean){
|
||
// this.node.getChildByName("top").getChildByName("buff").getChildByName(icon).active=value
|
||
}
|
||
|
||
|
||
|
||
add_shield(shield:number){
|
||
this.shield =shield
|
||
if(this.shield > 0){
|
||
this.node.getChildByName("shielded").active=true
|
||
}
|
||
}
|
||
|
||
tooltip(type:number=1,value:string="",s_uuid:number=1001,y:number=60){
|
||
// console.log("tooltip",type);
|
||
let tip =ecs.getEntity<Tooltip>(Tooltip);
|
||
let pos = this.node.getPosition();
|
||
let node =this.node.parent
|
||
pos.y=pos.y+y;
|
||
tip.load(pos,type,value,s_uuid,node);
|
||
}
|
||
|
||
/** 静止时间 */
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
|
||
in_atked() {
|
||
var path = "game/skills/atked";
|
||
var prefab: Prefab = oops.res.get(path, Prefab)!;
|
||
var node = instantiate(prefab);
|
||
let pos =v3(0,30);
|
||
node.setPosition(pos)
|
||
node.parent = this.node;
|
||
}
|
||
dead(){
|
||
var path = "game/skills/dead";
|
||
var prefab: Prefab = oops.res.get(path, Prefab)!;
|
||
var node = instantiate(prefab);
|
||
let pos = v3(this.node.position.x,this.node.position.y+30,this.node.position.z);
|
||
node.parent = this.node.parent;
|
||
node.setPosition(pos);
|
||
}
|
||
toDestroy(){
|
||
|
||
}
|
||
toAlive(){
|
||
let pos =v3(HeroSet.StartPos[this.hero_type],this.node.position.y,this.node.position.z)
|
||
this.node.setPosition(pos)
|
||
}
|
||
reset() {
|
||
this.is_dead = false;
|
||
this.node.destroy();
|
||
}
|
||
|
||
} |