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
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
--消息循环 单例
--[[
--使用方法
local message = require("scripts.app.Node.MessageManager")
message:PostMessage(message)
num = time / dt
message:PostDelayMessage(message, num)
--检测并发送消息
message:UpdateManager_(dt)
]]--

local MessageManager = class("MessageManager")
local queue = import("..Node.queue")

--local scheduler = require("framework.scheduler")

MessageManager.time_ = 0
MessageManager.queue_ = queue.new()
MessageManager.delayMessage_ = {}

function MessageManager:ctor()
print("MessageManager")
self.time_ = 1

--scheduler.scheduleGlobal(MessageManager.UpdateManager_, 1)
end

function MessageManager:PostMessage(message)
self.queue_:puch(message)
print("PostMessage queue:", message)
end

function MessageManager:PostDelayMessage(message, time)
local m = {}
m.message = message
m.time = time
table.insert(self.delayMessage_, m)
print("PostDelayMessage", message, time)
end

function MessageManager:UpdateManager_(dt)
print("message dt", dt, "update time", self.time_)
self.time_ = self.time_ + 1
--[[
print(self.time_)
print(self.queue_:size())
print(self.queue_.list[0])
self.queue_:puch(1)
print("top", self.queue_:top())
self.queue_.list[0] = self.time_
print(self.queue_.list[-1])
self.queue_.list[-1] = self.time_
]]--

while false == self.queue_:empty() do
print("queue top", self.queue_:top())
self.queue_:pop()
end

for k, v in pairs(self.delayMessage_) do
if 0 == v.time then
print("send delay message", k, "v", v.message, "send", self.time_)
self.delayMessage_[k] = nil
else
print("delay message key", k, "v", v.message, "time", v.time)
self.delayMessage_[k].time = v.time - 1
--v.time = v.time - 1
end
end

end

return MessageManager
--[[ 测试消息循环 ]]--
local MessageManager = import("..Node.MessageManager")
local scheduler = require("framework.scheduler")

local MyScene = class("MyScene", function ()
return display.newScene("MyScene")
end)

messagetime = 1

local function UpdateMessage(dt)
print("UpdateMessage")
local message = require("scripts.app.Node.MessageManager")
message:UpdateManager_(dt)
end

local function InsertMessage(dt)
print("InsertMessage")
local message = require("scripts.app.Node.MessageManager")
if 1 == messagetime then
message:PostMessage(messagetime)
else
message:PostDelayMessage(messagetime, messagetime)
end
messagetime = messagetime + 1
if messagetime > 2 then messagetime = 1 end
end


function MyScene:ctor()
-- test MessageManager
scheduler.scheduleGlobal(UpdateMessage, 1)
scheduler.scheduleGlobal(InsertMessage, 3)

local message = require("scripts.app.Node.MessageManager")

message:PostMessage(10)
message:PostMessage(11)
message:PostMessage(12)

message:PostDelayMessage(2, 2)
message:PostDelayMessage(3, 3)
message:PostDelayMessage(4, 4)
message:PostDelayMessage(5, 5)

end

return MyScene