Serge Bazanski | 18c1a26 | 2022-07-07 14:24:53 +0200 | [diff] [blame] | 1 | local ThreadNode = Node:extend('ThreadNode', { |
| 2 | threadFile = nil, |
| 3 | threadChannel = nil, |
| 4 | updateInterval = 5, |
| 5 | lastUpdate = 0, |
| 6 | state = nil, |
| 7 | }) |
| 8 | |
| 9 | function ThreadNode:update(dt) |
| 10 | if not self.state then self:checkUpdate() end |
| 11 | end |
| 12 | |
| 13 | function ThreadNode:checkUpdate() |
| 14 | if self.threadFile and self.threadChannel then |
| 15 | if self.lastUpdate < love.timer.getTime() - self.updateInterval or |
| 16 | (not self.state and self.lastUpdate < love.timer.getTime() - 5) then |
| 17 | self.lastUpdate = love.timer.getTime() |
| 18 | print(self.threadChannel, "Updating...") |
| 19 | |
| 20 | local updateThread = love.thread.newThread(self.threadFile) |
| 21 | updateThread:start() |
| 22 | end |
| 23 | |
| 24 | local v = love.thread.getChannel(self.threadChannel):pop() |
| 25 | if v then |
| 26 | self:onUpdate(v) |
| 27 | end |
| 28 | end |
| 29 | end |
| 30 | |
| 31 | function ThreadNode:afterExit() |
| 32 | print('exit') |
| 33 | self:checkUpdate() |
| 34 | end |
| 35 | |
| 36 | function ThreadNode:onUpdate(v) |
| 37 | self.state = v |
| 38 | end |
| 39 | |
| 40 | return ThreadNode |