diff --git a/bottleblaster/bottleblaster.lua b/bottleblaster/bottleblaster.lua new file mode 100644 index 0000000..b5d51dc --- /dev/null +++ b/bottleblaster/bottleblaster.lua @@ -0,0 +1,165 @@ +-- Speak with speech-dispatcher +local function speak(text) + os.execute('spd-say "' .. text .. '"') +end + +-- Window related variables. +local gameName = "Bottle Blaster" + +local SDL = require "SDL" +local mixer = require "SDL.mixer" +local ret, err = SDL.init { SDL.flags.Video } +if not ret then + error(err) +end + +local function trySDL(func, ...) + local t = { func(...) } + + if not t[1] then + error(t[#t]) + end + + return table.unpack(t) +end + +local function exit_game(SDL, mixer) + SDL.quit() + mixer.quit() + return false +end + +local win, err = SDL.createWindow { + title = gameName, + width = 320, + height = 320 +} + +if not win then + error(err) +end + +trySDL(mixer.openAudio, 44100, SDL.audioFormat.S16, 2, 1024) +-- Load all game sounds here: +-- Format: local variableName = trySDL(mixer.loadWAV, "path/to/file") +-- Supported file types flac, ogg, wav +local bottle = +{ + trySDL(mixer.loadWAV, "sounds/glass1.ogg"), + trySDL(mixer.loadWAV, "sounds/glass2.ogg"), + trySDL(mixer.loadWAV, "sounds/glass3.ogg") +} +local gun = +{ + trySDL(mixer.loadWAV, "sounds/gun1.ogg"), + trySDL(mixer.loadWAV, "sounds/gun2.ogg"), + trySDL(mixer.loadWAV, "sounds/gun3.ogg"), + trySDL(mixer.loadWAV, "sounds/gun4.ogg"), + trySDL(mixer.loadWAV, "sounds/gun5.ogg") +} +local empty = trySDL(mixer.loadWAV, "sounds/empty.ogg") +local load = {} +load[3] = trySDL(mixer.loadWAV, "sounds/load3.ogg") +load[4] = trySDL(mixer.loadWAV, "sounds/load3.ogg") +load[5] = trySDL(mixer.loadWAV, "sounds/load5.ogg") + +local function play_sound(sound, channel, loop) + channel = channel or -1 + loop = loop or 0 + sound:playChannel(channel, loop) +end + +-- I would rather get rid of this function and just have the one play function +local function pan_sound(mixer, sound) + mixer.SetPanning(-1, 255, 127) + sound:playChannel(-1, 0) +end + +local function game_intro() + local sound = trySDL(mixer.loadWAV, "sounds/game-intro.ogg") +sound:playChannel(-1, 0) + -- It would be better if the SDL.delay could calculate the link of the intro sound and use that instead of 4 * 1000 + -- while sound.sfxIsPlaying(-1) do + SDL.delay(5000) + -- end +end + +-- Game variables +local direction = "" +local holdKey = {} +local keyName = "" +local loaded = true +local playerPosition = math.random(0, 30) +local running = true +local weapon = 1 + +-- game functions. +local function player_move(position, direction) + if direction == "Left" and position > 0 then + position = position - 1 + end + if direction == "Right" and position < 30 then + position = position + 1 + end + return position +end + +game_intro() +-- Main game loop. +while running do + -- Need a timer to make holding arrows move at a slower speed. for player_move() + playerPosition = player_move(playerPosition, direction) + -- Iterate over all events, this function does not block. + for e in SDL.pollEvent() do + if e.type == SDL.event.KeyUp then --chrys just recognice the keyup and free the loop + keyName = SDL.getKeyName(e.keysym.sym) + holdKey.keyName = false + direction = "" + -- speak(playerPosition) + end + if e.type == SDL.event.Quit then + running = false + elseif e.type == SDL.event.KeyDown and not holdKey.keyName then -- chrysif not already down ( see below) + keyName = SDL.getKeyName(e.keysym.sym) + holdKey.keyName = true --chrys mark the remember the keydown + if keyName == "Q" then + running = exit_game(SDL, mixer) + elseif keyName == "Left Shift" or keyName == "Right Shift" then + if weapon >= 3 and loaded == false then + -- Need to not allow firing until loading is complete. + play_sound(load[weapon]) + end + loaded = true + elseif keyName == "Space" then + if loaded == true then + play_sound(gun[weapon]) + play_sound(bottle[math.random(1, #bottle)]) + else + play_sound(empty) + end + if weapon >= 3 then + loaded = false + end + elseif keyName == "Left" or keyName == "Right" then + direction = keyName + elseif tonumber(keyName) == nil then -- make sure keyName can be converted to a number for remaing if statements to avoid a crash. + keyName = "0" + elseif tonumber(keyName) >= 1 and tonumber(keyName) <= 5 then + weapon = tonumber(keyName) + loaded = true + if weapon == 1 then + speak("pistal") + elseif weapon == 2 then + speak("beretta") + elseif weapon == 3 then + speak("boomstick shotgun") + elseif weapon == 4 then + speak("pump action shotgun") + elseif weapon == 5 then + speak("bo and arrow") + end + end + end + end +end + diff --git a/bottleblaster/sounds/bottle.ogg b/bottleblaster/sounds/bottle.ogg new file mode 100644 index 0000000..d107835 Binary files /dev/null and b/bottleblaster/sounds/bottle.ogg differ diff --git a/bottleblaster/sounds/empty.ogg b/bottleblaster/sounds/empty.ogg new file mode 100644 index 0000000..a18f5be Binary files /dev/null and b/bottleblaster/sounds/empty.ogg differ diff --git a/bottleblaster/sounds/game-intro.ogg b/bottleblaster/sounds/game-intro.ogg new file mode 100644 index 0000000..dc372e9 Binary files /dev/null and b/bottleblaster/sounds/game-intro.ogg differ diff --git a/bottleblaster/sounds/glass1.ogg b/bottleblaster/sounds/glass1.ogg new file mode 100644 index 0000000..8a6a183 Binary files /dev/null and b/bottleblaster/sounds/glass1.ogg differ diff --git a/bottleblaster/sounds/glass2.ogg b/bottleblaster/sounds/glass2.ogg new file mode 100644 index 0000000..5507c91 Binary files /dev/null and b/bottleblaster/sounds/glass2.ogg differ diff --git a/bottleblaster/sounds/glass3.ogg b/bottleblaster/sounds/glass3.ogg new file mode 100644 index 0000000..8698ebf Binary files /dev/null and b/bottleblaster/sounds/glass3.ogg differ diff --git a/bottleblaster/sounds/gun1.ogg b/bottleblaster/sounds/gun1.ogg new file mode 100644 index 0000000..b9fbf3e Binary files /dev/null and b/bottleblaster/sounds/gun1.ogg differ diff --git a/bottleblaster/sounds/gun2.ogg b/bottleblaster/sounds/gun2.ogg new file mode 100644 index 0000000..41ca37d Binary files /dev/null and b/bottleblaster/sounds/gun2.ogg differ diff --git a/bottleblaster/sounds/gun3.ogg b/bottleblaster/sounds/gun3.ogg new file mode 100644 index 0000000..87de4f1 Binary files /dev/null and b/bottleblaster/sounds/gun3.ogg differ diff --git a/bottleblaster/sounds/gun4.ogg b/bottleblaster/sounds/gun4.ogg new file mode 100644 index 0000000..52953b3 Binary files /dev/null and b/bottleblaster/sounds/gun4.ogg differ diff --git a/bottleblaster/sounds/gun5.ogg b/bottleblaster/sounds/gun5.ogg new file mode 100644 index 0000000..d05e1e1 Binary files /dev/null and b/bottleblaster/sounds/gun5.ogg differ diff --git a/bottleblaster/sounds/load3.ogg b/bottleblaster/sounds/load3.ogg new file mode 100644 index 0000000..af7819c Binary files /dev/null and b/bottleblaster/sounds/load3.ogg differ diff --git a/bottleblaster/sounds/load4.ogg b/bottleblaster/sounds/load4.ogg new file mode 100644 index 0000000..af7819c Binary files /dev/null and b/bottleblaster/sounds/load4.ogg differ diff --git a/bottleblaster/sounds/load5.ogg b/bottleblaster/sounds/load5.ogg new file mode 100644 index 0000000..0ff00a4 Binary files /dev/null and b/bottleblaster/sounds/load5.ogg differ