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
| var GameStates = {
"debut": {
texte: "Début",
actions: {
"milieu1": "Choix numéro 1",
"milieu2": "Choix numéro 2",
}
},
"milieu1": {
texte: "Scène 1",
actions: {
"milieu2": "Action 1",
"debut": "Action 2",
"gagne": "Action 3",
}
},
"milieu2": {
texte: "Scène 2",
actions: {
"milieu1": "Bouton 1",
"gameover": "Bouton 2",
}
},
"gameover": {
texte: "Perdu",
actions: {
"debut": "Recommencer",
}
},
"gagne": {
texte: "Gagné",
actions: {
"debut": "Recommencer",
}
},
};
var currentState;
function afficherEtape(etape){
currentState = GameStates[etape];
document.getElementById("texte").textContent = currentState.texte;
var listeActions = document.querySelector("#game ul");
listeActions.innerHTML=""; //vide la liste précédente
for(var action in currentState.actions){
var li = document.createElement("li");
var bouton = document.createElement("input");
bouton.setAttribute("type", "button");
bouton.value = currentState.actions[action];
bouton.onclick = (function(){
afficherEtape(this);
}).bind(action);
li.appendChild(bouton);
listeActions.appendChild(li);
}
}
afficherEtape("debut"); |
Partager