/****
* Classes
****/
//<Write entity 'classes' with empty functions for important behavior here>
var Bird = Container.expand(function () {
  var self = Container.call(this);
  var birdGraphics = self.attachAsset('bird', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speedY = 0;
  self.gravity = 0.5;
  self.lift = -10;
  self.update = function () {
    self.speedY += self.gravity;
    self.y += self.speedY;
    if (self.y > 2732) {
      self.y = 2732;
      self.speedY = 0;
    }
    if (self.y < 0) {
      self.y = 0;
      self.speedY = 0;
    }
  };
  self.flap = function () {
    self.speedY = self.lift;
  };
});
var Obstacle = Container.expand(function () {
  var self = Container.call(this);
  var obstacleGraphics = self.attachAsset('obstacle', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speedX = -5;
  self.update = function () {
    self.x += self.speedX;
    if (self.x < -100) {
      self.destroy();
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x87ceeb //Init game with sky blue background 
});
/****
* Game Code
****/
//<Write game logic code here, including initializing arrays and variables>
//<Assets used in the game will automatically appear here>
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
  size: 150,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnObstacle() {
  var gap = 300;
  var topHeight = Math.random() * (2732 - gap);
  var bottomHeight = 2732 - topHeight - gap;
  var topObstacle = new Obstacle();
  topObstacle.height = topHeight;
  topObstacle.x = 2048;
  topObstacle.y = topHeight / 2;
  obstacles.push(topObstacle);
  game.addChild(topObstacle);
  var bottomObstacle = new Obstacle();
  bottomObstacle.height = bottomHeight;
  bottomObstacle.x = 2048;
  bottomObstacle.y = 2732 - bottomHeight / 2;
  obstacles.push(bottomObstacle);
  game.addChild(bottomObstacle);
}
game.down = function (x, y, obj) {
  bird.flap();
};
game.update = function () {
  bird.update();
  for (var i = obstacles.length - 1; i >= 0; i--) {
    obstacles[i].update();
    if (bird.intersects(obstacles[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  if (LK.ticks % 90 == 0) {
    spawnObstacle();
  }
  score = Math.min(score + 1, 999);
  scoreTxt.setText(score);
  if (score >= 999) {
    LK.showGameOver();
  }
}; /****
* Classes
****/
//<Write entity 'classes' with empty functions for important behavior here>
var Bird = Container.expand(function () {
  var self = Container.call(this);
  var birdGraphics = self.attachAsset('bird', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speedY = 0;
  self.gravity = 0.5;
  self.lift = -10;
  self.update = function () {
    self.speedY += self.gravity;
    self.y += self.speedY;
    if (self.y > 2732) {
      self.y = 2732;
      self.speedY = 0;
    }
    if (self.y < 0) {
      self.y = 0;
      self.speedY = 0;
    }
  };
  self.flap = function () {
    self.speedY = self.lift;
  };
});
var Obstacle = Container.expand(function () {
  var self = Container.call(this);
  var obstacleGraphics = self.attachAsset('obstacle', {
    anchorX: 0.5,
    anchorY: 0.5
  });
  self.speedX = -5;
  self.update = function () {
    self.x += self.speedX;
    if (self.x < -100) {
      self.destroy();
    }
  };
});
/****
* Initialize Game
****/
var game = new LK.Game({
  backgroundColor: 0x87ceeb //Init game with sky blue background 
});
/****
* Game Code
****/
//<Write game logic code here, including initializing arrays and variables>
//<Assets used in the game will automatically appear here>
var bird = game.addChild(new Bird());
bird.x = 2048 / 4;
bird.y = 2732 / 2;
var obstacles = [];
var score = 0;
var scoreTxt = new Text2('0', {
  size: 150,
  fill: "#ffffff"
});
scoreTxt.anchor.set(0.5, 0);
LK.gui.top.addChild(scoreTxt);
function spawnObstacle() {
  var gap = 300;
  var topHeight = Math.random() * (2732 - gap);
  var bottomHeight = 2732 - topHeight - gap;
  var topObstacle = new Obstacle();
  topObstacle.height = topHeight;
  topObstacle.x = 2048;
  topObstacle.y = topHeight / 2;
  obstacles.push(topObstacle);
  game.addChild(topObstacle);
  var bottomObstacle = new Obstacle();
  bottomObstacle.height = bottomHeight;
  bottomObstacle.x = 2048;
  bottomObstacle.y = 2732 - bottomHeight / 2;
  obstacles.push(bottomObstacle);
  game.addChild(bottomObstacle);
}
game.down = function (x, y, obj) {
  bird.flap();
};
game.update = function () {
  bird.update();
  for (var i = obstacles.length - 1; i >= 0; i--) {
    obstacles[i].update();
    if (bird.intersects(obstacles[i])) {
      LK.effects.flashScreen(0xff0000, 1000);
      LK.showGameOver();
    }
  }
  if (LK.ticks % 90 == 0) {
    spawnObstacle();
  }
  score = Math.min(score + 1, 999);
  scoreTxt.setText(score);
  if (score >= 999) {
    LK.showGameOver();
  }
};