2015-10-19 11:17:38 +02:00
|
|
|
H5P.MemoryGame = (function (EventDispatcher, $) {
|
2014-03-28 14:52:41 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-19 11:17:38 +02:00
|
|
|
* Memory Game Constructor
|
2014-03-28 14:52:41 +01:00
|
|
|
*
|
2015-10-19 11:17:38 +02:00
|
|
|
* @class
|
2014-03-28 14:52:41 +01:00
|
|
|
* @param {Object} parameters
|
|
|
|
* @param {Number} id
|
|
|
|
*/
|
2015-10-19 11:17:38 +02:00
|
|
|
function MemoryGame(parameters, id) {
|
2014-03-28 14:52:41 +01:00
|
|
|
var self = this;
|
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
// Initialize event inheritance
|
|
|
|
EventDispatcher.call(self);
|
2014-03-28 14:52:41 +01:00
|
|
|
|
2015-10-19 11:17:38 +02:00
|
|
|
var flipped, timer, counter, popup, $feedback;
|
|
|
|
var cards = [];
|
|
|
|
var removed = 0;
|
2014-03-28 14:52:41 +01:00
|
|
|
|
|
|
|
/**
|
2015-10-19 11:17:38 +02:00
|
|
|
* Check if these two cards belongs together.
|
2014-03-28 14:52:41 +01:00
|
|
|
*
|
2015-10-19 11:17:38 +02:00
|
|
|
* @private
|
|
|
|
* @param {H5P.MemoryGame.Card} card
|
|
|
|
* @param {H5P.MemoryGame.Card} mate
|
|
|
|
* @param {H5P.MemoryGame.Card} correct
|
2014-03-28 14:52:41 +01:00
|
|
|
*/
|
|
|
|
var check = function (card, mate, correct) {
|
|
|
|
if (mate === correct) {
|
|
|
|
// Remove them from the game.
|
|
|
|
card.remove();
|
|
|
|
mate.remove();
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
removed += 2;
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-09-16 14:33:14 +02:00
|
|
|
var finished = (removed === cards.length);
|
|
|
|
var desc = card.getDescription();
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2016-05-26 14:07:14 +02:00
|
|
|
if (finished) {
|
|
|
|
self.triggerXAPIScored(1, 1, 'completed');
|
|
|
|
}
|
|
|
|
|
2014-09-16 14:33:14 +02:00
|
|
|
if (desc !== undefined) {
|
|
|
|
// Pause timer and show desciption.
|
|
|
|
timer.stop();
|
2015-08-26 15:51:43 +02:00
|
|
|
popup.show(desc, card.getImage(), function () {
|
2014-09-16 14:33:14 +02:00
|
|
|
if (finished) {
|
|
|
|
// Game has finished
|
|
|
|
$feedback.addClass('h5p-show');
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Popup is closed, continue.
|
|
|
|
timer.start();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (finished) {
|
|
|
|
// Game has finished
|
2014-03-28 14:52:41 +01:00
|
|
|
timer.stop();
|
|
|
|
$feedback.addClass('h5p-show');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Flip them back
|
|
|
|
card.flipBack();
|
|
|
|
mate.flipBack();
|
|
|
|
}
|
2014-09-16 14:33:14 +02:00
|
|
|
};
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
/**
|
2015-10-19 11:17:38 +02:00
|
|
|
* Adds card to card list and set up a flip listener.
|
2014-03-28 14:52:41 +01:00
|
|
|
*
|
2015-10-19 11:17:38 +02:00
|
|
|
* @private
|
|
|
|
* @param {H5P.MemoryGame.Card} card
|
|
|
|
* @param {H5P.MemoryGame.Card} mate
|
2014-03-28 14:52:41 +01:00
|
|
|
*/
|
|
|
|
var addCard = function (card, mate) {
|
2015-10-19 11:17:38 +02:00
|
|
|
card.on('flip', function () {
|
2015-11-28 22:06:19 +01:00
|
|
|
self.triggerXAPI('interacted');
|
2014-03-28 14:52:41 +01:00
|
|
|
// Keep track of time spent
|
|
|
|
timer.start();
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
if (flipped !== undefined) {
|
|
|
|
var matie = flipped;
|
|
|
|
// Reset the flipped card.
|
|
|
|
flipped = undefined;
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
setTimeout(function () {
|
|
|
|
check(card, matie, mate);
|
|
|
|
}, 800);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Keep track of the flipped card.
|
|
|
|
flipped = card;
|
|
|
|
}
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
// Count number of cards turned
|
|
|
|
counter.increment();
|
|
|
|
});
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
cards.push(card);
|
|
|
|
};
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
// Initialize cards.
|
|
|
|
for (var i = 0; i < parameters.cards.length; i++) {
|
|
|
|
// Add two of each card
|
2015-10-19 11:17:38 +02:00
|
|
|
var cardOne = new MemoryGame.Card(parameters.cards[i], id);
|
|
|
|
var cardTwo = new MemoryGame.Card(parameters.cards[i], id);
|
2014-03-28 14:52:41 +01:00
|
|
|
addCard(cardOne, cardTwo);
|
|
|
|
addCard(cardTwo, cardOne);
|
|
|
|
}
|
|
|
|
H5P.shuffleArray(cards);
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
/**
|
2015-10-19 11:17:38 +02:00
|
|
|
* Attach this game's html to the given container.
|
2014-03-28 14:52:41 +01:00
|
|
|
*
|
2015-10-19 11:17:38 +02:00
|
|
|
* @param {H5P.jQuery} $container
|
2014-03-28 14:52:41 +01:00
|
|
|
*/
|
2015-10-19 11:17:38 +02:00
|
|
|
self.attach = function ($container) {
|
2015-11-28 22:06:19 +01:00
|
|
|
this.triggerXAPI('attempted');
|
2015-10-19 11:17:38 +02:00
|
|
|
// TODO: Only create on first!
|
2014-03-28 14:52:41 +01:00
|
|
|
$container.addClass('h5p-memory-game').html('');
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
// Add cards to list
|
|
|
|
var $list = $('<ul/>');
|
|
|
|
for (var i = 0; i < cards.length; i++) {
|
|
|
|
cards[i].appendTo($list);
|
|
|
|
}
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
if ($list.children().length) {
|
|
|
|
$list.appendTo($container);
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
$feedback = $('<div class="h5p-feedback">' + parameters.l10n.feedback + '</div>').appendTo($container);
|
2015-10-19 11:17:38 +02:00
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
// Add status bar
|
2015-10-19 11:17:38 +02:00
|
|
|
var $status = $('<dl class="h5p-status">' +
|
|
|
|
'<dt>' + parameters.l10n.timeSpent + '</dt>' +
|
|
|
|
'<dd class="h5p-time-spent">0:00</dd>' +
|
|
|
|
'<dt>' + parameters.l10n.cardTurns + '</dt>' +
|
|
|
|
'<dd class="h5p-card-turns">0</dd>' +
|
|
|
|
'</dl>').appendTo($container);
|
|
|
|
|
|
|
|
timer = new MemoryGame.Timer($status.find('.h5p-time-spent'));
|
|
|
|
counter = new MemoryGame.Counter($status.find('.h5p-card-turns'));
|
|
|
|
popup = new MemoryGame.Popup($container);
|
|
|
|
|
2014-09-16 14:33:14 +02:00
|
|
|
$container.click(function () {
|
|
|
|
popup.close();
|
|
|
|
});
|
2014-03-28 14:52:41 +01:00
|
|
|
}
|
|
|
|
};
|
2015-10-19 11:17:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Extends the event dispatcher
|
|
|
|
MemoryGame.prototype = Object.create(EventDispatcher.prototype);
|
|
|
|
MemoryGame.prototype.constructor = MemoryGame;
|
|
|
|
|
2014-03-28 14:52:41 +01:00
|
|
|
return MemoryGame;
|
2015-10-19 11:17:38 +02:00
|
|
|
})(H5P.EventDispatcher, H5P.jQuery);
|