Skip to content

Instantly share code, notes, and snippets.

@biovisualize
Created November 6, 2016 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biovisualize/564b1f8eeca68b4105430f255b9798b7 to your computer and use it in GitHub Desktop.
Save biovisualize/564b1f8eeca68b4105430f255b9798b7 to your computer and use it in GitHub Desktop.
Minimal quest framework
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
.view-container {
min-height: 300px;
border: 1px solid black;
padding: 20px;
font-size: 18px;
background-color: black;
color: white;
}
.widget-container {
padding: 20px;
}
</style>
</head>
<body>
<div class="view-container">
<div class="info-container"></div>
</div>
<div class="widget-container">
<div class="widget-choices"></div>
</div>
<script>
var infoContainer = document.querySelector('.info-container');
var widgetMessage = document.querySelector('.widget-message');
var widgetChoices = document.querySelector('.widget-choices');
var etats = {
chambreDepart: {
description: 'Vous vous réveillez dans une chambre obscure.',
choix: [
{
prochainEtat: 'chambreClaire',
transition: 'ouvrirPorteRouge'
},
{
prochainEtat: 'chambreSombre',
transition: 'ouvrirPorteBleue'
}
]
},
chambreClaire: {
description: 'Vous êtes dans la chambre claire.',
choix: [
{
prochainEtat: 'lectureLivre',
transition: 'prendreLivre'
}
]
},
chambreSombre: {
description: 'Il fait trop sombre. Vous êtes mort. Pour rien.',
choix: []
},
lectureLivre: {
description: 'Le livre est empoisonné. Vous êtes mort.',
choix: []
}
};
var transitions = {
ouvrirPorteRouge: {
description: 'Vous ouvrez la porte rouge.'
},
ouvrirPorteBleue: {
description: 'Vous ouvrez la porte bleue.'
},
prendreLivre: {
description: 'Vous prenez le livre.'
}
};
function listeChoix(choix){
var html = '';
choix.forEach(function(d){
html += '<a href="#' + d.prochainEtat + '" class="choice">' + transitions[d.transition].description + '</a><br>'
});
return html;
}
window.addEventListener('hashchange', function(d) {
var hash = window.location.hash.slice(1);
var etat = etats[hash];
infoContainer.innerText = etat.description;
widgetChoices.innerHTML = listeChoix(etat.choix);
});
window.location.hash = 'chambreDepart';
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment