Advertisement
Guest User

Untitled

a guest
Aug 18th, 2013
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Player(){
  2.  
  3.     //========================
  4.     // Properties related networking
  5.     //======================== 
  6.     this.id = "1";
  7.  
  8.     //These are used in moving us around later
  9.     this.old_state = {};
  10.     this.cur_state = {};
  11.     this.state_time = new Date().getTime()
  12.  
  13.  
  14.     //Our local history of inputs
  15.     this.key_inputs = [];
  16.     this.inputs = [];
  17.  
  18.  
  19.     //========================
  20.     // Properties related to the world
  21.     //========================
  22.     this.playerspeed = 1;
  23.  
  24.     this.submap_size = 32;
  25.  
  26.     //view mao size
  27.     this.view_map_radius = 1
  28.  
  29.     //current surrounding buffer
  30.     this.view_map = { "xorigin": 0, "yorigin": 0, "xoffset" : 0, "xoffset" : 0,  "background" : [], "detail" : [] };
  31.     //surrounding buffer used fgor switching
  32.     this.buffer_view_map = { "xorigin": 0, "yorigin": 0, "xoffset" : 0, "xoffset" : 0,  "background" : [], "detail" : [] };
  33.  
  34.     //relative to teh visible view port
  35.     this.view_relative_pos = {
  36.         x : 0,
  37.         y : 0
  38.     };
  39.  
  40.     //absolute character position in the world
  41.     this.global_pos = {
  42.         x : 0,
  43.         y : 0
  44.     };
  45.  
  46.     //relative to the current submao
  47.     this.submap_pos = {
  48.         x : 0,
  49.         y : 0
  50.     };
  51.  
  52.     //submap as supplied by terrain generator
  53.     this.submap = {
  54.         x : 0,
  55.         y : 0
  56.     };
  57.  
  58.     //covertion functions
  59.     this.submap_to_global = function(submap, pos)
  60.     {
  61.         return {
  62.             x : this.submap.x * this.submap_size + this.submap_pos.x,
  63.             y : this.submap.y * this.submap_size + this.submap_pos.y
  64.         }      
  65.     }
  66.  
  67.     this.global_to_submap = function(pos)
  68.     {
  69.         return {
  70.             submap : {
  71.                 x : Math.floor(pos.x / this.submap_size),
  72.                 y : Math.floor(pos.y / this.submap_size),
  73.             },
  74.  
  75.             submap_pos : {
  76.                 x : (pos.x % this.submap_size),
  77.                 y : (pos.y % this.submap_size),
  78.             }
  79.         }      
  80.     }
  81.  
  82.  
  83.     // sets the current view map of the player
  84.     // stitches the surrounding submaps in one to be used for rendering
  85.     this.set_view_map = function(map)
  86.     {
  87.  
  88.         var n = this.view_map_radius;
  89.         for(var i = this.submap.x-n; i <= this.submap.x+n; i++)
  90.             for(var j = this.submap.y-n; j <= this.submap.y+n; j++)
  91.                 map.load_submap(i,j);
  92.        
  93.         for(var i = this.submap.x-n; i <= this.submap.x+n; i++){
  94.             for(var j = this.submap.y-n; j <= this.submap.y+n; j++){
  95.                 var smap = map.submaps[i][j]["map"]["content"];
  96.  
  97.                 //add backgrounds
  98.                 this.view_map["background"] = this.view_map["background"].concat(smap["background"]);
  99.                 //add detail
  100.                 this.view_map["detail"] = this.view_map["detail"].concat(smap["detail"]);
  101.  
  102.                
  103.             }
  104.         }
  105.  
  106.         this.view_map["xoffset"] = this.view_map["background"][0]["x"];
  107.         this.view_map["yoffset"] = this.view_map["background"][0]["y"];
  108.         this.view_map["xorigin"] = this.submap.x;
  109.         this.view_map["yorigin"] = this.submap.y;
  110.     }
  111.  
  112.     //create a buffered view map for the given coords
  113.     this.fill_buffer_view_map = function(map, new_submap_x, new_submap_y)
  114.     {
  115.  
  116.         var n = this.view_map_radius;
  117.         for(var i = new_submap_x-n; i <= new_submap_x-n+n; i++)
  118.             for(var j = new_submap_y-n; j <= new_submap_y+n; j++)
  119.                 map.load_submap(i,j);
  120.  
  121.  
  122.         for(var i = new_submap_x-n; i <= new_submap_x-n+n; i++){
  123.             for(var j = new_submap_y-n; j <= new_submap_y+n; j++){
  124.                 var smap = map.submaps[i][j]["map"]["content"];
  125.  
  126.                 //add backgrounds
  127.                 this.buffer_view_map["background"] = this.buffer_view_map["background"].concat(smap["background"]);
  128.                 //add detail
  129.                 this.buffer_view_map["detail"] = this.buffer_view_map["detail"].concat(smap["detail"]);
  130.  
  131.                
  132.             }
  133.         }
  134.  
  135.         this.buffer_view_map["xoffset"] = this.buffer_view_map["background"][0]["x"];
  136.         this.buffer_view_map["yoffset"] = this.buffer_view_map["background"][0]["y"];
  137.         this.buffer_view_map["xorigin"] = this.submap.x;
  138.         this.buffer_view_map["yorigin"] = this.submap.y;
  139.  
  140.     }
  141.  
  142.     this.switch_view_maps = function()
  143.     {
  144.         var t = this.view_map;
  145.         this.view_map = this.buffer_view_map;
  146.         this.buffer_view_map = t;
  147.         t = undefined;
  148.     }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement