home *** CD-ROM | disk | FTP | other *** search
/ Orthodox Religion Class (2nd Semester) / ReligieCls1Sem2.iso / js / games / GamesComplex.js < prev    next >
Encoding:
JavaScript  |  2015-03-16  |  5.9 KB  |  205 lines

  1. var mainGame;
  2. var dates;
  3. var config;
  4. var noLevelImages; 
  5. var gameName;
  6. var result;
  7. var isActive;
  8. var isRepeatActive;
  9. var currentLevel;
  10. var column;
  11. var scaleImages;
  12. var util = new Util();
  13. var index;
  14. var columns;
  15. var game;
  16. var background;
  17. var gamesComplex = [];
  18. var task;
  19.  
  20. function GamesComplex(json) {
  21.     $("#bodyGame").empty();
  22.     isActive = true;
  23.     isRepeatActive = true;
  24.     dates = (json.dates === undefined)?new GameDates():json.dates;
  25.     gameName = (json.gameDates === undefined)?"GamesComplex":json.gameDates;
  26.     background = (json.background === undefined)?"":json.background;
  27.     task = (json.task === undefined)?"":json.task;
  28. //    console.log(background);
  29.     config = dates.dates[gameName];
  30.     sarcina = (json.sarcina === undefined)?"":json.sarcina;
  31. //    noLevelImages = config.objects.length;
  32. //    columns = (dates.dates[gameName].columns ===
  33. //    undefined)?5:dates.dates[gameName].columns;
  34. //    result = (json.result === undefined)?{}:json.result;
  35.     mainGame = new MainGame(json.title);
  36.     gamesComplex = [];
  37.     game = new GamesComplexMain();
  38.     game.initialize();
  39.     $("#gameDialog").dialog("open");
  40.     if(sarcina.length != 0)
  41.         playSound(sarcina);
  42.     $("#stage").off();
  43.     index = -1;
  44. };
  45.  
  46. function GamesComplexMain(){
  47.  
  48. }
  49. GamesComplexMain.prototype.checkResults = function() {
  50.     player.pause();
  51.     isRepeatActive = false;
  52.     isActive = false;
  53.     wrongAnswerCount = 0;
  54.     index = -1;
  55.     var indexGame = 0;
  56.     var config = gamesComplex[indexGame].config;
  57.     var checkLength = gamesComplex[indexGame].getCheckLength();
  58.     var isWrong = false;
  59.     
  60.     index = -1;
  61.     var check = function() {
  62.         if (++index < checkLength) {
  63.             if( !gamesComplex[indexGame].checkSingleResultIndex(index) ){
  64.                 wrongAnswerCount++;
  65.                 isWrong = true;
  66.             }
  67.             check();
  68.         }else{
  69.             if (index == checkLength) {
  70.                 util.storageResults(wrongAnswerCount, gamesComplex[indexGame].getResult());
  71.                 if(++indexGame<gamesComplex.length){
  72.                     index = -1;
  73.                     config = gamesComplex[indexGame].config;
  74.                     checkLength = gamesComplex[indexGame].getCheckLength();
  75.                     wrongAnswerCount = 0;
  76.                     check();
  77.                 }
  78.             }
  79.         }
  80.     };
  81.     check();
  82.     index = -1;
  83.     indexGame = 0;
  84.     wrongAnswerCount = 0;
  85.     config = gamesComplex[indexGame].config;
  86.     checkLength = gamesComplex[indexGame].getCheckLength();
  87.      
  88.     check = function() {
  89.         if (++index < checkLength) {
  90.             setTimeout(check, 1500);
  91.             if( gamesComplex[indexGame].checkSingleResultIndex(index) ){
  92.                 playSound(soundsCommon.checkRight);
  93.                 
  94.                 $("#"+config.nameGame+"checkImage" +(index)).attr("src",imagesCommon.ok).css("visibility","");
  95.             }else{
  96.                 playSound(soundsCommon.checkWrong);
  97.                 $("#"+config.nameGame+"checkImage" +(index)).attr("src",imagesCommon.wrong).css("visibility","");
  98.                 wrongAnswerCount++;
  99.             }
  100.         }
  101.         if (index == checkLength) {
  102.             
  103.             if(++indexGame<gamesComplex.length){
  104.                 index = -1;
  105.                 config = gamesComplex[indexGame].config;
  106.                 checkLength = gamesComplex[indexGame].getCheckLength();
  107.                 wrongAnswerCount = 0;
  108.                 check();
  109.             }else{
  110.                 if(isWrong){
  111.                     playSound(soundsCommon["wrongAnswer"+util.getSoundWrong(wrongAnswerCount,checkLength)]);
  112.                     player.addEventListener('ended', game.wrongAnswerComplete, false);
  113.                 }else{
  114.                     playSound(soundsCommon.rightAnswer);
  115.                     isRepeatActive = true;
  116.                 }
  117.             }
  118.         }
  119.     };
  120.     check();
  121. };
  122.  
  123. GamesComplexMain.prototype.checkResult = function(event) {
  124.     var isFinish = true;
  125.     var count = 0;
  126.     $.each(gamesComplex, function(key, game) {
  127.         count++;
  128.         if(game.checkIfFinish()==true){
  129.             isFinish = false;
  130.         }
  131.     });    
  132.     if(!isFinish){
  133.         playSound(soundsCommon.notFinish);
  134.     }else{
  135.         game.checkResults();
  136.     }
  137. };
  138.  
  139. GamesComplexMain.prototype.wrongAnswerComplete = function(e) { 
  140.     player.removeEventListener('ended', game.wrongAnswerComplete, false);
  141.     playSound(soundsCommon.correctAnswer);
  142.     player.addEventListener('ended', game.correctAnswerComplete, false);
  143. };
  144.  
  145. GamesComplexMain.prototype.correctAnswerComplete = function(e){// afiseaza
  146.     player.removeEventListener('ended', game.correctAnswerComplete, false);
  147. //    $("#bodyGame").append("<div id='correctAnswer'class='correctAnswer'style=' position:absolute;"+
  148. //            "align:center; bottom:"+(scale*(420))+"px; right:"+(scale*800)+"px; font-family:Arial;font-weight:bold; color:red;font-size:30px' ></div>");
  149.     //$("#correctAnswer").html("RASPUNSUL CORECT ESTE");
  150.     $.each(gamesComplex, function(key, game) {
  151.         game.getCorrectAnswer();
  152.     });
  153.  
  154.     $(".chkImg").attr("src",imagesCommon.empty);
  155.     isRepeatActive = true;
  156. };
  157.  
  158. GamesComplexMain.prototype.initialize = function(){
  159.     $("#bodyGame").append("<div id='gameStage' style='position:static;" +
  160.     "width:"+($("#gameDiv").width()-210*scale)+"px;height:"+$("#gameDiv").height()+"px'></div>");
  161.     
  162.      if(task!=undefined && task!=null && task!="undefined"&& task!=""){
  163.         $("#gameStage").append("<span style='position:absolute;top:"+task.y+"px;" +
  164.                      "left:"+task.x+"px;width:750px;" +
  165.                      "font-family:Arial; font-size:20px;color:black'>"+ task.text +"</span>");
  166.     }
  167.     
  168.     if(background!=undefined){
  169.         $("#gameStage").append("<img id='background' style='z-index:-1'>");
  170.         util.setImageStyle($("#background"), background);
  171.     }
  172.     
  173.     
  174.     
  175.     $.each(config, function(key, value) {
  176.         gamesComplex[key] = new window[value.nameGame](value);
  177.         gamesComplex[key].initialize();
  178.     });
  179.     util.resizeGameStage();
  180.     $("#verifica").off();
  181.     $("#repeta").off();
  182.     
  183.     $("#verifica").click(function(event) { 
  184.         if(!isActive) 
  185.             return false;
  186.         game.checkResult(); 
  187.     });
  188.     
  189.     $("#repeta").click(function(event) {
  190.         if(!isRepeatActive)
  191.             return false;
  192.         isActive = true;
  193.         index=-1;
  194.         $(".chkImg").attr("src",imagesCommon.empty);
  195.         for(var i=0;i<gamesComplex.length;i++){
  196.             gamesComplex[i].resetLevel();
  197.         }
  198.         if(sarcina.length != 0)
  199.             playSound(sarcina);
  200.     });    
  201.     $("#gameStage").click(function(e) {
  202.         console.log("left:"+Math.round((e.pageX-$("#gameStage").offset().left)/scale/3)+",top:"+
  203.         Math.round((e.pageY-$("#gameStage").offset().top)/scale/3)+"");}); 
  204. };
  205.