最終更新 1734243656

修正履歴 b124751a4107b0b3b49822165bf6e67340cc2495

hello-lvera.lua Raw Playground
1local min, max, huge = math.min, math.max, math.huge
2local machine = {state = {}}
3
4machine.counters = {
5 ["run main loop"] = 1,
6 ["show message"] = 0,
7 ["@step timer"] = 0,
8 ["present current frame"] = 0,
9 ["sleep for 1ms"] = 0,
10 ["run current frame"] = 0,
11 ["@clear screen"] = 0,
12 ["@exit code"] = 0,
13 ["@present"] = 0,
14 ["@sleep"] = 0,
15 ["@ms"] = 0,
16 ["@print hello from vera"] = 0,
17 ["@poll inputs"] = 0,
18 ["next cycle"] = 0,
19 ["poll inputs"] = 0,
20 ["@quit love"] = 0,
21 ["@quit"] = 0,
22 ["step timer"] = 0,
23 ["clear the screen"] = 0,
24}
25
26machine.state = {
27}
28
29function machine:on_poll_inputs(counters)
30end
31
32function machine:on_quit_love(counters)
33end
34
35function machine:on_step_timer(counters)
36end
37
38function machine:on_clear_screen(counters)
39end
40
41function machine:on_present(counters)
42end
43
44function machine:on_sleep(counters, sleep)
45end
46
47function machine:on_hello_from_vera(counters)
48end
49
50function match(self, counters)
51 if counters["@poll inputs"] > 0 then
52 self:on_poll_inputs(counters)
53 counters["@poll inputs"] = 0
54 return true
55 end
56 if counters["@quit love"] > 0 then
57 counters["@exit code"] = 0
58 self:on_quit_love(counters)
59 counters["@quit love"] = 0
60 return true
61 end
62 if counters["@step timer"] > 0 then
63 self:on_step_timer(counters)
64 counters["@step timer"] = 0
65 return true
66 end
67 if counters["@clear screen"] > 0 then
68 self:on_clear_screen(counters)
69 counters["@clear screen"] = 0
70 return true
71 end
72 if counters["@present"] > 0 then
73 self:on_present(counters)
74 counters["@present"] = 0
75 return true
76 end
77 if counters["@sleep"] > 0 then
78 self:on_sleep(counters, counters["@sleep"])
79 counters["@sleep"] = 0
80 counters["@sleep"] = 0
81 return true
82 end
83 if counters["@print hello from vera"] > 0 then
84 self:on_hello_from_vera(counters)
85 counters["@print hello from vera"] = 0
86 return true
87 end
88 if counters["run main loop"] > 0 then
89 local acc = counters["run main loop"]
90 counters["run main loop"] = max(counters["run main loop"] - acc, 0)
91 counters["poll inputs"] = counters["poll inputs"] + acc * 1
92 counters["run current frame"] = counters["run current frame"] + acc * 1
93 return true
94 end
95 if counters["poll inputs"] > 0 then
96 local acc = counters["poll inputs"]
97 counters["poll inputs"] = max(counters["poll inputs"] - acc, 0)
98 counters["@poll inputs"] = counters["@poll inputs"] + acc * 1
99 return true
100 end
101 if counters["run current frame"] > 0 and counters["@quit"] > 0 then
102 local acc = huge
103 acc = min(acc, counters["run current frame"])
104 acc = min(acc, counters["@quit"])
105 counters["run current frame"] = max(counters["run current frame"] - acc, 0)
106 counters["@quit"] = max(counters["@quit"] - acc, 0)
107 counters["@quit love"] = counters["@quit love"] + acc * 1
108 return true
109 end
110 if counters["run current frame"] > 0 then
111 local acc = counters["run current frame"]
112 counters["run current frame"] = max(counters["run current frame"] - acc, 0)
113 counters["show message"] = counters["show message"] + acc * 1
114 counters["present current frame"] = counters["present current frame"] + acc * 1
115 counters["sleep for 1ms"] = counters["sleep for 1ms"] + acc * 1
116 counters["next cycle"] = counters["next cycle"] + acc * 1
117 counters["step timer"] = counters["step timer"] + acc * 1
118 counters["clear the screen"] = counters["clear the screen"] + acc * 1
119 return true
120 end
121 if counters["step timer"] > 0 then
122 local acc = counters["step timer"]
123 counters["step timer"] = max(counters["step timer"] - acc, 0)
124 counters["@step timer"] = counters["@step timer"] + acc * 1
125 return true
126 end
127 if counters["clear the screen"] > 0 then
128 local acc = counters["clear the screen"]
129 counters["clear the screen"] = max(counters["clear the screen"] - acc, 0)
130 counters["@clear screen"] = counters["@clear screen"] + acc * 1
131 return true
132 end
133 if counters["show message"] > 0 then
134 local acc = counters["show message"]
135 counters["show message"] = max(counters["show message"] - acc, 0)
136 counters["@print hello from vera"] = counters["@print hello from vera"] + acc * 1
137 return true
138 end
139 if counters["present current frame"] > 0 then
140 local acc = counters["present current frame"]
141 counters["present current frame"] = max(counters["present current frame"] - acc, 0)
142 counters["@present"] = counters["@present"] + acc * 1
143 return true
144 end
145 if counters["sleep for 1ms"] > 0 then
146 local acc = counters["sleep for 1ms"]
147 counters["sleep for 1ms"] = max(counters["sleep for 1ms"] - acc, 0)
148 counters["@ms"] = counters["@ms"] + acc * 1
149 return true
150 end
151 if counters["next cycle"] > 0 then
152 local acc = counters["next cycle"]
153 counters["next cycle"] = max(counters["next cycle"] - acc, 0)
154 counters["run main loop"] = counters["run main loop"] + acc * 1
155 return true
156 end
157 return false
158end
159
160function machine:run()
161 local counters = self.counters
162 while match(self, counters) do end
163end
164
165return machine