Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var ZombieGame = function() {
- var _c1 = null; //canvas obj
- var _pinceau = null; //context obj
- var _map = {
- tile_size : 0,
- height : 0, // note : map goes from [0 to (height)], not [0 to (height-1)]
- width : 0, // same
- origin : {},
- safe_zone : 0, //minimal starting distance between the left of the map (player start point) and the ennemies
- player : {},
- ennemies : [],
- position_list : [] // for easy check when moving an unit
- };
- /* translate a map array coordinate position to its "physical" position on the canvas
- * (center of a tile)
- */
- this.x_to_pixels = function(x) {
- return _map.origin.x + ( x * _map.tile_size );
- }
- this.y_to_pixels = function(y) {
- return _map.origin.y - ( y * _map.tile_size );
- }
- this.coordinates_to_pos_code = function(x, y) {
- return pos_code = x + "-" + y
- }
- /*
- * clear the content of a map tile. Used when (re)moving units
- */
- this.clear_tile = function(x, y) {
- var top = this.y_to_pixels(y) - (_map.tile_size / 2) + 2
- var left = this.x_to_pixels(x) - (_map.tile_size / 2) + 2
- _pinceau.beginPath();
- _pinceau.clearRect(left, top, _map.tile_size - 4, _map.tile_size - 4);
- _pinceau.closePath();
- }
- /**
- * draw an unit on the map
- * type is either "player" or "ennemy"
- * */
- this.draw_unit = function(x, y, type) {
- _pinceau.beginPath();
- _pinceau.lineWidth=1;
- _pinceau.strokeStyle="black";
- _pinceau.arc( this.x_to_pixels(x), this.y_to_pixels(y), _map.tile_size / 3, 0, 2 * Math.PI );
- if(type == "player") {
- _pinceau.fillStyle = 'yellow';
- } else if(type == "ennemy") {
- _pinceau.fillStyle = 'red';
- } else { //boo, error, unrecognised type
- _pinceau.fillStyle = 'black';
- }
- _pinceau.fill();
- _pinceau.stroke(); //trace the circle contour
- _pinceau.closePath();
- }
- this.initialize_canvas = function(canvas_id, tile_size, safe_zone) {
- _c1 = document.getElementById(canvas_id);
- _pinceau = _c1.getContext("2d");
- //from tile_size, we get :
- //-height, width (in number of tiles). Total gets a -1 because coordinates starts at 0, not 1.
- _map.tile_size = tile_size;
- _map.height = Math.floor(_c1.height / _map.tile_size) - 1;
- _map.width = Math.floor(_c1.width / _map.tile_size) - 1;
- //-the origin point of the cartesian system (center of the most bottom-left tile)
- _map.origin.x = Math.floor(_map.tile_size / 2)
- _map.origin.y = _c1.height - Math.floor(_map.tile_size / 2)
- //no ennemy can can spawn within the (safe_zone) leftmost columns of the map.
- _map.safe_zone = safe_zone;
- }
- /**
- * this function "decides" the start point of the player (currently hardcoded) and of the various ennemies
- * a "safe zone" (the xx leftmost columns) prevents ennemies from spawing too close to the player
- */
- this.initialize_positions = function(nb_ennemies) {
- //=== set player position : leftmost column, middle (map coordinates starts at 0)
- _map.player.y = Math.round( _map.height / 2);
- _map.player.x = 0;
- //=== set ennemies position.
- //safety check : number of ennemies can't be higher than available tiles (if untested, this function can turn into an infinite loop)
- var max_ennemies = (_map.height + 1) * (_map.width + 1 - _map.safe_zone);
- if( nb_ennemies > max_ennemies ) {
- alert("initialize_positions() : parameter asks for more ennemies than tiles available !");
- return false;
- }
- for(var a = 1 ; a <= nb_ennemies ; a++) {
- var enm = {};
- var pos_code;
- do { //!\ indexOf() doesn't exist in IE8 and earlier (introduced JavaScript 1.6)
- enm.x = _map.safe_zone + Math.round( Math.random() * (_map.width - _map.safe_zone) );
- enm.y = Math.round( Math.random() * _map.height );
- 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
- } while( _map.position_list.indexOf(pos_code) != -1 );
- _map.position_list.push(pos_code);
- 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
- _map.ennemies.push(enm);
- }
- }
- /**
- * Draw map tiles + player & ennemy position.
- * Only called at game start, mid-game unit moves (for now) are made using clear_tile() and draw_unit()
- */
- this.draw_map = function() {
- _pinceau.beginPath();
- //lines
- for(var h = _c1.height ; h > 0 ; h -= _map.tile_size) {
- _pinceau.moveTo(0, h);
- _pinceau.lineTo(_c1.width, h);
- }
- //columns
- for(var w = _map.tile_size ; w < _c1.width ; w += _map.tile_size) {
- _pinceau.moveTo(w, 0);
- _pinceau.lineTo(w, _c1.height);
- }
- _pinceau.stroke();
- _pinceau.closePath();
- //player
- this.draw_unit(_map.player.x, _map.player.y, "player");
- //ennemies
- for (var a = 0 ; a < _map.ennemies.length ; a++ ) {
- this.draw_unit(_map.ennemies[a].x, _map.ennemies[a].y, "ennemy");
- }
- }
- //!\ currently incomplete ! coding in progress
- this.action = function(act) {
- var noise_propagation_distance = 0; //a player action makes noises that each zombie may detect (in nb of tiles)
- //player tries to shoot down an ennemy
- if(act == "fire") {
- //@TODO here check for the nearest zombie, and if in range, the shoot animation and action.
- //For simplicity, it's always the closest zombie that's shot
- noise_propagation_distance = 12; //LOUD BANG ! -- temp value, pending balancing
- } else { //player tries to move
- var new_x = _map.player.x;
- var new_y = _map.player.y;
- if( act == "up") {
- new_y += 1;
- } else if( act == "down") {
- new_y -= 1;
- } else if( act == "left") {
- new_x -= 1;
- } else if( act == "right") {
- new_x += 1;
- }
- //@TODO fonction testant la validité de la case/tuile de destination ici
- //-destination ne doit pas etre hors carte
- //-destination ne doit pas contenir un autre pion
- //si incorrect, return false immediat (pas de déplacement ennemi) pour pour que le player retente.
- //if ( ! this.is_a_valid_move(new_x, new_y)) {
- // return false;
- //}
- //update of the position list
- _map.position_list.splice( _map.position_list.indexOf( this.coordinates_to_pos_code(_map.player.x, _map.player.y) ), 1);
- _map.position_list.push( this.coordinates_to_pos_code(new_x, new_y) );
- this.draw_unit( new_x, new_y, "player");
- this.clear_tile(_map.player.x, _map.player.y);
- _map.player.x = new_x;
- _map.player.y = new_y;
- noise_propagation_distance = 3; //The player is no ninja. -- temp value, pending balancing
- }
- //@TODO this.update_zombie_noise_detection(noise_propagation_distance);
- //@TODO déplacements ennemis ici
- return false;
- }
- };
- var zz = new ZombieGame;
- zz.initialize_canvas("canv", 28, 7); //canvas_id, tile size (in pixels), safe zone (in nb of columns)
- zz.initialize_positions(9); //nb ennemies
- zz.draw_map();
- //the game uses the "player pressed a key" event to capture keyboard input and translate it to player action.
- document.onkeydown=function(e){
- e = e||window.event;
- var key = e.keyCode||e.which;
- if(key == 37) { // left arrow
- zz.action("left");
- } else if(key == 38) { //up arrow
- zz.action("up");
- } else if(key == 39) { //right arrow
- zz.action("right");
- } else if(key == 40) { //down arrow
- zz.action("down");
- } else if(key == 32) { //space key
- zz.action("fire");
- } else {
- //alert(key);
- return true; //event passes throught unaffected
- }
- return false; //stops event propagation
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement