(function(){
var game = this.colorQuestGame = this.colorQuestGame || {};
// composition module
game.compositionView = {
node: document.getElementById('your-composition'),
};
})();
Before the end of the gameScene.visualize method, we add the visualization logic for the player's composition:
// randomize the patterns array
patternsToShow.sort(function(a, b){
return Math.random() - 0.5;
});
// empty the current deck view
var deckNode = document.getElementById('deck');
deckNode.removeAllChildren();
// add the pattern to the deck view
for (var i in patternsToShow) {
var patternSlotNode = document.querySelector('#element-template .pattern-slot').cloneNode(/*clone children=*/true);
patternSlotNode.querySelector('.pattern').setAttribute('data-pattern', patternsToShow[i]);
deckNode.appendChild(patternSlotNode);
}
From the game.js file, we remove all the selected patterns before starting a new level:
We should have the following screenshot once this task is completed. The deck shows the patterns that a player needs to compose the quest:
Objective complete – mini debriefing
JavaScript comes with a sort method for the array. Normally, we compare the given two array elements and return +1 or -1 to control elements' swapping.
We can randomize an array by randomly returning either +1 or -1: