Started working on dialogue

This commit is contained in:
2026-01-23 21:28:25 +03:00
parent 1d358d206e
commit 8320b14358
7 changed files with 184 additions and 82 deletions

View File

@@ -3,7 +3,7 @@ set(LUA_SCRIPTS_SRC
data.lua
)
set(LUA_SCRIPTS_OUTPUT)
set(LUA_PACKAGES narrator stories)
set(LUA_PACKAGES narrator stories json)
foreach(LUA_SCRIPT_FILE ${LUA_SCRIPTS_SRC})
get_filename_component(FILE_NAME ${LUA_SCRIPT_FILE} NAME_WE)
set(LUA_SCRIPT_OUTPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${FILE_NAME}.lua)
@@ -21,4 +21,4 @@ foreach(LUA_PACKAGE ${LUA_PACKAGES})
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${LUA_PACKAGE})
list(APPEND LUA_PACKAGES_OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${LUA_PACKAGE})
endforeach()
add_custom_target(stage_lua_scripts ALL DEPENDS ${LUA_SCRIPTS_OUTPUT} ${LUA_PACKAGES_OUTPUT})
add_custom_target(stage_lua_scripts ALL DEPENDS ${LUA_SCRIPTS_OUTPUT} ${LUA_PACKAGES_OUTPUT})

View File

@@ -62,6 +62,7 @@ dropped you into the sea. Last thing you heard before you hit the water was happ
}
]]--
local narrator = require('narrator.narrator')
local json = require('json.json')
function dump(o)
if type(o) == 'table' then
@@ -657,26 +658,90 @@ setup_handler(function(event, trigger_entity, what_entity)
end)
setup_action_handler("talk", {
count = 0,
activate = function(this)
this._narration("Booo!! Lua...", {"One", "Two", "Three"})
local book = narrator.parse_file('stories.talk')
this.story = narrator.init_story(book)
local crash_bind = function()
crash()
end
this.story:bind('crash', crash_bind)
this.story:begin()
this:narration_update()
local props = this._get_properties()
print(props)
local json_data = json.decode(props)
print(dump(json_data))
crash()
end,
event = function(this, event)
this.count = this.count + 1
if event == "narration_progress" then
this.count = this.count + 1
this._narration("Booo!!!", {})
this:narration_update()
end
if event == "narration_answered" then
this.count = this.count + 1
this._narration("Booo!!!_", {})
end
if this.count > 10 then
this._finish()
local answer = this._get_narration_answer()
this.story:choose(answer)
this:narration_update()
end
end,
finish = function(this)
this.count = 0
end,
narration_update = function(this)
local ret = ""
local choices = {}
local have_choice = false
local have_paragraph = false
print("narration_update")
if this.story:can_continue() then
print("CAN continue")
have_paragraph = true
local paragraph = this.story:continue(1)
print(dump(paragraph))
local text = paragraph.text
if paragraph.tags then
-- text = text .. ' #' .. table.concat(paragraph.tags, ' #')
for i, tag in ipairs(paragraph.tags) do
if tag == 'discard' then
text = ''
elseif tag == 'crash' then
print(text)
crash()
end
this:handle_tag(tag)
end
end
ret = text
if this.story:can_choose() then
have_choice = true
local ch = this.story:get_choices()
for i, choice in ipairs(ch) do
table.insert(choices, choice.text)
print(i, dump(choice))
end
if #choices == 1 and choices[1] == "Ascend" then
this.story:choose(1)
choices = {}
end
if #choices == 1 and choices[1] == "Continue" then
this.story:choose(1)
choices = {}
end
end
else
print("can NOT continue")
end
print(ret)
if (#choices > 0) then
print("choices!!!")
this._narration(ret, choices)
else
this._narration(ret, {})
end
if not have_choice and not have_paragraph then
this._finish()
end
end,
handle_tag = function(this, tag)
print("tag: " .. tag)
end,
})
--[[

View File

@@ -1,6 +1,20 @@
Dialogue...
Whatever...
* [Ascend]
~ crash()
- ->END
Yes?
-> start
=== start ===
+ [Common] -> common
+ [Debug] -> debug
+ [Leave] -> END
=== common ===
# discard
+ [What do you think about me?]
I don't know.
-> END
+ [How are you?]
I think everything is allright.
-> END
=== debug ===
Debug #discard
+ Crash
#crash
-> END