Various changes to awful.util and invaders

This commit changes various aspects of awful.util and invaders:

awful.util:

- added awful.util.getdir(d)
  This function takes one argument and returns a matching directory,
  as of now, only "cache" is supported. The return value is either
  $XDG_CACHE_HOME/awesome/ or $HOME/.cache/awesome/, XDG_CACHE_HOME
  takes precedence

invaders:

- renamed invaders to awesome invaders
  at two places in the sourcecode, invaders is referred to as
  "Space Invaders for Awesome". As Taiko holds the trademark for
  the term "Space Invaders", I changed both of its occurences to
  "Awesome Invaders" to avoid conflicts with the law of Japan and
  the United States of America (and possibly others)

- added support for XDG_CACHE_HOME
  this change adds support for XDG_CACHE_HOME as the cache directory
  for highscores and screenshots

- added some parameters to invaders.run()
  this change adds three parameters to invaders.run, supplied as a
  table. They are "x", "y" and "solidbg".

  "x" sets the X coordinate of the playfield
  "y" sets the Y coordinate of the playfield
  "solidbg" sets the color of the playfield background for people who
  have problems with transparency. This still looks rather hackish and
  needs to be polished

- changed startup position
  up until now, invaders always started at (100,100) on the first
  screen, now it starts centered to the screen on which the mouse cursor
  is.
This commit is contained in:
Gregor Best 2008-10-17 19:05:57 +02:00 committed by Julien Danjou
parent 1f8d52b3f5
commit 072937ec70
2 changed files with 64 additions and 32 deletions

View File

@ -137,4 +137,12 @@ function restart()
capi.awesome.restart() capi.awesome.restart()
end end
function getdir(d)
if d == "cache" then
if os.getenv("XDG_CACHE_HOME") then
return os.getenv("XDG_CACHE_HOME").."/awesome"
end
return os.getenv("HOME").."/.cache/awesome"
end
end
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80 -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80

View File

@ -4,7 +4,7 @@
-- @release @AWESOME_VERSION@ -- @release @AWESOME_VERSION@
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
--{{{ Space Invaders for awesome 3 GIT by Gregor "farhaven" Best --{{{ Awesome Invaders by Gregor "farhaven" Best
-- The ultra-cool retro graphics are done by Andrei "Garoth" Thorp. -- The ultra-cool retro graphics are done by Andrei "Garoth" Thorp.
-- --
-- Use Left and Right to control motion, Space to fire, q quits the game, -- Use Left and Right to control motion, Space to fire, q quits the game,
@ -28,6 +28,7 @@ local awful = require("awful")
local beautiful = require("awful.beautiful") local beautiful = require("awful.beautiful")
local keygrabber = keygrabber local keygrabber = keygrabber
local image = image local image = image
local capi = { screen = screen, mouse = mouse }
local tonumber = tonumber local tonumber = tonumber
local table = table local table = table
@ -35,7 +36,7 @@ local math = math
local os = os local os = os
local io = io local io = io
--- Space invaders game --- Space Invaders look-alike
module("invaders") module("invaders")
local gamedata = { } local gamedata = { }
@ -55,24 +56,22 @@ local shots = { }
local enemies = { } local enemies = { }
function player.new () function player.new ()
local p = { } p = wibox({ position = "floating", bg = gamedata.solidbg or "#12345600" })
p:geometry({ width = 24,
p.base = wibox({ position = "floating", bg = "#12345600" })
p.base:geometry({ width = 24,
height = 16, height = 16,
x = gamedata.field.x + (gamedata.field.w / 2), x = gamedata.field.x + (gamedata.field.w / 2),
y = gamedata.field.y + gamedata.field.h - (16 + 5) }) y = gamedata.field.y + gamedata.field.h - (16 + 5) })
p.base.screen = gamedata.screen p.screen = 1
local w = widget({ type = "imagebox", name = "player", align = "right" }) w = widget({ type = "imagebox", name = "player" })
w.image = image("@AWESOME_ICON_PATH@/invaders/player.png") w.image = image("@AWESOME_ICON_PATH@/invaders/player.png")
p.base:widgets({ w }) p:widgets({ w })
return p return p
end end
function player.move(x) function player.move(x)
local gb = gamedata.player.base:geometry() local gb = gamedata.player:geometry()
if x < 0 and gb.x > gamedata.field.x then if x < 0 and gb.x > gamedata.field.x then
gb.x = gb.x + x gb.x = gb.x + x
@ -80,14 +79,14 @@ function player.move(x)
gb.x = gb.x + x gb.x = gb.x + x
end end
gamedata.player.base:geometry(gb) gamedata.player:geometry(gb)
end end
function player.fire() function player.fire()
if gamedata.ammo > 0 then if gamedata.ammo > 0 then
gamedata.ammo = gamedata.ammo - 1 gamedata.ammo = gamedata.ammo - 1
gamedata.field.status.text = gamedata.score.." | "..gamedata.ammo .. " " gamedata.field.status.text = gamedata.score.." | "..gamedata.ammo .. " "
local gb = gamedata.player.base:geometry() local gb = gamedata.player:geometry()
shots.fire(gb.x + 9, gb.y - 10, "#00FF00") shots.fire(gb.x + 9, gb.y - 10, "#00FF00")
end end
end end
@ -159,7 +158,7 @@ function shots.handle_enemy ()
g.y = g.y + 3 g.y = g.y + 3
s:geometry(g) s:geometry(g)
end end
if game.collide(gamedata.player.base, s) then if game.collide(gamedata.player, s) then
game.over() game.over()
end end
end end
@ -167,7 +166,7 @@ function shots.handle_enemy ()
end end
function enemies.new (t) function enemies.new (t)
e = wibox({ position = "floating", bg = "#12345600" }) e = wibox({ position = "floating", bg = gamedata.solidbg or "#12345600" })
e:geometry({ height = gamedata.enemies.h, width = gamedata.enemies.w, e:geometry({ height = gamedata.enemies.h, width = gamedata.enemies.w,
x = gamedata.field.x, x = gamedata.field.x,
y = gamedata.field.y }) y = gamedata.field.y })
@ -228,12 +227,12 @@ function enemies.handle ()
g.y = math.floor(gamedata.field.y + gamedata.enemies.y + ((y - 1) * gamedata.enemies.h * 2)) g.y = math.floor(gamedata.field.y + gamedata.enemies.y + ((y - 1) * gamedata.enemies.h * 2))
g.x = math.floor(gamedata.field.x + gamedata.enemies.x + ((x - 1) * gamedata.enemies.w * 2)) g.x = math.floor(gamedata.field.x + gamedata.enemies.x + ((x - 1) * gamedata.enemies.w * 2))
e:geometry(g) e:geometry(g)
if game.collide(gamedata.player.base, e) or g.y > gamedata.field.y + gamedata.field.h - 20 then if game.collide(gamedata.player, e) or g.y > gamedata.field.y + gamedata.field.h - 20 then
game.over() game.over()
end end
end end
if gamedata.ammo < gamedata.ammo_max then if gamedata.ammo < gamedata.ammo_max then
for i = 1, #gamedata.shots do for i = 1, gamedata.ammo_max do
local s = gamedata.shots[i] local s = gamedata.shots[i]
if s and s.screen and game.collide(e, s) then if s and s.screen and game.collide(e, s) then
gamedata.enemies.number = gamedata.enemies.number - 1 gamedata.enemies.number = gamedata.enemies.number - 1
@ -300,7 +299,7 @@ function keyhandler(mod, key)
elseif key == " " then elseif key == " " then
player.fire() player.fire()
elseif key == "s" then elseif key == "s" then
awful.util.spawn("import -window root ~/.cache/awesome/invaders-"..os.time()..".png") awful.util.spawn("import -window root "..gamedata.cachedir.."/awesome/invaders-"..os.time()..".png")
end end
end end
return true return true
@ -332,9 +331,13 @@ function game.quit()
gamedata.highscore.window:widgets({ }) gamedata.highscore.window:widgets({ })
end end
gamedata.player.base.screen = nil if gamedata.field.background then
gamedata.player.base:widgets({ }) gamedata.field.background.screen = nil
gamedata.player.base = nil end
gamedata.player.screen = nil
gamedata.player:widgets({ })
gamedata.player = nil
gamedata.field.north.screen = nil gamedata.field.north.screen = nil
gamedata.field.north = nil gamedata.field.north = nil
@ -381,7 +384,8 @@ function game.highscore_show ()
gamedata.highscore.table.text = gamedata.highscore.table.text .. "\n\n Press Q to quit" gamedata.highscore.table.text = gamedata.highscore.table.text .. "\n\n Press Q to quit"
local fh = io.open(os.getenv("HOME") .. "/.cache/awesome/highscore_invaders","w") local fh = io.open(gamedata.cachedir.."/highscore_invaders", "w")
if not fh then if not fh then
return false return false
end end
@ -407,7 +411,8 @@ function game.highscore_add (score, name)
end end
function game.highscore (score) function game.highscore (score)
local fh = io.open(os.getenv("HOME").."/.cache/awesome/highscore_invaders", "r") local fh = io.open(gamedata.cachedir.."/highscore_invaders", "r")
if fh then if fh then
for i = 1, 5 do for i = 1, 5 do
gamedata.highscore[i] = fh:read("*line") gamedata.highscore[i] = fh:read("*line")
@ -445,16 +450,34 @@ function game.highscore (score)
end end
end end
function run() function run(args)
gamedata.running = true gamedata.screen = capi.screen[capi.mouse.screen]
gamedata.screen = 1 gamedata.field.x = gamedata.screen.coords.x + math.floor((gamedata.screen.coords.width - gamedata.field.w) / 2)
gamedata.field.y = gamedata.screen.coords.y + math.floor((gamedata.screen.coords.height - gamedata.field.h) / 2)
if args then
if args['x'] then gamedata.field.x = args['x'] end
if args['y'] then gamedata.field.y = args['y'] end
if args['solidbg'] then gamedata.solidbg = args['solidbg'] end
end
gamedata.ammo_max = 10 gamedata.ammo_max = 10
gamedata.score = 0 gamedata.score = 0
gamedata.name = "" gamedata.name = ""
gamedata.ammo = gamedata.ammo_max gamedata.ammo = gamedata.ammo_max
gamedata.btheme = beautiful.get() gamedata.btheme = beautiful.get()
gamedata.player = player.new()
gamedata.cachedir = awful.util.getdir("cache")
if gamedata.solidbg then
gamedata.field.background = wibox({ position = "floating",
bg = gamedata.solidbg })
gamedata.field.background:geometry({ x = gamedata.field.x,
y = gamedata.field.y,
height = gamedata.field.h,
width = gamedata.field.w })
gamedata.field.background.screen = 1
end
gamedata.field.north = wibox({ position = "floating", gamedata.field.north = wibox({ position = "floating",
bg = gamedata.btheme.bg_focus or "#333333", bg = gamedata.btheme.bg_focus or "#333333",
@ -463,13 +486,13 @@ function run()
height = 15, height = 15,
x = gamedata.field.x - 5, x = gamedata.field.x - 5,
y = gamedata.field.y - 15 }) y = gamedata.field.y - 15 })
gamedata.field.north.screen = gamedata.screen gamedata.field.north.screen = 1
gamedata.field.status = widget({ type = "textbox", name = "status", align = "right" }) gamedata.field.status = widget({ type = "textbox", name = "status", align = "right" })
gamedata.field.status.text = gamedata.score.." | "..gamedata.ammo .. " " gamedata.field.status.text = gamedata.score.." | "..gamedata.ammo .. " "
gamedata.field.caption = widget({ type = "textbox", name = "caption", align = "left" }) gamedata.field.caption = widget({ type = "textbox", name = "caption", align = "left" })
gamedata.field.caption.text = " SpaceInvaders for Awesome 3" gamedata.field.caption.text = " Awesome Invaders"
gamedata.field.north:widgets({ gamedata.field.caption, gamedata.field.status }) gamedata.field.north:widgets({ gamedata.field.caption, gamedata.field.status })
@ -479,7 +502,7 @@ function run()
gamedata.field.south:geometry({ width = gamedata.field.w, height = 5, gamedata.field.south:geometry({ width = gamedata.field.w, height = 5,
x = gamedata.field.x, x = gamedata.field.x,
y = gamedata.field.y + gamedata.field.h - 5 }) y = gamedata.field.y + gamedata.field.h - 5 })
gamedata.field.south.screen = gamedata.screen gamedata.field.south.screen = 1
gamedata.field.west = wibox({ position = "floating", gamedata.field.west = wibox({ position = "floating",
bg = gamedata.btheme.bg_focus or "#333333", bg = gamedata.btheme.bg_focus or "#333333",
@ -488,7 +511,7 @@ function run()
height = gamedata.field.h, height = gamedata.field.h,
x = gamedata.field.x - 5, x = gamedata.field.x - 5,
y = gamedata.field.y }) y = gamedata.field.y })
gamedata.field.west.screen = gamedata.screen gamedata.field.west.screen = 1
gamedata.field.east = wibox({ position = "floating", gamedata.field.east = wibox({ position = "floating",
bg = gamedata.btheme.bg_focus or "#333333", bg = gamedata.btheme.bg_focus or "#333333",
@ -497,13 +520,14 @@ function run()
height = gamedata.field.h, height = gamedata.field.h,
x = gamedata.field.x + gamedata.field.w, x = gamedata.field.x + gamedata.field.w,
y = gamedata.field.y }) y = gamedata.field.y })
gamedata.field.east.screen = gamedata.screen gamedata.field.east.screen = 1
gamedata.enemies.speed = 5 gamedata.enemies.speed = 5
enemies.setup() enemies.setup()
gamedata.player = player.new()
keygrabber.run(keyhandler) keygrabber.run(keyhandler)
gamedata.running = true
end end
awful.hooks.timer.register(0.03, shots.handle) awful.hooks.timer.register(0.03, shots.handle)