Files
heros/assets/script/game/monster/MonsterViewComp.ts

329 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.

/*
* @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,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 { MonsterSpine } from "./MonsterSpine";
import { Monster } from "./Monster";
import { Hero } from "./Hero";
import { MonsterModelComp } from "./MonsterModelComp";
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 { BoxRangComp } from "./BoxRangComp";
import { Tooltip } from "../skills/Tooltip";
import { MonsterBuffComp } from "./MonsterBuffComp";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('MonsterViewComp') // 定义为 Cocos Creator 组件
@ecs.register('MonsterView', false) // 定义为 ECS 组件
export class MonsterViewComp extends CCComp {
@property(Material)
hitFlashMaterial: Material;
orginalFlashMaterial: Material;
sprite: Sprite;
@property(Node)
BoxRang:Node =null!
buff:MonsterBuffComp =null!
/** 角色动画 */
as: MonsterSpine = null!;
hero_uuid:number = 1001;
hero_name : string = "hero";
level:number =1;
scale: number = 1; /** 角色阵营 1hero -1 :monster */
type: number = 1; /**角色类型 1前排 2 后排 */
state: number = 1; /** 状态 1move ,2: act 3: stop */
hp: number = 100; /** 血量 */
hp_max: number = 100; /** 最大血量 */
hp_speed: number = 0; //每秒回复量
power: number = 0; /**能量**/
power_max: number = 60; /** 能量最大值 */
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; /**攻击力 */
// atk_speed: number = 1;
atk_cd: number = 1.3; /**攻击速度 攻击间隔 */
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 = 0;
shield_time:number = 0; //护盾持续时间
box_group:number = 2;
private timer:Timer = new Timer(0.1); //计时器
is_dead:boolean = false; //是否摧毁
is_stop:boolean = false;
is_atking:boolean = false;
onLoad() {
this.as = this.getComponent(MonsterSpine);
this.buff=this.node.getComponent(MonsterBuffComp);
this.BoxRang = this.node.getChildByName("range_box");
} /** 视图层逻辑代码分离演示 */
start () {
this.sprite = this.node.getChildByName("avatar").getChildByName("TNode").getChildByName("bb").getComponent(Sprite);
this.orginalFlashMaterial = this.sprite.getRenderMaterial(0);
this.BoxRang.getComponent(BoxRangComp).box_group = this.box_group;
this.BoxRang.getComponent(BoxRangComp).offset_x = this.scale*smc.skills[this.skill_uuid].dis;
// console.log("monseter ",this.BoxRang);
this.buff.group=this.box_group
// 注册单个碰撞体的回调函数
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&&otherCollider.tag == 0){
this.is_atking = true;
this.stop_cd = 0.1;
}
}
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D) {
if(selfCollider.group == otherCollider.group&&otherCollider.tag == 0&&selfCollider.tag == 0){
let self_pos=selfCollider.node.getPosition();
let other_pos=otherCollider.node.getPosition();
// console.log('monster view group 相同');
switch (selfCollider.group) {
case BoxSet.HERO:
if(self_pos.x < other_pos.x&&Math.abs(self_pos.x-other_pos.x) <= BoxSet.MOVE_RANGE_X && self_pos.y==other_pos.y){
this.stop_cd=0.1
}
break;
case BoxSet.MONSTER:
if(self_pos.x > other_pos.x&&Math.abs(self_pos.x-other_pos.x) <= BoxSet.MOVE_RANGE_X && self_pos.y==other_pos.y){
this.stop_cd=0.1
}
break
}
}
}
update(dt: number){
if (this.timer.update(dt)) {
this.power_change(this.power_speed)
}
this.in_destroy();
this.in_shield();
this.in_stop(dt);
this.in_act(dt);
this.move(dt);
}
move(dt: number){
if(this.stop_cd > 0){
return
}
if (this.scale === 1 && this.node.position.x >= 120) {
return;
}
this.node.setPosition(this.node.position.x+dt*this.speed*this.scale, this.node.position.y, this.node.position.z);
}
power_change(power: number){
this.power += power;
if(this.power >= this.power_max){
this.as.maxSkill()
this.scheduleOnce(()=>{
this.do_max_skill();
},0.5)
this.power = 0
}
let power_progress= this.power/this.power_max;
this.node.getChildByName("power").getComponent(ProgressBar)!.progress = power_progress;
}
shoot(skill_uuid:number){
// console.log("monster shoot");
let skill = ecs.getEntity<Skill>(Skill);
let x=32
let pos = v3(35*this.scale,40)
let scale = this.scale
let speed =smc.skills[skill_uuid].speed;
let dis = smc.skills[skill_uuid].dis;
let atk = smc.skills[skill_uuid].atk+this.atk;
skill.load(pos,speed,dis,scale,this.node,skill_uuid,atk);
// this.tooltip(3,smc.skills[skill_uuid].name,this.skill_uuid);
}
//使用max_skill
do_max_skill(){
let skill = ecs.getEntity<Skill>(Skill);
let x=32
let pos = v3(-40*this.scale,BoxSet.MAX_SKILL_SY)
let scale = this.scale
let speed =smc.skills[this.max_skill_uuid].speed;
let dis = smc.skills[this.max_skill_uuid].dis;
let atk = smc.skills[this.max_skill_uuid].atk+this.atk;
skill.load(pos,speed,dis,scale,this.node,this.max_skill_uuid,atk);
this.tooltip(3,smc.skills[this.max_skill_uuid].name,this.max_skill_uuid);
}
in_act(dt: number) {
if(this.atk_time >= this.atk_cd){
if(this.is_atking){
this.atk_time = 0;
// console.log("atk_cd:"+this.atk_cd);
this.as.atk();
this.scheduleOnce(()=>{
this.shoot(this.skill_uuid);
},0.2)
}
}else{
this.atk_time += dt;
}
}
in_shield(){
if(this.shield <= 0){
this.node.getChildByName("shield").active=false
}else{
this.node.getChildByName("shield").active=true
let shield_progress= this.shield/this.shield_max;
this.node.getChildByName("shield").getComponent(ProgressBar)!.progress = shield_progress;
}
}
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("hp").getComponent(ProgressBar)!.progress = hp_progress;
if(this.hp <= 0){
this.dead();
this.is_dead = true;
setTimeout(() => {
this.ent.destroy();
}, 15);
}
}
shield_change(hp: number){
let ls=this.shield - hp;
if(ls <= 0){
this.shield = 0;
return ls;
}else{
this.shield = ls;
return 0;
}
}
tooltip(type:number=1,value:string="",s_uuid:number=1001){
// console.log("tooltip",type);
let tip =ecs.getEntity<Tooltip>(Tooltip);
let pos = this.node.getPosition();
let node =this.node.parent
pos.y=pos.y+60;
tip.load(pos,type,value,s_uuid,node);
}
get_monster_pos(){
return this.node.getPosition()
}
/** 静止时间 */
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(){
// switch (this.scale) {
// case -1:
// if(this.node.position.x < BoxSet.LETF_END){
// this.toDestroy();
// }
// break;
// case 1:
// if(this.node.position.x > BoxSet.RIGHT_END){
// this.toDestroy();
// }
// break;
// }
}
in_atked() {
this.sprite.setSharedMaterial(this.hitFlashMaterial, 0);
this.scheduleOnce(() => {
this.sprite.setSharedMaterial(this.orginalFlashMaterial, 0);
}, 0.1);
// 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);
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();
}
}