570 lines
13 KiB
JavaScript
570 lines
13 KiB
JavaScript
appControllers.controller('Suchspiel', function($scope, $ionicPlatform, $cordovaMedia, backgroundsounds_mediahandler, $state, $timeout,$q, $http) {
|
|
|
|
|
|
/*
|
|
* Initialisierung
|
|
*/
|
|
|
|
//Mentor-Animation ausschalten
|
|
$scope.showmentor=false;
|
|
$scope.showmentorbravo=false;
|
|
|
|
//Kartenarray + ListenAnsichtArray
|
|
$scope.spiele=[];
|
|
$scope.spielealletiere=[];
|
|
$scope.spielelistensort=[];
|
|
|
|
//Zufallsgenerator ID = TierindexID
|
|
$scope.randomid=0;
|
|
$scope.spielrunden=0;
|
|
$scope.falscheantwort=0;
|
|
|
|
//Karten-Index zum geben / nehmen
|
|
$scope.cardindex=0;
|
|
|
|
//CSS-Steuerung (alias für addClass)
|
|
$scope.CSSErrorCode=99;
|
|
$scope.CSSAusblendung='';
|
|
$scope.CSSEinblendung='';
|
|
$scope.CSSFalscheAntwort = false;
|
|
$scope.CCSInittimer = false;
|
|
|
|
//SpieleTimer
|
|
$scope.Spielzeit = 90;
|
|
$scope.showgametimer = false;
|
|
|
|
//Spielzeit vorbei
|
|
$scope.spielvorbei=false;
|
|
$scope.beantwortet=false;
|
|
|
|
//Mentor Zufallseinblendung
|
|
$scope.randomMentor=0;
|
|
|
|
$scope.punkte=0;
|
|
$scope.punktebonus=0;
|
|
|
|
|
|
//Spiele sperren bis alle Karten ausgegeben sind
|
|
$scope.gamelock=true;
|
|
|
|
//Zeitmessung für PunkteBonus
|
|
$scope.rundenstart = '';
|
|
$scope.rundenende = '';
|
|
|
|
$ionicPlatform.ready(function() {
|
|
|
|
/*
|
|
* Sounds definieren
|
|
*/
|
|
//Globale MediaVariable fure Soundeffekte
|
|
var gamesound='';
|
|
var bravosound='';
|
|
var falschsound='';
|
|
|
|
$scope.bravosounds=[];
|
|
$scope.falschsounds=[];
|
|
$scope.aufloesung='';
|
|
|
|
/*
|
|
* Spielset laden
|
|
*/
|
|
$http.get('spielsets/spielset1/spielset.json').success(function(data) {
|
|
//console.log(data);
|
|
$scope.spielealletiere = data;
|
|
});
|
|
|
|
|
|
|
|
/*
|
|
* 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/spielset1/systemsounds.json').success(function(data) {
|
|
|
|
for(var i = 0; i < data.length; i++) {
|
|
|
|
if (data[i].type=='successsound'){
|
|
$scope.bravosounds.push(data[i].sound);
|
|
}
|
|
else if (data[i].type=='failuresound'){
|
|
$scope.falschsounds.push(data[i].sound);
|
|
}
|
|
else if (data[i].type=='cardsound'){
|
|
$scope.kartegebensound=data[i].sound;
|
|
}
|
|
else if (data[i].type=='solutionsound'){
|
|
$scope.aufloesung=data[i].sound;
|
|
}
|
|
else if (data[i].type=='gameoversound'){
|
|
$scope.spielendesound=data[i].sound;
|
|
}
|
|
else if (data[i].type=='backgroundmusic'){
|
|
backgroundsounds_mediahandler.setbackgroundsoundfile(data[i].sound);
|
|
backgroundsounds_mediahandler.playbackgroundmusic();
|
|
}
|
|
|
|
}//for-Schleife
|
|
|
|
});//$http
|
|
|
|
|
|
});
|
|
|
|
/*
|
|
* 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=='bravosound'){
|
|
|
|
try{
|
|
|
|
//versuchen Soundfile freizugeben
|
|
bravosound.release();
|
|
|
|
}
|
|
catch(e){
|
|
|
|
//Soundfile Objekt gabs nicht oder Freigabefehler
|
|
console.log("Sounddatei nicht da");
|
|
}
|
|
|
|
bravosound = new Media(soundfile, callbackfunction ,null);
|
|
bravosound.play();
|
|
|
|
}
|
|
else if (playtype=='falschsound'){
|
|
|
|
try{
|
|
|
|
//versuchen Soundfile freizugeben
|
|
falschsound.release();
|
|
|
|
}
|
|
catch(e){
|
|
|
|
//Soundfile Objekt gabs nicht oder Freigabefehler
|
|
console.log("Sounddatei nicht da");
|
|
}
|
|
|
|
falschsound = new Media(soundfile, callbackfunction ,null);
|
|
falschsound.play();
|
|
|
|
|
|
}
|
|
else if (playtype=='gamesound'){
|
|
|
|
try{
|
|
|
|
//versuchen Soundfile freizugeben
|
|
gamesound.release();
|
|
|
|
}
|
|
catch(e){
|
|
|
|
//Soundfile Objekt gabs nicht oder Freigabefehler
|
|
console.log("Sounddatei nicht da");
|
|
}
|
|
|
|
gamesound = new Media(soundfile, callbackfunction ,null);
|
|
gamesound.play();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
* Wiederholung der Aufgabe
|
|
*/
|
|
$scope.hilfe = function() {
|
|
console.log("Hilfe geklickt");
|
|
$scope.showmentor=true;
|
|
playsound('gamesound', $scope.spiele[$scope.randomid].sound, mentorausblenden,'1.0');
|
|
}
|
|
|
|
|
|
/*
|
|
* Mentor ausblenden, nach Soundausgabe
|
|
*/
|
|
function mentorausblenden(){
|
|
$timeout(function () { $scope.showmentor=false; $scope.cardindex=0}, 10);
|
|
}
|
|
|
|
|
|
/*
|
|
* Antwort prüfen
|
|
*/
|
|
$scope.PlayerAnswer = function(src) {
|
|
|
|
if ($scope.gamelock==false){
|
|
|
|
|
|
/*
|
|
* Einblende-effekte zurücksetzen
|
|
*/
|
|
$scope.CSSEinblendung='';
|
|
|
|
/*
|
|
* Prüfen ob schon einmal geklickt wurde
|
|
*/
|
|
if ($scope.beantwortet==false){
|
|
|
|
/*
|
|
* Antwort ist richtig
|
|
*/
|
|
$scope.beantwortet=true;
|
|
if (src==$scope.randomid){
|
|
|
|
$timeout(function () {
|
|
$scope.punkte=$scope.punkte + 1000;
|
|
$scope.rundenende = new Date().getTime();
|
|
|
|
//Zeitbonus
|
|
$scope.punktebonus=1000 / (($scope.rundenende - $scope.rundenstart)/1000);
|
|
console.log($scope.rundenende - $scope.rundenstart + " Start:" + $scope.rundenstart + " Ende:" + $scope.rundenende);
|
|
$scope.punktebonus=Math.round($scope.punktebonus);
|
|
|
|
$scope.punkte = $scope.punkte + $scope.punktebonus;
|
|
|
|
$scope.randomMentor=getRandomInt(1, 2);
|
|
|
|
if ($scope.randomMentor==2){
|
|
$scope.showmentorbravo=true;
|
|
}
|
|
else{
|
|
$scope.showmentor=true;
|
|
}
|
|
|
|
$scope.cardindex=0;
|
|
|
|
}, 0);//$timeoutfunction
|
|
|
|
/*
|
|
* Zufällig einen Bravosound wiedergeben lassen aus Bravosound-Array
|
|
*/
|
|
playsound('bravosound', $scope.bravosounds[getRandomInt(0, $scope.bravosounds.length - 1)], karteausblenden,'1.0');
|
|
|
|
}//If Abfrage ob antowrt richtig
|
|
else{
|
|
|
|
/*
|
|
* Antwort war dreimal falsch
|
|
*/
|
|
if ($scope.falscheantwort==2){
|
|
|
|
/*
|
|
* Richtige Karte pulsieren lassen
|
|
*/
|
|
$scope.CSSErrorCode=$scope.randomid;
|
|
$scope.CSSFalscheAntwort= false;
|
|
$scope.showmentor=true;
|
|
|
|
//Auflösungssound wiedergeben
|
|
playsound('gamesound', $scope.aufloesung, aufloesung, '1.0');
|
|
}//If Abfrage ob antowrt 3x falsch war
|
|
else {
|
|
/*
|
|
* Antwort ist falsch
|
|
*/
|
|
$scope.showmentor=true;
|
|
|
|
/*
|
|
* Zufällig einen Falschsound wiedergeben lassen aus Falschsound-Array
|
|
*/
|
|
playsound('falschsound', $scope.falschsounds[getRandomInt(0, $scope.falschsounds.length - 1)], mentorausblenden,'1.0');
|
|
|
|
$scope.CSSFalscheAntwort = true;
|
|
|
|
$timeout(function () {
|
|
$scope.CSSFalscheAntwort = false;
|
|
$scope.beantwortet=false;
|
|
$scope.falscheantwort=$scope.falscheantwort + 1
|
|
}, 1000); //$timeoutfunction
|
|
}//Else Abfrage ob antowrt 3x falsch war
|
|
}//Else Abfrage ob antowrt richtig
|
|
}//IFAbfrage ob Frage beantwortet wurde
|
|
}//Ifabfrage obSpielsperre aktiv ist
|
|
}
|
|
|
|
|
|
/*
|
|
* Neues Spiel starten
|
|
*/
|
|
function goto_newspiel(){
|
|
$scope.gamelock=true;
|
|
$scope.CSSErrorCode=99;
|
|
$scope.falscheantwort=0;
|
|
$scope.CSSEinblendung='';
|
|
$scope.cardindex=0;
|
|
|
|
kartenmischen();
|
|
|
|
kartegeben();
|
|
}
|
|
|
|
|
|
/*
|
|
* Karten austeilen
|
|
*/
|
|
function kartegeben(){
|
|
|
|
$timeout(function () {
|
|
$scope.spielelistensort[$scope.cardindex].display=true;
|
|
|
|
for(var i = 0; i < $scope.spiele.length; i++) {
|
|
var data= $scope.spiele[i];
|
|
if (data.id==$scope.spielelistensort[$scope.cardindex].id){
|
|
$scope.CSSEinblendung=data.einblendecssid;
|
|
console.log("Einblende Schleife ID: " + $scope.CSSEinblendung);
|
|
}
|
|
}
|
|
$scope.cardindex=$scope.cardindex + 1;
|
|
}, 0);
|
|
/*
|
|
* Interner Zähler bis alle Karten gegeben wurden
|
|
*/
|
|
if ($scope.cardindex < 7 ){
|
|
playsound('gamesound', $scope.kartegebensound, kartegeben,'1.0');
|
|
}
|
|
else{
|
|
$scope.CSSEinblendung='';
|
|
/*
|
|
* Zufallszahl generieren lassen
|
|
*/
|
|
$scope.randomid=getRandomInt(0, 7);
|
|
|
|
//Spiel-Eingabesperre entfernen
|
|
$scope.gamelock=false;
|
|
//Zeit messen
|
|
$scope.rundenstart = new Date().getTime();
|
|
/*
|
|
* Mentor anzeigen und Sound wiedergeben
|
|
*/
|
|
$scope.showmentor=true;
|
|
playsound('gamesound', $scope.spiele[$scope.randomid].sound, mentorausblenden,'1.0');
|
|
console.log("ELSE-Zweig Einblende -ID: " + $scope.CSSEinblendung);
|
|
console.log("CardIndex: " + $scope.cardindex);
|
|
console.log("Zufallsid: " + $scope.randomid);
|
|
console.log("Tiersuchen: " + $scope.spiele[$scope.randomid].bild);
|
|
}
|
|
console.log("Einblende funktion ID: " + $scope.CSSEinblendung);
|
|
}
|
|
|
|
/*
|
|
* Karten wieder einsammeln, runde beendet
|
|
*/
|
|
function karteausblenden(){
|
|
|
|
$timeout(function () { $scope.showmentorbravo=false; $scope.showmentor=false;}, 0);
|
|
|
|
if ($scope.spielvorbei==false){
|
|
|
|
if ($scope.cardindex < 8 ){
|
|
|
|
for(var i = 0; i < $scope.spiele.length; i++) {
|
|
var data= $scope.spiele[i];
|
|
if (data.id==$scope.spielelistensort[$scope.cardindex].id){
|
|
$scope.CSSAusblendung=data.ausblendecssid;
|
|
}
|
|
}
|
|
$timeout(function () {
|
|
$scope.spielelistensort[$scope.cardindex].display=false;
|
|
$scope.chunkedData = chunk($scope.spielelistensort, 4);
|
|
$scope.cardindex=$scope.cardindex + 1;
|
|
karteausblenden();
|
|
}, 500);
|
|
}
|
|
else{
|
|
$scope.beantwortet=false;
|
|
$scope.CSSAusblendung='';
|
|
goto_newspiel();
|
|
}
|
|
|
|
}
|
|
else{
|
|
spielende();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
* Spielende
|
|
*/
|
|
function spielende(){
|
|
|
|
//Belohnung hier rein
|
|
//$state.go('home');
|
|
|
|
$timeout(function () {
|
|
$scope.showmentorbravo=true;
|
|
//$scope.spielendesound.play();
|
|
playsound('gamesound', $scope.spielendesound, minispielintro,'1.0');
|
|
}, 10);
|
|
|
|
|
|
}
|
|
|
|
function minispielintro(){
|
|
$state.go('minispiel1');
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
* SpieleTimer Funktion
|
|
*/
|
|
function SpieleTimer(){
|
|
if ($scope.Spielzeit > 0){
|
|
|
|
$scope.Spielzeit=$scope.Spielzeit - 1;
|
|
$timeout(function () {
|
|
SpieleTimer();
|
|
}, 1000);
|
|
|
|
}
|
|
else{
|
|
$scope.spielvorbei=true;
|
|
}
|
|
}
|
|
|
|
function aufloesung(){
|
|
|
|
$scope.CSSErrorCode=99;
|
|
$scope.cardindex=0;
|
|
karteausblenden();
|
|
|
|
}
|
|
|
|
|
|
/*
|
|
* Drei mal mischen :)
|
|
*/
|
|
function kartenmischen(){
|
|
$scope.spielealletiere=shuffleArray($scope.spielealletiere);
|
|
$scope.spielealletiere=shuffleArray($scope.spielealletiere);
|
|
$scope.spielealletiere=shuffleArray($scope.spielealletiere);
|
|
|
|
$scope.spiele=[];
|
|
for(var i = 0; i < 8; i++) {
|
|
var data= $scope.spielealletiere[i];
|
|
data.id=i;
|
|
$scope.spiele.push(data);
|
|
}
|
|
|
|
|
|
$scope.spielelistensort=[];
|
|
//for(var i = 0; i < $scope.spiele.length; i++) {
|
|
for(var i = 0; i < 8; i++) {
|
|
var data= $scope.spiele[i];
|
|
data.sortid=i;
|
|
$scope.spielelistensort.push(data);
|
|
}
|
|
$scope.chunkedData = chunk($scope.spielelistensort, 4);
|
|
}
|
|
|
|
|
|
/*
|
|
* 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;
|
|
}
|
|
|
|
/*
|
|
* Karten per Zufall mischen
|
|
*/
|
|
var shuffleArray = function(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;
|
|
}
|
|
|
|
/*
|
|
* TierArray nach x Spalten Aufteilen (Darstellung)
|
|
*/
|
|
function chunk(arr, size) {
|
|
var newArr = [];
|
|
for (var i=0; i<arr.length; i+=size) {
|
|
newArr.push(arr.slice(i, i+size));
|
|
}
|
|
return newArr;
|
|
}
|
|
//$scope.chunkedData = chunk($scope.spiele, 4);
|
|
|
|
|
|
/*
|
|
* Initial - Neues Spiel starten (erste runde) von 3 runter zählen
|
|
*/
|
|
$scope.inittimercounter=3;
|
|
$scope.showinittimer=true;
|
|
$scope.CCSInittimer=true;
|
|
|
|
$timeout(function () {
|
|
$scope.CCSInittimer=false;
|
|
inittimer();
|
|
}, 1500);
|
|
|
|
|
|
|
|
function inittimer(){
|
|
$scope.CCSInittimer=true;
|
|
if ($scope.inittimercounter > 1){
|
|
$scope.inittimercounter=$scope.inittimercounter - 1;
|
|
|
|
$timeout(function () {
|
|
$scope.CCSInittimer=false;
|
|
inittimer();
|
|
}, 1000);
|
|
|
|
}
|
|
else{
|
|
$scope.inittimercounter='Los gehts!';
|
|
$timeout(function () {
|
|
$scope.showinittimer=false;
|
|
$scope.showgametimer = true;
|
|
SpieleTimer();
|
|
goto_newspiel();
|
|
}, 1000);
|
|
}
|
|
}
|
|
|
|
|
|
}); |