Guest User

Untitled

a guest
Jun 1st, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var player = {
  2.     color: "#ff0000",
  3.     x: 0,
  4.     y: 0,
  5.     width: 16,
  6.     height: 16,
  7.     speed: 4,
  8.     velX: 0,
  9.     velY: 0,
  10.     keys: [],
  11.     colliding: [],
  12.     LEFT: false,
  13.     RIGHT: false,
  14.     UP: false,
  15.     DOWN: false,
  16.     updateKeys: function() {
  17.         if(this.keys[68] || this.keys[39]) {
  18.             this.RIGHT = true;
  19.         } else {
  20.             this.RIGHT = false;
  21.         }
  22.        
  23.         if(this.keys[65] || this.keys[37]) {
  24.             this.LEFT = true;
  25.         } else {
  26.             this.LEFT = false;
  27.         }
  28.  
  29.         if(this.keys[87] || this.keys[38]) {
  30.             this.UP = true;
  31.         } else {
  32.             this.UP = false;
  33.         }
  34.  
  35.         if(this.keys[83] || this.keys[40]) {
  36.             this.DOWN = true;
  37.         } else {
  38.             this.DOWN = false;
  39.         }
  40.     },
  41.     update: function() {
  42.         if(this.RIGHT) {
  43.             this.velX = this.speed;
  44.         }
  45.        
  46.         if(this.LEFT) {
  47.             this.velX = -this.speed;
  48.         }
  49.  
  50.         if(this.UP) {
  51.             this.velY = -this.speed;
  52.         }
  53.  
  54.         if(this.DOWN) {
  55.             this.velY = this.speed;
  56.         }
  57.        
  58.         this.x += this.velX;
  59.         this.y += this.velY;
  60.        
  61.         if(this.y >= canvas_height - this.height) {
  62.             this.y = canvas_height - this.height;
  63.         } else if(this.y <= 0) {
  64.             this.y = 0;
  65.         }
  66.        
  67.         if (this.x >= canvas_width-this.width) {
  68.             this.x = canvas_width-this.width;
  69.         } else if (this.x <= 0) {
  70.             this.x = 0;
  71.         }
  72.        
  73.         this.velX = 0;
  74.         this.velY = 0;
  75.     },
  76.    
  77.     draw: function() {
  78.         context.fillStyle = "red";
  79.         context.fillRect(this.x, this.y, this.width, this.height);
  80.     },
  81.    
  82.     collides: function(block) {
  83.         var collide = (Math.abs(this.x - block.tile.x) * 2 < (this.width + block.tile.width)) && (Math.abs(this.y - block.tile.y) * 2 < (this.height + block.tile.height));
  84.         if(collide) {
  85.             if(!inArray(block.id, this.colliding)) {
  86.                 this.colliding.push(block.id);
  87.             }
  88.             this.handleCollision(block);
  89.         } else {
  90.             if(inArray(block.id, this.colliding)) {
  91.                 this.colliding = removeFromArray(block.id, this.colliding);
  92.             }
  93.         }
  94.         return false;
  95.     },
  96.  
  97.     handleCollision: function(block) {
  98.         // Approaching from left
  99.         if(this.x + this.width > block.tile.x) {
  100.             this.x = block.tile.x;
  101.         }
  102.        
  103.         // Approaching from right
  104.         if(this.x < block.tile.x + block.tile.width) {
  105.             this.x = block.tile.x + block.tile.width;
  106.         }
  107.        
  108.         // Approaching from top
  109.         if(this.y + this.height > block.tile.y) {
  110.             this.y = block.tile.y;
  111.         }
  112.        
  113.         // Approaching from bottom
  114.         if(this.y < block.tile.y + block.tile.height) {
  115.             this.y = block.tile.y + block.tile.height;
  116.         }
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment