|
var simonApp = angular.module('simonApp', []); |
|
|
|
simonApp.directive('simonButton', ['$timeout', 'simonService', function($timeout, simonService) { |
|
var template = ''; |
|
|
|
template += '<div id="' + '{{position}}' + '" class="col-xs-6">'; |
|
template += '<div class="button" ng-class="{\'unselected\':getSelectedButton() !== \'{{position}}\','; |
|
template += '\'clickable\': isEnabled() }" ng-click="isEnabled() ? setSelectedButton() : null"></div>'; |
|
template += '</div>'; |
|
|
|
return { |
|
restrict: 'E', |
|
replace: true, |
|
scope: { |
|
position: '@position' |
|
}, |
|
link: function(scope, element, attrs) { |
|
scope.getSelectedButton = function() { |
|
return simonService.getSelectedButton(); |
|
}; |
|
|
|
scope.setSelectedButton = function() { |
|
selectedButton = scope.position; |
|
simonService.addUserStep(scope.position); |
|
}; |
|
|
|
scope.isEnabled = function() { |
|
return simonService.isEnabled(); |
|
} |
|
}, |
|
template: template |
|
}; |
|
|
|
}]); |
|
|
|
simonApp.service('simonService', ['$rootScope', '$timeout', 'soundService', function($rootScope, $timeout, soundService) { |
|
var _pattern = [], |
|
_userPattern = [], |
|
_currentRound = 0, |
|
_buttonsEnabled = false, |
|
_selectedButton = '', |
|
_speed = 1000; |
|
|
|
var self = this; |
|
|
|
this.startRound = function() { |
|
$timeout(function() { |
|
_currentRound++; |
|
|
|
self.addStep(); |
|
|
|
self.disableButtons(); |
|
|
|
self.clearUserPattern(); |
|
|
|
self.showPattern(); |
|
}, 250); |
|
}; |
|
|
|
this.startGame = function() { |
|
self.resetGame(); |
|
self.startRound(); |
|
}; |
|
|
|
this.resetGame = function() { |
|
_pattern = []; |
|
_userPattern = []; |
|
_currentRound = 0; |
|
_buttonsEnabled = false; |
|
_selectedButton = ''; |
|
_speed = 1000; |
|
}; |
|
|
|
this.getRound = function() { |
|
return _currentRound; |
|
}; |
|
|
|
this.speedUp = function(speed) { |
|
_speed = _speed / 2; |
|
}; |
|
|
|
this.addUserStep = function(position) { |
|
_userPattern.push(position); |
|
|
|
soundService.play(position); |
|
|
|
self.disableButtons(); |
|
self.setSelectedButton(position); |
|
$timeout(function() { |
|
self.clearSelectedButton(); |
|
self.enableButtons(); |
|
soundService.stop(); |
|
$rootScope.$broadcast('userPatternChange'); |
|
}, 250); |
|
}; |
|
|
|
this.isUserPatternComplete = function() { |
|
return _userPattern.length === _currentRound; |
|
}; |
|
|
|
this.clearUserPattern = function() { |
|
_userPattern = []; |
|
}; |
|
|
|
this.addStep = function() { |
|
var element = Math.floor(Math.random() * 4 + 1); |
|
|
|
switch (element) { |
|
case 1: |
|
element = 'upper-left'; |
|
break; |
|
case 2: |
|
element = 'upper-right'; |
|
break; |
|
case 3: |
|
element = 'lower-left'; |
|
break; |
|
case 4: |
|
element = 'lower-right'; |
|
break; |
|
} |
|
|
|
_pattern.push(element); |
|
}; |
|
|
|
this.showPattern = function(index) { |
|
var index = index || 0; |
|
self.clearSelectedButton(); |
|
soundService.stop(); |
|
$timeout(function() { |
|
if (index < _pattern.length) { |
|
self.setSelectedButton(_pattern[index]); |
|
soundService.play(_pattern[index]); |
|
index = index + 1; |
|
$timeout(function() { |
|
self.showPattern(index); |
|
}, _speed); |
|
} else { |
|
$rootScope.$broadcast('patternGenerated'); |
|
} |
|
}, 250); |
|
}; |
|
|
|
this.checkPattern = function() { |
|
return _pattern[_userPattern.length - 1] === _userPattern[_userPattern.length - 1]; |
|
}; |
|
|
|
this.isEnabled = function() { |
|
return _buttonsEnabled; |
|
}; |
|
|
|
this.enableButtons = function() { |
|
_buttonsEnabled = true; |
|
}; |
|
|
|
this.disableButtons = function() { |
|
_buttonsEnabled = false; |
|
}; |
|
|
|
this.getSelectedButton = function() { |
|
return _selectedButton; |
|
}; |
|
|
|
this.setSelectedButton = function(btn) { |
|
_selectedButton = btn; |
|
}; |
|
|
|
this.clearSelectedButton = function() { |
|
self.setSelectedButton(''); |
|
}; |
|
|
|
}]); |
|
|
|
simonApp.factory('soundService', function() { |
|
var sound = { |
|
'upperLeft': new buzz.sound('https://s3.amazonaws.com/freecodecamp/simonSound1.mp3', { |
|
loop: false, |
|
preload: true |
|
}), |
|
'upperRight': new buzz.sound('https://s3.amazonaws.com/freecodecamp/simonSound2.mp3', { |
|
loop: false, |
|
preload: true |
|
}), |
|
'lowerLeft': new buzz.sound('https://s3.amazonaws.com/freecodecamp/simonSound3.mp3', { |
|
loop: false, |
|
preload: true |
|
}), |
|
'lowerRight': new buzz.sound('https://s3.amazonaws.com/freecodecamp/simonSound4.mp3', { |
|
loop: false, |
|
preload: true |
|
}), |
|
'play': function(position) { |
|
switch (position) { |
|
case 'upper-left': |
|
this.upperLeft.togglePlay(); |
|
break; |
|
case 'upper-right': |
|
this.upperRight.togglePlay(); |
|
break; |
|
case 'lower-left': |
|
this.upperLeft.togglePlay(); |
|
break; |
|
case 'lower-right': |
|
this.lowerRight.togglePlay(); |
|
break; |
|
} |
|
}, |
|
'playAll': function() { |
|
buzz.all().play(); |
|
}, |
|
'stop': function() { |
|
buzz.all().stop(); |
|
} |
|
}; |
|
|
|
return sound; |
|
}); |
|
|
|
simonApp.controller('MainController', ['$scope', '$timeout', 'simonService', 'soundService', function($scope, $timeout, simonService, soundService) { |
|
|
|
$scope.step = 0; |
|
|
|
$scope.message = ''; |
|
|
|
$scope.strict = false; |
|
|
|
$scope.stopDisabled = true; |
|
|
|
$scope.startGame = function() { |
|
$scope.message = 'Starting Game!'; |
|
$scope.gameOngoing = true; |
|
$scope.stopDisabled = true; |
|
simonService.disableButtons(); |
|
$timeout(function() { |
|
$scope.message = ''; |
|
simonService.startRound(); |
|
}, 1500); |
|
}; |
|
|
|
$scope.stopGame = function() { |
|
$scope.gameOngoing = false; |
|
$scope.stopDisabled = true; |
|
simonService.resetGame(); |
|
simonService.disableButtons(); |
|
}; |
|
|
|
$scope.$on('patternGenerated', function() { |
|
simonService.enableButtons(); |
|
$scope.stopDisabled = false; |
|
$scope.step = simonService.getRound(); |
|
}); |
|
|
|
$scope.$on('userPatternChange', function() { |
|
if (simonService.checkPattern()) { |
|
if (simonService.isUserPatternComplete()) { |
|
$scope.message = 'Correct!'; |
|
if (simonService.getRound() === 5 || simonService.getRound() === 9 || simonService.getRound() === 13) { |
|
simonService.speedUp(); |
|
} |
|
if (simonService.getRound() === 20) { |
|
$scope.message = 'Congratulations! You won!'; |
|
simonService.resetGame(); |
|
} |
|
$timeout(function() { |
|
$scope.message = ''; |
|
$scope.stopDisabled = true; |
|
simonService.startRound(); |
|
}, 1500); |
|
} |
|
|
|
} else { |
|
$scope.message = 'Wrong!'; |
|
simonService.clearUserPattern(); |
|
simonService.disableButtons(); |
|
$scope.stopDisabled = true; |
|
$timeout(function() { |
|
$scope.message = ''; |
|
if ($scope.strict) { |
|
simonService.resetGame(); |
|
simonService.startRound(); |
|
} else { |
|
simonService.showPattern() |
|
} |
|
}, 1500); |
|
} |
|
}); |
|
|
|
}]); |