diff --git a/examples/game.ink b/examples/game.ink index 312cf77..5186782 100644 --- a/examples/game.ink +++ b/examples/game.ink @@ -1,3 +1,5 @@ +Hello world + === back_in_london === We arrived into London at 9.45pm exactly. diff --git a/examples/love2d/main.lua b/examples/love2d/main.lua index 1673318..a466491 100644 --- a/examples/love2d/main.lua +++ b/examples/love2d/main.lua @@ -2,14 +2,15 @@ local pink = require('pink.pink') local story = pink.getStory('examples/game.ink') +-- you can also jump to a story knot based on some event in your game +story.choosePathString('back_in_london'); + local currentText = nil local a=10 function love.update(dt) a = a + dt*3 - -- TODO to start from a new knot on a game event just set pink to that knot here - if not currentText then if story.canContinue then currentText = story.continue() .. '\n\n(press space to continue)' diff --git a/pink/runtime.lua b/pink/runtime.lua index ef59f0b..5aee7f6 100644 --- a/pink/runtime.lua +++ b/pink/runtime.lua @@ -17,6 +17,11 @@ return function (tree) local knots = {} + -- TODO state should contain tab/pointer to be able to save / load + s.state = { + visitCount = {} + } + local process = function () for _, v in ipairs(tree) do if is('knot', v) then @@ -25,20 +30,25 @@ return function (tree) end end + local goToKnot = function(knotName) + if knots[knotName] then + s.state.visitCount[knotName] = (s.state.visitCount[knotName] or 0) + 1 + tab = knots[knotName] + pointer = 3 + return tab[pointer] + else + print('unknown knot', knotName) + end + end + local update = function () local next = tab[pointer] - if is('knot', next) then - tab = next - pointer = 3 - next = tab[pointer] + if is('knot', next) then -- FIXME: we shouldn't continue to next knot automatically probably - how about stitches? + next = goToKnot(next) end - if is('divert', next) then - tab = knots[next[2]] - pointer = 3 - next = tab[pointer] - end + if is('divert', next) then next = goToKnot(next[2]) end s.canContinue = is('para', next) @@ -91,7 +101,16 @@ return function (tree) stepTo(option, 5) end - s.choosePathString = function(knotName) end + s.choosePathString = function(knotName) + goToKnot(knotName) + update() + end + + s.state.visitCountAtPathString = function(knotName) + return s.state.visitCount[knotName] or 0 + end + + s.variablesState = {} -- s.state.ToJson();s.state.LoadJson(savedJson); diff --git a/test.lua b/test.lua index c34affd..a73e983 100644 --- a/test.lua +++ b/test.lua @@ -1,5 +1,6 @@ local luaunit = require('luaunit') local parser = require('pink.parser') +local pink = require('pink.pink') function testEmpt() doTestS( @@ -26,8 +27,21 @@ function testKnot() doTest('knot') end function testBranching() doTest('branching') end function testGlue() doTest('glue') end --- TODO test runtime +function testVisitCount() + local story = pink.getStory('test/branching.lua') + story.choosePathString('hurry_outside'); + luaunit.assertEquals(story.state.visitCountAtPathString('as_fast_as_we_could'), 0) + while story.canContinue do story.continue() end + luaunit.assertEquals(story.state.visitCountAtPathString('as_fast_as_we_could'), 1) + story.choosePathString('hurry_outside'); + while story.canContinue do story.continue() end + luaunit.assertEquals(story.state.visitCountAtPathString('as_fast_as_we_could'), 2) + luaunit.assertEquals(story.state.visitCountAtPathString('as_fast_as_we_could'), 2) +end + + +-- TODO test runtime more, test public pink API -----------------------------