diff --git a/examples/trade.gamebook b/examples/trade.gamebook new file mode 100644 index 0000000..51a887f --- /dev/null +++ b/examples/trade.gamebook @@ -0,0 +1,14 @@ += Introduction +Testing trade of things. You can buy things using +[count gold]Gold[/count] of which you start with [set gold]100[/set]. +You also have an [collect item]Inventory[/item]. At start you carry +a [add item]sword[/add]. + +* 1 start +You can buy a [found item]shield[/found] here for [cost gold]50[/cost] Gold. +There is also a [found item]chain mail[/found] costing [cost gold]150[/cost]. +You can pick up some [found item]spiderweb[/found] for free. +You can go on to the end at [[theend]]. + +* theend +It ends here. diff --git a/expected/items.html b/expected/items.html index 7378e5f..5d0a1b9 100644 --- a/expected/items.html +++ b/expected/items.html @@ -99,8 +99,8 @@ if (this.gamebook) {
6
- You found something valuable , but there is no way forward, so you head back to 12. @@ -139,8 +139,8 @@ if (this.gamebook) {
9
- There is a cursed bracelet here. You can go on to 7 or go back to + + diff --git a/expected/trade.rtf b/expected/trade.rtf new file mode 100644 index 0000000..25ffaef --- /dev/null +++ b/expected/trade.rtf @@ -0,0 +1,35 @@ +{\rtf1\ansi\ansicpg1252\cocoartf1038\cocoasubrtf360 +{\fonttbl\f0\fswiss\fcharset0 Helvetica;} +{\colortbl;\red255\green255\blue255;} +\paperw11900\paperh16840\margl1440\margr1440\vieww14140\viewh14860\viewkind0 +\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural + +\f0\b\fs24 \cf0 + +\b \qc Gamebook +\b0\ +\ +\b \qc Introduction +\b0\ + + + +\ql Testing trade of things. You can buy things using Gold of which you start with 100. You also have an Inventory. At start you carry a sword. \ +\ + +\b Turn to 1 to begin. +\b0\ +\ +\b \qc 1 +\b0\ +\ql You can buy a shield here for 50 Gold. There is also a chain mail costing 150. You can pick up some spiderweb for free. You can go on to the end at \b 2 +\b0 +. \ +\ + +\b \qc 2 +\b0\ +\ql It ends here. \ +\ + +} diff --git a/expected/trade.tex b/expected/trade.tex new file mode 100644 index 0000000..ece7f65 --- /dev/null +++ b/expected/trade.tex @@ -0,0 +1,61 @@ +\documentclass[a5paper,onecolumn]{book} + +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage[hidelinks]{hyperref} +\usepackage{graphicx} + +\usepackage[top=3.3cm, bottom=3.3cm, left=2cm, right=2cm]{geometry} +\newif\ifpdf +\ifx\pdfoutput\undefined + \pdffalse +\else + \ifnum\pdfoutput=1 + \pdftrue + \else + \pdffalse + \fi +\fi + +\title{Gamebook} +\author{} +\date{} + +\newcounter{sectionnr} + +\begin{document} + +\maketitle + +\thispagestyle{empty} + +\pagestyle{empty} + +\clearpage + +\subsection*{\begin{center} \textbf{Introduction} \end{center}} + + + \noindent + Testing trade of things. You can buy things using Gold of which you start with 100. You also have an Inventory. At start you carry a \textbf{sword}. +\vspace{1em} + + +Turn to 1 to begin. +\phantomsection +\refstepcounter{sectionnr} +\label{section1} +\subsection*{\begin{center} \textbf{1} \end{center}} + + \noindent + You can buy a \textbf{shield} here for 50 Gold. There is also a \textbf{chain mail} costing 150. You can pick up some \textbf{spiderweb} for free. You can go on to the end at \textbf{\autoref{section2}}. +\vspace{1em} +\phantomsection +\refstepcounter{sectionnr} +\label{section2} +\subsection*{\begin{center} \textbf{2} \end{center}} + + \noindent + It ends here. +\vspace{1em} +\end{document} diff --git a/expected/trade.txt b/expected/trade.txt new file mode 100644 index 0000000..856deef --- /dev/null +++ b/expected/trade.txt @@ -0,0 +1,14 @@ + Gamebook + +Introduction + Testing trade of things. You can buy things using Gold of which you start with 100. You also have an Inventory. At start you carry a sword. + + + +Turn to 1 to begin. +1 + You can buy a shield here for 50 Gold. There is also a chain mail costing 150. You can pick up some spiderweb for free. You can go on to the end at 2. + +2 + It ends here. + diff --git a/gamebookformatplay.js b/gamebookformatplay.js index d0654c2..9a1a658 100644 --- a/gamebookformatplay.js +++ b/gamebookformatplay.js @@ -254,6 +254,7 @@ var gamebook = { var hasXorScope = false; var hasAutoScope = false; var xorEnableNext = false; + var lastCanHaveCost = null; var autoDisableAllRemaining = ( gamebook.player.started && e.classList.contains('introsectionbody')); @@ -261,6 +262,7 @@ var gamebook = { if (!c.classList) { return; } + // FIXME yes, this must be split up if (c.classList.contains('sectionref')) { if (enableNextLink && !autoDisableAllRemaining) { gamebook.enableLink(c); @@ -321,10 +323,29 @@ var gamebook = { c.classList.add("enabledlink"); c.classList.remove("disabledlink"); autoDisableAllRemaining = true; + } else if (c.classList.contains('found')) { + lastCanHaveCost = c; + c.addEventListener('click', gamebook.takeFound); + } else if (c.classList.contains('cost')) { + gamebook.addCost(c, lastCanHaveCost); } }); }, + addCost : function(c, e) { + var cost = parseInt(c.dataset.amount); + var counter = gamebook.player.counters[c.dataset.type]; + if (counter.value - cost >= counter.minValue) { + e.classList.add("enabledlink"); + e.classList.remove("disabledlink"); + e.dataset.cost = c.dataset.amount; + e.dataset.costtype = c.dataset.type; + } else { + e.classList.add("disabledlink"); + e.classList.remove("enabledlink"); + } + }, + 'enableLink' : function(e) { e.addEventListener('click', gamebook.getTurnToFunction(e.dataset.ref)); @@ -411,6 +432,25 @@ var gamebook = { this.turnToFunctions[nr] = f; return f; } + }, + + 'takeFound' : function(evt) { + evt.preventDefault(); + this.classList.remove("enabledlink"); + this.classList.add("disabledlink"); + var what = this.dataset.what; + var type = this.dataset.type; + if ('cost' in this.dataset && 'costtype' in this.dataset) { + var cost = parseInt(this.dataset.cost); + var counter = gamebook.player.counters[this.dataset.costtype]; + if (counter.value - cost < counter.minValue) { + // this should not happen, link should be disabled + return; + } + counter.dec(cost); + gamebook.updateCountersView(); + } + gamebook.player.add(type, what); } }; diff --git a/templates/html/cost.html b/templates/html/cost.html new file mode 100644 index 0000000..c2b6afa --- /dev/null +++ b/templates/html/cost.html @@ -0,0 +1,3 @@ +%(inner)s diff --git a/templates/html/found.html b/templates/html/found.html index 789163c..34d64b5 100644 --- a/templates/html/found.html +++ b/templates/html/found.html @@ -1,3 +1,3 @@ -%(inner)s diff --git a/todo.org b/todo.org index 8e55d0b..6e1a2eb 100644 --- a/todo.org +++ b/todo.org @@ -1,4 +1,4 @@ -* TODO [51/75] [68%] +* TODO [52/75] [69%] - [X] Debug output - [X] DOT output - [X] LaTeX output @@ -63,12 +63,16 @@ and in inferior browsers - [X] Make sure counters start at 0 (not 1). - [X] Remove the counter names from counter-tags output (and templates). -- [ ] Buy (optionally decrease a counter to add something to a collection) +- [X] Buy (optionally decrease a counter to add something to a collection) buy tag to specify what can be bought, eg [buy item]sword[/buy] followed by cost tag to specify what it will cost, eg [cost gold]3[/cost] + Implemented by adding cost to the old found tag (put cost tag after it). + Might reconsider and make explicit buy tag for clarity? - [ ] Sell (optionally increase a counter to drop something from a collection) sell tag to specify what can be bought, eg [sell item]sword[/sell] followed by cost tag to specify what it will cost, eg [cost gold]2[/cost] + Might want to wait with this and add normal drop function first? +- [ ] Trade decrease on counter to increase another counter. - [ ] Pick a specific number of things from those found in a(n intro) section Used to pick up one or more of something found, or before starting. Create counter and set to how many are allowed to be picked.