//話す機能
function MyBaseEvent(){
	MyBaseEvent.prototype.constructor = function(){
		MyBaseEvent.prototype.eventList = [];	//イベントデータリスト
		MyBaseEvent.prototype.eventIndex = -1;	//実行インデックス
		MyBaseEvent.prototype.waitIndex = -1;	//実行次実行待ちインデックス
		//データ構造イデックス
		var index = 0;
		MyBaseEvent.prototype.ID = index++;
		MyBaseEvent.prototype.JudgePos = index++;
		MyBaseEvent.prototype.Before = index++;
		MyBaseEvent.prototype.Message = index++;
		MyBaseEvent.prototype.ImageGo = 1;
		MyBaseEvent.prototype.Excute = index++;
		MyBaseEvent.prototype.After = index++;
		MyBaseEvent.prototype.End = index++;
		//保存データ
		MyBaseEvent.prototype.Flg = [];
		MyBaseEvent.prototype.EndFlg = [];
		MyBaseEvent.prototype.LoadFunction = null;
		MyBaseEvent.prototype.LastEventNo = null;
		MyBaseEvent.prototype.EventSkip = false;
	}
	this.constructor();
	
	//データ構造取得
	MyBaseEvent.prototype.getEventDataStruct = function(){
		var evData = [
			//イベント番号
			-1,
			//判定場所
			-1,
			//イベント実行判定&前処理
			function(){return true;},
			//イベントメッセージ
			[""],
			//イベント中処理
			function(){return true;},
			//イベント後処理
			function(){return true;},
			//イベント終了
			false,
		];
		
		return evData;
	}
	
	//イベント追加
	MyBaseEvent.prototype.addBaseEvent = function(evData){
		this.EndFlg[evData[this.ID]] = evData[this.End];
		this.eventList.push(evData);
	}
	
	//イベントNoからイベント取得
	MyBaseEvent.prototype.getBaseEvent = function(evNo){
		for(var loop in this.eventList){
			var myNo = this.eventList[loop][this.ID];
			if(evNo == myNo)return this.eventList[loop];
		}
		return false;
	}
	
	//実行可能イベント検索
	MyBaseEvent.prototype.serchExecuteEvent = function(){
		myProc.LoadFlg = false;
		for(var loop in this.eventList){
			var pos = this.eventList[loop][this.JudgePos];
			var exeFlg = this.eventList[loop][this.Before](pos);
			var endFlg = this.eventList[loop][this.End];
			if(exeFlg&&(!endFlg))return loop;
		}
		return false;
	}
	
	//イベント実行
	MyBaseEvent.prototype.excuteEvent = function(exeNo){
		//実行中なら待つ
		if(this.eventIndex!==-1){
			_self.setTimer(this.excuteEvent,100,exeNo);
			if(this.waitIndex==-1)this.waitIndex = exeNo;
			return;
		}
		//イベント実行判定
		if(this.waitIndex == exeNo)this.waitIndex = -1;
		var event = this.getBaseEvent(exeNo);
		var pos = event[this.JudgePos];
		if(!event)return;
		if(event[this.Before](pos)){//実行条件を満たしていれば
			if(myProc.talkLock){
				_self.setTimer(this.excuteEvent,100,exeNo);
				return;
			}
			this.eventIndex = event[this.ID];
			myBT.setMessage(event[this.Message]);//メッセージ設定
			event[this.Excute]();//イベント実行中処理
		}
	}
	
	//イベント終了
	MyBaseEvent.prototype.eventEnd = function(){
		this.EventSkip = false;
		if(this.eventIndex!==-1){
			var event = this.getBaseEvent(this.eventIndex);
			if(event[this.After]()){
				this.eventIndex = -1;
			}
			this.EndFlg[event[this.ID]] = event[this.End];
			this.LastEventNo = event[this.ID];
		}else{
			_self.setTimer(eventEnd,100);
		}
	}
	
	//イベントフラグ
	MyBaseEvent.prototype.setEventFlg = function(key,value){
		this.Flg[key] = value;
	}
	MyBaseEvent.prototype.getEventFlg = function(key){
		return this.Flg[key];
	}
}
myBE = new MyBaseEvent();