Advertisement
terorama

JQuery Shooter example

Dec 11th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 17.25 KB | None | 0 0
  1.  
  2. //-------------------------------
  3. _glo ={
  4.  
  5.   bullets:[],
  6.   foes: []
  7. };
  8. _hlp = {
  9.  
  10.   scrollX : function() {
  11.     return (window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0) -
  12.       (document.documentElement.clientLeft || document.body.clientLeft || 0);
  13.   },
  14.  
  15.   //--------------------
  16.   scrollY : function() {
  17.     return (window.pageYOffset ||  document.documentElement.scrollTop || document.body.scrollTop || 0) -
  18.       (document.documentElement.clientTop || document.body.clientTop || 0);
  19.   },
  20.   //--------------------
  21.   wr : function(tx) {
  22.     _glo.debug.wr(tx);
  23.   },
  24.  
  25.   //--------------------
  26.   map: function (func, arr) {
  27.  
  28.     //_hlp.wr(arr.length);
  29.  
  30.     for (var i=0; i<arr.length; i++) {
  31.         func(arr[i]);
  32.      }
  33.    },
  34.  
  35.   //--------------------
  36.   hex2dec : function (hnumber) {
  37.      
  38.      return parseInt(hnumber,16);
  39.   },
  40.  
  41.   //--------------------
  42.   dec2hex : function (dnumber) {
  43.    
  44.     return dnumber.toString(16);
  45.   },
  46.  
  47.   //--------------------
  48.   col2rgb : function(hcolor) {
  49.    
  50.     var cols = [];
  51.     var c = hcolor.slice(1);
  52.    
  53.     cols.push(this.hex2dec(c.slice(0,2)));
  54.     cols.push(this.hex2dec(c.slice(2,4)));
  55.     cols.push(this.hex2dec(c.slice(4,6)));
  56.    
  57.     return cols;
  58.   },
  59.  
  60.   //--------------------
  61.   rn : function (x4) {
  62.      
  63.      var x = x4 || 255;
  64.      var colors = [];
  65.      
  66.      for (var i=0; i<3; i++) {
  67.         colors.push(Math.floor(Math.random()*x));        
  68.      }
  69.      
  70.      return 'rgb('+colors[0]+','+colors[1]+','+colors[2]+')';
  71.   },
  72.  
  73.   //--------------------
  74.   sdate : function() {
  75.    
  76.     var d = new Date();
  77.    
  78.     var y = d.getFullYear();
  79.     var m = d.getMonth();
  80.     var day =d.getDay();
  81.     var h = d.getHours();
  82.     var min = d.getMinutes();
  83.     var s = d.getSeconds();
  84.    
  85.     return h+':'+min+':'+s;
  86.   },
  87.   //--------------------
  88.   radcoord : function(xc, yc, psy, rad) {
  89.  
  90.     return {x:xc+rad*Math.cos(psy), y: yc+rad*Math.sin(psy)};
  91.   },
  92.  
  93.   //--------------------
  94.   getangle : function(xc, yc, x, y ) {
  95.    
  96.     var hpt = Math.sqrt(Math.pow(y-yc,2)+ Math.pow(x-xc,2));
  97.     var angle = Math.acos((x-xc)/(hpt))* ((y-yc>0)?1:-1);
  98.    
  99.     return angle;
  100.   }
  101.  
  102. };
  103.  
  104.  
  105. //-------------------------------
  106. function extend(Child, Parent) {
  107.  
  108.   var F =function() {};
  109.   F.prototype = Parent.prototype;
  110.   Child.prototype = new F();
  111.   Child.prototype.constructor = Child;
  112.   Child.superclass = Parent.prototype;
  113. }
  114. //-------------------------------
  115. function moveObj() {
  116.  
  117.   var _self = this;  
  118.  
  119.   this.busyfin=false;
  120.  
  121.   this.speedx = 1;
  122.   this.speedy = 1;  
  123.  
  124.   this.targX = 0;
  125.   this.targY = 0;
  126.   var targ=false;
  127.  
  128.  
  129.  
  130.   //-------------------------------
  131.   this.mkEl = function(classz) {
  132.    
  133.     var bo = $(document.body);
  134.     var el = $('<div/>');
  135.  
  136.     bo.append(el);
  137.    
  138.     el.data('xz',0);
  139.     el.data('yz',0);
  140.    
  141.     if (classz)
  142.        el.addClass(classz);
  143.    
  144.     return el;
  145.    
  146.   };
  147.   //-------------------------------
  148.   this.getX = function () {
  149.    
  150.     return this.el.data('xz');
  151.   };
  152.   //-------------------------------  
  153.   this.getY = function () {
  154.    
  155.     return this.el.data('yz');
  156.   };
  157.  
  158.   //-------------------------------
  159.   this.setVis = function() {
  160.    
  161.    
  162.     var x = this.el.data('xz');
  163.     var y = this.el.data('yz');
  164.     var w = this.el.width()/2;
  165.     var h = this.el.height()/2;
  166.    
  167.     if ((x+w<window.innerWidth-5) && (x-w>0) &&
  168.       (y+h<window.innerHeight-5) && (y-h>0)) {
  169.        
  170.         this.el.css('visibility','visible');
  171.        
  172.        this.el.css('left',x-w);
  173.        this.el.css('top',y-h);
  174.        
  175.       } else {
  176.        
  177.         this.el.css('visibility','hidden');
  178.        
  179.         this.el.css('left','');
  180.         this.el.css('top','');
  181.        
  182.       }
  183.   };
  184.   //-------------------------------
  185.   this.setX = function(x) {
  186.    
  187.  
  188.     this.el.data('xz',x);
  189.     this.setVis();
  190.  
  191.   };
  192.   //-------------------------------
  193.   this.setY = function(y) {
  194.    
  195.     this.el.data('yz',y);
  196.     this.setVis();
  197.    
  198.   };
  199.   //-------------------------------
  200.   this.setXY = function(x,y) {
  201.        
  202.     this.setX(x);
  203.     this.setY(y);
  204.   };
  205.   //-------------------------------
  206.   this.checkBound = function(x,y) {
  207.    
  208.     return false;
  209.   };
  210.   //-------------------------------
  211.   this.moveTo = function(x,y) {
  212.    
  213.     var xo = this.getX();
  214.     var yo = this.getY();
  215.    
  216.     if ((Math.abs(x-xo)<10) && (Math.abs(y-yo)<10)) {
  217.       this.finished();
  218.       return;
  219.     }
  220.    
  221.    
  222.     var xn = xo+ this.speedx;
  223.     var yn = yo+ this.speedy;
  224.    
  225.    
  226.     if (this.checkBound(xn,yn)) {
  227.       this.finished();
  228.       return;
  229.     }
  230.      
  231.     //_hlp.wr('movenum='+this.num+' '+_hlp.sdate());
  232.     //_hlp.wr('inf='+x+' '+xo+' '+this.speedx);
  233.     this.setXY(xn,yn);
  234.    
  235.    
  236.   };
  237.    
  238.   //-------------------------------
  239.   this.finished = function() {
  240.    
  241.   };
  242.  
  243.   //-------------------------------
  244.   this.setSpeed = function (dx, dy) {
  245.    
  246.     this.speedx = dx;
  247.     this.speedy = dy;
  248.   };
  249.    
  250.   //-------------------------------  
  251.   this.setSpeedTarg = function (steps) {
  252.    
  253.     var dx = (this.targX - this.getX())/steps;
  254.     var dy = (this.targY - this.getY())/steps;
  255.    
  256.     this.setSpeed(dx,dy);
  257.     };
  258.    
  259.   //-------------------------------
  260.   this.setTarg = function(x,y) {
  261.    
  262.     this.targX = x;
  263.     this.targY = y;
  264.     targ = true;
  265.   };
  266.   //-------------------------------
  267.   this.clearTarg = function() {
  268.    
  269.        targ = false;
  270.     };
  271.  
  272.   //-------------------------------
  273.   this.moveToTarg = function() {
  274.    
  275.     if (targ===false)
  276.        return;
  277.    
  278.     this.moveTo(this.targX, this.targY);
  279.   };
  280. }
  281.  
  282. //-------------------------------
  283. //         debug
  284. //-------------------------------
  285. function Debug() {
  286.  
  287.   Debug.superclass.constructor.apply(this,arguments);
  288.  
  289.   this.el = this.mkEl('debug');
  290.  
  291.   this.setXY(150,150);
  292.  
  293.   this.wr = function(tx) {
  294.     this.el.prepend($('<p/>').html(tx));
  295.   };
  296. }
  297. //-------------------------------
  298. //         hero
  299. //-------------------------------
  300. function Hero () {
  301.  
  302.   Hero.superclass.constructor.apply(this, arguments);
  303.  
  304.   this.el = this.mkEl('mainHero');
  305.   this.setXY(window.innerWidth/2, window.innerHeight/2);
  306.  
  307.  
  308. }
  309. //-------------------------------
  310. //        cannon
  311. //-------------------------------
  312. function Cannon() {
  313.  
  314.   var _self = this;
  315.   var num_bullet = 0;
  316.  
  317.   Cannon.superclass.constructor.apply(this, arguments);
  318.   //this.speed = 10;
  319.   this.el= this.mkEl('cannon');
  320.  
  321.   //---------------------------------------
  322.   this.setAngle = function (x,y) {
  323.    
  324.     var xh = _glo.hero.getX();
  325.     var yh = _glo.hero.getY();
  326.    
  327.     var angle = _hlp.getangle(xh,yh,x,y);
  328.    
  329.     var coords = _hlp.radcoord(xh,yh, angle,40);
  330.    
  331.     _self.setXY(coords.x,coords.y);
  332.   };
  333.  
  334.    //---------------------------------------
  335.     window.onmousemove = function(ev) {
  336.       ev = ev || event;
  337.      
  338.      
  339.       var mx = ev.clientX + _hlp.scrollX();
  340.       var my = ev.clientY + _hlp.scrollY();
  341.      
  342.       _self.setAngle(mx,my);
  343.     };
  344.  
  345.    //---------------------------------------
  346.     window.onclick = function (ev) {
  347.      
  348.       ev = ev || event;
  349.      
  350.       var mx = ev.clientX + _hlp.scrollX();
  351.       var my = ev.clientY + _hlp.scrollY();
  352.      
  353.       var xc = _self.getX();
  354.       var yc = _self.getY();
  355.      
  356.       var bullet = new Bullet(_self);
  357.      
  358.            
  359.       bullet.setXY(xc, yc);
  360.      
  361.       var angle = _hlp.getangle( xc, yc, mx, my);
  362.       var coords = _hlp.radcoord(xc, yc, angle, 1000);
  363.      
  364.       bullet.setTarg(coords.x, coords.y);
  365.       bullet.setSpeedTarg(60);
  366.      
  367.       bullet.num = num_bullet;
  368.      
  369.       num_bullet++;
  370.      
  371.       _glo.bullets.push(bullet);
  372.    
  373.    };
  374.    //---------------------------------------
  375.  
  376. }
  377.  
  378. //-------------------------------
  379. //       flame
  380. //-------------------------------
  381. function Flame(par) {
  382.  
  383.   Flame.superclass.constructor.apply(this, arguments);
  384.   this.el = this.mkEl('flame');
  385.   this.el.css('display','none');
  386.   this.setXY(par.getX()-20+Math.random()*40, par.getY()-20+Math.random()*40);
  387.   this.el.css('background-color',_hlp.rn());
  388.  
  389.  
  390.  
  391. }
  392.  
  393. //-------------------------------
  394. //           bullet
  395. //-------------------------------
  396. function Bullet(par) {
  397.  
  398.  
  399.   Bullet.superclass.constructor.apply(this, arguments);
  400.  
  401.   var _self = this;
  402.   this._parent = par;
  403.   this.checkedin=false;
  404.  
  405.   this.el = this.mkEl('bullet');
  406.  
  407.   //-----------------------------------
  408.   this.fireworks = function () {
  409.    
  410.       var flames = [];
  411.      
  412.    
  413.       for (var i=0; i<10; i++) {
  414.            
  415.            var flame = new Flame(_self);
  416.            flames.push(flame);
  417.                            
  418.          }
  419.      //---------------------------------
  420.      return $.Deferred(
  421.        
  422.        function(dfd) {
  423.          
  424.          var numfires = 0;
  425.          //--------------------------
  426.          for (i=0; i<flames.length; i++) {
  427.            
  428.            flames[i].el.delay(10*i)
  429.              .queue( function () {
  430.                $(this).css('display','block');
  431.                $(this).dequeue();
  432.              })
  433.              .animate (
  434.              {'left': Math.random()*window.innerWidth-200,
  435.               'top' : Math.random()*window.innerHeight-200
  436.              },
  437.              { 'duration': 500,
  438.               'easing': '',
  439.               complete : function() {
  440.                 numfires++;
  441.                
  442.                
  443.                 if (numfires==flames.length) {
  444.                  
  445.                    dfd.resolve();
  446.                 }
  447.               }
  448.              }).queue(function() {$(this).remove()});
  449.          }
  450.          
  451.          //--------------------------
  452.          
  453.          
  454.          
  455.          
  456.          
  457.        });
  458.   };
  459.   //-----------------------------------
  460.   this.checkTarget = function(x,y) {
  461.    
  462.     if (this.checkedin===true)
  463.        return true;
  464.     //--------------------
  465.     checkIn = function (inf) {
  466.      
  467.       var xz = inf.el.data('xz');
  468.       var yz = inf.el.data('yz');
  469.      
  470.       var w = inf.el.width()/2;
  471.       var h = inf.el.height()/2;
  472.      
  473.      
  474.       if ((x<xz+w) && (x>xz-w) && (y<yz+w) && (y>yz-w)) {
  475.        
  476.         return true;
  477.       }
  478.       return false;
  479.     };
  480.     //--------------------
  481.    
  482.     for (var i=0;i<_glo.foes.length; i++) {
  483.      
  484.       if (checkIn(_glo.foes[i])) {
  485.        
  486.         $.when(_glo.foes[i].killed()).done(
  487.          
  488.           function() {
  489.               _glo.foes.splice(i,1);
  490.               //_hlp.wr('foes=' +_glo.foes.length);                
  491.           })
  492.        
  493.         this.checkedin = true;
  494.         return true;
  495.       }
  496.      }
  497.     return false;
  498.      
  499.        
  500.   };
  501.   //-----------------------------------
  502.   this.checkBound = function(x,y) {
  503.    
  504.      
  505.     var w = this.el.width()/2;
  506.     var h = this.el.height()/2;
  507.    
  508.     if (x+w>window.innerWidth) return true;
  509.     if (x-w<0) return true;
  510.     if (y+h>window.innerHeight) return true;
  511.     if (y-h<0) return true;
  512.    
  513.     if (this.checkTarget(x,y)) {
  514.      
  515.       return true;}
  516.        
  517.    
  518.     return false;
  519.   };
  520.  
  521.   //-----------------------------------
  522.   this.finished = function () {
  523.    
  524.     if (this._parent.busyfin===true) {
  525.      
  526.       return;
  527.     }
  528.    
  529.     this._parent.busyfin = true;
  530.    
  531.     //_hlp.wr('s='+this.num);
  532.    
  533.     for (i=0; i<_glo.bullets.length; i++) {
  534.       if (_glo.bullets[i].num==this.num) {
  535.          
  536.           _glo.bullets.splice(i,1);
  537.           //_hlp.wr('len='+_glo.bullets.length);
  538.       }
  539.     }
  540.    
  541.    
  542.     this.el.css('visibility','hidden');
  543.    
  544.     $.when (this.fireworks()).done(
  545.      
  546.       function() {
  547.        
  548.           _self.el.remove();
  549.           //delete _self;
  550.          _self._parent.busyfin = false;
  551.          
  552.       }
  553.       );
  554.      
  555.  
  556.     //_hlp.wr('test4');
  557.    
  558.            
  559.   };
  560.  
  561.  
  562. }
  563.  
  564. //-------------------------------
  565. //          foe
  566. //-------------------------------
  567. function Foe () {
  568.  
  569.   Foe.superclass.constructor.apply(this, arguments);
  570.  
  571.   var _self = this;
  572.  
  573.   this.el = this.mkEl('foe');
  574.  
  575.  
  576.   //--------------------------------
  577.   this.bleeding = function() {
  578.    
  579.     var drops = [];
  580.     for (var i=0; i<3; i++) {
  581.      
  582.       var drop = new Flame(_self);
  583.       drop.el.css('background-color','red');
  584.       drop.el.css('width',5).css('height',5);
  585.      
  586.       drops.push(drop);
  587.     }
  588.     //-------------
  589.     return $.Deferred (
  590.       function(dfd) {
  591.        
  592.         var numdrops = 0;
  593.        
  594.         var tx = _self.el.offset().left;
  595.         var ty = _self.el.offset().top;
  596.    
  597.         for (var i=0; i<drops.length; i++) {
  598.          
  599.    
  600.           drops[i].el.delay(i*4).queue (
  601.             function() {
  602.               $(this).css('display','block');
  603.               $(this).dequeue();
  604.             })
  605.             .animate({
  606.               'left': -20+Math.random()*40 + tx,
  607.               'top': -20+Math.random()*40 + ty },
  608.                      
  609.                      {'duration': 200,
  610.                       'easing': '',
  611.                      
  612.                       complete: function () {
  613.                        
  614.                         numdrops++;
  615.                         if (numdrops==drops.length)
  616.                            dfd.resolve();
  617.                       }
  618.                      })
  619.             .queue(
  620.              
  621.               function() {
  622.                
  623.                 $(this).remove();
  624.             });
  625.         }
  626.       });
  627.   };
  628.   //--------------------------------
  629.   this.killed = function () {
  630.    
  631.     this.setTarg(Math.random()*window.innerWidth,
  632.                    Math.random()*window.innerHeight);
  633.     this.setSpeedTarg(100);
  634.    
  635.     return $.Deferred(
  636.      
  637.       function(dfd) {
  638.        
  639.          $.when (_self.bleeding()).done(
  640.      
  641.            function() {
  642.            _self.el.remove();
  643.            dfd.resolve();
  644.          });
  645.      
  646.       });
  647.   };
  648.  
  649.   //--------------------------------
  650.   this.explode = function() {
  651.    
  652.     var flames = [];
  653.    
  654.     //----------------
  655.     for (var i=0; i<400; i++) {
  656.      
  657.       var flame = new Flame(_self);
  658.       flame.el.css('background-color','red');
  659.       flames.push(flame);
  660.     }
  661.     //----------------
  662.     return $.Deferred (
  663.      
  664.       function(dfd) {
  665.        
  666.         var numfires = 0;
  667.        
  668.         for (var i=0; i<flames.length; i++) {
  669.           flames[i].el.delay(4*i).queue (
  670.             function() {
  671.               $(this).css('display','block');
  672.               $(this).dequeue();
  673.             })
  674.             .animate({'left': Math.random()*window.innerWidth-20,
  675.                       'top': Math.random()*window.innerHeight-20},
  676.                      
  677.                      {'duration': 500,
  678.                       'easing': '',
  679.                       complete: function () {
  680.                        
  681.                         numfires++;
  682.                         if (numfires==flames.length)
  683.                           dfd.resolve();
  684.                       }
  685.                      
  686.                      }).queue(function() {
  687.                            
  688.                           $(this).remove();              
  689.                         });
  690.                      
  691.            
  692.         }
  693.       });
  694.    
  695.   };
  696.   //--------------------------------
  697.   this.finished = function () {
  698.    
  699.     clearInterval(gl_fint);
  700.     clearInterval(gl_int);
  701.    
  702.     $.when (this.explode()).done (
  703.      
  704.       function () {
  705.        
  706.         var glass = $('<div/>', {'class':'glass'});
  707.         $(document.body).append(glass);
  708.        
  709.        
  710.         var fdv = $('<div/>',{'class':'final'});
  711.         fdv.html('tower captured');
  712.         $(document.body).append(fdv);
  713.        
  714.         fdv.css('left', window.innerWidth/2 - fdv.width()/2);
  715.         fdv.css('top', window.innerHeight/2 - fdv.height()/2);
  716.        
  717.         fdv.fadeIn('slow');
  718.         glass.fadeIn('slow');
  719.       }
  720.       );
  721.   };
  722. }
  723.  
  724. //-------------------------------
  725. extend(Debug, moveObj);
  726. extend(Hero, moveObj);
  727. extend(Cannon, moveObj);
  728. extend(Bullet, moveObj);
  729. extend(Flame, moveObj);
  730. extend(Foe, moveObj);
  731.  
  732.  
  733. //-------------------------------
  734.  
  735. function frame() {
  736.  
  737.   //_glo.cannon.moveToTarg();
  738.  
  739.   //_hlp.wr(_glo.bullets.length)
  740.   //_hlp.wr(window.innerWidth);
  741.  
  742.   movef = function (el) {
  743.    
  744.     el.moveToTarg();
  745.    
  746.   };
  747.  
  748.   _hlp.map(movef, _glo.bullets);
  749.   _hlp.map(movef, _glo.foes);
  750.  
  751.  
  752.    
  753. }
  754. //-------------------------------
  755. function addHero() {
  756.  
  757.   _glo.debug = new Debug();
  758.   _glo.hero = new Hero();      
  759.   _glo.cannon = new Cannon();
  760.  
  761.                  
  762. }
  763. //-------------------------------
  764. function addFoe() {
  765.  
  766.   var foe = new Foe();
  767.  
  768.   var xc = _glo.hero.getX();
  769.   var yc = _glo.hero.getY();
  770.  
  771.   var angle = -7+ Math.random()*14;
  772.   var coords = _hlp.radcoord(xc, yc, angle, 500);
  773.  
  774.   foe.setXY(coords.x, coords.y);
  775.  
  776.   foe.setTarg(xc,yc);
  777.   foe.setSpeedTarg(500+Math.random()*400);
  778.  
  779.   _glo.foes.push(foe);
  780.  
  781.                  
  782. }
  783.  
  784. //-------------------------------
  785. function init() {
  786.  
  787.   addHero();
  788.  
  789.   gl_int = setInterval(frame,10);
  790.   gl_fint = setInterval(addFoe,1000);
  791. }
  792. //-------------------------
  793. $(document).ready(
  794.   init);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement