Engine 0.0.4
Game engine in lua
Loading...
Searching...
No Matches
game.lua
Go to the documentation of this file.
1--! @file examples/launcher/game.lua
2--!
3--! @startuml
4--! hide empty description
5--! skinparam State {
6--! FontColor white
7--! }
8--! state 1 as "boot" #sienna
9--! state 2 as "download csv" #darkblue
10--! state 3 as "parse csv" #darkgreen
11--! state 4 as "menu launcher" #gray
12--! state 5 as "download game" #blue
13--! state 6 as "load game" #green
14--! state 7 as "run game" #black
15--! state 8 as "exit game" #brown
16--! state 9 as "http failed" #orange
17--! state 10 as "error" #red
18--!
19--! [*] --> 1
20--! 1 --> 2
21--! 2 --> 3
22--! 3 --> 4
23--! 4 --> 5
24--! 5 --> 6
25--! 6 --> 7
26--! 7 --> 8
27--! 8 --> 4
28--! 2 --> 9
29--! 5 --> 9
30--! 1 -[dotted]-> 10
31--! 2 -[dotted]-> 10
32--! 3 -[dotted]-> 10
33--! 4 -[dotted]-> 10
34--! 5 -[dotted]-> 10
35--! 6 -[dotted]-> 10
36--! 7 -[dotted]-> 10
37--! 8 -[dotted]-> 10
38--! 10 --> [*]
39--! 9 --> [*]
40--! @enduml
41
42--! @cond
43local function next_state(game, new_state)
44 if game._state + 1 == new_state then
45 game._state = new_state
46 end
47 if game._state == 2 and new_state == 9 then
48 game._state = new_state
49 end
50 if game._state == 5 and new_state == 9 then
51 game._state = new_state
52 end
53 if game._state == 8 and new_state == 4 then
54 game._state = new_state
55 end
56end
57
58local function halt_state(game)
59 return function (func)
60 local ok, message = pcall(func)
61 if not ok then
62 game._state = 10
63 game._error = message
64 end
65 end
66end
67--! @endcond
68
69local function init(std, game)
70 if not game._state then
71 game._state = 0
72 game._menu = 1
73 game._csv = ''
74 game._list = {}
75 game._source = ''
76 game._menu_time = game.milis
77 game._want_leave = false
78 std.game.exit = function ()
79 game._want_leave = true
80 end
81 end
82 if game._state == 7 then
83 halt_state(game)(function()
84 game._app.callbacks.init(std, game)
85 end)
86 end
87end
88
89local function http(std, game)
90 halt_state(game)(function ()
91 if std.http.error then
93 end
94 if not std.http.ok then
95 next_state(game, 9)
96 game._status = std.http.status
97 game._error = std.http.body
98 end
99 if std.http.body and #std.http.body == 0 then
100 next_state(game, 9)
101 game._status = std.http.status
102 game._error = '<empty>'
103 end
104 if game._state == 2 then
105 game._csv = std.http.body
106 end
107 if game._state == 5 then
108 game._source = std.http.body
109 end
110 end)
111end
112
113local function loop(std, game)
114 if game._state == 0 then
115 next_state(game, 1)
116 elseif game._state == 1 then
117 halt_state(game)(function()
118 next_state(game, 2)
119 std.http.get('http://gh.dornelles.me/games.csv'):run()
120 end)
121 elseif game._state == 2 and #game._csv > 0 then
122 next_state(game, 3)
123 elseif game._state == 3 then
124 halt_state(game)(function()
125 std.csv.decode(game._csv, game._list)
126 game._csv = ''
127 next_state(game, 4)
128 end)
129 elseif game._state == 4 then
130 halt_state(game)(function()
131 local key = std.key.press.down - std.key.press.up
132 if key ~= 0 and game.milis > game._menu_time + 250 then
133 game._menu = std.math.clamp2(game._menu + key, 1, #game._list)
134 game._menu_time = game.milis
135 end
136 if std.key.press.enter == 1 and game.milis > game._menu_time + 250 then
137 game._menu_time = game.milis
138 next_state(game, 5)
139 std.http.get(game._list[game._menu].raw_url):run()
140 end
141 end)
142 elseif game._state == 5 and #game._source > 0 then
143 next_state(game, 6)
144 elseif game._state == 6 then
145 halt_state(game)(function()
146 game._app = std.game.load(game._source)
147 game._app.callbacks.init(std, game)
148 game._source = ''
149 next_state(game, 7)
150 end)
151 elseif game._state == 7 then
152 halt_state(game)(function()
153 if not game._want_leave then
154 game._app.callbacks.loop(std, game)
155 else
156 game._app.callbacks.exit(std, game)
157 game._want_leave = false
158 next_state(game, 8)
159 end
160 end)
161 elseif game._state == 8 then
162 halt_state(game)(function()
163 game._menu_time = game.milis
164 game._app.callbacks.exit(std, game)
165 next_state(game, 4)
166 end)
167 end
168end
169
170local function draw(std, game)
171 if game._state == 1 then
174 std.draw.text(8, 8, 'booting...')
175 elseif game._state == 2 then
178 std.draw.text(8, 8, 'downloading csv...')
179 elseif game._state == 3 then
182 std.draw.text(8, 8, 'parsing csv...')
183 elseif game._state == 4 then
184 std.draw.clear(0x333333FF)
186 std.draw.font('Tiresias', 12)
187 local index = 1
188 while index <= #game._list do
189 std.draw.text(16, 8 + (index * 14), game._list[index].title)
190 std.draw.text(200, 8 + (index * 14), game._list[index].version)
191 std.draw.text(300, 8 + (index * 14), game._list[index].author)
192 index = index + 1
193 end
194 std.draw.color(std.color.red)
195 std.draw.rect(1, 16, 9 + (game._menu * 14), game.width - 32, 16)
196 elseif game._state == 5 then
197 std.draw.clear(std.color.blue)
198 std.draw.color(std.color.white)
199 std.draw.text(8, 8, 'download game...')
200 elseif game._state == 6 then
201 std.draw.clear(std.color.green)
202 std.draw.color(std.color.white)
203 std.draw.text(8, 8, 'loading game...')
204 elseif game._state == 7 then
205 halt_state(game)(function()
206 game._app.callbacks.draw(std, game)
207 end)
208 elseif game._state == 8 then
209 std.draw.clear(std.color.gold)
210 std.draw.color(std.color.white)
211 std.draw.text(8, 8, 'exiting game...')
212 elseif game._state == 9 then
213 std.draw.clear(std.color.orange)
214 std.draw.color(std.color.white)
215 std.draw.text(8, 8, 'HTTP ERROR:')
216 std.draw.text(200, 8, game._status)
217 std.draw.text(8, 32, game._error)
218 elseif game._state == 10 then
219 std.draw.clear(std.color.red)
220 std.draw.color(std.color.white)
221 std.draw.text(8, 8, 'FATAL ERROR:')
222 std.draw.text(8, 32, game._error)
223 end
224end
225
226local function exit(std, game)
227 if game._state == 7 then
228 halt_state(game)(function()
229 game._app.callbacks.exit(std, game)
230 end)
231 end
232end
233
234local P = {
235 meta={
236 title='Launcher Games',
237 description='online multi game list',
238 author='Rodrigo Dornelles',
239 version='1.0.0'
240 },
241 config={
242 require='http random math csv load'
243 },
244 callbacks={
245 init=init,
246 loop=loop,
247 draw=draw,
248 http=http,
249 exit=exit
250 }
251}
252
253return P
local function decode(in_str, out_table)
local function run(self)
local function file(self, file)
local function require(std, game, application)
local function body(self, content)
local function error(self, handler_func)
local function clamp2(value, value_min, value_max)
clamp
local darkbrown
Definition color.lua:27
local green
Definition color.lua:16
local orange
Definition color.lua:12
local brown
Definition color.lua:26
local white
Definition color.lua:6
local blue
Definition color.lua:20
local gray
Definition color.lua:8
local darkgreen
Definition color.lua:18
local red
Definition color.lua:14
local darkblue
Definition color.lua:21
local black
Definition color.lua:28
local game
Definition main.lua:17
local std
Definition main.lua:18
function love load(args)
local color
Definition main.lua:12
local function clear(c)
local function font(a, b)
local math
Definition draw.lua:1
local function text(x, y, text)
local function draw(std, game)
local function init(std, game)
local function exit(std, game)
local function loop(std, game)
local function http(std, game)