SHARE
TWEET

Untitled

a guest Mar 20th, 2019 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function rnd(nMax) {
  2.     return Math.floor(Math.random() * nMax);
  3. }
  4.  
  5. const RENDERER_WIDTH = 800;
  6. const RENDERER_HEIGHT = 600;
  7.  
  8. const app = new PIXI.Application(
  9.   RENDERER_WIDTH,
  10.   RENDERER_HEIGHT,
  11.   {backgroundColor : 0x1099bb}
  12. );
  13. document.body.appendChild(app.view);
  14.  
  15. const INITIAL_BUNNIES_AMOUNT = 10;
  16. const bunnies = [];
  17.  
  18. function makeBunny(width = 20, height = 32) {
  19.     const bunny = PIXI.Sprite.fromImage('examples/assets/bunny.png');
  20.    
  21.     bunny.anchor.set(0.5);
  22.     bunny.width = width;
  23.     bunny.height = height;
  24.    
  25.    
  26.     bunny.body = new PIXI.Rectangle(0, 0, width, height);
  27.     bunny.velocity = new PIXI.Point(0, 0);
  28.  
  29.     bunnies.push(bunny);
  30.  
  31.     return bunny;
  32. }
  33.  
  34.  
  35. function initialize() {    
  36.     for (let i = 0; i < INITIAL_BUNNIES_AMOUNT; i++) {
  37.       const bunny = makeBunny();
  38.  
  39.       bunny.body.x = rnd(app.view.width);
  40.       bunny.body.y = rnd(app.view.height);
  41.       bunny.velocity = new PIXI.Point(rnd(10), rnd(10));
  42.  
  43.       app.stage.addChild(bunny);
  44.     }
  45. }
  46.  
  47. initialize();
  48.  
  49. app.ticker.add(function(delta) {
  50.     bunnies.forEach((b, i) => {
  51.         b.body.x += b.velocity.x;
  52.         b.body.y += b.velocity.y;
  53.      
  54.         const collisions = bunnies.filter((b2, j) => {
  55.             if (i === j || b2.checked) return;
  56.          
  57.             const bunnyA_x1 = b.body.x;
  58.             const bunnyA_x2 = bunnyA_x1 + b.body.width;
  59.             const bunnyA_y1 = b.body.y;
  60.             const bunnyA_y2 = bunnyA_y1 + b.body.height;
  61.          
  62.             const bunnyB_x1 = b2.body.x;
  63.             const bunnyB_x2 = bunnyB_x1 + b2.body.width;
  64.             const bunnyB_y1 = b2.body.y;
  65.             const bunnyB_y2 = bunnyB_y1 + b2.body.height;
  66.          
  67.             return bunnyA_x1 < bunnyB_x2
  68.                 && bunnyA_x2 > bunnyB_x1
  69.                 && bunnyA_y1 < bunnyB_y2
  70.                 && bunnyA_y2 > bunnyB_y1
  71.         });
  72.        
  73.         collisions.forEach(c => {
  74.             const velocity = new PIXI.Point(
  75.               b.velocity.x + c.velocity.x,
  76.               b.velocity.y + c.velocity.y
  77.             );
  78.          
  79.             b.velocity = velocity;
  80.             c.velocity = velocity;
  81.          
  82.             b.checked = c.checked = true;
  83.          
  84.         });
  85.      
  86.          if (b.body.x >= RENDERER_WIDTH || b.body.x <= 0) {
  87.           b.velocity.x *= -1;
  88.         }
  89.         if (b.body.y >= RENDERER_HEIGHT || b.body.y <= 0) {
  90.           b.velocity.y *= -1;
  91.         }
  92.        
  93.        
  94.  
  95.  
  96.         b.x = b.body.x;
  97.         b.y = b.body.y;
  98.     });
  99. });
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