class Project { constructor(project) { this.data = project; //diagram data matching this.defaultObjectSrc = project.defaultObjectSrc; //this.backgroundSrc = project.backgroundSrc; //this.projectSeq = project.projectSeq; //this.objectUrl = project.objectUrl; this.canvas = project.canvas; //this.defaultObject = project.defaultObject; this.defaultSubObjects = new Array(); this.baseObject = null; this.action = project.action; this.object; this.isFull = false; this.isSetDefaultObject = false; this.hasSetDefaultObject = false; this.isClickBackground = false; this.isSetBackground = false; this.nowConditionPriority = 0; this._isPlaying = false; this.isIncorrect = false; this.beforeState = "none"; //project 1 this.condition1 = false; //project 2; this.animationIndex = 0; this.animationIndex2 = 0; this.goalCount = [false, false]; } initCanvas() { //if (this._isPlaying) { console.group(`fnc initCanvas()`); canvas.clear(); this.initProps(); if (this.hasSetDefaultObject) { this.setDefaultObject(); } //this.isSetDefaultObject = false; if (this.isClickBackground) { setTimeout(() => { let a = this.setBackground(this.data.backgroundSrc); }, 50); } //} console.groupEnd(); } initProps() { this.isSetDefaultObject = false; this.nowConditionPriority = 0; this.isIncorrect = false; this.animationIndex = 0; this.animationIndex2 = 0; this.condition1 = false; } initObjects() { this.initProps(); let objectArr = canvas.getObjects(); objectArr.forEach((ele) => { canvas.remove(ele); }); canvas.renderAll(); } async setDefaultObject() { if (!this.isSetDefaultObject) { //베이스 오브젝트 배치 if (this.data.defaultObject.type == "sprite") { console.log("sprite"); this.baseObject = await addSprite( this.data.defaultObject.url, this.data.defaultObject.pos, this.data.defaultObject.spriteData, "defaultObject" ); } else { this.baseObject = await addObject( this.data.defaultObject.url, this.data.defaultObject.pos, this.data.defaultObject.size, this.data.defaultObject.scale, "defaultObject" ); } } this.isSetDefaultObject = true; this.hasSetDefaultObject = true; return; } async setBackground(src) { console.log("setBackground()"); setBackground(src); switch (this.data.projectSeq) { case 2: //나비 //서브오브젝트 체크 및 배치 if (this.data.defaultHasSub) { console.log("defaultHasSub"); this.defaultSubObjects = []; for (let i = 0; i < this.data.defaultSubCnt; i++) { if (this.data.defaultSub[i].type == "object") { this.defaultSubObjects.push( await addObject( this.data.defaultSub[i].url, this.data.defaultSub[i].pos, this.data.defaultSub[i].size, this.data.defaultSub[i].scale ) ); } } } break; } if (this.baseObject) { } } async doAction(action) { let type = action.type; console.log(action); ///원래는 프로젝트 통합으로 작동되어야함 ///프로젝트별 조건부가 수시로 바뀌는 이유로 우선 프로젝트별 분기 처리 및 차후 통합. if (!this.isIncorrect) { switch (this.data.projectSeq) { case 1: switch (type) { case "sprite": addSprite(action.url, action.pos, action.spriteData, ""); if(this.animationIndex <4 && !this.condition1){ this.animationIndex++; } if(this.animationIndex2 <4 && this.condition1){ this.animationIndex2++; } this.beforeState = "sprite"; break; case "objectChange": var _animateIndex = this.animationIndex-1; if(_animateIndex > 2){ _animateIndex = 2; } if(this.beforeState == "sprite" && this.condition1 == false){ this.baseObject.setSrc(action.url[_animateIndex], function (img) { // var leftcenter= canvas.width/2 // var halfleft = img.width*img.scale /2; img.setOptions({ opacity: 1, //scaleX : 1, //scaleY : 1, originX: "center", originY: "center", // left : this.baseObject.left, top: action.pos[_animateIndex].top, width :action.size[_animateIndex].width, height :action.size[_animateIndex].height, }); canvas.renderAll(); }); this.beforeState = "objectChange"; // 개화 조건 this.condition1 = true; }else{ this.isIncorrect = true; console.log("fail."); } break; case "end" : var _animateIndex = this.animationIndex2; if(this.beforeState == "sprite" && (_animateIndex) > 0 && this.condition1) { console.log( action.url[_animateIndex-1]); this.baseObject.setSrc(action.url[_animateIndex-1], function (img) { // var leftcenter= canvas.width/2 // var halfleft = img.width*img.scale /2; img.setOptions({ opacity: 1, //scaleX : 1, //scaleY : 1, originX: "center", originY: "center", // left : this.baseObject.left, top: action.pos[_animateIndex-1].top, }); canvas.renderAll(); }); this.beforeState = "end"; }else{ this.isIncorrect = true; console.log("fail."); } break; } break; //4월 4주차 나비야 뭐하냐? case 2: switch (type) { case `animateLeft`: if (this.animationIndex < this.data.animateArray.length) { console.log( "animateLeft ::", this.data.animateArray[this.animationIndex] ); animateLeft( this.data.animateArray[this.animationIndex].pos, action.delay ); this.animationIndex++; this.beforeState = `animateLeft`; } else { //this.isIncorrect = true; } break; case `animateDown`: if ( this.beforeState == "animateLeft" && (this.animationIndex == 1 || this.animationIndex == 2) ) { animateDown(action.pos.top, action.delay); this.beforeState = `animateDown`; if (this.animationIndex == 1) //왼쪽 수분 여부 this.goalCount[0] = true; if (this.animationIndex == 2) // 오른쪽 수분 여부 this.goalCount[1] = true; } else { this.isIncorrect = true; } break; case `end`: let goalCnt = 0; for (const goal of this.goalCount) { if (goal) goalCnt++; } if (goalCnt > 0) { backgroundChange( "./img/background2_night_big.png", action.delay ); setTimeout(() => { console.log(">> ", this.goalCount[0]); if (this.goalCount[0]) { this.defaultSubObjects[0].setSrc( this.data.defaultSub[2].url ); this.defaultSubObjects[0].top += calculatePos( { left: "", top: "ratio|0.05" }, "" ).top; } if (this.goalCount[1]) { this.defaultSubObjects[1].setSrc( this.data.defaultSub[3].url ); this.defaultSubObjects[1].top += calculatePos( { left: "", top: "ratio|0.08" }, "" ).top; } canvas.renderAll(); this.goalCount = [false, false]; }, (action.delay * 3) / 4); } else { this.isIncorrect = true; } break; default: break; } break; } } } async doAction2(action) { let type = action.type; } checkCondition(condition) { let cond = true; for (let i = 0; i < condition.length; i++) { if (!this.condition[condition[i]]) { cond = false; } } return cond; } get isPlaying() { return this._isPlaying; } set isPlaying(state) { this._isPlaying = state; } } //dump // this.condition[action.seq] = true; // console.group(this.condition); // console.groupEnd(); // //각 액션별 조건 우선 순위 판별 // //console.log(`this.nowConditionPriority :: ` + this.nowConditionPriority); // let checkConditionPriority = // action.conditionPriority >= this.nowConditionPriority; // if ( // !this.isIncorrect && // (checkConditionPriority || action.condition == null) // ) { // // 선행 조건 판별 // if ( // action.condition == null || // (checkConditionPriority && this.checkCondition(action.condition)) // ) { // if (checkConditionPriority && action.condition !== null) { // console.log( // `this.condition[action.condition] == ${ // this.condition[action.condition] // }` // ); // console.log( // `increase condition ${this.nowConditionPriority} => ${action.conditionPriority}` // ); // this.nowConditionPriority = action.conditionPriority; // } else { // console.log(`keep condition ${this.nowConditionPriority} `); // } // switch (type) { // case `sprite`: // //addSprite(action.url, action.pos); // addSprite( // action.url, // action.pos, // action.spriteData, // "" // ); // break; // case `objectChange`: // //object = addSprite(action.url, action.pos); // this.baseObject.setSrc(action.url, function (img) { // // var leftcenter= canvas.width/2 // // var halfleft = img.width*img.scale /2; // img.setOptions({ // opacity: 1, // //scaleX : 1, // //scaleY : 1, // originX: "center", // originY: "center", // // left : this.baseObject.left, // top: action.pos.top, // }); // canvas.renderAll(); // }); // break; // case `img`: // addObject(action.url, action.pos, action.size); // default: // break; // } // //프로젝트별 종속 조건 처리 // // 튤립 개화시 다시 물을 줘야해서 물 컨디션 다시 초기화. // // projectSeq 프로젝트 seq, 액션 키값 조합으로 매핑. // if (this.data.projectSeq == 1 && action.seq == 1) { // this.condition[0] = false; // console.log("물 주기 조건 초기화"); // } // } else { // console.debug(`wrong block by condition sequence`); // this.isIncorrect = true; // } // } else { // console.debug(`blocked action by wrong block`); // this.isIncorrect = true; // }