import { _decorator, Component, easing, instantiate, Label, Node, tween, UITransform, Vec3 } from 'cc';
import { PoolMgr } from './PoolMagr';

import { Global } from './Global';
import { Clips } from './Enums';
import { AudioMgr } from './AudioMgr';
import { Tools } from './Tools';
const { ccclass, property } = _decorator;

@ccclass('CoinsAction')
export class CoinsAction extends Component {
    @property({type:Label})
    level_label:Label = null;

    start() {
        this.refrush_coins();
    }

    update(deltaTime: number) {
        
    }

    refrush_coins(){
        this.level_label.string =  Global.cur_coins+"";
    }

    put_coins(num:number,world_pos:Vec3,play_sound:boolean = true){
        //记录
        Global.cur_coins = Global.cur_coins+num;
        let prefab = PoolMgr.ins.getPrefab("coin");
        
        for(let i =0;i<num;i++){
            let coin = instantiate(prefab);
            let local = this.node.getComponent(UITransform).convertToNodeSpaceAR(world_pos);
            this.node.addChild(coin);
            local.x = local.x + Tools.random_between(-30,80);
            local.y = local.y + Tools.random_between(0,150);
            coin.setPosition(local);
            let show = false;
            if(num-1 == i){
                show = true;
            }
            tween(coin)
            .to(Tools.random_between(0.5,1.5),{position:new Vec3(0,0,0)},Global.our_easing)
            .call(()=>{
                if(show){
                    this.refrush_coins();
                }
                if(Global.cur_coins>0 && play_sound){
                    AudioMgr.ins.playSound(Clips.coins);
                }
            })
            .removeSelf()
            .start();
        }
    }
        

}


