daily pastebin goal
26%
SHARE
TWEET

Untitled

a guest Jul 23rd, 2013 229 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. var ZombieGame = function() {
  3.    var _c1 = null; //canvas obj
  4.    var _pinceau = null; //context obj
  5.    var _map = {
  6.       tile_size : 0,
  7.       height : 0, // note :  map goes from [0 to (height)], not [0 to (height-1)]
  8.       width : 0,  // same
  9.       origin : {},
  10.       safe_zone : 0, //minimal starting distance between the left of the map (player start point) and the ennemies
  11.       player : {},
  12.       ennemies : [],
  13.       position_list : [] // for easy check when moving an unit
  14.    };
  15.  
  16.    
  17.    /* translate a map array coordinate position to its "physical" position on the canvas
  18.     * (center of a tile)
  19.     */
  20.    this.x_to_pixels = function(x) {
  21.       return _map.origin.x + ( x * _map.tile_size );
  22.    }
  23.    this.y_to_pixels = function(y) {
  24.       return _map.origin.y - ( y * _map.tile_size );
  25.    }
  26.  
  27.    this.coordinates_to_pos_code = function(x, y) {
  28.       return pos_code = x + "-" + y
  29.      
  30.    }
  31.    
  32.    /*
  33.     * clear the content of a map tile. Used when (re)moving units
  34.     */
  35.    this.clear_tile = function(x, y) {
  36.       var top  = this.y_to_pixels(y) - (_map.tile_size / 2) + 2
  37.       var left = this.x_to_pixels(x) - (_map.tile_size / 2) + 2
  38.  
  39.       _pinceau.beginPath();
  40.       _pinceau.clearRect(left, top, _map.tile_size - 4, _map.tile_size - 4);
  41.       _pinceau.closePath();
  42.    }
  43.  
  44.  
  45.    /**
  46.     * draw an unit on the map
  47.     * type is either "player" or "ennemy"
  48.     * */
  49.    this.draw_unit = function(x, y, type) {
  50.       _pinceau.beginPath();
  51.  
  52.       _pinceau.lineWidth=1;
  53.       _pinceau.strokeStyle="black";
  54.  
  55.       _pinceau.arc( this.x_to_pixels(x), this.y_to_pixels(y), _map.tile_size / 3, 0, 2 * Math.PI );
  56.       if(type == "player") {
  57.          _pinceau.fillStyle = 'yellow';
  58.       } else if(type == "ennemy") {
  59.          _pinceau.fillStyle = 'red';
  60.       } else  { //boo, error, unrecognised type
  61.          _pinceau.fillStyle = 'black';
  62.       }
  63.       _pinceau.fill();
  64.  
  65.       _pinceau.stroke(); //trace the circle contour
  66.       _pinceau.closePath();
  67.    }
  68.  
  69.  
  70.    this.initialize_canvas = function(canvas_id, tile_size, safe_zone) {
  71.       _c1 = document.getElementById(canvas_id);
  72.       _pinceau = _c1.getContext("2d");
  73.  
  74.       //from tile_size, we get :
  75.       //-height, width (in number of tiles). Total gets a -1 because coordinates starts at 0, not 1.
  76.       _map.tile_size = tile_size;
  77.       _map.height = Math.floor(_c1.height / _map.tile_size) - 1;
  78.       _map.width  = Math.floor(_c1.width  / _map.tile_size) - 1;
  79.       //-the origin point of the cartesian system (center of the most bottom-left tile)
  80.       _map.origin.x = Math.floor(_map.tile_size / 2)
  81.       _map.origin.y = _c1.height - Math.floor(_map.tile_size / 2)
  82.      
  83.       //no ennemy can can spawn within the (safe_zone) leftmost columns of the map.
  84.       _map.safe_zone = safe_zone;
  85.    }
  86.  
  87.  
  88.    /**
  89.     * this function "decides" the start point of the player (currently hardcoded) and of the various ennemies
  90.     * a "safe zone" (the xx leftmost columns) prevents ennemies from spawing too close to the player
  91.     */
  92.    this.initialize_positions = function(nb_ennemies) {
  93.       //=== set player position : leftmost column, middle (map coordinates starts at 0)
  94.       _map.player.y = Math.round( _map.height / 2);
  95.       _map.player.x = 0;
  96.  
  97.       //=== set ennemies position.
  98.  
  99.       //safety check : number of ennemies can't be higher than available tiles (if untested, this function can turn into an infinite loop)
  100.       var max_ennemies = (_map.height + 1) * (_map.width + 1 - _map.safe_zone);
  101.       if( nb_ennemies > max_ennemies ) {
  102.          alert("initialize_positions() : parameter asks for more ennemies than tiles available !");
  103.          return false;
  104.       }
  105.      
  106.       for(var a = 1 ; a <= nb_ennemies ; a++) {
  107.          var enm = {};
  108.          var pos_code;
  109.          do { //!\ indexOf() doesn't exist in IE8 and earlier (introduced JavaScript 1.6)
  110.             enm.x = _map.safe_zone + Math.round( Math.random() * (_map.width - _map.safe_zone) );
  111.             enm.y = Math.round( Math.random() * _map.height );
  112.             pos_code = this.coordinates_to_pos_code(enm.x, enm.y) ; //this is an "unique" code stored to check wether there's already an ennemy here
  113.          } while( _map.position_list.indexOf(pos_code) != -1 );
  114.          
  115.          _map.position_list.push(pos_code);
  116.      
  117.          enm.destination = false; // this will contain a set of coordinates to head toward if a "noise" is detected by this unit. /!\ data format isn't fixed yet
  118.          _map.ennemies.push(enm);
  119.       }
  120.    }
  121.  
  122.    
  123.    /**
  124.    * Draw map tiles + player & ennemy position.
  125.    * Only called at game start, mid-game unit moves (for now) are made using clear_tile() and draw_unit()
  126.    */
  127.    this.draw_map = function() {
  128.       _pinceau.beginPath();
  129.       //lines
  130.       for(var h = _c1.height ; h > 0 ; h -= _map.tile_size) {
  131.          _pinceau.moveTo(0, h);
  132.          _pinceau.lineTo(_c1.width, h);
  133.       }
  134.       //columns
  135.       for(var w = _map.tile_size ; w < _c1.width ; w += _map.tile_size) {
  136.          _pinceau.moveTo(w, 0);
  137.          _pinceau.lineTo(w, _c1.height);
  138.       }
  139.       _pinceau.stroke();
  140.       _pinceau.closePath();
  141.  
  142.       //player
  143.       this.draw_unit(_map.player.x, _map.player.y, "player");
  144.  
  145.       //ennemies
  146.       for (var a = 0 ; a < _map.ennemies.length ; a++ ) {
  147.          this.draw_unit(_map.ennemies[a].x, _map.ennemies[a].y, "ennemy");
  148.       }
  149.    }
  150.  
  151.  
  152.    //!\ currently incomplete ! coding in progress
  153.    this.action = function(act) {
  154.       var noise_propagation_distance = 0; //a player action makes noises that each zombie may detect (in nb of tiles)
  155.      
  156.       //player tries to shoot down an ennemy
  157.       if(act == "fire") {
  158.          //@TODO here check for the nearest zombie, and if in range, the shoot animation and action.
  159.          //For simplicity, it's always the closest zombie that's shot
  160.  
  161.          noise_propagation_distance = 12; //LOUD BANG ! -- temp value, pending balancing
  162.       } else { //player tries to move
  163.          var new_x = _map.player.x;
  164.          var new_y = _map.player.y;
  165.  
  166.          if( act == "up") {
  167.             new_y += 1;
  168.          } else if( act == "down") {
  169.             new_y -= 1;
  170.          } else if( act == "left") {
  171.             new_x -= 1;
  172.          } else if( act == "right") {
  173.             new_x += 1;
  174.          }
  175.  
  176.          //@TODO fonction testant la validité de la case/tuile de destination ici
  177.          //-destination ne doit pas etre hors carte
  178.          //-destination ne doit pas contenir un autre pion
  179.          //si incorrect, return false immediat (pas de déplacement ennemi) pour pour que le player retente.
  180.          //if ( ! this.is_a_valid_move(new_x, new_y)) {
  181.          //   return false;
  182.          //}
  183.  
  184.          //update of the position list
  185.          _map.position_list.splice( _map.position_list.indexOf( this.coordinates_to_pos_code(_map.player.x, _map.player.y)  ), 1);
  186.          _map.position_list.push( this.coordinates_to_pos_code(new_x, new_y) );
  187.  
  188.          this.draw_unit( new_x, new_y, "player");
  189.          this.clear_tile(_map.player.x, _map.player.y);
  190.        
  191.          _map.player.x = new_x;
  192.          _map.player.y = new_y;
  193.  
  194.          noise_propagation_distance = 3; //The player is no ninja. -- temp value, pending balancing
  195.       }
  196.      
  197.       //@TODO  this.update_zombie_noise_detection(noise_propagation_distance);
  198.  
  199.       //@TODO déplacements ennemis ici
  200.      
  201.       return false;
  202.    }
  203. };
  204.  
  205.  
  206.  
  207.  
  208. var zz = new ZombieGame;
  209. zz.initialize_canvas("canv", 28, 7); //canvas_id, tile size (in pixels), safe zone (in nb of columns)
  210. zz.initialize_positions(9); //nb ennemies
  211. zz.draw_map();
  212.  
  213.  
  214.  
  215. //the game uses the "player pressed a key" event to capture keyboard input and translate it to player action.
  216. document.onkeydown=function(e){
  217.    e = e||window.event;
  218.    var key = e.keyCode||e.which;
  219.  
  220.    if(key == 37) { // left arrow
  221.       zz.action("left");
  222.    } else if(key == 38) { //up arrow
  223.       zz.action("up");
  224.    }  else if(key == 39) { //right arrow
  225.       zz.action("right");
  226.    }  else if(key == 40) { //down arrow
  227.       zz.action("down");
  228.    } else if(key == 32) { //space key
  229.       zz.action("fire");
  230.    } else {
  231.       //alert(key);
  232.       return true; //event passes throught unaffected
  233.    }
  234.    return false; //stops event propagation
  235. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top