story.state.visitCountAtPathString
This commit is contained in:
parent
c3a60e3126
commit
1eadf9b3bb
|
@ -1,3 +1,5 @@
|
|||
Hello world
|
||||
|
||||
=== back_in_london ===
|
||||
|
||||
We arrived into London at 9.45pm exactly.
|
||||
|
|
|
@ -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)'
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
16
test.lua
16
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
|
||||
|
||||
-----------------------------
|
||||
|
||||
|
|
Loading…
Reference in New Issue