Advertisement
Guest User

Modified joysticks

a guest
Oct 23rd, 2012
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ig.module(
  2.     'game.director.analog-stick'
  3. )
  4. .requires(
  5.     'impact.system'
  6. )
  7. .defines(function(){
  8.  
  9. ig.AnalogStick = ig.Class.extend({
  10.     stickSize: 30,
  11.     baseSize: 70,
  12.    
  13.     stickColor: 'rgba(255,255,255,0.3)',
  14.     baseColor: 'rgba(255,255,255,0.3)',
  15.     baseWidth: 3,
  16.     touchZoneStart: {x: 0, y: 0},
  17.     touchZoneSize: {x: 0, y: 0},
  18.    
  19.     pos: {x: 0, y: 0},
  20.     input: {x: 0, y: 0},
  21.     delta: {x: 0, y: 0},
  22.     pressed: false,
  23.    
  24.     angle: 0,
  25.     amount: 0,
  26.    
  27.     _touchId: null,
  28.    
  29.     init: function( startX, startY, width, height, baseSize, stickSize ) {
  30.         this.pos = {x: 0, y: 0};
  31.         this.baseSize = baseSize || this.baseSize;
  32.         this.stickSize = stickSize || this.stickSize;
  33.         this.max = this.baseSize - this.stickSize/3;
  34.        
  35.         this.touchZoneStart.x = startX;
  36.         this.touchZoneStart.y = startY;
  37.         this.touchZoneSize.x = width;
  38.         this.touchZoneSize.y = height;
  39.         document.addEventListener( 'touchstart', this.touchStart.bind(this), false );
  40.         document.addEventListener( 'touchmove', this.touchMove.bind(this), false );
  41.         document.addEventListener( 'touchend', this.touchEnd.bind(this), false );
  42.     },
  43.    
  44.     touchStart: function( ev ) {
  45.         ev.preventDefault();
  46.        
  47.         if( this.pressed ) { return; }
  48.         for( var i = 0; i < ev.touches.length; i++ ) {
  49.             var touch = ev.touches[i];
  50.             if (
  51.                 touch.pageX > this.touchZoneStart.x && touch.pageX < this.touchZoneStart.x + this.touchZoneSize.x &&
  52.                     touch.pageY > this.touchZoneStart.y && touch.pageY < this.touchZoneStart.y + this.touchZoneSize.y
  53.                 ) {
  54.                 this.pos.x = touch.pageX;
  55.                 this.pos.y = touch.pageY;
  56.                 var xd = this.pos.x - touch.pageX;
  57.                 var yd = this.pos.y - touch.pageY;
  58.                 if( Math.sqrt(xd*xd + yd*yd) < this.baseSize ) {
  59.                     this.pressed = true;
  60.                     this._touchId = touch.identifier;
  61.                     this._moved( touch );
  62.                     return;
  63.                 }
  64.             }
  65.         }
  66.     },
  67.    
  68.     touchMove: function( ev ) {
  69.         ev.preventDefault();
  70.        
  71.         for( var i = 0; i < ev.changedTouches.length; i++ ) {
  72.             if( ev.changedTouches[i].identifier == this._touchId ) {
  73.                 this._moved( ev.changedTouches[i] );
  74.                 return;
  75.             }
  76.         }
  77.     },
  78.    
  79.     _moved: function( touch ) {
  80.         var x = touch.pageX - this.pos.x;
  81.         var y = touch.pageY - this.pos.y;
  82.        
  83.         this.angle = Math.atan2(x, -y);
  84.         this.amount = Math.min( 1, Math.sqrt(x*x + y*y)/this.max );
  85.         this.input.x = Math.sin(this.angle) * this.amount;
  86.         this.input.y = -Math.cos(this.angle) * this.amount;
  87.         this.delta.x = touch.pageX - this.pos.x;
  88.         this.delta.y = touch.pageY - this.pos.y;
  89.     },
  90.    
  91.     touchEnd: function( ev ) {
  92.         // ev.preventDefault();
  93.        
  94.         ig.input.delayedKeyup['shoot'] = true;
  95.        
  96.         for( var i = 0; i < ev.changedTouches.length; i++ ) {
  97.             if( ev.changedTouches[i].identifier == this._touchId ) {
  98.                 this.pressed = false;
  99.                 this.input.x = 0;
  100.                 this.input.y = 0;
  101.                 this.amount = 0;
  102.                 this._touchId = null;
  103.                 this.delta.x = 0;
  104.                 this.delta.y = 0;
  105.                 return;
  106.             }
  107.         }
  108.     },
  109.    
  110.     draw: function() {
  111.         var ctx = ig.system.context;
  112.         ctx.beginPath();
  113.         if (this.touchZoneStart.x < ig.system.width / 2) {
  114.             ctx.arc(this.pos.x, this.pos.y, this.baseSize, 0, (Math.PI * 2), true);
  115.  
  116.             ctx.lineWidth = this.baseWidth;
  117.             ctx.strokeStyle = this.baseColor;
  118.             ctx.stroke();
  119.  
  120.             // Input
  121.             ctx.beginPath();
  122.             ctx.arc(
  123.                 this.pos.x + this.input.x * this.max,
  124.                 this.pos.y + this.input.y * this.max,
  125.                 this.stickSize, 0, (Math.PI * 2), true
  126.             );
  127.             ctx.fillStyle = this.stickColor;
  128.             ctx.fill();
  129.         }
  130.  
  131.         else if (this.touchZoneStart.x >= ig.system.width / 2) {
  132.             ctx.beginPath();
  133.             ctx.moveTo(this.pos.x, this.pos.y - 50);
  134.             ctx.lineTo(this.pos.x, this.pos.y + 50);
  135.             ctx.stroke();      
  136.            
  137.             var markerPos = {x: this.pos.x, y: this.delta.y + this.pos.y};
  138.             if (markerPos.y < this.pos.y - 50) {
  139.                 markerPos.y = this.pos.y - 50;
  140.             }
  141.             else if (markerPos.y > this.pos.y + 50) {
  142.                 markerPos.y = this.pos.y + 50;
  143.             }  
  144.             ctx.beginPath();   
  145.             ctx.arc(markerPos.x, markerPos.y, 20,0,2*Math.PI);
  146.             ctx.fill();
  147.         }
  148.     }
  149. });
  150.  
  151.  
  152. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement