<commit_before>(function() {
  jslash.prefetchImg([ "../img/ranger_m.png", "../img/mine.png", "../img/bombas.png"]);

  var GAP = 16;
  var TXTPADDING = 10;
  
  var player, tileMap, canvas;
  var obstacles = [];
  var drawnObstacles = [];

  function createMine() {
    var obstacle = new jslash.Sprite(jslash.images['../img/mine.png']);
    obstacle.scale(0.5);
    var x = Math.random() * (tileMap.width * tileMap.tilewidth - obstacle.width() );
    var y = Math.random() * (tileMap.height * tileMap.tileheight - obstacle.height() );
    obstacle.absPosition = new jslash.Point(x,y);
    jslash.mix(obstacle,new jslash.behaviors.Collidable(jslash.Rectangle,'canvasRect'));
    obstacles.push(obstacle);
    return obstacle;
  }

  jslash.ready(function() {
    tileMap = new jslash.TiledTileset();
    tileMap.load("../tmx/run_background.json");
    canvas = new jslash.Canvas('canvas');
    canvas.width(400);
    canvas.height(600);
    var rgerImg = jslash.images['../img/ranger_m.png'];
    var frames = [ new jslash.Frame(rgerImg,new jslash.Rect(0,0,32,36)),
                   new jslash.Frame(rgerImg,new jslash.Rect(32,0,32,36)),
                   new jslash.Frame(rgerImg,new jslash.Rect(64,0,32,36)) ];
    
    player = new jslash.AnimatedSprite(frames);
    player.onrefresh = jslash.AnimatedSprite.makeRefresh(30);
    var bombImg = jslash.images['../img/bombas.png'];
    frames = [ new jslash.Frame(bombImg, new jslash.Rectangle(16, 0, 64-16, bombImg.height - 1)),
               new jslash.Frame(bombImg, new jslash.Rectangle(72, 8, 104-72, 32))];
    var explosionAnim = new jslash.AnimatedSprite(frames);
    explosionAnim.onrefresh = jslash.AnimatedSprite.makeRefresh(5);
    explosionAnim.canvasRect();
    
    jslash.mix(player, new jslash.behaviors.Collidable(jslash.Rect,'canvasRect'));

    var info = new jslash.Text("Use WASD to move the ranger!",0);
    info.color = 'white'; info.font = 'Verdana'; info.size = 16;
    info.y = canvas.height() - info.height() - TXTPADDING;
    info.weight = 'bold';

    speeds = { A: { x: -GAP, y : 0, scroll: function() { return tileMap.scrollLeft(canvas); } },
               S: { x: 0, y: GAP, scroll: function() { return tileMap.scrollDown(canvas); }},
               D: { x: GAP, y: 0, scroll: function() { return tileMap.scrollRight(canvas); }},
               W: { x: 0, y: -GAP, scroll: function() { return tileMap.scrollUp(canvas); }} };

    function makeSpeedFunc(key) {
      var speed = speeds[key];
      var makedFunc = function() {
        var p = player.position();
        p.x += speed.x;
        p.y += speed.y;
        var pp = tileMap.toAbsolute(new jslash.Point(p.x,p.y));
        var cr = jslash.deepcopy(player.canvasRect());
        cr.x = pp.x; cr.y = pp.y;
        if (tileMap.isFreeArea(cr)) {
          if (!speed.scroll() ) {
            player.position(p.x,p.y);
          }
        }
      };
      return makedFunc;
    }

    jslash.addKeyEvent(jslash.KEYS.A,makeSpeedFunc('A'));
    jslash.addKeyEvent(jslash.KEYS.D,makeSpeedFunc('D'));
    jslash.addKeyEvent(jslash.KEYS.S,makeSpeedFunc('S'));
    jslash.addKeyEvent(jslash.KEYS.W,makeSpeedFunc('W'));<commit_msg>Improved run demo scrolling<commit_after>(function() {
  jslash.prefetchImg([ "../img/ranger_m.png", "../img/mine.png", "../img/bombas.png"]);

  var GAP = 16;
  var TXTPADDING = 10;
  
  var player, tileMap, canvas;
  var obstacles = [];
  var drawnObstacles = [];

  function createMine() {
    var obstacle = new jslash.Sprite(jslash.images['../img/mine.png']);
    obstacle.scale(0.5);
    var x = Math.random() * (tileMap.width * tileMap.tilewidth - obstacle.width() );
    var y = Math.random() * (tileMap.height * tileMap.tileheight - obstacle.height() );
    obstacle.absPosition = new jslash.Point(x,y);
    jslash.mix(obstacle,new jslash.behaviors.Collidable(jslash.Rectangle,'canvasRect'));
    obstacles.push(obstacle);
    return obstacle;
  }

  jslash.ready(function() {
    tileMap = new jslash.TiledTileset();
    tileMap.load("../tmx/run_background.json");
    canvas = new jslash.Canvas('canvas');
    canvas.width(400);
    canvas.height(600);
    var rgerImg = jslash.images['../img/ranger_m.png'];
    var frames = [ new jslash.Frame(rgerImg,new jslash.Rect(0,0,32,36)),
                   new jslash.Frame(rgerImg,new jslash.Rect(32,0,32,36)),
                   new jslash.Frame(rgerImg,new jslash.Rect(64,0,32,36)) ];
    
    player = new jslash.AnimatedSprite(frames);
    player.onrefresh = jslash.AnimatedSprite.makeRefresh(30);
    var bombImg = jslash.images['../img/bombas.png'];
    frames = [ new jslash.Frame(bombImg, new jslash.Rectangle(16, 0, 64-16, bombImg.height - 1)),
               new jslash.Frame(bombImg, new jslash.Rectangle(72, 8, 104-72, 32))];
    var explosionAnim = new jslash.AnimatedSprite(frames);
    explosionAnim.onrefresh = jslash.AnimatedSprite.makeRefresh(5);
    explosionAnim.canvasRect();
    
    jslash.mix(player, new jslash.behaviors.Collidable(jslash.Rect,'canvasRect'));

    var info = new jslash.Text("Use WASD to move the ranger!",0);
    info.color = 'white'; info.font = 'Verdana'; info.size = 16;
    info.y = canvas.height() - info.height() - TXTPADDING;
    info.weight = 'bold';

    speeds = { A: { x: -GAP, y : 0, scroll: function() { return tileMap.scrollLeft(canvas); } },
               S: { x: 0, y: GAP, scroll: function() { return tileMap.scrollDown(canvas); }},
               D: { x: GAP, y: 0, scroll: function() { return tileMap.scrollRight(canvas); }},
               W: { x: 0, y: -GAP, scroll: function() { return tileMap.scrollUp(canvas); }} };

    function euclideanDistance(pt1,pt2) {
    	var x = pt1.x - pt2.x;
    	var y = pt1.y - pt2.y;
    	return Math.sqrt(x*x + y*y);
    }
    
    function isNearerPoint(before,after,refPoint) {
    	var dBefore = euclideanDistance(before,refPoint);
    	var dAfter = euclideanDistance(after, refPoint);
    	return dAfter < dBefore;
    }
    
    function makeSpeedFunc(key) {
      var speed = speeds[key];
      var makedFunc = function() {
        var p = player.position();
        var newPt = new jslash.Point(p.x + speed.x,p.y + speed.y);
        var pp = tileMap.toAbsolute(newPt);
        var cr = jslash.deepcopy(player.canvasRect());
        cr.x = pp.x; cr.y = pp.y;
        if (tileMap.isFreeArea(cr)) {
          if (isNearerPoint(p, newPt, canvas.center()) || !speed.scroll() ) {
            player.position(newPt.x,newPt.y);
          }
        }
      };
      return makedFunc;
    }

    jslash.addKeyEvent(jslash.KEYS.A,makeSpeedFunc('A'));
    jslash.addKeyEvent(jslash.KEYS.D,makeSpeedFunc('D'));
    jslash.addKeyEvent(jslash.KEYS.S,makeSpeedFunc('S'));
    jslash.addKeyEvent(jslash.KEYS.W,makeSpeedFunc('W'));
{'fraction_non_alphanumeric': 0.16389795312963512, 'fraction_numerical': 0.01868881637496292, 'mean_word_length': 2.922629435718441, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, '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': '2674693', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2361, 'n_words': 429}