503 lines
18 KiB
TypeScript
503 lines
18 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} 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 } 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 { MoveToComp } from "../common/ecs/position/MoveTo";
|
||
import { BoxRangComp } from "./BoxRangComp";
|
||
import { MonViewComp } from "../mon/MonViewComp";
|
||
import { RandomManager } from "../../../../extensions/oops-plugin-framework/assets/core/common/random/RandomManager";
|
||
const { ccclass, property } = _decorator;
|
||
|
||
/** 角色显示组件 */
|
||
@ccclass('HeroViewComp') // 定义为 Cocos Creator 组件
|
||
@ecs.register('HeroView', false) // 定义为 ECS 组件
|
||
export class HeroViewComp extends CCComp {
|
||
@property(Material)
|
||
hitFlashMaterial: Material;
|
||
orginalFlashMaterial: Material;
|
||
sprite: Sprite;
|
||
|
||
@property(Node)
|
||
BoxRang:Node =null!
|
||
|
||
is_role:boolean = false;
|
||
enemy_pos:Vec3=null!;
|
||
enemy:any=null!;
|
||
/** 角色动画 */
|
||
as: HeroSpine = null!;
|
||
hero_uuid:number = 1001;
|
||
hero_name : string = "hero";
|
||
level:number =1;
|
||
scale: number = 1; /** 角色阵营 1:hero -1 :mon */
|
||
type: number = 1; /**角色类型 1:前排 2 后排 */
|
||
state: number = 1; /** 状态 1:move ,2: act 3: stop */
|
||
|
||
hp: number = 100; /** 血量 */
|
||
hp_max: number = 100; /** 最大血量 */
|
||
hp_speed: number = 0; //每秒回复量
|
||
|
||
power: number = 0; /**能量**/
|
||
power_max: number = 1200; /** 能量最大值 */
|
||
power_speed: number = 1; //能量回复速度每0.1秒回复量
|
||
|
||
skill_name: string = "base"; //技能名称
|
||
max_skill_name: string = "base"; //大技能名称
|
||
skill_uuid:number = 9001;
|
||
max_skill_uuid:number = 1001;
|
||
atk: number = 10; /**攻击力 */
|
||
buff_atk: number = 0;
|
||
// atk_speed: number = 1;
|
||
atk_cd: number = 1.3; /**攻击速度 攻击间隔 */
|
||
atk_dis: number = 80;
|
||
atk_time: number = 0; /** 冷却时间 */
|
||
|
||
speed: number = 100; /** 角色移动速度 */
|
||
ospeed: number = 100; /** 角色初始速度 */
|
||
Tpos: Vec3 = v3(0,-60,0);
|
||
stop_cd: number = 0.5; /*停止倒计时*/
|
||
|
||
shield:number = 0; //护盾量
|
||
shield_max:number = 200;
|
||
shield_time:number = 0; //护盾持续时间
|
||
|
||
box_group:number = BoxSet.HERO;
|
||
atk_range:number = 150;
|
||
private timer:Timer = new Timer(1); //计时器
|
||
private m_timer:Timer = new Timer(0.1);
|
||
is_dead:boolean = false; //是否摧毁
|
||
is_stop:boolean = false;
|
||
is_atking:boolean = false;
|
||
|
||
buff_shields:any=[];
|
||
buff_atks:any = [];
|
||
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.move()
|
||
this.sprite = this.node.getChildByName("anm").getComponent(Sprite);
|
||
// this.node.getChildByName("top").getChildByName("shield").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.atk.toString();
|
||
// this.node.getChildByName("hp_max").setScale(this.scale,1);
|
||
// this.node.getChildByName("hp_max").getComponent(Label).string=this.hp_max.toString();
|
||
this.orginalFlashMaterial = this.sprite.getRenderMaterial(0);
|
||
this.BoxRang.getComponent(BoxRangComp).box_group = this.box_group;
|
||
this.BoxRang.getComponent(BoxRangComp).atk_range = this.atk_range
|
||
// this.BoxRang.getComponent(BoxRangComp).offset_x = 300;
|
||
// console.log("monseter ",this.BoxRang);
|
||
// console.log("monseter ",this.BoxRang);
|
||
// if(this.box_group == BoxSet.MON){
|
||
// this.enemy=smc.Role.RoleView.node
|
||
// // console.log("mon enemy ",this.enemy);
|
||
// }
|
||
// 注册单个碰撞体的回调函数
|
||
let collider = this.getComponent(Collider2D);
|
||
collider.group = this.box_group;
|
||
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);
|
||
}
|
||
// this.node.getChildByName("level").getChildByName("level").getComponent(Label).string = this.level.toString();
|
||
|
||
|
||
}
|
||
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
|
||
}
|
||
this.hp_change(skill.atk);
|
||
}
|
||
}
|
||
|
||
}
|
||
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
|
||
}
|
||
onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
if(selfCollider.group == otherCollider.group&&selfCollider.tag==otherCollider.tag){
|
||
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());
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D) {
|
||
}
|
||
|
||
|
||
update(dt: number){
|
||
if (this.timer.update(dt)) {
|
||
this.power_change(this.power_speed)
|
||
}
|
||
this.check_buff_atks(dt)
|
||
this.in_destroy();
|
||
this.in_shield(dt);
|
||
this.in_stop(dt);
|
||
this.in_atk(dt);
|
||
this.in_speek(dt);
|
||
this.move(dt);
|
||
// if(this.m_timer.update(dt)){
|
||
// this.move_to()
|
||
// }
|
||
}
|
||
|
||
move(dt: number){
|
||
if(this.stop_cd > 0){
|
||
return
|
||
}
|
||
if (this.scale === 1 && this.node.position.x >= BoxSet.MONSTER_START-150) {
|
||
return;
|
||
}
|
||
if(this.enemy){
|
||
return
|
||
}
|
||
// this.set_diry()
|
||
this.node.setPosition(this.node.position.x+dt*this.speed*this.scale, this.node.position.y+dt*this.dir_y, this.node.position.z);
|
||
}
|
||
set_diry(){
|
||
this.dir_y=-(this.node.position.y-BoxSet.GAME_LINE)/20
|
||
}
|
||
move_to(){
|
||
var move = this.ent.get(MoveToComp) || this.ent.add(MoveToComp);
|
||
move.target = v3(smc.Role.RoleView.node.position.x+10,smc.Role.RoleView.node.position.y);
|
||
move.node = this.node;
|
||
move.speed = this.ospeed;
|
||
}
|
||
power_change(power: number){
|
||
this.power += power;
|
||
if(this.power >= this.power_max&&this.check_enemy_alive()){
|
||
this.as.atk()
|
||
this.to_speek(smc.skills[this.max_skill_uuid].name)
|
||
this.scheduleOnce(()=>{
|
||
this.handle_skill(this.max_skill_uuid);
|
||
},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,BoxSet.GAME_LINE)
|
||
if(this.enemy){
|
||
if(!this.enemy.isValid){
|
||
console.log("move_to",this.enemy.isValid);
|
||
return
|
||
}
|
||
t_pos = v3(this.enemy.position.x-this.node.position.x,this.enemy.position.y-(this.node.position.y+pos.y)+BoxSet.ATK_Y)
|
||
}
|
||
return {pos,t_pos}
|
||
}
|
||
get_hero_pos(hero:any){
|
||
let pos =this.skill_pos()
|
||
let t_pos:Vec3 = v3(720,BoxSet.GAME_LINE)
|
||
if(!hero.HeroView.node.isValid){
|
||
console.log("hero.heroView.node.isValid",hero.HeroView);
|
||
return
|
||
}else{
|
||
t_pos = v3(hero.HeroView.node.position.x-this.node.position.x,hero.HeroView.node.position.y-(this.node.position.y+pos.y)+BoxSet.ATK_Y)
|
||
}
|
||
return {pos,t_pos}
|
||
}
|
||
shoot(skill_uuid:number,y:number=0,x:number=0){
|
||
// console.log("mon shoot");
|
||
let skill = ecs.getEntity<Skill>(Skill);
|
||
let atk = smc.skills[skill_uuid].atk+this.atk;
|
||
let {pos,t_pos}=this.get_enemy_pos()
|
||
pos.y=pos.y + y
|
||
pos.x=pos.x + x
|
||
skill.load(pos,BoxSet.HERO,this.node,skill_uuid,atk,t_pos);
|
||
// this.tooltip(3,smc.skills[skill_uuid].name,this.skill_uuid);
|
||
}
|
||
to_add_buff(hero:any,s_uuid:number){
|
||
let skill = ecs.getEntity<Skill>(Skill);
|
||
let atk = smc.skills[s_uuid].atk+this.atk;
|
||
let {pos,t_pos}=this.get_hero_pos(hero)
|
||
skill.load(pos,BoxSet.HERO,this.node,this.max_skill_uuid,atk,t_pos);
|
||
hero.HeroView.add_hp(smc.skills[s_uuid].atk)
|
||
}
|
||
push_least_buff(skill:number){
|
||
let heros:any = ecs.query(ecs.allOf(HeroModelComp));
|
||
let least_hp:number=999999
|
||
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)
|
||
}else{
|
||
for (let i = 0; i < heros.length; i++) {
|
||
let hero = heros[i];
|
||
if(smc.skills[skill].type==91){ //血量最少单体
|
||
if(hero.HeroView.hp < least_hp){
|
||
least_hp = hero.HeroView.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){
|
||
switch (smc.skills[skill].tg) {
|
||
case 0: //自己
|
||
|
||
break;
|
||
case 1: //伙伴
|
||
this.push_least_buff(skill)
|
||
break;
|
||
case 2: //自己和伙伴
|
||
|
||
break;
|
||
case 3: //敌人
|
||
this.shoot(skill)
|
||
break;
|
||
case 4: //敌人和自己
|
||
this.shoot(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;
|
||
}
|
||
}
|
||
|
||
check_enemy_alive(){
|
||
if(this.enemy){
|
||
if(!this.enemy.isValid){
|
||
return false
|
||
}
|
||
if(Math.abs(this.enemy.position.x-this.node.position.x) < this.atk_dis) return true;
|
||
return false
|
||
}else{
|
||
return false
|
||
}
|
||
}
|
||
in_atk(dt: number) {
|
||
if(this.atk_time >= this.atk_cd){
|
||
if(this.is_atking&&this.check_enemy_alive()){
|
||
this.atk_time = 0;
|
||
// console.log("atk_cd:"+this.atk_cd);
|
||
this.as.atk();
|
||
this.scheduleOnce(()=>{
|
||
this.handle_skill(this.skill_uuid)
|
||
},0.4)
|
||
}
|
||
}else{
|
||
this.atk_time += dt;
|
||
}
|
||
}
|
||
|
||
hp_change(hp: number){
|
||
if(this.is_dead){
|
||
return;
|
||
}
|
||
let lhp=this.shield_change(hp);
|
||
if(lhp == 0){
|
||
return;
|
||
}
|
||
this.hp += lhp;
|
||
this.tooltip(1,hp.toString());
|
||
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);
|
||
}
|
||
}
|
||
heathed(){
|
||
this.node.getChildByName("heathed").active=true
|
||
}
|
||
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_atk(atk: number,time:number=0){
|
||
if(time > 0){
|
||
let buff={atk:atk,time:time}
|
||
this.buff_atks.push(buff);
|
||
this.buff_atks.forEach((element: { atk: number; }) => {
|
||
this.buff_atk += element.atk
|
||
});
|
||
}else{
|
||
this.atk += atk;
|
||
}
|
||
}
|
||
check_buff_atks(dt: number){
|
||
for(let i=0;i<this.buff_atks.length;i++){
|
||
let buff=this.buff_atks[i];
|
||
buff.time -= dt;
|
||
if(buff.time <= 0){
|
||
this.buff_atks.splice(i,1);
|
||
}else{
|
||
this.buff_atk += buff.atk
|
||
}
|
||
}
|
||
if(this.buff_atks.length <= 0){
|
||
this.buff_atk = 0
|
||
this.buff_icon_change("atk",false)
|
||
}else{
|
||
this.buff_icon_change("atk",true)
|
||
}
|
||
}
|
||
buff_icon_change(icon:string,value:boolean){
|
||
this.node.getChildByName("top").getChildByName("buff").getChildByName(icon).active=value
|
||
}
|
||
add_shield(shield: number,time:number=0){
|
||
this.shield =this.shield_max=shield
|
||
this.shield_time = time;
|
||
}
|
||
shield_change(hp: number){
|
||
let ls=this.shield - hp;
|
||
if(ls <= 0){
|
||
this.shield = 0;
|
||
return ls;
|
||
}else{
|
||
this.shield = ls;
|
||
return 0;
|
||
}
|
||
}
|
||
in_shield(dt: number){
|
||
if(this.shield_time <= 0){
|
||
return
|
||
}
|
||
if(this.shield_time > 0){
|
||
this.shield_time -= dt;
|
||
if(this.shield_time <= 0){
|
||
this.shield_time = 0;
|
||
this.shield = this.shield_max=0;
|
||
// this.node.getChildByName("top").getChildByName("shield").active=false
|
||
}
|
||
// let shield_progress= this.shield/this.shield_max;
|
||
// this.node.getChildByName("top").getChildByName("shield").getComponent(ProgressBar)!.progress = shield_progress;
|
||
}
|
||
if(this.shield <= 0){
|
||
this.shield_time=0
|
||
// this.node.getChildByName("top").getChildByName("shield").active=false
|
||
}else{
|
||
// this.node.getChildByName("top").getChildByName("shield").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_destroy(){
|
||
|
||
}
|
||
|
||
in_atked() {
|
||
// var path = "game/skills/atked";
|
||
// var prefab: Prefab = oops.res.get(path, Prefab)!;
|
||
// var node = instantiate(prefab);
|
||
// let pos = v3(0,60)
|
||
// node.setPosition(pos)
|
||
// node.parent = this.node;
|
||
this.sprite.setSharedMaterial(this.hitFlashMaterial, 0);
|
||
this.scheduleOnce(() => {
|
||
this.sprite.setSharedMaterial(this.orginalFlashMaterial, 0);
|
||
}, 0.1);
|
||
|
||
|
||
}
|
||
dead(){
|
||
var path = "game/skills/dead";
|
||
var prefab: Prefab = oops.res.get(path, Prefab)!;
|
||
var node = instantiate(prefab);
|
||
node.setPosition(this.node.position.x,this.node.position.y+30,this.node.position.z);
|
||
node.parent = this.node.parent;
|
||
}
|
||
toDestroy(){
|
||
|
||
}
|
||
reset() {
|
||
this.is_dead = false;
|
||
this.node.destroy();
|
||
}
|
||
|
||
} |