Files
heros/assets/script/game/monster/HeroCardViewComp.ts
2024-08-12 09:38:47 +08:00

94 lines
3.3 KiB
TypeScript

import { _decorator ,Vec2,NodeEventType,EventTouch} 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 { data } from "../data/data";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { smc } from "../common/SingletonModuleComp";
import { CardSet } from "../common/config/CardSet";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('HeroCardViewComp')
@ecs.register('HeroCardView', false)
export class HeroCardViewComp extends CCComp {
card_name:string = "hero_card";
card_type:string = "hero";
card_uid:number = 1000;
/** 方向 */
private _dir: Vec2 = new Vec2(0, 0);
public get dir(): Vec2 {
return this._dir;
}
public set dir(value: Vec2) {
this._dir = value;
}
pos_x=0;
pos_y=0;
protected onLoad(): void {
this.node.on(NodeEventType.TOUCH_START, this.onTouchMove, this);
this.node.on(NodeEventType.TOUCH_MOVE, this.onTouchMove, this);
this.node.on(NodeEventType.TOUCH_END, this.onTouchEnd, this);
this.node.on(NodeEventType.TOUCH_CANCEL, this.onTouchEnd, this);
oops.message.on("destroy hero_card", this.on_destroy_node, this);
}
private on_destroy_node(event: string, args: any) {
// if(this.ent.eid == args){
// this.reset();
// }
}
onTouchMove(event: EventTouch) {
smc.vm_data.cards.eid = this.ent.eid;
let parent = this.node.parent;
let active = parent.getChildByName("active");
active.setPosition(this.node.position.x,this.node.position.y)
active.active = true;
// if(this.ent.eid == smc.vm_data.cards.eid){
// this.node.getChildByName("active").active = true;
// }else{
// this.node.getChildByName("active").active = false;
// }
let delta = event.getDelta();
this.node.setPosition(this.node.position.x+delta.x,this.node.position.y+delta.y);
}
onTouchEnd(){
let parent = this.node.parent;
let active = parent.getChildByName("active");
active.setPosition(this.pos_x,this.pos_y)
if(this.node.position.y-this.pos_y > 110){
active.active = false;
this.use_card()
}else{
this.node.setPosition(this.pos_x,this.pos_y);
}
}
use_card(){
if(smc.vm_data.gold.min >= CardSet[this.card_uid].level){
oops.message.dispatchEvent("do_add_hero",{uuid:this.card_uid})
this.ent.destroy();
smc.vm_data.gold.min -= CardSet[this.card_uid].level;
}else{
this.node.setPosition(this.pos_x,this.pos_y);
}
}
/** 视图层逻辑代码分离演示 */
start() {
this.pos_x=this.node.position.x;
this.pos_y=this.node.position.y;
}
/** 全局消息逻辑处理 */
// private onHandler(event: string, args: any) {
// switch (event) {
// case ModuleEvent.Cmd:
// break;
// }
// }
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.node.destroy();
}
}