Advertisement
Guest User

Untitled

a guest
Jun 1st, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Tile Match Game Logic */
  2. $(document).ready(function($){ 
  3.    
  4.     var tileSelected = false;
  5.     var timer = false;
  6.     // The name of the character set that will be used as the name of the best time
  7.     var timeName = (hiragana == true) ? 'hiragana' : 'katakana';
  8.    
  9.     var kanaTotal = kanaCount;
  10.     // Set the on screen counter
  11.     $('#kanaCount').text(kanaCount);
  12.    
  13.     $('#runner').runner({
  14.         stopAt: 3600000
  15.     });
  16.    
  17.     if (Modernizr.localstorage)
  18.     {
  19.         // Set time if it's better than their previous
  20.         if(localStorage.getItem(timeName + 'Time') !== null)
  21.         {
  22.             $('<p id="best-time">Your best time is '+ msToTime(localStorage[timeName + 'Time']) +'</p>').insertAfter('#tile-intro');
  23.         }
  24.        
  25.     }
  26.    
  27.     // If a tile that hasn't already been slotted is clicked. Use on because events will be detached when elements are moved around the page
  28.     $('.tiles').on('click', '.tile:not(.set-tile)', function(){
  29.        
  30.         // Remove the active status from any other tiles
  31.         $('.tile').removeClass('active-tile');
  32.         // Select the tile
  33.         $(this).addClass('active-tile');
  34.         // Indicate we have a tile selected
  35.         tileSelected = true;
  36.  
  37.         // Start the timer
  38.         if(timer == false)
  39.         {
  40.             $('#runner').runner('start');
  41.             timer = true;
  42.         }
  43.     });
  44.    
  45.     $('.tile-slot').click(function(){
  46.    
  47.         // If a tile is currently selected
  48.         if(tileSelected == true)
  49.         {
  50.             // If the currently selected tile and the clicked tile slot match
  51.             if($('.tile.active-tile').data('kana') == $(this).data('kana'))
  52.             {
  53.                 // Deselect the tile
  54.                 tileSelected = false;
  55.                
  56.                 // Clone the tile that is being moved
  57.                 var tile = $('.tile.active-tile').clone();
  58.                 // Delete the original tile
  59.                 $('.tile.active-tile').remove();
  60.                 // Add the cloned tile to the slot
  61.                 $(this).find('span').hide();
  62.                 $(this).append(tile);
  63.                 // Remove the active status of the tile
  64.                 $(tile).removeClass('active-tile');
  65.                 // Add a class the remove the pointer cursor from the slot
  66.                 $(this).addClass('filled-slot');
  67.                 $(tile).addClass('set-tile');
  68.                
  69.                 // Decrement the kana count and check if any more tiles remain
  70.                 kanaCount--;
  71.                 // Update the on screen counter
  72.                 $('#kanaCount').text(kanaCount);
  73.                 // If the user is done
  74.                 if(kanaCount == 0)
  75.                 {
  76.                     // Update the on screen counter with an exclamation mark!
  77.                     $('#kanaCount').text(kanaCount + ' !');
  78.                    
  79.                     // Stop the timer
  80.                     $('#runner').runner('stop');
  81.                     // Get the users time
  82.                     var userTime = $('#runner').runner('info').time;
  83.                    
  84.                     // Save their time
  85.                     if (Modernizr.localstorage)
  86.                     {
  87.                         // Set time if it's better than their previous
  88.                         if(localStorage.getItem(timeName + 'Time') !== null)
  89.                         {
  90.                             // If this is their best time
  91.                             if(userTime < localStorage[timeName + 'Time'])
  92.                             {
  93.                                 localStorage[timeName + 'Time'] = userTime;
  94.                                 $('.congratulations #best-time').fadeIn('slow');
  95.                                 $('#best-time').remove();
  96.                                 $('<p id="best-time">Your best time is '+ msToTime(localStorage[timeName + 'Time']) +'</p>').insertAfter('#tile-intro');
  97.                             }
  98.                             else
  99.                             {
  100.                                 $('.congratulations #you-did-it').fadeIn('slow');
  101.                             }
  102.                         } // If they have no previously recorded time
  103.                         else
  104.                         {
  105.                             $('.congratulations #you-did-it').fadeIn('slow');
  106.                             localStorage[timeName + 'Time'] = userTime;
  107.                             $('<p id="best-time">Your best time is '+ msToTime(localStorage[timeName + 'Time']) +'</p>').insertAfter('#tile-intro');
  108.                         }
  109.                        
  110.                     }
  111.                     else
  112.                     {
  113.                         // Congratulate them
  114.                         $('.congratulations #you-did-it').fadeIn('slow');
  115.                     }
  116.                 }
  117.             }
  118.         }
  119.    
  120.     });
  121.    
  122.     // Reset the tile game
  123.     $('#reset').click(function(){
  124.         // Move any slotted tiles back
  125.         $('.tiles').append($('.tile-slot > .tile'));
  126.         // Remove the set-tile class from any previously slotted tiles
  127.         $('.tile').removeClass('set-tile');
  128.         // Remove the filled-slot class from any slots which previously held tiles
  129.         $('.tile-slot').removeClass('filled-slot');
  130.         // Deselect any tile that is selected
  131.         $('.tile').removeClass('active-tile');
  132.         // Shuffle all of the tiles
  133.         $('.tile').shuffle();
  134.         // Show the text of the tile slots
  135.         $('.tile-slot > span').show();
  136.         // Hide any congratulation messages that may be visible
  137.         $('.congratulations #you-did-it, .congratulations #best-time').hide();
  138.         // Reset the kana counter
  139.         kanaCount = kanaTotal;
  140.         // Reset the on screen counter
  141.         $('#kanaCount').text(kanaCount);
  142.         // Reset the timer
  143.         timer = false;
  144.         $('#runner').runner('stop');
  145.         $('#runner').runner('reset');
  146.     });
  147.    
  148.     // Shuffle array function used when resetting the tile game
  149.     (function($){
  150.      
  151.         $.fn.shuffle = function() {
  152.      
  153.             var allElems = this.get(),
  154.                 getRandom = function(max) {
  155.                     return Math.floor(Math.random() * max);
  156.                 },
  157.                 shuffled = $.map(allElems, function(){
  158.                     var random = getRandom(allElems.length),
  159.                         randEl = $(allElems[random]).clone(true)[0];
  160.                     allElems.splice(random, 1);
  161.                     return randEl;
  162.                });
  163.      
  164.             this.each(function(i){
  165.                 $(this).replaceWith($(shuffled[i]));
  166.             });
  167.      
  168.             return $(shuffled);
  169.      
  170.         };
  171.      
  172.     })(jQuery);
  173.    
  174.     // Formats the milliseconds that runner records into a readable time format. Taken from the runner code
  175.     function msToTime(time)
  176.     {
  177.         var i, len, ms, output, prefix, separator, step, steps, value, _i, _len;
  178.         steps = [3600000, 60000, 1000, 10];
  179.         separator = ['', ':', ':', '.'];
  180.         prefix = '';
  181.         output = '';
  182.         ms = time;
  183.         len = steps.length;
  184.         value = 0;
  185.        
  186.         if (time < 0) {
  187.             time = Math.abs(time);
  188.             prefix = '-';
  189.         }
  190.         for (i = _i = 0, _len = steps.length; _i < _len; i = ++_i) {
  191.             step = steps[i];
  192.             value = 0;
  193.            
  194.             if (time >= step) {
  195.                 value = Math.floor(time / step);
  196.                 time -= value * step;
  197.             }
  198.            
  199.             if ((value || i > 1 || output) && (i !== len - 1 || ms)) {
  200.                 output += (output ? separator[i] : '') + pad(value);
  201.             }
  202.         }
  203.        
  204.         return prefix + output;
  205.     };
  206.    
  207.     function pad(num) {
  208.         return (num < 10 ? '0' : '') + num;
  209.     };
  210.    
  211. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement