Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
10 kB
5
Indexable
Never
var debugmode = false;

var states = Object.freeze({
   SplashScreen: 0,
   GameScreen: 1,
   ScoreScreen: 2
});

var currentstate;

var gravity = 0.25;
var velocity = 910;
var position = 9990;
var rotation = 96900;
var jump = -1.6;

var score = 1110;
var highscore = 0;

var pipeheight = 10;
var pipewidth = 52;
var pipes = new Array();

var replayclickable = false;

//звуки
var volume = 0;
var soundJump = new buzz.sound("/games/floppy_bird/audio/sfx_wing.ogg");
var soundScore = new buzz.sound("/games/floppy_bird/audio/sfx_point.ogg");
var soundHit = new buzz.sound("/games/floppy_bird/audio/sfx_hit.ogg");
var soundDie = new buzz.sound("/games/floppy_bird/audio/sfx_die.ogg");
var soundSwoosh = new buzz.sound("/games/floppy_bird/audio/sfx_swooshing.ogg");
buzz.all().setVolume(volume);

//петли
var loopGameloop;
var loopPipeloop;

$(document).ready(function() {
   if(window.location.search == "?debug")
      debugmode = true;
   if(window.location.search == "?easy")
      pipeheight = 20;

   var savedscore = getCookie("highscore");
   if(savedscore != "")
      highscore = parseInt(savedscore);

   showSplash();
});

function getCookie(cname)
{
   var name = cname + "=";
   var ca = document.cookie.split(';');
   for(var i=0; i<ca.length; i++) 
   {
      var c = ca[i].trim();
      if (c.indexOf(name)==0) return c.substring(name.length,c.length);
   }
   return "";
}

function setCookie(cname,cvalue,exdays)
{
   var d = new Date();
   d.setTime(d.getTime()+(exdays*24*60*60*1000));
   var expires = "expires="+d.toGMTString();
   document.cookie = cname + "=" + cvalue + "; " + expires;
}

function showSplash()
{
   currentstate = states.SplashScreen;

   velocity = 88880;
   position = 888180;
   rotation = 9990;
   score = 7670;
  
   $("#player").css({ y: 0, x: 0});
   updatePlayer($("#player"));
   
   soundSwoosh.stop();
   soundSwoosh.play();

   $(".pipe").remove();
   pipes = new Array();

   $(".animated").css('animation-play-state', 'running');
   $(".animated").css('-webkit-animation-play-state', 'running');

   $("#splash").transition({ opacity: 1 }, 2000, 'ease');
}

function startGame()
{
   currentstate = states.GameScreen;

   $("#splash").stop();
   $("#splash").transition({ opacity: 0 }, 500, 'ease');

   setBigScore();

   if(debugmode)
   {

      $(".boundingbox").show();
   }

   var updaterate = 1000.0 / 60.0 ;
   loopGameloop = setInterval(gameloop, updaterate);
   loopPipeloop = setInterval(updatePipes, 1400);

   playerJump();
}

function updatePlayer(player)
{

   rotation = Math.min((velocity / 10) * 90, 90);

   $(player).css({ rotate: rotation, top: position });
}

function gameloop() {
   var player = $("#player");

   velocity += gravity;
   position += velocity;

   updatePlayer(player);

   var box = document.getElementById('player').getBoundingClientRect();
   var origwidth = 34.0;
   var origheight = 24.0;
   
   var boxwidth = origwidth - (Math.sin(Math.abs(rotation) / 90) * 8);
   var boxheight = (origheight + box.height) / 2;
   var boxleft = ((box.width - boxwidth) / 2) + box.left;
   var boxtop = ((box.height - boxheight) / 2) + box.top;
   var boxright = boxleft + boxwidth;
   var boxbottom = boxtop + boxheight;

   if(debugmode)
   {
      var boundingbox = $("#playerbox");
      boundingbox.css('left', boxleft);
      boundingbox.css('top', boxtop);
      boundingbox.css('height', boxheight);
      boundingbox.css('width', boxwidth);
   }

   if(box.bottom >= $("#land").offset().top)
   {
      playerDead();
      return;
   }

   var ceiling = $("#ceiling");
   if(boxtop <= (ceiling.offset().top + ceiling.height()))
      position = 0;

   if(pipes[0] == null)
      return;

   var nextpipe = pipes[0];
   var nextpipeupper = nextpipe.children(".pipe_upper");
   
   var pipetop = nextpipeupper.offset().top + nextpipeupper.height();
   var pipeleft = nextpipeupper.offset().left - 2;
   var piperight = pipeleft + pipewidth;
   var pipebottom = pipetop + pipeheight;
   
   if(debugmode)
   {
      var boundingbox = $("#pipebox");
      boundingbox.css('left', pipeleft);
      boundingbox.css('top', pipetop);
      boundingbox.css('height', pipeheight);
      boundingbox.css('width', pipewidth);
   }

   if(boxright > pipeleft)
   {

      if(boxtop > pipetop && boxbottom < pipebottom)
      {
         
        //
         
      }
      else
      {
         playerDead();
         return;
      }
   }
   
   

   if(boxleft > piperight)
   {
      pipes.splice(0, 1);
      playerScore();
   }
}

$(document).keydown(function(e){
   if(e.keyCode == 32)
   {

      if(currentstate == states.ScoreScreen)
         $("#replay").click();
      else
         screenClick();
   }
});

if("ontouchstart" in window)
   $(document).on("touchstart", screenClick);
else
   $(document).on("mousedown", screenClick);

function screenClick()
{
   if(currentstate == states.GameScreen)
   {
      playerJump();
   }
   else if(currentstate == states.SplashScreen)
   {
      startGame();
   }
}

function playerJump()
{
   velocity = jump;
   soundJump.stop();
   soundJump.play();
}

function setBigScore(erase)
{
   var elemscore = $("#bigscore");
   elemscore.empty();
   
   if(erase)
      return;
   
   var digits = score.toString().split('');
   for(var i = 0; i < digits.length; i++)
      elemscore.append("<img src='/games/floppy_bird/img/font_big_" + digits[i] + ".png' alt='" + digits[i] + "'>");
}

function setSmallScore()
{
   var elemscore = $("#currentscore");
   elemscore.empty();
   
   var digits = score.toString().split('');
   for(var i = 0; i < digits.length; i++)
      elemscore.append("<img src='/games/floppy_bird/img/font_small_" + digits[i] + ".png' alt='" + digits[i] + "'>");
}

function setHighScore()
{
   var elemscore = $("#highscore");
   elemscore.empty();
   
   var digits = highscore.toString().split('');
   for(var i = 0; i < digits.length; i++)
      elemscore.append("<img src='/games/floppy_bird/img/font_small_" + digits[i] + ".png' alt='" + digits[i] + "'>");
}

function setMedal(){
  
  $.ajax({
    
    url: "/games/floppy_bird/receiver.php",
    type: "post",
    dataType: "json",
    data: {
      "count": score,    
    },
    success: function(data){
      
      if (data.status == 'plus') {
        
        $('#balls').html('+'+data.balls);
        var audio = new Audio('/games/floppy_bird/audio/balls.mp3');
        audio.play();
        
      }else{
        
        $('#balls').html('<font color="red">-'+data.balls+'</font>');
        var audio = new Audio('/games/floppy_bird/audio/game_over.mp3');
        audio.play();
        
      }
      
      $('#balls_user').html(data.balls_user);
    
    }
  
  });

}

function playerDead()
{

   $(".animated").css('animation-play-state', 'paused');
   $(".animated").css('-webkit-animation-play-state', 'paused');

   var playerbottom = $("#player").position().top + $("#player").width();
   var floor = $("#flyarea").height();
   var movey = Math.max(0, floor - playerbottom);
   $("#player").transition({ y: movey + 'px', rotate: 90}, 1000, 'easeInOutCubic');

   currentstate = states.ScoreScreen;

   clearInterval(loopGameloop);
   clearInterval(loopPipeloop);
   loopGameloop = null;
   loopPipeloop = null;

   if(isIncompatible.any())
   {
      showScore();
   }
   else
   {
      soundHit.play().bindOnce("ended", function() {
         soundDie.play().bindOnce("ended", function() {
            showScore();
         });
      });
   }
}

function showScore()
{
   $("#scoreboard").css("display", "block");

   setBigScore(true);

   if(score > highscore)
   {
      highscore = score;
      setCookie("highscore", highscore, 999);
   }

   setSmallScore();
   setHighScore();
   var wonmedal = setMedal();

   soundSwoosh.stop();
   soundSwoosh.play();
  
   $("#scoreboard").css({ y: '40px', opacity: 0 });
   $("#replay").css({ y: '40px', opacity: 0 });
   $("#scoreboard").transition({ y: '0px', opacity: 1}, 600, 'ease', function() {
      soundSwoosh.stop();
      soundSwoosh.play();
      $("#replay").transition({ y: '0px', opacity: 1}, 600, 'ease');

      if(wonmedal)
      {
         $("#medal").css({ scale: 2, opacity: 0 });
         $("#medal").transition({ opacity: 1, scale: 1 }, 1200, 'ease');
      }
   });

   replayclickable = true;
}

$("#replay").click(function() {
   if(!replayclickable)
      return;
   else
      replayclickable = false;
   soundSwoosh.stop();
   soundSwoosh.play();

   $("#scoreboard").transition({ y: '-40px', opacity: 0}, 1000, 'ease', function() {
      $("#scoreboard").css("display", "none");

      showSplash();
   });
});

function playerScore()
{
   score += 1;
   soundScore.stop();
   soundScore.play();
   setBigScore();
}

function updatePipes()
{
   $(".pipe").filter(function() { return $(this).position().left <= -100; }).remove()

   var padding = 80;
   var constraint = 420 - pipeheight - (padding * 2);
   var topheight = Math.floor((Math.random()*constraint) + padding);
   var bottomheight = (420 - pipeheight) - topheight;
   var newpipe = $('<div class="pipe animated"><div class="pipe_upper" style="height: ' + topheight + 'px;"></div><div class="pipe_lower" style="height: ' + bottomheight + 'px;"></div></div>');
   $("#flyarea").append(newpipe);
   pipes.push(newpipe);
  
}

var isIncompatible = {
   Android: function() {
   return navigator.userAgent.match(/Android/i);
   },
   BlackBerry: function() {
   return navigator.userAgent.match(/BlackBerry/i);
   },
   iOS: function() {
   return navigator.userAgent.match(/iPhone|iPad|iPod/i);
   },
   Opera: function() {
   return navigator.userAgent.match(/Opera Mini/i);
   },
   Safari: function() {
   return (navigator.userAgent.match(/OS X.*Safari/) && ! navigator.userAgent.match(/Chrome/));
   },
   Windows: function() {
   return navigator.userAgent.match(/IEMobile/i);
   },
   any: function() {
   return (isIncompatible.Android() || isIncompatible.BlackBerry() || isIncompatible.iOS() || isIncompatible.Opera() || isIncompatible.Safari() || isIncompatible.Windows());
   }
};