Esempio n. 1
0
  /**
   * renderEntities - called by the render function and each game tick
   */
  function renderEntities() {

    /* Render Enemies
     * Loop through all of the objects within the allEnemies array and call
     * the render method.
     */
    Game.allEnemies.forEach(function(enemy) {
      enemy.render();
    });

    /* Render player
     * Renders the player on the canvas.
     */
    Game.player.render();

    /* Render gems
     * Loop through all of the objects within the allGems array and call
     * the render method.
     */
    Game.allGems.forEach(function(gem) {
    	gem.render();
    });

    /* Render stats
     * Renders the stat panel and containing elements at top of canvas
     */
    Game.stats.render();

  }
Esempio n. 2
0
  /**
   * checkCollisions - determines if game entities have collided
   * @return {[type]} [description]
   */
  function checkCollisions() {

		/**
		* Check for the collision of two entities.
		* Function accepts two arguments.
		*/

    /**
     * collision
     * @param  {string} a - entity a
     * @param  {string} b - entity b
     * @return {boolean}
     */
		function collision(a, b) {
		  return a.x < b.x + b.width &&
		         a.x + a.width > b.x &&
		         a.y < b.y + b.height &&
		         a.y + a.height > b.y;
		}

  	/* Check enemy collisions.
  	 * If there is a collision, reset the player's position
  	 * and update the players lives or reset the game.
  	 */
  	Game.allEnemies.forEach(function(enemy) {

			if (collision(Game.player, enemy)) {

				/* Reset the players position.
				 */
				Game.player.hit();

				/* If the player has more than one life remaining,
				 * call the player.updateLives method and remove a life.
				 * If the player has no more lives remaining, reset the game.
				 */
				return Game.player.lives > 1 ? Game.player.updateLives('remove', 1) : Game.level.reset();

			}

  	});

  	/* Check gem collisions.
  	 * If there is a collision, call the gem.clear() method to
  	 * clear the gem from the canvas and call the stats.updateGems
  	 * to update the gems count and increase the score by 300 points.
  	 */
  	Game.allGems.forEach(function(gem) {

    	if (collision(Game.player, gem)) {

	    	gem.clear();

	    	Game.stats.updateGems();

    	}

  	});

  	/* Check goal collisions.
  	 * If the player gets to the other side, update the level
  	 */
  	if (Game.player.y == 70) {

				Game.level.update();

		}

  }