Skip to content

Commit

Permalink
Merge branch 'dev/improvedflow'
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn Ritzl committed Aug 19, 2017
2 parents 82a5042 + a43632e commit c237826
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 50 deletions.
56 changes: 55 additions & 1 deletion examples/flow/a.gui
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
script: ""
script: "/examples/flow/a.gui_script"
fonts {
name: "system_font"
font: "/builtins/fonts/system_font.font"
Expand Down Expand Up @@ -71,6 +71,60 @@ nodes {
text_leading: 1.0
text_tracking: 0.0
}
nodes {
position {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
size {
x: 200.0
y: 100.0
z: 0.0
w: 1.0
}
color {
x: 1.0
y: 1.0
z: 1.0
w: 1.0
}
type: TYPE_BOX
blend_mode: BLEND_MODE_ALPHA
texture: ""
id: "box"
xanchor: XANCHOR_NONE
yanchor: YANCHOR_NONE
pivot: PIVOT_CENTER
adjust_mode: ADJUST_MODE_FIT
layer: ""
inherit_alpha: true
slice9 {
x: 0.0
y: 0.0
z: 0.0
w: 0.0
}
clipping_mode: CLIPPING_MODE_NONE
clipping_visible: true
clipping_inverted: false
alpha: 1.0
template_node_child: false
size_mode: SIZE_MODE_AUTO
}
material: "/builtins/materials/gui.material"
adjust_reference: ADJUST_REFERENCE_PARENT
max_nodes: 512
12 changes: 12 additions & 0 deletions examples/flow/a.gui_script
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local flow = require "ludobits.m.flow"

function init(self)
for k,v in pairs(_G) do print(k,v) end
local box = gui.get_node("box")
flow(function()
print("flow a")
flow.delay(1)
print("a delay")
gui.set_position(box, vmath.vector3(200, 200, 0))
end)
end
2 changes: 1 addition & 1 deletion examples/flow/flow.collection
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ embedded_instances {
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/examples/assets/examples.atlas\\\"\\n"
"default_animation: \\\"green_player\\\"\\n"
"default_animation: \\\"green_player_walk\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
Expand Down
98 changes: 50 additions & 48 deletions ludobits/m/flow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-- @usage
--
-- local flow = require "ludobits.m.flow"
--
--
-- function init(self)
-- flow.start(function()
-- -- animate a gameobject and wait for animation to complete
Expand All @@ -18,19 +18,19 @@
-- flow.go_animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, 400, go.EASING_INCUBIC, 2)
-- end)
-- end
--
--
-- function final(self)
-- flow.stop()
-- end
--
--
-- function update(self, dt)
-- flow.update()
-- end
--
--
-- function on_message(self, message_id, message, sender)
-- flow.on_message(message_id, message, sender)
-- end
--
--

local M = {}

Expand Down Expand Up @@ -80,10 +80,11 @@ local function create_or_get(co)
url = msg.url(),
state = READY,
co = co,
script_instance = _G.__dm_script_instance__,
}
end
return instances[co]
end
end


--- Start a new flow. If this function is called from
Expand Down Expand Up @@ -168,6 +169,11 @@ function M.frames(frames)
end


function M.yield()
return coroutine.yield()
end


--- Wait until a function returns true
-- @param fn
function M.until_true(fn)
Expand Down Expand Up @@ -244,7 +250,7 @@ function M.load(collection_url)
assert(collection_url, "You must provide a URL to a collection proxy")
collection_url = ensure_url(collection_url)
local instance = create_or_get(coroutine.running())
instance.state = WAITING
instance.state = WAITING
instance.on_message = function(message_id, message, sender)
if message_id == PROXY_LOADED and sender == collection_url then
msg.post(sender, "enable")
Expand Down Expand Up @@ -371,62 +377,58 @@ function M.ray_cast(from, to, groups)
end


local function resume(instance)
instance.state = RUNNING
local result = instance.result or {}
instance.result = nil
local ok, error = coroutine.resume(instance.co, table_unpack(result))
if not ok then
if instance.on_error then
instance.on_error(error)
else
print("Warning: Flow resulted in error", error)
end
end
end

--- Call this as often as needed (every frame)
function M.update(dt)
if not dt then
print("WARN: flow.update() now requires dt. Assuming 0.0167 for now.")
dt = 0.0167
end
local url = msg.url()
for co,instance in pairs(instances) do
if instance.url == url then
local status = coroutine.status(co)
if status == "dead" then
instances[co] = nil
else
if instance.state == WAITING and instance.condition then
if instance.condition(dt) then
instance.condition = nil
instance.on_message = nil
instance.state = READY
end
end

if instance.state == READY then
instance.state = RESUMING
msg.post(instance.url, MSG_RESUME, { url = instance.url, id = instance.id })
local status = coroutine.status(co)
if status == "dead" then
instances[co] = nil
else
local current_script_instance = _G.__dm_script_instance__
_G.__dm_script_instance__ = instance.script_instance

if instance.state == WAITING and instance.condition then
if instance.condition(dt) then
instance.condition = nil
instance.on_message = nil
instance.state = READY
end
end

if instance.state == READY then
resume(instance)
end

_G.__dm_script_instance__ = current_script_instance
end
end
end


--- Forward any received messages in your scripts to this function
function M.on_message(message_id, message, sender)
if message_id == MSG_RESUME then
for co,instance in pairs(instances) do
if instance.id == message.id then
instance.state = RUNNING
local result = instance.result or {}
instance.result = nil
local ok, error = coroutine.resume(co, table_unpack(result))
if not ok then
if instance.on_error then
instance.on_error(error)
else
print("Warning: Flow resulted in error", error)
end
end
return
end
end
else
local url = msg.url()
for _,instance in pairs(instances) do
if instance.on_message and instance.url == url then
instance.on_message(message_id, message, sender)
end
local url = msg.url()
for _,instance in pairs(instances) do
if instance.on_message and instance.url == url then
instance.on_message(message_id, message, sender)
end
end
end
Expand All @@ -435,4 +437,4 @@ return setmetatable(M, {
__call = function(self, ...)
return M.start(...)
end
})
})

0 comments on commit c237826

Please sign in to comment.