
import { _decorator, Component, Node, EditBox,Sprite,SpriteAtlas,director, Label} from 'cc';
import { setting } from '../config/setting';
import { LoadConfig } from '../loadConfig';
const { ccclass, property } = _decorator;
 
@ccclass('creatoruser')
export class creatoruser extends Component {
   

	private str='勇敢的骚年哦\n欢迎来到新世界'


    private xing1:any;
    private name1:any;
    private num:number=0;

    @property({type:Node})
    	private img: Node|null = null;
    @property({type:SpriteAtlas})
        private res_head:SpriteAtlas|null=null;

	private dataflag=false;
	private strflag=false;
	private strtime=0;

    onLoad(){
        this.node.getChildByName('image').active=false;
		this.node.getChildByName('name').active=false;
		this.node.getChildByName('Button').active=false;
        setting.Data.game['Sex']='男'
    }
    start () {
        this.dataflag=true;
		this.strflag=true;
    }

	update(dt:any){
		if(this.dataflag){
			this.dataflag=false; 
			LoadConfig.loadData("Dict",(data:any)=>{
				this.xing1=data['xing1']
				this.name1=data['name1']
			});
		}

		if(this.strflag){
			this.strtime+=dt;
			if(this.strtime>=0.1){
				if(this.node.getChildByName("Label").getComponent(Label).string.length != this.str.length){
					var s=this.node.getChildByName("Label").getComponent(Label).string;
					this.node.getChildByName("Label").getComponent(Label).string=this.str.slice(0,s.length+1);
					this.strtime=0;
				}else{
					this.strflag=false;
					this.node.getChildByName('image').active=true;
					this.node.getChildByName('name').active=true;
					this.node.getChildByName('Button').active=true;
				}
			}
		}
	}

    image(event: Event, val: string){
        this.num=this.num+parseInt(val);
        if(this.num < 1){
            this.num=10
        }else if(this.num > 10){
            this.num=1
        }
        var n:number;
        if(this.num<=5){
            n=100+this.num;
            this.img.getComponent(Sprite).spriteFrame=this.res_head.getSpriteFrame('res_head/head'+n)
            setting.Data.game['Sex']='男'
        }else{
            n=115+this.num;
            this.img.getComponent(Sprite).spriteFrame=this.res_head.getSpriteFrame('res_head/head'+n)
            setting.Data.game['Sex']='女'
        }
        //console.log(this.num)
        //console.log(n)
    }

  
    randomName (){
		var minNum = 0;
        var maxNum = this.xing1.length-1;
        var value = Math.random() * (maxNum - minNum + 1) + minNum + "";
        var value_1 = parseInt(value);

        maxNum = this.name1.length-1;
        value = Math.random() * (maxNum - minNum + 1) + minNum + "";
        var value_2 = parseInt(value);

        var s=this.xing1[value_1]+this.name1[value_2];
        this.node.getChildByName("name").getComponent(EditBox).string=s;

    }

    creator(){
        setting.Data.game['Name']=this.node.getChildByName("name").getChildByName('NAME').getComponent(Label).string
		if(setting.Data.game['Name'].length == 0){
			this.node.getChildByName("name").getComponent(EditBox).placeholder='名称不能为空'
			return;
		}else{

            setting.Data.game['Lv']=1;
            setting.Data.game['Money']=50;
            setting.Data.game['Power']=0.5;
            setting.Data.game['Purity']=0;
            setting.Data.game['Develop']=0;
            setting.Data.game['Identity']=0;
            setting.Data.game['Vigor']=1;
            setting.Data.game['Breath']=1;
            setting.Data.game['Spirit']=1;
            setting.Data.game['Renown']={"Good":0, "Evil":0};

		    LoadConfig.dataWrite('user',setting.Data.game)
            director.loadScene('scene2');
        }
    }

    show(){
        this.node.active=true;
    }
    
}
