mirror of
https://github.com/Oreolek/gamebookformat.git
synced 2024-06-17 07:30:43 +03:00
Random link selection. Stylesheet fixes for links. Some fixes with hiding/showing.
This commit is contained in:
parent
f60a650598
commit
df76c64e7f
|
@ -8,14 +8,18 @@ section at [[next]].
|
|||
|
||||
* 1 start
|
||||
This is where the adventure begins. You can go on to the
|
||||
next section, see [[next]] or try the other instead, see [[other]].
|
||||
next section, see [[next]] or try the random selection
|
||||
feature at [[random]].
|
||||
|
||||
* next
|
||||
This is the next section. Go on to the end at [[end]].
|
||||
|
||||
* other
|
||||
This is another section. You can try the next section now,
|
||||
see [[next]], or go on to the end, see [[end]].
|
||||
* random
|
||||
[random]Pick a destination at random[/random] from
|
||||
[[next]] or [[end]] or [[altend]].
|
||||
|
||||
* altend
|
||||
Alternative ending.
|
||||
|
||||
* end
|
||||
The end.
|
||||
|
|
1
templates/debug/random.debug
Normal file
1
templates/debug/random.debug
Normal file
|
@ -0,0 +1 @@
|
|||
[RANDOM]%(inner)s[/RANDOM]
|
|
@ -1,2 +1,3 @@
|
|||
if (this.gamebook) {
|
||||
gamebook.runActionsInIntroSections();
|
||||
}
|
||||
|
|
1
templates/html/random.html
Normal file
1
templates/html/random.html
Normal file
|
@ -0,0 +1 @@
|
|||
<span class="random enabledlink">%(inner)s</span>
|
|
@ -1,5 +1,6 @@
|
|||
var gamebook = {
|
||||
'player' : {
|
||||
'started' : false,
|
||||
'currentSection' : null,
|
||||
'collections' : {},
|
||||
|
||||
|
@ -56,7 +57,9 @@
|
|||
},
|
||||
|
||||
'turnTo' : function(nr) {
|
||||
console.log("Turning to " + nr + ".");
|
||||
if (!gamebook.player.started) {
|
||||
gamebook.start();
|
||||
}
|
||||
if (!nr in this.sections) {
|
||||
throw new Exception("Can not turn to non-existing section "
|
||||
+ nr + ".");
|
||||
|
@ -67,7 +70,7 @@
|
|||
'start' : function() {
|
||||
this.hideIntroSections();
|
||||
this.addClassToClass('startlink', 'nodisplay');
|
||||
this.turnTo(1);
|
||||
gamebook.player.started = true;
|
||||
},
|
||||
|
||||
'displaySection' : function(nr) {
|
||||
|
@ -87,12 +90,19 @@
|
|||
},
|
||||
|
||||
'showIntroSections' : function() {
|
||||
this.runActionsInIntroSections();
|
||||
this.removeClassFromClass('introsection', 'nodisplay');
|
||||
this.addClassToClass('displayintrolink', 'nodisplay');
|
||||
this.removeClassFromClass('hideintrolink', 'nodisplay');
|
||||
document.body.scrollIntoView();
|
||||
},
|
||||
|
||||
'runActionsInIntroSections' : function() {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName('introsectionbody'),
|
||||
gamebook.runActions);
|
||||
},
|
||||
|
||||
'addClassToClass' : function(className, addClass) {
|
||||
Array.prototype.forEach.call(
|
||||
document.getElementsByClassName(className),
|
||||
|
@ -114,9 +124,13 @@
|
|||
var hasXorScope = false;
|
||||
var hasAutoScope = false;
|
||||
var xorEnableNext = false;
|
||||
var autoDisableAllRemainingLinks = false;
|
||||
var autoDisableAllRemainingLinks = (
|
||||
gamebook.player.started && e.classList.contains('introsectionbody'));
|
||||
Array.prototype.forEach.call(e.childNodes, function(c) {
|
||||
if (/sectionref$/.test(c.className)) {
|
||||
if (!c.classList) {
|
||||
return;
|
||||
}
|
||||
if (c.classList.contains('sectionref')) {
|
||||
if (enableNextLink && !autoDisableAllRemainingLinks) {
|
||||
gamebook.enableLink(c);
|
||||
if (hasAutoScope) {
|
||||
|
@ -137,18 +151,20 @@
|
|||
} else if (c.className === 'has') {
|
||||
enableNextLink = gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
console.log("has " + c.dataset.type +
|
||||
" " + c.dataset.what + " " + enableNextLink);
|
||||
} else if (c.className === 'hasnot') {
|
||||
enableNextLink = !gamebook.player.has(c.dataset.type,
|
||||
c.dataset.what);
|
||||
console.log("has not " + c.dataset.type +
|
||||
" " + c.dataset.what + " " + enableNextLink);
|
||||
} else if (c.className === 'xor') {
|
||||
hasXorScope = true;
|
||||
xorEnableNext = !enableNextLink;
|
||||
} else if (c.className === 'auto') {
|
||||
hasAutoScope = true;
|
||||
} else if (c.classList.contains('random')) {
|
||||
c.addEventListener('click',
|
||||
gamebook.enableRandomLinkAfter);
|
||||
c.classList.add("enabledlink");
|
||||
c.classList.remove("disabledlink");
|
||||
autoDisableAllRemainingLinks = true;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
@ -156,13 +172,35 @@
|
|||
'enableLink' : function(e) {
|
||||
e.addEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.className = "enabledsectionref";
|
||||
e.classList.add("enabledLink");
|
||||
e.classList.remove("disabledlink");
|
||||
},
|
||||
|
||||
'disableLink' : function(e) {
|
||||
e.removeEventListener('click',
|
||||
gamebook.getTurnToFunction(e.dataset.ref));
|
||||
e.className = "disabledsectionref";
|
||||
e.classList.remove("enabledLink");
|
||||
e.classList.add("disabledlink");
|
||||
},
|
||||
|
||||
'enableRandomLinkAfter' : function(event) {
|
||||
this.classList.remove("enabledlink");
|
||||
this.classList.add("disabledlink");
|
||||
var links = [];
|
||||
var e = this.nextSibling;
|
||||
while (e) {
|
||||
if (e.classList && e.classList.contains('sectionref')) {
|
||||
links.push(e);
|
||||
}
|
||||
e = e.nextSibling;
|
||||
}
|
||||
if (links.length > 0) {
|
||||
var selected = links[Math.floor(Math.random()*links.length)]
|
||||
gamebook.enableLink(selected);
|
||||
} else {
|
||||
console.log("Random with nothing to select?");
|
||||
}
|
||||
event.preventDefault();
|
||||
},
|
||||
|
||||
'addCollectionView' : function(type, name) {
|
||||
|
|
|
@ -1 +1 @@
|
|||
<a class="sectionref" data-ref="%(nr)d">%(nr)d</a>
|
||||
<a class="sectionref enabledlink" data-ref="%(nr)d">%(nr)d</a>
|
|
@ -1,2 +1,2 @@
|
|||
<div class="startlink"
|
||||
onclick="gamebook.start()">%(starttext)s</div>
|
||||
onclick="gamebook.turnTo(1)">%(starttext)s</div>
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
<div class="startlink"
|
||||
onclick="gamebook.start()">%(starttext)s</div>
|
|
@ -1,5 +1,17 @@
|
|||
.enabledsectionref {font-weight: bold; cursor: pointer;}
|
||||
.disabledsectionref {font-weight: bold; color: #aaa; cursor: not-allowed;}
|
||||
.startlink,.sectionref,.displayintrolink,.hideintrolink {
|
||||
font-weight: bold;
|
||||
padding-left: 1.5em;
|
||||
padding-right: 1.5em;
|
||||
margin: 0.2em;
|
||||
vertical-align: middle;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.startlink,.enabledlink,.displayintrolink,.hideintrolink {cursor: pointer;
|
||||
background: #eef;}
|
||||
.startlink:hover,.enabledlink:hover,.displayintrolink:hover,
|
||||
.hideintrolink:hover {background: #a9f;}
|
||||
.disabledlink {color: #bbb; cursor: not-allowed; background: #fee;}
|
||||
.disabledlink:hover {background: #fee;}
|
||||
.sectionnumber {font-weight: bolder;
|
||||
margin-left: 50%%;
|
||||
margin-right: 50%%;}
|
||||
|
|
4
todo.org
4
todo.org
|
@ -1,4 +1,4 @@
|
|||
* TODO [37/65] [56%]
|
||||
* TODO [38/65] [58%]
|
||||
- [X] Debug output
|
||||
- [X] DOT output
|
||||
- [X] LaTeX output
|
||||
|
@ -43,7 +43,7 @@
|
|||
- [X] HTML hide intro sections with link to display again
|
||||
- [X] Only accept specific characters in section names
|
||||
eg [a-z][a-z_0-9]+
|
||||
- [ ] Random pick of link to follow from a section.
|
||||
- [X] Random pick of link to follow from a section.
|
||||
- [ ] Possibility to make predictable random numbers and shuffling for testing
|
||||
- [ ] Test generate examples and compare to expected output in all formats
|
||||
- [ ] Unit tests (finally...)
|
||||
|
|
Loading…
Reference in a new issue