Files
heros/assets/script/game/map/LuckHomeComp.ts
2024-12-20 13:44:33 +08:00

246 lines
8.3 KiB
TypeScript

import { _decorator, instantiate, Prefab ,Node, Label, Sprite, resources, SpriteAtlas} 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 { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { smc } from "../common/SingletonModuleComp";
import { Item } from "./Item";
import { Items } from "../common/config/Items";
import { HChipComp } from "../hero/HChipComp";
import { BoxDrop } from "../common/config/RewardSet";
import { RandomManager } from "db://oops-framework/core/common/random/RandomManager";
import { SChipComp } from "../hero/SChipComp";
const { ccclass, property } = _decorator;
/** 视图层对象 */
@ccclass('LuckHomeCompComp')
@ecs.register('LuckHomeComp', false)
export class LuckHomeCompComp extends CCComp {
@property({ type: Node })
sbn: Node = null;
@property({ type: Node })
mbn: Node | null = null;
@property({ type: Node })
lbn: Node | null = null;
@property({ type: Node })
btnno: Node | null = null;
@property({ type: Node })
itmes: Node | null = null;
selected: string = "";
/** 视图层逻辑代码分离演示 */
start() {
// var entity = this.ent as ecs.Entity; // ecs.Entity 可转为当前模块的具体实体对象
// this.on(ModuleEvent.Cmd, this.onHandler, this);
this.frist();
}
frist(){
if(smc.items[1001].num > 0){this.select_box(null,"sb");}
else if(smc.items[1002].num > 0){this.select_box(null,"mb");}
else if(smc.items[1003].num > 0){this.select_box(null,"lb");}
this.check_num();
}
check_num(){
this.sbn.getChildByName("num").getComponent(Label).string = smc.items[1001].num.toString();
this.mbn.getChildByName("num").getComponent(Label).string = smc.items[1002].num.toString();
this.lbn.getChildByName("num").getComponent(Label).string = smc.items[1003].num.toString();
}
select_box(e:any, val:string){
this.sbn.setScale(1,1,1);
this.mbn.setScale(1,1,1);
this.lbn.setScale(1,1,1);
switch(val){
case "sb":
this.sbn.setScale(1.2,1.2,1);
if(smc.items[1001].num > 0){
this.btnno.active = false;
this.selected="sb"
}else{
this.btnno.active = true
}
break;
case "mb":
this.mbn.setScale(1.2,1.2,1);
if(smc.items[1002].num > 0){
this.btnno.active = false;
this.selected="mb"
}else{
this.btnno.active = true
}
break;
case "lb":
this.lbn.setScale(1.2,1.2,1);
if(smc.items[1003].num > 0){
this.btnno.active = false;
this.selected="lb"
}else{
this.btnno.active = true
}
break;
default:
this.selected="";
break;
}
}
open_sb() {
smc.items[1001].num--;
this.show_bbox()
this.check_num()
this.scheduleOnce(()=>{
this.get_reward(1)
}, 0.5)
}
open_mb() {
smc.items[1002].num--;
this.show_bbox()
this.check_num()
this.scheduleOnce(()=>{
this.get_reward(2)
}, 0.5)
}
open_lb() {
smc.items[1003].num--;
this.show_bbox()
this.check_num()
this.scheduleOnce(()=>{
this.get_reward(3)
}, 0.5)
}
get_reward(reward:number){
let list =BoxDrop[reward];
let uuid= 0
let num =1
let i=0
while (uuid==0) {
for (let x = 0; x < list.length; x++) {
let rate = Math.random()*100
if(rate < list[x].dropRate){
uuid=list[x].uuid;
num =RandomManager.instance.getRandomInt(list[x].num_max/2,list[x].num_max);
break;
}
}
i++;
}
switch(reward){
case 3:
this.show_hero_chip(uuid,num)
smc.heros[uuid].num += num
break;
case 2:
this.show_skill_chip(uuid,num)
smc.skills[uuid].num += num
break;
case 1:
this.show_item(uuid,num)
smc.items[uuid].num += num
break;
}
}
show_hero_chip(uuid:number,num:number){
let path = "game/gui/hchip";
let prefab: Prefab = oops.res.get(path, Prefab)!;
let node = instantiate(prefab);
let hc= node.getComponent(HChipComp)
hc.update_data(uuid,num)
node.parent = this.node.getChildByName("bbox");
this.scheduleOnce(()=>{
node.parent = this.node.getChildByName("items");
}, 0.5)
this.scheduleOnce(()=>{
node.destroy();
}, 10)
}
show_item(uuid:number,num:number){
var path = "game/gui/item";
var prefab: Prefab = oops.res.get(path, Prefab)!;
var node = instantiate(prefab);
node.getChildByName("num").getComponent(Label).string = num.toString();
if(num==0){
node.getChildByName("num").getComponent(Label).string ="";
}
let url = "gui/items";
let pathName= Items[uuid].path;
switch(Items[uuid].lv){
case 1:
node.getChildByName("lv1").active = true;
break;
case 2:
node.getChildByName("lv2").active = true;
break;
case 3:
node.getChildByName("lv3").active = true;
break;
case 4:
node.getChildByName("lv4").active = true;
break;
}
resources.load(url, SpriteAtlas, (err: any, atlas) => {
const sprite = node.getChildByName("icon").getComponent(Sprite);
sprite.spriteFrame = atlas.getSpriteFrame(pathName);
});
node.parent = this.node.getChildByName("bbox");
this.scheduleOnce(()=>{
node.parent = this.node.getChildByName("items");
}, 0.5)
this.scheduleOnce(()=>{
node.destroy();
}, 10)
}
show_skill_chip(uuid:number,num:number){
let path = "game/gui/schip";
let prefab: Prefab = oops.res.get(path, Prefab)!;
let node = instantiate(prefab);
let sc= node.getComponent(SChipComp)
sc.update_data(uuid,num)
node.parent = this.node.getChildByName("bbox");
this.scheduleOnce(()=>{
node.parent = this.node.getChildByName("items");
}, 0.5)
this.scheduleOnce(()=>{
node.destroy();
}, 10)
}
show_bbox() {
var parent = this.node.getChildByName("bbox")
var path = "game/gui/bbox";
var prefab: Prefab = oops.res.get(path, Prefab)!;
let box = instantiate(prefab);
box.parent = parent;
// parent.getChildByName("light").active = true;
this.scheduleOnce(()=>{
parent.getChildByName("light").active = false;
}, 0.5)
this.scheduleOnce(()=>{
box.destroy();
}, 0.7)
}
to_open(){
if(this.selected==""){return;}
switch(this.selected){
case "sb":
if(smc.items[1001].num <=0){oops.gui.toast("宝箱数量不足"); this.btnno.active = true;return;}
this.open_sb();
break;
case "mb":
if(smc.items[1002].num <=0){oops.gui.toast("宝箱数量不足");this.btnno.active = true;return;}
this.open_mb();
break;
case "lb":
if(smc.items[1003].num <=0){oops.gui.toast("宝箱数量不足");this.btnno.active = true;return;}
this.open_lb();
break;
}
}
/** 视图对象通过 ecs.Entity.remove(ModuleViewComp) 删除组件是触发组件处理自定义释放逻辑 */
reset() {
this.node.destroy();
}
}