From 07a649aefd49ea1ef321203937c3f5374327ea19 Mon Sep 17 00:00:00 2001 From: Frode Petterson Date: Fri, 28 Mar 2014 14:52:41 +0100 Subject: [PATCH] Initial commit. --- README.md | 16 +++ library.json | 21 ++++ memory-game.css | 150 ++++++++++++++++++++++++++ memory-game.js | 275 ++++++++++++++++++++++++++++++++++++++++++++++++ semantics.json | 41 ++++++++ 5 files changed, 503 insertions(+) create mode 100644 README.md create mode 100644 library.json create mode 100644 memory-game.css create mode 100644 memory-game.js create mode 100644 semantics.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a096c8 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +H5P Memory Game +========== + +Test your users with memory games. + +## License + +(The MIT License) + +Copyright (c) 2014 Amendor AS + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/library.json b/library.json new file mode 100644 index 0000000..aca5624 --- /dev/null +++ b/library.json @@ -0,0 +1,21 @@ +{ + "title": "Memory Game", + "description": "See how many cards you can remember!", + "majorVersion": 1, + "minorVersion": 0, + "patchVersion": 0, + "runnable": 1, + "author": "Amendor AS", + "license": "MIT", + "machineName": "H5P.MemoryGame", + "preloadedCss": [ + { + "path": "memory-game.css" + } + ], + "preloadedJs": [ + { + "path": "memory-game.js" + } + ] +} diff --git a/memory-game.css b/memory-game.css new file mode 100644 index 0000000..11f7424 --- /dev/null +++ b/memory-game.css @@ -0,0 +1,150 @@ +.h5p-memory-game { + overflow: hidden; +} +html .h5p-memory-game > ul { + list-style: none; + padding: 0; + margin: 0; +} +.h5p-memory-game .h5p-memory-card, +.h5p-memory-game .h5p-memory-card .h5p-back, +.h5p-memory-game .h5p-memory-card .h5p-front { + width: 100px; + height: 100px; + -webkit-user-select: none; + -moz-user-select: none; + -ms-user-select: none; + user-select: none; +} +.h5p-memory-game .h5p-memory-card .h5p-back > img { + -webkit-user-drag: none; +} +.h5p-memory-game .h5p-memory-card { + float: left; + outline: none; + position: relative; + margin: 1em; + -webkit-perspective: 400px; + -moz-perspective: 400px; + perspective: 400px; + -webkit-transform-style: preserve-3d; + -moz-transform-style: preserve-3d; + transform-style: preserve-3d; + -webkit-transition: opacity 0.4s; + -moz-transition: opacity 0.4s; + transition: opacity 0.4s; +} +.h5p-memory-game .h5p-memory-card .h5p-back, +.h5p-memory-game .h5p-memory-card .h5p-front { + position: absolute; + top: 0; + left: 0; + width: 100%; + height: 100%; + background: #cfcfcf; + border: 2px solid #d0d0d0; + box-sizing: border-box; + -moz-box-sizing: border-box; + border-radius: 4px; + -webkit-backface-visibility: hidden; + -moz-backface-visibility: hidden; + backface-visibility: hidden; + -webkit-transform: translateZ(0); + -moz-transform: translateZ(0); + transform: translateZ(0); + -webkit-transition: -webkit-transform 0.6s; + -moz-transition: -moz-transform 0.6s; + transition: transform 0.6s; + -webkit-transform-style: preserve-3d; + -moz-transform-style: preserve-3d; + transform-style: preserve-3d; +} +.h5p-memory-game .h5p-memory-card .h5p-front { + cursor: pointer; +} +.h5p-memory-game .h5p-memory-card .h5p-front:hover { + background: #dfdfdf; +} +.h5p-memory-game .h5p-memory-card .h5p-front:before { + position: absolute; + content: "?"; + font-size: 60px; + color: #909090; + line-height: 100px; + left: 35px; +} +.h5p-memory-game .h5p-memory-card .h5p-front:after { + content: ""; + display: block; + position: absolute; + top: 85%; + height: 50%; + width: 100%; + -webkit-transform: rotateX(90deg); + -moz-transform: rotateX(90deg); + transform: rotateX(90deg); + background-image: -webkit-radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0) 100%); + background-image: -moz-radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0) 100%); + background-image: radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.1) 0%, rgba(0, 0, 0, 0) 100%); +} +.h5p-memory-game .h5p-memory-card .h5p-back { + background: #f0f0f0; + -webkit-transform: rotateY(-180deg); + -moz-transform: rotateY(-180deg); + transform: rotateY(-180deg); + filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=2); + -ms-transform: scale(0,1.1); +} +.h5p-memory-game .h5p-memory-card.h5p-flipped .h5p-back { + -webkit-transform: rotateY(0deg); + -moz-transform: rotateY(0deg); + transform: rotateY(0deg); + -ms-transform: scale(1,1); +} +.h5p-memory-game .h5p-memory-card.h5p-flipped .h5p-front { + -webkit-transform: rotateY(180deg); + -moz-transform: rotateY(180deg); + -ms-transform: rotateY(180deg); + transform: rotateY(180deg); + -ms-transform: scale(0,1.1); +} +.h5p-memory-game .h5p-memory-card.h5p-matched { + opacity: 0.25; +} + +.h5p-memory-game .h5p-feedback { + clear: both; + float: right; + line-height: 1.5em; + font-size: 2em; + visibility: hidden; + -webkit-transform: scale(0,0) rotate(90deg); + -moz-transform: scale(0,0) rotate(90deg); + -ms-transform: scale(0,0) rotate(90deg); + transform: scale(0,0) rotate(90deg); + -webkit-transition: -webkit-transform 0.2s; + -moz-transition: -webkit-transform 0.2s; + transition: -webkit-transform 0.2s; +} +.h5p-memory-game .h5p-feedback.h5p-show { + visibility: visible; + -webkit-transform: scale(1,1) rotate(0deg); + -moz-transform: scale(1,1) rotate(0deg); + -ms-transform: scale(1,1) rotate(0deg); + transform: scale(1,1) rotate(0deg); +} + +.h5p-memory-game .h5p-status { + clear: left; +} +.h5p-memory-game .h5p-status > dt { + float: left; + margin-right: 1em; + font-weight: bold; +} +.h5p-memory-game .h5p-status > dt:after { + content: ":"; +} +.h5p-memory-game .h5p-status > dd { + margin: 0; +} diff --git a/memory-game.js b/memory-game.js new file mode 100644 index 0000000..6fe1ca5 --- /dev/null +++ b/memory-game.js @@ -0,0 +1,275 @@ +var H5P = H5P || {}; + +/** + * H5P Memory Game Module. + */ +H5P.MemoryGame = (function ($) { + + /** + * Memory Card Constructor + * + * @param {Object} parameters + * @param {Number} id + */ + function MemoryCard(parameters, id) { + var self = this; + var path = H5P.getPath(parameters.path, id); + var width, height, margin, $card; + + var a = 96; + if (parameters.width !== undefined && parameters.height !== undefined) { + if (parameters.width > parameters.height) { + width = a; + height = parameters.height * (width / parameters.width); + margin = '' + ((a - height) / 2) + 'px 0 0 0'; + } + else { + height = a; + width = parameters.width * (height / parameters.height); + margin = '0 0 0 ' + ((a - width) / 2) + 'px'; + } + } + else { + width = height = a; + } + + // Public jQuery wrapper. + this.$ = $(this); + + /** + * Public. Flip card. + */ + this.flip = function () { + $card.addClass('h5p-flipped'); + }; + + /** + * Public. Flip card back. + */ + this.flipBack = function () { + $card.removeClass('h5p-flipped'); + } + + /** + * Public. Remove. + */ + this.remove = function () { + $card.addClass('h5p-matched'); + } + + /** + * Public. Append card to the given container. + * + * @param {jQuery} $container + */ + this.appendTo = function ($container) { + $card = $('
  • \ +
    \ +
    \ + Memory Card\ +
    \ +
  • ') + .appendTo($container) + .children('.h5p-front') + .click(function () { + self.$.trigger('flip'); + }) + .end(); + }; + }; + + function MemoryTimer($container) { + var interval, started; + + /** + * Private. Make timer more readable for humans. + * + * @param {Number} seconds + * @returns {String} + */ + var humanizeTime = function (seconds) { + var minutes = Math.floor(seconds / 60); + var hours = Math.floor(minutes / 60); + + minutes = minutes % 60; + seconds = Math.floor(seconds % 60); + + var time = ''; + + if (hours !== 0) { + time += hours + ':'; + + if (minutes < 10) { + time += '0'; + } + } + + time += minutes + ':'; + + if (seconds < 10) { + time += '0'; + } + + time += seconds; + + return time; + }; + + /** + * Private. Update the timer element. + */ + var update = function () { + $container.text(humanizeTime(Math.floor(((new Date()).getTime() - started) / 1000))); + }; + + /** + * Public. Starts the counter. + */ + this.start = function () { + if (interval === undefined) { + started = new Date(); + + interval = setInterval(function () { + update(); + }, 1000); + } + }; + + /** + * Public. Stops the counter. + */ + this.stop = function () { + if (interval !== undefined) { + clearInterval(interval); + update(); + } + }; + }; + + /** + * Memory Counter Constructor + * + * @param {jQuery} $container + */ + function MemoryCounter($container) { + var current = 0; + + this.increment = function () { + current++; + $container.text(current); + }; + } + + /** + * Memory Game Constructor + * + * @param {Object} parameters + * @param {Number} id + */ + function MemoryGame(parameters, id) { + var flipped, timer, counter, $feedback, cards = [], removed = 0; + + /** + * Private. Check if these two cards belongs together. + * + * @param {MemoryCard} card + * @param {MemoryCard} mate + * @param {MemoryCard} correct + */ + var check = function (card, mate, correct) { + if (mate === correct) { + // Remove them from the game. + card.remove(); + mate.remove(); + + removed += 2; + + if (removed === cards.length) { + timer.stop(); + $feedback.addClass('h5p-show'); + } + } + else { + // Flip them back + card.flipBack(); + mate.flipBack(); + } + } + + /** + * Private. Adds card to card list and set up a flip listener. + * + * @param {MemoryCard} card + * @param {MemoryCard} mate + */ + var addCard = function (card, mate) { + card.$.on('flip', function () { + // Keep track of time spent + timer.start(); + + if (flipped !== undefined) { + var matie = flipped; + // Reset the flipped card. + flipped = undefined; + + setTimeout(function () { + check(card, matie, mate); + }, 800); + } + else { + // Keep track of the flipped card. + flipped = card; + } + + // Count number of cards turned + counter.increment(); + }); + + cards.push(card); + }; + + // Initialize cards. + for (var i = 0; i < parameters.cards.length; i++) { + // Add two of each card + var cardOne = new MemoryCard(parameters.cards[i], id); + var cardTwo = new MemoryCard(parameters.cards[i], id); + addCard(cardOne, cardTwo); + addCard(cardTwo, cardOne); + } + H5P.shuffleArray(cards); + + /** + * Public. Attach this game's html to the given container. + * + * @param {jQuery} $container + */ + this.attach = function ($container) { + $container.addClass('h5p-memory-game').html(''); + + // Add cards to list + var $list = $('