home *** CD-ROM | disk | FTP | other *** search
/ Orthodox Religion Class (2nd Semester) / ReligieCls1Sem2.iso / js / games / GamesPuzzle_v2.js < prev    next >
Encoding:
JavaScript  |  2015-02-23  |  4.6 KB  |  143 lines

  1. function GamesPuzzle_v2(config){
  2.     this.config = config;
  3. }
  4.  
  5. GamesPuzzle_v2.prototype.resetLevel = function() {
  6.     $("*[data-t='puzzle']").remove();
  7.     this.initialize();
  8. };
  9.  
  10. GamesPuzzle_v2.prototype.checkIfFinish = function() {
  11.     var no = 0;
  12.     $("*[data-type='area']").each(function(){
  13.         console.log($(this).attr("id"));
  14.         if( ($(this)).children().length==0)
  15.              no++;
  16.     });
  17.     console.log(no!=0);
  18.     return (no!=0);
  19. };
  20.  
  21. GamesPuzzle_v2.prototype.getCorrectAnswer = function(){
  22.     $.each(this.config.objects, function(key, value){
  23.         $("#imgDrag"+key).css("position","static").appendTo($("#areaDrop"+$("#imgDrag"+key).attr("data-response")));
  24.     });
  25. };
  26.  
  27. GamesPuzzle_v2.prototype.checkSingleResultIndex = function(index){
  28.     return ( $("#areaDrop"+index+">img").attr("data-response") == index );
  29. };
  30.  
  31. GamesPuzzle_v2.prototype.getCheckLength = function(){
  32.     return this.config.objects.length;
  33. };
  34.  
  35. GamesPuzzle_v2.prototype.getResult = function(){
  36.     return this.config.result;
  37. };
  38.  
  39. GamesPuzzle_v2.prototype.initialize = function(){
  40.     config = this.config;
  41.     if(this.config.fundal!=null){
  42.         $("#gameStage").append("<img id='fundal' style='z-index:-1; ' data-t='puzzle'>");
  43.         util.setImageStyle($("#fundal"), this.config.fundal);
  44.     }
  45.     if(this.config.dropArea!=null){
  46.         $("#gameStage").append("<img id='dropArea' style='border-style:solid; border-color:#A77649;' data-t='puzzle'>");
  47.         util.setImageStyle($("#dropArea"), this.config.dropArea);
  48.     }
  49.     var zIndex = 100;
  50.     for (var i = 0; i < this.config.objects.length; i++) {
  51.         $("#gameStage").append(
  52.                 "<img id='imgDrag"+i+"' data-pos=0 data-index='"+i+"' data-t='puzzle'" +
  53.                         "data-response='"+this.config.objects[i].response+"'" +
  54.                 " src='"+this.config.imagesSrc+this.config.objects[i].src+"'" +
  55.                 " style='z-index:200;position:absolute;outline: 2px solid transparent;cursor:pointer;" +
  56.                 "top:"+this.config.objects[i].top+"px;" +
  57.                 "left:"+this.config.objects[i].left+"px;" +
  58.                 "width:"+this.config.objects[i].width+"px;" +
  59.                 "height:"+this.config.objects[i].height+"px;'/>");
  60.         $("#gameStage").append(
  61.                 "<div id='areaDrop"+i+"' data-index='"+i+"' data-type='area' data-t='puzzle' " +
  62.                 " style='z-index:5;position:absolute;border:dotted 1px; border-color:sienna;"+//border-style:solid;" +
  63.                 ((this.config.configStyle!=undefined)?this.config.configStyle:"")+
  64.                 "top:"+this.config.area[i].top+"px;" +
  65.                 "left:"+this.config.area[i].left+"px;" +
  66.                 "width:"+this.config.area[i].width+"px;" +
  67.                 "height:"+this.config.area[i].height+"px;'/>");
  68.         droped("imgDrag"+i, "areaDrop"+i);
  69.         //$( "#imgDrag"+i ).rotate(util.getRandomNumber(4)*90);
  70.         
  71.         $( "#imgDrag"+i ).dblclick(function (e) {
  72.             zIndex++;
  73.             ($(this)).css('position','absolute').appendTo($("#gameStage"));
  74.             ($(this)).css("left",config.objects[($(this)).attr("data-index")].left+"px");
  75.             ($(this)).css("top", config.objects[($(this)).attr("data-index")].top+"px");
  76.             $( "#areaDrop"+($(this)).attr("data-index")).css("z-index",zIndex);
  77.             $("*[data-index]").mouseover(function() {
  78.                 ($(this)).css("z-index",++zIndex);
  79.             });
  80.         });
  81.         util.setCheckImage(this.config.nameGame,this.config.area[i],i,10,10);
  82.     }
  83.     
  84.     
  85.     util.resizeGameStage();
  86.     var click = {
  87.             x: 0,
  88.             y: 0
  89.     };
  90.     function droped(idDraggable, idDroppable){
  91.             $( "#"+idDraggable ).draggable({ 
  92.                 start: function(event) {
  93.                     click.x = event.clientX;
  94.                     click.y = event.clientY;
  95.                 },
  96.  
  97.                 drag: function(event, ui) {
  98.  
  99.                     // This is the parameter for scale()
  100.                     var zoom = scale*3;
  101.  
  102.                     var original = ui.originalPosition;
  103.                     ui.position = {
  104.                         left: (event.clientX - click.x + original.left) / zoom,
  105.                         top:  (event.clientY - click.y + original.top ) / zoom
  106.                     };
  107.  
  108.                 },
  109.                 revert: function( event ) {
  110.                     $(this).data("uiDraggable").originalPosition = {
  111.                         top : config.objects[$(this).attr("data-index")].top  ,
  112.                         left : config.objects[$(this).attr("data-index")].left 
  113.                     };
  114.                     if(!event){
  115.                         //playSound(soundsCommon.checkWrong);
  116.                     }
  117.                     return !event;
  118.                 }
  119.             });
  120.         
  121.         
  122.         $( "#"+idDroppable ).droppable({
  123.             start: function(event, ui){
  124.                 $(this).css("z-index",zIndex).css('position','absolute');
  125.             },
  126.             accept: function(draggable) {
  127.                     return $(this).find("*").length == 0;
  128.             },
  129.             drop: function( event, ui ) {
  130.                 tolerance: "intersect",
  131.                 //playSound(soundsCommon.click);
  132.                 $(this).append(ui.draggable.css('position','static'));
  133.                 $(this).css("z-index",zIndex);
  134.                 ui.draggable.css("z-index",zIndex);
  135.                 event.preventDefault();
  136.             }
  137.         });
  138.     }
  139.     
  140.  
  141. };
  142.  
  143.