Created
April 7, 2012 07:38
-
-
Save tonistiigi/2326275 to your computer and use it in GitHub Desktop.
Joystick control for LimeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goog.provide('remote.Joystick'); | |
goog.require('lime.Sprite'); | |
goog.require('lime.animation.FadeTo'); | |
goog.require('lime.Circle'); | |
goog.require('goog.events.Event'); | |
/** | |
Usage: | |
var joystick = new remote.Joystick().setSize(200, 200).setFill('#c00').setPosition(100, 100); | |
layer.appendChild(joystick); | |
goog.events.listen(joystick, 'impulse', function(e){ | |
var velocityX = e.v[0]; | |
var velocityY = e.v[1]; | |
}); | |
*/ | |
remote.Joystick = function(){ | |
lime.Sprite.call(this); | |
this.EASING = 0.2; | |
this.SPRING = 0.005; | |
this.FRICTION = 0.95; | |
this.LAG = 50; | |
this.RADIUS = 80; | |
this.control = new lime.Layer(); | |
this.mousePosition = this.control.getPosition().clone(); | |
this.realPosition = this.control.getPosition().clone(); | |
this.outer = new lime.Circle().setFill(0,0,100,.3).setSize(150,150); | |
this.inner = new lime.Circle().setFill(255,215,0).setSize(60,60); | |
this.control.appendChild(this.outer); | |
this.control.appendChild(this.inner); | |
this.appendChild(this.control); | |
this.control.setOpacity(0); | |
goog.events.listen(this,['mousedown','touchstart'],this.downHandler_,false,this); | |
lime.scheduleManager.schedule(this.draw_,this); | |
} | |
goog.inherits(remote.Joystick,lime.Sprite); | |
remote.Joystick.prototype.downHandler_ = function(e){ | |
e.swallow(['mousemove','touchmove'],this.moveHandler_); | |
e.swallow(['mouseup','touchend','touchcancel'],this.upHandler_); | |
this.control.setPosition(e.position.clone()); | |
this.mousePosition=(e.position.clone()); | |
this.realPosition=(e.position.clone()); | |
this.move(e); | |
this.control.runAction(new lime.animation.FadeTo(1).setDuration(.3)); | |
} | |
remote.Joystick.prototype.moveHandler_ = function(e){ | |
this.move(e); | |
} | |
remote.Joystick.prototype.upHandler_ = function(e){ | |
this.cancel(); | |
this.control.runAction(new lime.animation.FadeTo(0).setDuration(.3)); | |
} | |
remote.Joystick.prototype.move = function(e){ | |
var len1 = this.RADIUS; | |
var len2 = this.RADIUS*4; | |
var mp = goog.math.Vec2.difference(e.position,this.control.getPosition()); | |
var dt = goog.math.Vec2.dot(mp,mp); | |
if(dt>len2*len2) return this.cancel(); | |
if(dt>len1*len1) mp = mp.normalize().scale(len1); | |
this.mousePosition = goog.math.Coordinate.sum(mp,this.control.getPosition()); | |
} | |
remote.Joystick.prototype.cancel = function(){ | |
this.mousePosition = this.control.getPosition().clone(); | |
} | |
remote.Joystick.prototype.draw_ = function(){ | |
var delta = goog.math.Vec2.difference(this.mousePosition,this.realPosition); | |
var v = new goog.math.Vec2( | |
delta.x * Math.abs(delta.x) * (this.EASING / this.LAG), | |
delta.y * Math.abs(delta.y) * (this.EASING / this.LAG) | |
); | |
this.realPosition.x+=v.x; | |
this.realPosition.y+=v.y; | |
var pos = goog.math.Coordinate.difference(this.realPosition,this.control.getPosition()); | |
if(Math.abs(pos.x)>5 || Math.abs(pos.y)>5){ | |
var ev = new goog.events.Event('impulse'); | |
ev.v=[pos.x/this.RADIUS,pos.y/this.RADIUS]; | |
this.dispatchEvent(ev); | |
} | |
this.inner.setPosition(pos); | |
} | |
remote.Joystick.prototype.getValue = function(){ | |
var val = goog.math.Vec2.fromCoordinate(this.inner.getPosition()); | |
val.scale(1/this.RADIUS); | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment