Files
heros/assets/script/game/monster/MonsterViewComp.ts
2024-08-16 08:45:09 +08:00

288 lines
10 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,IPhysics2DContact,EPhysics2DDrawFlags,Label,Node,Prefab,instantiate,ProgressBar} 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 { baseCom } from "../skills/baseCom";
import { Skill } from "../skills/Skill";
import { Timer } from "../../../../extensions/oops-plugin-framework/assets/core/common/timer/Timer";
import { SkillCom } from "../skills/SkillCom";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('MonsterViewComp') // 定义为 Cocos Creator 组件
@ecs.register('MonsterView', false) // 定义为 ECS 组件
export class MonsterViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
hero_uuid:number = 1001;
hero_name : string = "hero";
level:number =1;
camp: 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 = 100; /** 能量最大值 */
power_speed: number = 1; //能量回复速度每0.1秒回复量
skill_name: string = "base"; //技能名称
max_skill_name: string = "base"; //大技能名称
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; /*停止倒计时*/
private timer:Timer = new Timer(0.1); //计时器
is_dead:boolean = false; //是否摧毁
/*暴击*/
crit_rate:number = 0; //暴击率
crit_damage:number = 2; //暴击伤害倍率
/*闪避*/
dodge_rate:number = 0; //闪避率
shield:number = 0; //护盾量
shield_time:number = 0; //护盾持续时间
/** 视图层逻辑代码分离演示 */
start () {
// 注册单个碰撞体的回调函数
let collider = this.getComponent(Collider2D);
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);
}
console.log(' hero_uuid:',this.hero_uuid);
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
}
onEndContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// 只在两个碰撞体结束接触时被调用一次
// console.log('onEndContact');
}
// onPreSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
// // 每次将要处理碰撞体接触逻辑时被调用
// console.log('onPreSolve');
// }
onPostSolve (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
if(otherCollider.group== BoxSet.HERO_SKILL || otherCollider.group== BoxSet.MONSTER_SKILL){
let skill = otherCollider.node.getComponent(SkillCom)!;
// console.log('onPostSolve',skill);
this.in_atked();
this.hp_change(skill.atk);
}
// switch (selfCollider.group) {
// case BoxSet.HERO:
// switch (otherCollider.group){
// case BoxSet.MONSTER:
// break;
// case BoxSet.MONSTER_SKILL:
// this.in_atked();
// this.hp_change(10);
// break;
// }
// case BoxSet.MONSTER:
// switch (otherCollider.group){
// case BoxSet.HERO:
// // console.log('onBeginContact',selfCollider,otherCollider);
// // setTimeout(() => {
// // this.toDestroy();
// // }, 10);
// // this.speed = 0;
// // this.stop_cd = 1;
// // console.log("speed:"+this.speed+" | stop_cd:"+this.stop_cd);
// break;
// case BoxSet.HERO_SKILL:
// this.in_atked();
// this.hp_change(10);
// break;
// // case BoxSet.MONSTER_SKILL:
// // break;
// }
// break;
// }
}
onLoad() {
this.as = this.getComponent(MonsterSpine);
// console.log('hero load ent:',this);
// PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Aabb |
// EPhysics2DDrawFlags.Pair |
// EPhysics2DDrawFlags.CenterOfMass |
// EPhysics2DDrawFlags.Joint |
// EPhysics2DDrawFlags.Shape;
}
change_name(hero_name:string='hero',camp:number=1){
this.name=hero_name;
let label:any =this.node.getChildByName("lab_name")
label.getComponent(Label)!.string = hero_name;
let collider = this.getComponent(Collider2D);
if(camp==1){
collider.group=BoxSet.HERO;
}else{
collider.group=BoxSet.MONSTER;
}
}
update(dt: number){
if (this.timer.update(dt)) {
this.power_change(this.power_speed)
}
this.in_destroy();
this.in_stop(dt);
this.in_act(dt);
switch (this.type) {
case 1:
this.as.walk();
break;
case 2:
this.as.idle();
break;
}
this.move(dt);
// this.update_pos();
}
move(dt: number){
this.node.setPosition(this.node.position.x+dt*this.speed*this.camp, this.node.position.y, this.node.position.z);
if(Math.abs(this.node.position.x) >= BoxSet.END_POINT){
this.node.setPosition(v3(this.camp*-BoxSet.END_POINT,BoxSet.GAME_LINE,this.node.position.z))
// console.log("speed:"+this.speed+" | stop_cd:"+this.stop_cd);
}
}
power_change(power: number){
this.power += power;
if(this.power >= this.power_max){
this.load_skill(this.max_skill_name);
this.power = 0
}
let power_progress= this.power/this.power_max;
this.node.getChildByName("power").getComponent(ProgressBar)!.progress = power_progress;
}
in_act(dt: number) {
if(this.atk_time >= this.atk_cd){
this.atk_time = 0;
// console.log("atk_cd:"+this.atk_cd);
this.as.atk();
this.load_skill(this.skill_name);
}
this.atk_time += dt;
}
hp_change(hp: number){
if(this.is_dead){
return;
}
this.hp -= hp;
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.is_dead = true;
setTimeout(() => {
this.toDestroy();
}, 15);
}
}
/** 静止时间 */
in_stop (dt: number) {
if(this.stop_cd > 0){
this.stop_cd -= dt;
if(this.stop_cd <= 0){
this.speed = this.ospeed;
this.stop_cd = 0;
}
}
}
in_destroy(){
// switch (this.camp) {
// 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;
// }
}
update_pos(){
smc.monsters_in.forEach((element,index) => {
if(element.eid == this.ent.eid){
element.pos_x = this.node.position.x;
}
});
}
load_skill(skill_name){
// console.log("load_skill");
let skill = ecs.getEntity<Skill>(Skill);
let pos = v3(0,0)
let speed =150
let scale = this.camp
let range = 80
skill.load(pos,speed,range,scale,this.node,skill_name,this.atk);
}
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);
node.setPosition(this.node.position.x,this.node.position.y+30,this.node.position.z);
node.parent = this.node.parent;
}
toDestroy(){
this.dead();
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
smc.monsters_in = smc.monsters_in.filter(element => element.eid !== this.ent.eid);
this.ent.destroy();
}
reset() {
this.is_dead = false;
this.node.destroy();
}
}