-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_choose_word.js
104 lines (85 loc) · 3.34 KB
/
game_choose_word.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Games["choose_word"] = {
_currentWordIndex: 0,
_fiWord1: 0,//variant to choose 1 (index)
_fiWord2: 0,//variant to choose 2
// "words" : [] - here will be words
_elementEnWord : 0,
_elementFiWord1 : 0,
_elementFiWord2: 0,
_elementImg: 0,
_elementSound: 0,
"start" : function() {
console.log("Start game choose_word");
this._currentWordIndex = Math.floor(Math.random() * this.words.length);
this._elementEnWord = document.getElementById("choose_word_en");
this._elementFiWord1 = document.getElementById("choose_word_fi_1");
this._elementFiWord2 = document.getElementById("choose_word_fi_2");
this._elementImg = document.getElementById("choose_word_img");
this._elementSound = document.getElementById("choose_word_sound");
this.showCurrentWord();
var that = this;
this._elementFiWord1.onclick = function() {that.onWordSelected(1);}
this._elementFiWord2.onclick = function() {that.onWordSelected(2);}
},
"stop" : function() {
console.log("Stop game choose_word");
this._elementSound.src = "";
},
"onPrevious": function() {
this._currentWordIndex--;
if (this._currentWordIndex < 0) {
this._currentWordIndex = this.words.length - 1;
}
this.showCurrentWord();
},
"onNext": function() {
this._currentWordIndex++;
if (this._currentWordIndex >= this.words.length) {
this._currentWordIndex = 0;
}
this.showCurrentWord();
},
"onWordSelected": function(_wordIndex) {
console.log("Selected: " + _wordIndex);
let wordIndex = (_wordIndex == 1)?this._fiWord1:this._fiWord2;
if (this.words[wordIndex].fi === this.words[this._currentWordIndex].fi) {
this._elementSound.src = helpers.getAudioSrc(this.words[wordIndex]);
this._elementSound.play();
this.win(_wordIndex == 1?this._elementFiWord1:this._elementFiWord2);
} else {
this.lose(_wordIndex == 1?this._elementFiWord1:this._elementFiWord2)
}
},
"win" : function(element) {
element.style.color = "green";
var that = this;
setTimeout(function() {that.onNext()}, 2000);
},
"lose": function(element) {
element.style.color = "red";
},
// shows current word and 2 choices
"showCurrentWord" : function() {
if (Math.random() < 0.5) {
this._fiWord1 = this._currentWordIndex;
this._fiWord2 = this.getRandomWord(this._fiWord1);
} else {
this._fiWord2 = this._currentWordIndex;
this._fiWord1 = this.getRandomWord(this._fiWord2);
}
this._elementEnWord.textContent = this.words[this._currentWordIndex].en;
this._elementFiWord1.textContent = this.words[this._fiWord1].fi;
this._elementFiWord1.style.color = "black";
this._elementFiWord2.textContent = this.words[this._fiWord2].fi;
this._elementFiWord2.style.color = "black";
this._elementImg.src = helpers.getImgSrc(this.words[this._currentWordIndex]);
},
"getRandomWord": function(excludeWord) {
do {
let r = Math.floor(Math.random() * this.words.length);
if (r != excludeWord) {
return r;
}
} while(true);
}
}