<commit_before>var myLab = myLab || {};

(function (ns) {

  var stage;
  var canvas;

  var sphere;
  var hero;
  var buildings;

  // Init the stage
  var initStage = function (canvasId) {
    canvas = document.getElementById(canvasId);
    if (canvas) {
      stage = new createjs.Stage(canvas);
    }
  };

  // init  the application
  ns.init = function () {
    initStage('canvas-main');

    var baseUrl = 'assets/spaceGodzilla/';

    // create bg box
    var bg = new createjs.Bitmap(baseUrl + 'bg.png');
    bg.width = canvas.width;
    bg.height = canvas.height;
    stage.addChild(bg);

    // create sphere
    sphere = new ns.Sphere();
    stage.addChild(sphere);

    // create hero
    hero = new ns.Hero();
    stage.addChild(hero);

    createBuildings();

    resetGame();
    startGame();
  };

  // reset all game properties
  var resetGame = function () {
    sphere.reset(canvas);
    hero.reset(canvas);
  };

  // start game
  var startGame = function () {
    // register event handler for input
    document.onkeydown = onKeyDown;
    document.onkeyup = onKeyUp;

    // start the animation
    createjs.Ticker.setFPS(60);
    createjs.Ticker.addEventListener('tick', onTick);
    createjs.Ticker.useRAF = true;
  };

  // Handle key pressed down event
  var onKeyDown = function (event) {
    switch (event.keyCode) {
    case 37: // LEFT
      hero.move(-1);
      break;

    case 39: // RIGHT
      hero.move(1);
      break;
    }
  };

  // Handle key release event
  var onKeyUp = function (event) {
    switch (event.keyCode) {
    case 38: // UP
    case 37: // LEFT
    case 39: // RIGHT
    case 40: // DOWN
      hero.move(0);
      break;
    }
  };

  // create buildings to be destroyed
  var createBuildings = function () {
    // create sprite sheet
    var spriteSheet = new createjs.SpriteSheet({
      images: [ 'assets/spaceGodzilla/buildings.png' ],
      frames: [
        [ 33, 0, 27, 62, 0, 13, 42 ],
        [ 96, 0, 15, 62, 0, 8, 42 ],
        [ 79, 0, 15, 62, 0, 8, 42 ],
        [ 62, 0, 15, 64, 0, 8, 42 ],
        [ 0, 0, 31, 59, 0, 15, 39 ]
      ],
      animations: {
        // dummy animations
        tilemap: [ 0, 4 ]
      }
    });

    // generate several buildings using the same spritesheet
    buildings = [];
    var numOfCols = 15;
    var numOfRows = 6;
    var padding = 10;
    var col, row, posX, posY = 100, frameNum;
    var building;
    for (row = 0; row < numOfRows; row++) {
      posX = 25;
      for (col = 0; col < numOfCols; col++) {
        building = new createjs.BitmapAnimation(spriteSheet);
        building.x = posX;
        building.y = posY;
        building.hitRadius = 20;  // for sphere.hitTest()
        stage.addChild(building);

        // show different frame, randomly
        building.gotoAndStop(Math.floor(Math.random() * 5));

        buildings.push(building);

        posX += 20 + padding;
      }
      posY += 20 + padding;
    }
  };

  // handle collision between sphere and the building
  var handleBuildingCollision = function () {
    var sphereHit = false;
    var numOfBuildings = buildings.length;
    var i = 0;
    var building;
    while (!sphereHit && (i < numOfBuildings)) {
      building = buildings[i];

      if (sphere.hitTest(building)) {
        sphereHit = true;

        // remove the building
        buildings.splice(i, 1);
        stage.removeChild(building);

        // check which area the sphere hit the ball
        // source: http://stackoverflow.com/questions/3309617/calculating-degrees-between-2-points-with-inverse-y-axis
        var hitAngle = Math.atan2(sphere.y - building.y, building.x - sphere.x);
        if (hitAngle < 0) {
          hitAngle += 2 * Math.PI;
        }
        hitAngle = Math.floor(hitAngle * 180 / Math.PI);

        // calculate bounce direction
        var bounceX = -1, bounceY = -1;
        if ((hitAngle >= 315) && (hitAngle < 45)) {
            // from top
            bounceX = 1;
            bounceY = -1;
        } else if ((hitAngle >= 45) && (hitAngle < 135)) {
            // from left
            bounceX = -1;
            bounceY = 1;
        } else if ((hitAngle >= 135) && (hitAngle < 225)) {
            // from below
            bounceX = 1;
            bounceY = -1;
        } else if ((hitAngle >= 225) && (hitAngle < 315)) {
            // from right
            bounceX = -1;
            bounceY = 1;
        }

        // bounce the ball
        sphere.moveSpeedY *= bounceX;
        sphere.moveSpeedX *= bounceY;
      }

      // next building
      i++;
    }
  };

  // update all game objects
  var onTick = function (event) {
    var delta = event.delta * 0.001;

    sphere.update(delta);
    hero.update(delta);

    // detect collision between hero and the sphere
    if (sphere.hitTest(hero)) {
      if (sphere.moveSpeedY > 0) {
        // only bounce if ball is moving downward
        sphere.moveSpeedY *= -1;
      }
    }

    // detect collision between sphere and the buildings
    handleBuildingCollision();

    // draw stage
    stage.update();
  };

})(myLab);<commit_msg>Add note on bonuce improvement<commit_after>var myLab = myLab || {};

(function (ns) {

  var stage;
  var canvas;

  var sphere;
  var hero;
  var buildings;

  // Init the stage
  var initStage = function (canvasId) {
    canvas = document.getElementById(canvasId);
    if (canvas) {
      stage = new createjs.Stage(canvas);
    }
  };

  // init  the application
  ns.init = function () {
    initStage('canvas-main');

    var baseUrl = 'assets/spaceGodzilla/';

    // create bg box
    var bg = new createjs.Bitmap(baseUrl + 'bg.png');
    bg.width = canvas.width;
    bg.height = canvas.height;
    stage.addChild(bg);

    // create sphere
    sphere = new ns.Sphere();
    stage.addChild(sphere);

    // create hero
    hero = new ns.Hero();
    stage.addChild(hero);

    createBuildings();

    resetGame();
    startGame();
  };

  // reset all game properties
  var resetGame = function () {
    sphere.reset(canvas);
    hero.reset(canvas);
  };

  // start game
  var startGame = function () {
    // register event handler for input
    document.onkeydown = onKeyDown;
    document.onkeyup = onKeyUp;

    // start the animation
    createjs.Ticker.setFPS(60);
    createjs.Ticker.addEventListener('tick', onTick);
    createjs.Ticker.useRAF = true;
  };

  // Handle key pressed down event
  var onKeyDown = function (event) {
    switch (event.keyCode) {
    case 37: // LEFT
      hero.move(-1);
      break;

    case 39: // RIGHT
      hero.move(1);
      break;
    }
  };

  // Handle key release event
  var onKeyUp = function (event) {
    switch (event.keyCode) {
    case 38: // UP
    case 37: // LEFT
    case 39: // RIGHT
    case 40: // DOWN
      hero.move(0);
      break;
    }
  };

  // create buildings to be destroyed
  var createBuildings = function () {
    // create sprite sheet
    var spriteSheet = new createjs.SpriteSheet({
      images: [ 'assets/spaceGodzilla/buildings.png' ],
      frames: [
        [ 33, 0, 27, 62, 0, 13, 42 ],
        [ 96, 0, 15, 62, 0, 8, 42 ],
        [ 79, 0, 15, 62, 0, 8, 42 ],
        [ 62, 0, 15, 64, 0, 8, 42 ],
        [ 0, 0, 31, 59, 0, 15, 39 ]
      ],
      animations: {
        // dummy animations
        tilemap: [ 0, 4 ]
      }
    });

    // generate several buildings using the same spritesheet
    buildings = [];
    var numOfCols = 15;
    var numOfRows = 6;
    var padding = 10;
    var col, row, posX, posY = 100, frameNum;
    var building;
    for (row = 0; row < numOfRows; row++) {
      posX = 25;
      for (col = 0; col < numOfCols; col++) {
        building = new createjs.BitmapAnimation(spriteSheet);
        building.x = posX;
        building.y = posY;
        building.hitRadius = 20;  // for sphere.hitTest()
        stage.addChild(building);

        // show different frame, randomly
        building.gotoAndStop(Math.floor(Math.random() * 5));

        buildings.push(building);

        posX += 20 + padding;
      }
      posY += 20 + padding;
    }
  };

  // handle collision between sphere and the building
  var handleBuildingCollision = function () {
    var sphereHit = false;
    var numOfBuildings = buildings.length;
    var i = 0;
    var building;
    while (!sphereHit && (i < numOfBuildings)) {
      building = buildings[i];

      if (sphere.hitTest(building)) {
        sphereHit = true;

        // remove the building
        buildings.splice(i, 1);
        stage.removeChild(building);

        // check which area the sphere hit the ball
        // source: http://stackoverflow.com/questions/3309617/calculating-degrees-between-2-points-with-inverse-y-axis
        var hitAngle = Math.atan2(sphere.y - building.y, building.x - sphere.x);
        if (hitAngle < 0) {
          hitAngle += 2 * Math.PI;
        }
        hitAngle = Math.floor(hitAngle * 180 / Math.PI);

        // calculate bounce direction
        var bounceX = -1, bounceY = -1;
        if ((hitAngle >= 315) && (hitAngle < 45)) {
            // from top
            bounceX = 1;
            bounceY = -1;
        } else if ((hitAngle >= 45) && (hitAngle < 135)) {
            // from left
            bounceX = -1;
            bounceY = 1;
        } else if ((hitAngle >= 135) && (hitAngle < 225)) {
            // from below
            bounceX = 1;
            bounceY = -1;
        } else if ((hitAngle >= 225) && (hitAngle < 315)) {
            // from right
            bounceX = -1;
            bounceY = 1;
        }

        // bounce the ball
        sphere.moveSpeedY *= bounceX;
        sphere.moveSpeedX *= bounceY;
      }

      // next building
      i++;
    }
  };

  // update all game objects
  var onTick = function (event) {
    var delta = event.delta * 0.001;

    sphere.update(delta);
    hero.update(delta);

    // detect collision between hero and the sphere
    if (sphere.hitTest(hero)) {
      if (sphere.moveSpeedY > 0) {
        // only bounce if ball is moving downward
        // TODO better bouncing angle
        // http://gamedev.stackexchange.com/questions/4253/how-do-you-calculate-where-a-ball-should-go-when-it-bounces-off-the-bar
        sphere.moveSpeedY *= -1;
      }
    }

    // detect collision between sphere and the buildings
    handleBuildingCollision();

    // draw stage
    stage.update();
  };

})(myLab);
{'fraction_non_alphanumeric': 0.1301158301158301, 'fraction_numerical': 0.02895752895752896, 'mean_word_length': 1.882058414464534, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7415246', 'n_tokens_mistral': 3600, 'n_tokens_neox': 3203, 'n_words': 890}