config check: be more precise about what we check
[awesome.git] / lib / invaders.lua.in
blobee65221092addf764e634a81e5909aa0677c1e65
1 ----------------------------------------------------------------------------
2 -- @author Gregor "farhaven" Best
3 -- @copyright 2008 Gregor "farhaven" Best
4 -- @release @AWESOME_VERSION@
5 ----------------------------------------------------------------------------
7 --{{{ Awesome Invaders by Gregor "farhaven" Best
8 -- The ultra-cool retro graphics are done by Andrei "Garoth" Thorp.
9 --
10 -- Use Left and Right to control motion, Space to fire, q quits the game,
11 -- s creates a screenshot in ~/.cache/awesome (needs ImageMagick).
13 -- Maybe there are some huge memory leaks here and there, if you notice one,
14 -- message me.
16 -- Anyway, have fun :)
18 -- Start this by adding
19 -- require("invaders")
20 -- to the top of your rc.lua and adding
21 -- invaders.run()
22 -- to the bottom of rc.lua or as a keybinding
23 --}}}
25 local wibox = wibox
26 local widget = widget
27 local awful = require("awful")
28 local beautiful = require("beautiful")
29 local image = image
30 local capi = { screen = screen, mouse = mouse, keygrabber = keygrabber }
32 local tonumber = tonumber
33 local table = table
34 local math = math
35 local os = os
36 local io = io
38 --- Space Invaders look-alike
39 module("invaders")
41 local gamedata = { }
42 gamedata.field = { }
43 gamedata.field.x = 100
44 gamedata.field.y = 100
45 gamedata.field.h = 400
46 gamedata.field.w = 600
47 gamedata.running = false
48 gamedata.highscore = { }
49 gamedata.enemies = { }
50 gamedata.enemies.h = 10
51 gamedata.enemies.w = 20
52 gamedata.enemies.rows = 5
53 gamedata.enemies.count = gamedata.enemies.rows * 6
54 gamedata.enemies[1] = image("@AWESOME_ICON_PATH@/invaders/enemy_1.png")
55 gamedata.enemies[2] = image("@AWESOME_ICON_PATH@/invaders/enemy_2.png")
56 gamedata.enemies[3] = image("@AWESOME_ICON_PATH@/invaders/enemy_3.png")
58 local player = { }
59 local game = { }
60 local shots = { }
61 local enemies = { }
63 function player.new ()
64 p = wibox({ position = "floating",
65 bg = gamedata.solidbg or "#12345600" })
66 p:geometry({ width = 24,
67 height = 16,
68 x = gamedata.field.x + (gamedata.field.w / 2),
69 y = gamedata.field.y + gamedata.field.h - (16 + 5) })
70 p.screen = 1
72 w = widget({ type = "imagebox", name = "player" })
73 w.image = image("@AWESOME_ICON_PATH@/invaders/player.png")
74 p.widgets = w
76 return p
77 end
79 function player.move(x)
80 if not gamedata.running then return false end
81 local g = gamedata.player:geometry()
83 if x < 0 and g.x > gamedata.field.x then
84 g.x = g.x + x
85 elseif x > 0 and g.x < gamedata.field.x + gamedata.field.w - 30 then
86 g.x = g.x + x
87 end
89 gamedata.player:geometry(g)
90 end
92 function player.fire()
93 if not gamedata.running then return false end
94 if gamedata.ammo == 1 then
95 gamedata.ammo = 0
96 local gb = gamedata.player:geometry()
97 shots.fire(gb.x + 9, gb.y - 10, "#00FF00")
98 end
99 end
101 function shots.fire (x, y, color)
102 local s = wibox({ position = "floating",
103 bg = color })
104 s:geometry({ width = 4,
105 height = 10,
106 x = x,
107 y = y })
108 s.screen = 1
110 if not gamedata.shot or gamedata.shot.screen == nil then
111 gamedata.shot = s
115 function shots.fire_enemy (x, y, color)
116 if gamedata.enemies.shots.fired < gamedata.enemies.shots.max then
117 gamedata.enemies.shots.fired = gamedata.enemies.shots.fired + 1
118 local s = wibox({ position = "floating",
119 bg = color })
120 s:geometry({ width = 4,
121 height = 10,
122 x = x,
123 y = y })
124 s.screen = 1
125 for i = 1, gamedata.enemies.shots.max do
126 if not gamedata.enemies.shots[i] or gamedata.enemies.shots[i].screen == nil then
127 gamedata.enemies.shots[i] = s
128 break
134 function shots.handle()
135 if not gamedata.running then return false end
136 if gamedata.ammo == 1 then return false end
138 local s = gamedata.shot
139 if s and s.screen then
140 gamedata.ammo = 0
141 local g = s:geometry()
142 if g.y < gamedata.field.y + 15 then
143 s.screen = nil
144 gamedata.ammo = 1
145 else
146 g.y = g.y - 6
147 s:geometry(g)
153 function shots.handle_enemy ()
154 if not gamedata.running then return false end
155 if gamedata.enemies.shots.fired == 0 then return false end
157 for i = 1, gamedata.enemies.shots.max do
158 local s = gamedata.enemies.shots[i]
159 if s and s.screen then
160 local g = s:geometry()
161 if g.y > gamedata.field.y + gamedata.field.h - 15 then
162 s.screen = nil
163 gamedata.enemies.shots.fired = gamedata.enemies.shots.fired - 1
164 else
165 g.y = g.y + 3
166 s:geometry(g)
168 if game.collide(gamedata.player, s) then
169 game.over()
175 function enemies.new (t)
176 e = wibox({ position = "floating",
177 bg = gamedata.solidbg or "#12345600" })
178 e:geometry({ height = gamedata.enemies.h,
179 width = gamedata.enemies.w,
180 x = gamedata.field.x,
181 y = gamedata.field.y })
182 e.screen = 1
183 w = widget({ type = "imagebox",
184 name = "enemy"..t })
185 w.image = gamedata.enemies[t]
187 e.widgets = w
188 return e
191 function enemies.setup()
192 gamedata.enemies.data = { }
193 gamedata.enemies.x = 10
194 gamedata.enemies.y = 5
195 gamedata.enemies.dir = 1
196 if not gamedata.enemies.shots then gamedata.enemies.shots = { } end
197 gamedata.enemies.shots.max = 10
198 gamedata.enemies.shots.fired = 0
200 gamedata.enemies.speed_count = 0
202 for y = 1, gamedata.enemies.rows do
203 gamedata.enemies.data[y] = { }
204 for x = 1, math.ceil((gamedata.enemies.count / gamedata.enemies.rows) + 1) do
205 gamedata.enemies.data[y][x] = enemies.new((y % 3) + 1)
209 if gamedata.shot then
210 gamedata.shot.screen = nil
213 for i = 1, gamedata.enemies.shots.max do
214 if gamedata.enemies.shots[i] then
215 gamedata.enemies.shots[i].screen = nil
220 function enemies.handle ()
221 if not gamedata.running then return false end
223 gamedata.enemies.number = 0
225 for y = 1, #gamedata.enemies.data do
226 for x = 1, #gamedata.enemies.data[y] do
227 local e = gamedata.enemies.data[y][x]
228 if e.screen then
229 local g = e:geometry()
230 gamedata.enemies.number = gamedata.enemies.number + 1
231 if gamedata.enemies.speed_count == (gamedata.enemies.speed - 1) then
232 g.y = math.floor(gamedata.field.y + gamedata.enemies.y + ((y - 1) * gamedata.enemies.h * 2))
233 g.x = math.floor(gamedata.field.x + gamedata.enemies.x + ((x - 1) * gamedata.enemies.w * 2))
234 e:geometry(g)
235 if game.collide(gamedata.player, e) or g.y > gamedata.field.y + gamedata.field.h - 20 then
236 game.over()
239 if gamedata.ammo == 0 then
240 local s = gamedata.shot
241 if s and s.screen and game.collide(e, s) then
242 gamedata.enemies.number = gamedata.enemies.number - 1
243 gamedata.ammo = 1
244 e.screen = nil
245 s.screen = nil
247 if (y % 3) == 0 then
248 gamedata.score = gamedata.score + 15
249 elseif (y % 3) == 1 then
250 gamedata.score = gamedata.score + 10
251 else
252 gamedata.score = gamedata.score + 5
254 gamedata.field.status.text = gamedata.score.." | "..gamedata.round.." "
260 local x = math.random(1, gamedata.enemies.count / gamedata.enemies.rows)
261 if gamedata.enemies.speed_count == (gamedata.enemies.speed - 1)
262 and math.random(0, 150) <= 1
263 and gamedata.enemies.data[y][x].screen then
264 shots.fire_enemy(math.floor(gamedata.field.x + gamedata.enemies.x + ((x - 1) * gamedata.enemies.w)),
265 gamedata.field.y + gamedata.enemies.y + gamedata.enemies.h,
266 "#FF0000")
270 if gamedata.enemies.number == 0 then
271 enemies.setup()
272 gamedata.round = gamedata.round + 1
273 gamedata.field.status.text = gamedata.score.." | "..gamedata.round.." "
274 if gamedata.enemies.speed > 1 then gamedata.enemies.speed = gamedata.enemies.speed - 1 end
275 return false
278 gamedata.enemies.speed_count = gamedata.enemies.speed_count + 1
279 if gamedata.enemies.speed_count < gamedata.enemies.speed then return false end
280 gamedata.enemies.speed_count = 0
281 gamedata.enemies.x = gamedata.enemies.x + math.floor((gamedata.enemies.w * gamedata.enemies.dir) / 4)
282 if gamedata.enemies.x > gamedata.field.w - (2 * gamedata.enemies.w * (gamedata.enemies.count / gamedata.enemies.rows + 1)) + 5
283 or gamedata.enemies.x <= 10 then
284 gamedata.enemies.y = gamedata.enemies.y + gamedata.enemies.h
285 gamedata.enemies.dir = gamedata.enemies.dir * (-1)
289 function keyhandler(mod, key)
290 if gamedata.highscore.getkeys then
291 if key:len() == 1 and gamedata.name:len() < 20 then
292 gamedata.name = gamedata.name .. key
293 elseif key == "BackSpace" then
294 gamedata.name = gamedata.name:sub(1, gamedata.name:len() - 1)
295 elseif key == "Return" then
296 gamedata.highscore.window.screen = nil
297 game.highscore_add(gamedata.score, gamedata.name)
298 game.highscore_show()
299 gamedata.highscore.getkeys = false
301 gamedata.namebox.text = " Name: " .. gamedata.name .. "|"
302 else
303 if key == "Left" then
304 player.move(-10)
305 elseif key == "Right" then
306 player.move(10)
307 elseif key == "q" then
308 game.quit()
309 return false
310 elseif key == " " then
311 player.fire()
312 elseif key == "s" then
313 awful.util.spawn("import -window root "..gamedata.cachedir.."/invaders-"..os.time()..".png")
314 elseif key == "p" then
315 gamedata.running = not gamedata.running
318 return true
321 function game.collide(o1, o2)
322 g1 = o1:geometry()
323 g2 = o2:geometry()
325 --check if o2 is inside o1
326 if g2.x >= g1.x and g2.x <= g1.x + g1.width
327 and g2.y >= g1.y and g2.y <= g1.y + g1.height then
328 return true
331 return false
334 function game.over ()
335 gamedata.running = false
336 game.highscore(gamedata.score)
339 function game.quit()
340 gamedata.running = false
342 if gamedata.highscore.window then
343 gamedata.highscore.window.screen = nil
344 gamedata.highscore.window.widgets = nil
347 if gamedata.field.background then
348 gamedata.field.background.screen = nil
351 gamedata.player.screen = nil
352 gamedata.player.widgets = nil
353 gamedata.player = nil
355 gamedata.field.north.screen = nil
356 gamedata.field.north = nil
358 gamedata.field.south.screen = nil
359 gamedata.field.south = nil
361 gamedata.field.west.screen = nil
362 gamedata.field.west = nil
364 gamedata.field.east.screen = nil
365 gamedata.field.east = nil
367 for y = 1, #gamedata.enemies.data do
368 for x = 1, #gamedata.enemies.data[y] do
369 gamedata.enemies.data[y][x].screen = nil
370 gamedata.enemies.data[y][x].widgets = nil
374 if gamedata.shot then gamedata.shot.screen = nil end
376 for i = 1, gamedata.enemies.shots.max do
377 if gamedata.enemies.shots[i] then gamedata.enemies.shots[i].screen = nil end
381 function game.highscore_show ()
382 gamedata.highscore.window:geometry({ height = 140,
383 width = 200,
384 x = gamedata.field.x + math.floor(gamedata.field.w / 2) - 100,
385 y = gamedata.field.y + math.floor(gamedata.field.h / 2) - 55 })
386 gamedata.highscore.window.screen = 1
388 gamedata.highscore.table = widget({ type = "textbox",
389 name = "highscore" })
390 gamedata.highscore.window.widgets = gamedata.highscore.table
392 gamedata.highscore.table.text = " Highscores:\n"
394 for i = 1, 5 do
395 gamedata.highscore.table.text = gamedata.highscore.table.text .. "\n\t" .. gamedata.highscore[i]
398 gamedata.highscore.table.text = gamedata.highscore.table.text .. "\n\n Press Q to quit"
400 local fh = io.open(gamedata.cachedir.."/highscore_invaders", "w")
402 if not fh then
403 return false
406 for i = 1, 5 do
407 fh:write(gamedata.highscore[i].."\n")
410 fh:close()
413 function game.highscore_add (score, name)
414 local t = gamedata.highscore
416 for i = 5, 1, -1 do
417 if tonumber(t[i]:match("(%d+) ")) <= score then
418 if t[i+1] then t[i+1] = t[i] end
419 t[i] = score .. " " .. name
423 gamedata.highscore = t
426 function game.highscore (score)
427 if gamedata.highscore.window and gamedata.highscore.window.screen then return false end
428 local fh = io.open(gamedata.cachedir.."/highscore_invaders", "r")
430 if fh then
431 for i = 1, 5 do
432 gamedata.highscore[i] = fh:read("*line")
434 fh:close()
435 else
436 for i = 1, 5 do
437 gamedata.highscore[i] = ((6-i)*20).." foo"
441 local newentry = false
442 for i = 1, 5 do
443 local s = tonumber(gamedata.highscore[i]:match("(%d+) .*"))
444 if s <= score then newentry = true end
447 gamedata.highscore.window = wibox({ position = "floating",
448 bg = gamedata.btheme.bg_focus or "#333333",
449 fg = gamedata.btheme.fg_focus or "#FFFFFF" })
450 gamedata.highscore.window:geometry({ height = 20,
451 width = 300,
452 x = gamedata.field.x + math.floor(gamedata.field.w / 2) - 150,
453 y = gamedata.field.y + math.floor(gamedata.field.h / 2) })
454 gamedata.highscore.window.screen = 1
456 gamedata.namebox = widget({ type = "textbox",
457 name = "foobar" })
458 gamedata.namebox.text = " Name: |"
459 gamedata.highscore.window.widgets = gamedata.namebox
461 if newentry then
462 gamedata.name = ""
463 gamedata.highscore.getkeys = true
464 else
465 game.highscore_show()
469 --- Run Awesome Invaders
470 -- @param args A table with parameters.
471 -- x the X coordinate of the playing field.
472 -- y the Y coordinate of the playing field.
473 -- if either of these is left out, the game is placed in the center of the focused screen.
474 -- solidbg the background color of the playing field. If none is given, the playing field is transparent.
475 function run(args)
476 gamedata.screen = capi.screen[capi.mouse.screen]
477 gamedata.field.x = gamedata.screen.geometry.x + math.floor((gamedata.screen.geometry.width - gamedata.field.w) / 2)
478 gamedata.field.y = gamedata.screen.geometry.y + math.floor((gamedata.screen.geometry.height - gamedata.field.h) / 2)
480 if args then
481 if args['x'] then gamedata.field.x = args['x'] end
482 if args['y'] then gamedata.field.y = args['y'] end
483 if args['solidbg'] then gamedata.solidbg = args['solidbg'] end
486 gamedata.score = 0
487 gamedata.name = ""
488 gamedata.ammo = 1
489 gamedata.round = 1
490 gamedata.btheme = beautiful.get()
492 gamedata.cachedir = awful.util.getdir("cache")
494 if gamedata.solidbg then
495 gamedata.field.background = wibox({ position = "floating",
496 bg = gamedata.solidbg })
497 gamedata.field.background:geometry({ x = gamedata.field.x,
498 y = gamedata.field.y,
499 height = gamedata.field.h,
500 width = gamedata.field.w })
501 gamedata.field.background.screen = 1
504 gamedata.field.north = wibox({ position = "floating",
505 bg = gamedata.btheme.bg_focus or "#333333",
506 fg = gamedata.btheme.fg_focus or "#FFFFFF" })
507 gamedata.field.north:geometry({ width = gamedata.field.w + 10,
508 height = 15,
509 x = gamedata.field.x - 5,
510 y = gamedata.field.y - 15 })
511 gamedata.field.north.screen = 1
513 gamedata.field.status = widget({ type = "textbox",
514 name = "status",
515 align = "right" })
516 gamedata.field.status.text = gamedata.score.." | "..gamedata.round .. " "
518 gamedata.field.caption = widget({ type = "textbox",
519 name = "caption",
520 align = "left" })
521 gamedata.field.caption.text = " Awesome Invaders"
523 gamedata.field.north.widgets = { gamedata.field.caption, gamedata.field.status }
525 gamedata.field.south = wibox({ position = "floating",
526 bg = gamedata.btheme.bg_focus or "#333333",
527 fg = gamedata.btheme.fg_focus or "#FFFFFF" })
528 gamedata.field.south:geometry({ width = gamedata.field.w,
529 height = 5,
530 x = gamedata.field.x,
531 y = gamedata.field.y + gamedata.field.h - 5 })
532 gamedata.field.south.screen = 1
534 gamedata.field.west = wibox({ position = "floating",
535 bg = gamedata.btheme.bg_focus or "#333333",
536 fg = gamedata.btheme.fg_focus or "#FFFFFF" })
537 gamedata.field.west:geometry({ width = 5,
538 height = gamedata.field.h,
539 x = gamedata.field.x - 5,
540 y = gamedata.field.y })
541 gamedata.field.west.screen = 1
543 gamedata.field.east = wibox({ position = "floating",
544 bg = gamedata.btheme.bg_focus or "#333333",
545 fg = gamedata.btheme.fg_focus or "#FFFFFF" })
546 gamedata.field.east:geometry({ width = 5,
547 height = gamedata.field.h,
548 x = gamedata.field.x + gamedata.field.w,
549 y = gamedata.field.y })
550 gamedata.field.east.screen = 1
552 gamedata.enemies.speed = 5
553 enemies.setup()
555 gamedata.player = player.new()
556 capi.keygrabber.run(keyhandler)
557 gamedata.running = true
560 awful.hooks.timer.register(0.02, shots.handle)
561 awful.hooks.timer.register(0.03, shots.handle_enemy)
562 awful.hooks.timer.register(0.01, enemies.handle)