Advertisement
Guest User

actor.js

a guest
Jan 22nd, 2016
293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Load third
  2. //The Actor superclass! Create subsclasses for all of your actors
  3. function Actor(world, posX, posY, imgSRC, imgWidth, imgHeight) {
  4.   this.X = posX;
  5.   this.Y = posY;
  6.   this.world = world;
  7.   this.width = imgWidth;
  8.   this.height = imgHeight;
  9.   var img = new Image();
  10.   img.src = imgSRC;
  11.   img.crossOrigin = "anonymous";
  12.   var ready = false;
  13.   var precision = world.getCollsionPrecision();
  14.   var objectID = world.newObjectID();
  15.  
  16.   var context = world.getContext();
  17.   var collisionGrid;
  18.   img.onload = function(){
  19.     ready = true;
  20.   };
  21.   var hasGrid = false;
  22.  
  23.   //Overwrite this to chnage your Actors behavior
  24.   this.act = function() {
  25.  
  26.   }
  27.  
  28.   //returns the actors object id (this is used in world to identify entities)
  29.   this.getID = function() {
  30.     return objectID;
  31.   }
  32.  
  33.   //returns the actors collisiongrid
  34.   this.getCollisionGrid = function() {
  35.     return collisionGrid;
  36.   }
  37.  
  38.   //Draws the Actor into the world
  39.   this.draw = function() {
  40.     if (!ready) return;
  41.     if(ready && !hasGrid){
  42.       this.generateCollisionGrid();
  43.     }
  44.     context.drawImage(img, this.X, this.Y, this.width, this.height);
  45.   }
  46.  
  47.   //Interface, overwrite this function in your subclasses to handle collisions
  48.   /*** Most efficient implementation:
  49.  
  50.     if(opponendActor instanceof classToCheckForCollisions){
  51.       if(this.collidesWith(opponendActor)){
  52.         //Collision detected handle it as you like
  53.       }
  54.     }
  55.   */
  56.   this.checkCollision = function(opponendActor) {
  57.  
  58.   }
  59.  
  60.   //Collision detection, only use within checkCollision to avoid unnecessary calls
  61.   this.collidesWith = function(opponendActor) {
  62.     var collision = this.getCollision(opponendActor);
  63.     if (collision == null) return false;
  64.     var opponendGrid = opponendActor.getCollisionGrid();
  65.  
  66.     var oTop, oBottom, oLeft, oRight;
  67.     var top, bottom, left, right;
  68.     if (opponendActor.X > this.X) {
  69.       oLeft = 0;
  70.       left = parseInt((collision.minX - this.X) / precision);
  71.     } else {
  72.       left = 0;
  73.       oLeft = parseInt((collision.minX - opponendActor.X) / precision);
  74.     }
  75.  
  76.     if (opponendActor.Y > this.Y) {
  77.       oTop = 0;
  78.       top = parseInt((collision.minY - this.Y) / precision);
  79.     } else {
  80.       top = 0;
  81.       oTop = parseInt((collision.minY - opponendActor.Y) / precision);
  82.     }
  83.  
  84.     //run collision grids against each other
  85.     var xAmount = parseInt((collision.maxX - collision.minX) / precision);
  86.     var yAmount = parseInt((collision.maxY - collision.minY) / precision);
  87.  
  88.     for (var a = 0; a < xAmount; a++) {
  89.       for (var b = 0; b < yAmount; b++) {
  90.         if (collisionGrid[left + a][top + b] == true && opponendGrid[oLeft + a][oTop + b] == true) return true;
  91.       }
  92.     }
  93.     return false;
  94.   }
  95.  
  96.   //This function calculates the outer bounds of a collision and checks if the bounding box is hit
  97.   this.getCollision = function(opponend) {
  98.     if ((opponend.X > this.x + this.width) || (this.X > opponend.X + opponend.width) || (opponend.Y > this.Y + this.height) || (this.Y > opponend.Y + opponend.height)) return null;
  99.     var collision = new Collision();
  100.     collision.minX = parseInt(Math.max(this.X, opponend.X));
  101.     collision.minY = parseInt(Math.max(this.Y, opponend.Y));
  102.     collision.maxX = parseInt(Math.min(this.X + this.width, opponend.X + opponend.width));
  103.     collision.maxY = parseInt(Math.min(this.Y + this.height, opponend.Y + opponend.height));
  104.     return collision;
  105.   }
  106.  
  107.   this.jump = function() {
  108.     this.Y += 50;
  109.   }
  110.  
  111.   //this function partitions the image into a grid of smaller pieces
  112.   this.generateCollisionGrid = function() {
  113.     hasGrid = true;
  114.     var h = parseInt(this.height / precision);
  115.     var w = parseInt(this.width / precision);
  116.     var cnv = document.createElement('canvas');
  117.  
  118.     cnv.width = this.width;
  119.     cnv.height = this.height;
  120.     var ctx = cnv.getContext('2d');
  121.     //ctx.drawImage(img, this.X, this.Y, this.width, this.height);
  122.     ctx.clearRect(0, 0, this.width, this.height);
  123.     ctx.drawImage(img, 0, 0, this.width, this.height);
  124.     collisionGrid = new Array(w);
  125.     for (var i = 0; i < collisionGrid.length; i++) {
  126.       collisionGrid[i] = new Array(h);
  127.     }
  128.     for (var a = 0; a < w; a++) {
  129.       for (var b = 0; b < h; b++) {
  130.  
  131.         //var imgData = context.getImageData(a*precision, b*precision, precision, precision);
  132.         var data = context.getImageData(a * precision, b * precision, precision, precision).data;
  133.         collisionGrid[a][b] = false;
  134.         var hCount = 0;
  135.         var log = "";
  136.         for (var i = 0; i < data.length; i += 4) {
  137.           if (data[i + 3] > 0) {
  138.             //if(data[i+3] != 255) console.log(data[i+3]);
  139.             console.log(data[i] + " "+ data[i+1] + " " + data[i+2] + " " + data[i + 3]); //<---------------------------------------------------------- ALPHA IS ALWAYS 255 (fully opaque), even with a fully transparent image! Strange!
  140.             hCount++;
  141.             if (hCount >= parseInt(precision * precision * 0.2)) { //<-------------Change the last value to alter the minimum amount of intransparent pixels required to consider a partition as present
  142.               collisionGrid[a][b] = true;
  143.               break;
  144.             }
  145.           }
  146.         }
  147.  
  148.       }
  149.     }
  150.   }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement