Files
heros/assets/script/game/monster/MonsterViewComp.ts
2024-08-05 09:58:06 +08:00

194 lines
6.4 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 ,tween, v3,Collider2D,Contact2DType,PhysicsSystem2D,IPhysics2DContact,EPhysics2DDrawFlags,Label,Node} 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 { MonsterModelComp } from "./MonsterModelComp";
import { BoxSet } from "../common/config/BoxSet";
import { smc } from "../common/SingletonModuleComp";
const { ccclass, property } = _decorator;
/** 角色显示组件 */
@ccclass('MonsterViewComp') // 定义为 Cocos Creator 组件
@ecs.register('MonsterView', false) // 定义为 ECS 组件
export class MonsterViewComp extends CCComp {
/** 角色动画 */
as: MonsterSpine = null!;
hero_name : string = "hero";
/** 角色阵营 1hero -1 :monster */
camp: number = 1;
/**角色类型 1近战 2 远程 */
type: number = 1;
/** 角色移动速度 */
speed: number = 100;
/** 角色初始速度 */
ospeed: number = 100;
/**攻击速度 */
atk_speed: number = 2;
atk_cd: number = 0;
/** 状态 1move ,2: act 3: stop */
state: number = 1;
Tpos: Vec3 = v3(0,-60,0);
timer: 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);
}
}
onBeginContact (selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {
switch (selfCollider.tag) {
case BoxSet.MONSTER:
switch (otherCollider.tag){
case BoxSet.HERO:
this.state = 2;
// console.log("im monster other is hero");
// this.speed = 0;
// this.timer = 1;
// console.log("speed:"+this.speed+" | timer:"+this.timer);
break;
case BoxSet.HERO_SKILL:
break;
case BoxSet.MONSTER_SKILL:
break;
}
break;
case BoxSet.HERO:
switch (otherCollider.tag){
case BoxSet.MONSTER:
this.state = 2;
break;
case BoxSet.HERO_SKILL:
break;
}
}
}
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.tag === BoxSet.HERO && selfCollider.tag === BoxSet.MONSTER) {
// console.log('onPostSolve otherCollider.tag :'+otherCollider.tag);
// this.speed = 0;
// this.timer = 1;
}
}
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("top").getChildByName("lab_name")
label.getComponent(Label)!.string = hero_name;
let collider = this.getComponent(Collider2D);
if(camp==1){
collider.tag=BoxSet.HERO;
}else{
collider.tag=BoxSet.MONSTER;
}
}
update(dt: number){
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);
}
in_act(dt: number) {
if(this.atk_cd >= this.atk_speed){
this.atk_cd = 0;
this.as.atk();
}
if(this.state == 2){
this.atk_cd += dt;
}
}
/** 静止时间 */
in_stop (dt: number) {
if(this.timer > 0){
this.timer -= dt;
if(this.timer <= 0){
this.speed = this.ospeed;
this.timer = 0;
}
}
}
in_destroy(){
switch (this.camp) {
case -1:
if(this.node.position.x < BoxSet.LETF_END){
this.reset();
}
break;
case 1:
if(this.node.position.x > BoxSet.RIGHT_END){
this.reset();
}
break;
}
}
update_pos(){
smc.monsters_in.forEach((element,index) => {
if(element.eid == this.ent.eid){
element.pos_x = this.node.position.x;
}
});
}
reset() {
// console.log("node destroy:",this.node,this.ent)
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.node.destroy();
}
}