-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathgame.lua
60 lines (43 loc) · 1.55 KB
/
game.lua
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
59
60
--
-- A simple command-line game example
-- Dependencies
local narrator = require('narrator.narrator')
-- Parse a book from the Ink file and save as module 'stories.game.lua'
local book = narrator.parse_file('stories.game', { save = true })
-- Init a story from the book
local story = narrator.init_story(book)
-- Start observing the Ink variable 'x'
story:observe('x', function(x) print('The x did change! Now it\'s ' .. x) end)
-- Bind local functions to call from ink as external functions
story:bind('beep', function() print('Beep! 😃') end)
story:bind('sum', function(x, y) return x + y end)
-- Begin the story
story:begin()
print('--- Game started ---\n')
while story:can_continue() do
-- Get current paragraphs to output
local paragraphs = story:continue()
for _, paragraph in ipairs(paragraphs) do
local text = paragraph.text
-- You can handle tags as you like, but we attach them to text here.
if paragraph.tags then
text = text .. ' #' .. table.concat(paragraph.tags, ' #')
end
-- Output text to the player
print(text)
end
-- If there is no choice, it seems the game is over
if not story:can_choose() then break end
print('')
-- Get available choices and output them to the player
local choices = story:get_choices()
for i, choice in ipairs(choices) do
print(i .. ') ' .. choice.text)
end
-- Read the choice from the player input
local answer = tonumber(io.read()) or 0
print('')
-- Send an answer to the story to generate new paragraphs
story:choose(answer)
end
print('\n--- Game over ---')