kinderspiele1/www/spielsets/minispielset2/js/minispiel_controller.js

338 lines
6.8 KiB
JavaScript

appControllers.controller('Minispiel2', function($scope, $rootScope, $ionicPlatform, $cordovaMedia, backgroundsounds_mediahandler, $state, $timeout,$q, $http) {
/*
* Initialisierung
*/
$scope.spieleallebilder=[];
$scope.chunkedDataBilder = [];
$scope.chunkedDataBilderMask = [];
$scope.spielendecounter=4;
$scope.complete=false;
/*
* Spielset laden
*/
$http.get('spielsets/minispielset2/spielset.json').success(function(data) {
$scope.spieleallebilder = data;
bildermischen();
});
//$scope.$on("$ionicView.beforeEnter", function(event, data){
//});
$ionicPlatform.ready(function() {
/*
* Sounds definieren
*/
var completesound='';
var failsound='';
var setsound='';
var rotatesound='';
/*
* Spiele IntroSounds laden
* Typen: successsound -> Bravo, super, tollgemacht Sounds
* Typen: failuresound -> Leider daneben, versuchs nochmal
* Typen: cardsound -> Kartenlegegeräusch
* Typen: solutionsound -> Auflösungsgeräusch, das wäre das Tier gewesen
* Typen: gameoversound -> Spielzu ende Zeit für minispiel etc.
* Typen: backgroundmusic -> Hintergrundmusik
*/
$http.get('spielsets/minispielset2/systemsounds.json').success(function(data) {
for(var i = 0; i < data.length; i++) {
if (data[i].type=='setsound'){
$scope.setsound=data[i].sound;
}
else if (data[i].type=='failsound'){
$scope.failsound=data[i].sound;
}
else if (data[i].type=='completesound'){
$scope.completesound=data[i].sound;
}
else if (data[i].type=='rotatesound'){
$scope.rotatesound=data[i].sound;
}
else if (data[i].type=='backgroundmusic'){
//backgroundsounds_mediahandler.setbackgroundsoundfile(data[i].sound);
//backgroundsounds_mediahandler.playbackgroundmusic();
}
}//for-Schleife
});//$http
});
/*
* Drei mal mischen :)
*/
function bildermischen(){
$scope.spieleallebilder=shuffleArray($scope.spieleallebilder);
$scope.spieleallebilder=shuffleArray($scope.spieleallebilder);
$scope.spieleallebilder=shuffleArray($scope.spieleallebilder);
$scope.spiele=[];
for(var i = 0; i < 4; i++) {
var data= $scope.spieleallebilder[i];
data.id=i;
$scope.spiele.push(data);
}
$scope.chunkedDataBilder = chunk($scope.spiele, 1);
$scope.chunkedDataBilderMask = chunk($scope.spiele, 2);
}
/*
* Play-Sound
* playtype-> Bravo,Falsch oder Gamesound
* soundfile-> Pfad zur sounddatei
* callbackfunction-> Funktion die nach erfolgreichem abspielen gestarte wird
* volume-> Lautstärke z.B. 1.0
*/
function playsound(playtype, soundfile, callbackfunction, volume){
$ionicPlatform.ready(function() {
if (playtype=='setsound'){
try{
setsound.release();
}
catch(e){
//Soundfile Objekt gabs nicht oder Freigabefehler
console.log("Sounddatei nicht da");
}
setsound = new Media(soundfile, callbackfunction ,null);
setsound.play();
}
else if (playtype=='failsound'){
try{
//versuchen Soundfile freizugeben
failsound.release();
}
catch(e){
//Soundfile Objekt gabs nicht oder Freigabefehler
console.log("Sounddatei nicht da");
}
failsound = new Media(soundfile, callbackfunction ,null);
failsound.play();
}
else if (playtype=='rotatesound'){
try{
//versuchen Soundfile freizugeben
rotatesound.release();
}
catch(e){
//Soundfile Objekt gabs nicht oder Freigabefehler
console.log("Sounddatei nicht da");
}
rotatesound = new Media(soundfile, callbackfunction ,null);
rotatesound.play();
}
else if (playtype=='completesound'){
try{
//versuchen Soundfile freizugeben
completesound.release();
}
catch(e){
//Soundfile Objekt gabs nicht oder Freigabefehler
console.log("Sounddatei nicht da");
}
completesound = new Media(soundfile, callbackfunction ,null);
completesound.play();
}
});
}
$scope.rotateitem=function(id){
if($scope.complete==true){
for(var i = 0; i < $scope.spiele.length; i++) {
if (id==$scope.spiele[i].id){
$scope.spiele[i].rotate=true;
playsound('rotatesound', $scope.rotatesound, null, '1.0');
$timeout(function () { resetrotate(id); }, 4100);
}
}
}
}
function resetrotate(id){
for(var i = 0; i < $scope.spiele.length; i++) {
if (id==$scope.spiele[i].id){
$scope.spiele[i].rotate=false;
}
}
}
$scope.onDragComplete=function(data,evt){
for(var i = 0; i < $scope.spiele.length; i++) {
if (data.id==$scope.spiele[i].id){
$scope.spiele[i].dragged=false;
}
}
console.log("drag success, data:", data);
}
$scope.onDragStart=function(data,evt){
for(var i = 0; i < $scope.spiele.length; i++) {
if (data.id==$scope.spiele[i].id){
$scope.spiele[i].dragged=true;
}
}
console.log("drag start, data:", data);
}
$scope.onDragStop=function(data,evt){
playsound('failsound', $scope.failsound, null, '1.0');
for(var i = 0; i < $scope.spiele.length; i++) {
if (data.id==$scope.spiele[i].id){
$scope.spiele[i].dragged=false;
}
}
console.log("drag stop, data:", data);
}
$scope.onDropComplete=function(data,evt,dropid){
//richtige form erwischt
if (data.id==dropid){
playsound('setsound', $scope.setsound, null, '1.0');
for(var i = 0; i < $scope.spiele.length; i++) {
if (data.id==$scope.spiele[i].id){
$scope.spiele[i].display=false;
$scope.spiele[i].bildmaske=$scope.spiele[i].bild;
}
}
$scope.spielendecounter=$scope.spielendecounter - 1;
}
console.log("dropid: " + dropid + " drop success, data:", data);
if ($scope.spielendecounter==0){
$scope.complete=true;
playsound('completesound', $scope.completesound, null, '1.0');
$timeout(function () { $state.go('home'); }, 15000);
}
}
/*
* Zufallsgenerator um per Zufall neue Aufgabe definieren
*/
function getRandomInt(min, max) {
var tmpInt=0;
min = Math.ceil(min);
max = Math.floor(max);
tmpInt = Math.floor(Math.random() * (max - min + 1)) + min;
if (tmpInt > max){
tmpInt=max;
}
if (tmpInt < min){
tmpInt=min;
}
return tmpInt;
}
/*
* Bilder per Zufall mischen
*/
function shuffleArray(array) {
var m = array.length, t, i;
// While there remain elements to shuffle
while (m) {
// Pick a remaining element…
i = Math.floor(Math.random() * m--);
// And swap it with the current element.
t = array[m];
array[m] = array[i];
array[i] = t;
}
return array;
}
/*
* Bilder richtig in Zeilen ausgeben
*/
function chunk(arr, size) {
var newArr = [];
for (var i=0; i<arr.length; i+=size) {
newArr.push(arr.slice(i, i+size));
}
return newArr;
}
});