Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. class Game
  4. {
  5.     preload()
  6.     {
  7.         game.load.image('water', 'assets/water.png');
  8.        
  9.         game.load.spritesheet('net', 'assets/net.png', 16, 16);
  10.     }
  11.    
  12.     create()
  13.     {
  14.         game.physics.enable(Phaser.Physics.ARCADE);
  15.    
  16.         this.obstacles = game.add.physicsGroup();
  17.         this.obstacles.create(240, 160, 'water');
  18.      
  19.         this.net = game.add.physicsGroup();
  20.        
  21.         game.input.addMoveCallback( (o) => this.updateGrid());
  22.        
  23.         this.createNet(10, 10);
  24.     }
  25.    
  26.    
  27.     createNet(width, height)
  28.     {
  29.         this.net.removeChildren();
  30.        
  31.         for(let i = 0; i < height; i++)
  32.         {
  33.             for( let j = 0; j < width; j++)
  34.             {
  35.                 this.net.create(j*16, i*16, 'net');
  36.             }
  37.         }
  38.        
  39.         this.net.forEach( o => o.anchor.set(0.5, 0.5));
  40.         this.net.setAll('alpha', '0.5');    
  41.     }
  42.    
  43.     updateGrid()
  44.     {
  45.         let x = game.input.activePointer.x ;
  46.         let y = game.input.activePointer.y;
  47.    
  48.         x -= (x-8) % 16;
  49.         y -= (y-8) % 16;
  50.        
  51.         if ( x != this.net.x || y != this.net.y )
  52.         {
  53.             this.net.x = x;
  54.             this.net.y = y;
  55.            
  56.             this.net.forEach(o => this.updateColor(o));
  57.         }
  58.     }
  59.    
  60.     updateColor(o)
  61.     {
  62.         o.frame = game.physics.arcade.overlap(o, this.obstacles ) ? 1 : 0;
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement