Skip to content

Instantly share code, notes, and snippets.

@busticated
Created March 28, 2012 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save busticated/2223339 to your computer and use it in GitHub Desktop.
Save busticated/2223339 to your computer and use it in GitHub Desktop.
AMDWTF: So you want to AMD...
define( [ 'jquery', 'mods/crazydot' ], function( $, CrazyDot ){
var dots = [];
var setup = function( cfg ){
while ( dots.length < cfg.count ){
dots.push( new CrazyDot( cfg.speed, 'body' ).move() );
}
};
var stopGame = function(){
eachDot(function( dot ){
dot.stop();
});
};
var startGame = function(){
eachDot(function( dot ){
dot.start();
});
};
var setSpeed = function( spd ){
eachDot(function( dot ){
dot.speed = spd;
});
};
var eachDot = function( callback, ctx ){
for ( var i = 0, l = dots.length; i < l; i += 1 ){
callback.call( ctx, dots[ i ], i );
}
};
// public api /////////////////////////////////////////////////////////////
return {
setup : setup,
stopGame : stopGame,
startGame : startGame,
setSpeed : setSpeed
};
});
define({
speed: 1000,
count: 10
});
define( [ 'jquery' ], function( $ ){
var dotCount = 0;
var CrazyDot = function ( speed, target ){
var self = this;
dotCount += 1;
this.speed = speed;
this.$dot = $('<div class="dot">' + dotCount + '</div>').appendTo( target );
this.width = this.$dot.outerWidth(true);
this.height = this.$dot.outerHeight(true);
//click handler
this.$dot.click(function() {
if (!self.isPaused){
self.stop();
} else {
self.start();
}
});
};
CrazyDot.prototype = {
isPaused : false,
start : function start(){
this.isPaused = false;
this.$dot.removeClass('is-paused');
this.move();
return this;
},
stop : function(){
this.isPaused = true;
this.$dot.addClass('is-paused');
return this;
},
move : function move(){
var maxX = $(document).width(),
maxY = $(document).height(),
self = this;
this.$dot.animate({
top: Math.floor(Math.random() * ( (maxY - this.height) - 0 + 1)) + 0,
left: Math.floor(Math.random() * ( (maxX - this.width) - 0 + 1)) + 0
},
this.speed * (Math.random() + .25),
function(){
if (!self.isPaused) {
self.move();
}
});
return this;
}
};
// public api /////////////////////////////////////////////////////////////
return CrazyDot;
});
require.config({
paths : {
'jquery' : 'http://code.jquery.com/jquery-latest'
}
});
require( [ 'jquery', 'mods/arena', 'mods/configs' ], function( $, arena, configs ){
$( document ).ready(function(){
arena.setup( configs );
$('#js-controls').on( 'click', '.control', function( e ){
e.preventDefault();
switch ( e.currentTarget.hash.replace( '#', '' ) ){
case 'stop':
arena.stopGame();
break;
case 'start':
arena.startGame();
break;
case 'halfspeed':
arena.setSpeed( configs.speed * 2 );
break;
case 'fullspeed':
arena.setSpeed( configs.speed );
break;
case 'dblspeed':
arena.setSpeed( configs.speed / 2 );
break;
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment