/
Coroutine.bit
214 lines (170 loc) · 3.78 KB
/
Coroutine.bit
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package:application/vnd.bitty-archive;
data:text/json;count=145;path=info.json;
{
"id": 0,
"title": "Basics/07. Coroutine",
"description": "",
"author": "Tony",
"version": "1.0",
"genre": "TUTORIAL",
"url": ""
}
data:text/lua;count=747;path=class.lua;
--[[
Example for the Bitty Engine
Copyright (C) 2020 - 2024 Tony Wang, all rights reserved
Homepage: https://paladin-t.github.io/bitty/
]]
-- Declares a class.
function class(kls, base)
if not base then
base = { }
end
kls.new = function (...)
local obj = { }
setmetatable(obj, kls)
if obj.ctor then -- `ctor` is short for "constructor", where you can initialize your instance.
obj:ctor(...)
end
return obj
end
kls.__index = kls
setmetatable(kls, base)
return kls
end
-- Determines whether an object is instance of a specific class.
function is(obj, kls)
if kls == nil then
error('Invalid class.')
end
repeat
if obj == kls then
return true
end
obj = getmetatable(obj)
until not obj
return false
end
data:text/lua;count=1725;path=co.lua;
--[[
Example for the Bitty Engine
Copyright (C) 2020 - 2024 Tony Wang, all rights reserved
Homepage: https://paladin-t.github.io/bitty/
]]
require 'class'
-- Coroutine class.
Coroutine = class({
_cos = nil,
waitFor = function (sec)
local start = DateTime.ticks()
sec = DateTime.fromSeconds(sec)
while DateTime.ticks() - start < sec do
coroutine.yield()
end
end,
ctor = function (self)
self._cos = nil
end,
__len = function (self)
return #self._cos
end,
count = function (self)
return #self._cos
end,
empty = function (self)
return self._cos == nil or #self._cos == 0
end,
clear = function (self)
self._cos = nil
return self
end,
start = function (self, co, ...)
if type(co) == 'function' then
co = coroutine.create(co)
end
if type(co) ~= 'thread' then
error('Unexpected data: ' .. tostring(co) .. '.')
end
if self._cos == nil then
self._cos = { }
end
table.insert(self._cos, co)
if coroutine.status(co) ~= 'dead' then
local ret, msg = coroutine.resume(co, ...)
if not ret then
error(msg)
end
end
return self
end,
update = function (self, ...)
if self._cos == nil or #self._cos == 0 then
return false
end
local dead = nil
for i = 1, #self._cos do
local co = self._cos[i]
if coroutine.status(co) ~= 'dead' then
local ret, msg = coroutine.resume(co, ...)
if not ret then
error(msg)
end
end
if coroutine.status(co) == 'dead' then
if dead == nil then
dead = { }
end
table.insert(dead, i)
end
if self._cos == nil then
return false
end
end
if dead ~= nil then
for i = #dead, 1, -1 do
table.remove(self._cos, dead[i])
end
end
return true
end
})
data:text/lua;count=1051;path=main.lua;
--[[
Example for the Bitty Engine
Copyright (C) 2020 - 2024 Tony Wang, all rights reserved
Homepage: https://paladin-t.github.io/bitty/
]]
require 'co' -- Require the updatable coroutine wrapper.
local co = nil
local acc = 0
function setup()
co = Coroutine.new() -- Create a coroutine object.
co:start( -- Start the coroutine with a yieldable function.
function (n)
coroutine.yield(n)
acc = n
while true do -- Loop forever.
Coroutine.waitFor(1) -- Wait for 1 second.
acc = acc + 1 -- Accumulate it.
end
end,
42 -- Arguments will be passed to the function above.
)
co:start( -- Start the coroutine with another function.
function ()
for i = 1, 5 do
print(i)
coroutine.yield(i)
Coroutine.waitFor(1)
end
print('End.')
end
)
end
function update(delta)
co:update() -- Update coroutines.
local canvasWidth, canvasHeight = Canvas.main:size()
local textWidth, textHeight = measure(acc)
local x, y = (canvasWidth - textWidth) * 0.5, (canvasHeight - textHeight) * 0.5
text(acc, x, y)
end