From 630e89d96b53dec92b0f12d6143235b8e58a9a05 Mon Sep 17 00:00:00 2001 From: Paul Liverman Date: Wed, 11 Feb 2015 12:02:33 -0800 Subject: [PATCH] Revert "fixed GitHub glitch hopefully" This reverts commit 898eefc21d6d531ffa85b16cee6909ae893624a6. --- src/gamestates/game.lua | 32 ++++++++++++++++---- src/gamestates/lost.lua | 50 +++++++++++++++++++++---------- src/gamestates/menu.lua | 30 ++++++++++++++++++- src/gamestates/paused.lua | 37 +++++++++++++++++++---- src/main.lua | 62 +++++++++------------------------------ 5 files changed, 137 insertions(+), 74 deletions(-) diff --git a/src/gamestates/game.lua b/src/gamestates/game.lua index f95b99b..a8c76ea 100644 --- a/src/gamestates/game.lua +++ b/src/gamestates/game.lua @@ -15,6 +15,7 @@ local boxes = {} local score, totalScore = 0, 0 local level, time, startingTime = 0, 0, 0 local previousState, gameSettings, controls +local pingTimer, session = 0, false --these are defined on each entry to this gamestate local screenWidth, screenHeight --defines where things are rendered local boxColumns, boxRows --defines how many boxes @@ -62,7 +63,7 @@ local function copyColor(A) return {A[1], A[2], A[3]} end -function game:enter(previous, settings, gameControls) +function game:enter(previous, settings, gameControls, gamejoltSession) log("Entering game...") -- save the state we came from previousState = previous @@ -74,6 +75,14 @@ function game:enter(previous, settings, gameControls) -- save the settings for later use gameSettings = settings or gameSettings controls = gameControls or controls + session = gamejoltSession + -- ping our active state immediately + if session then + local pingSuccess = Gamejolt.pingSession(true) + if not pingSuccess then + log("Couldn't ping Game Jolt session. Session may close.") + end + end -- set how to play the game based on settings boxSize = gameSettings.boxSize colorStep = gameSettings.colorStep @@ -97,6 +106,18 @@ function game:resume(previous, action) end function game:update(dt) + -- ping every 30 seconds if we are in a session + pingTimer = pingTimer + dt + if pingTimer >= 30 then + if session then + local pingSuccess = Gamejolt.pingSession(true) + if not pingSuccess then + log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care + end + end + pingTimer = pingTimer - 30 + end + -- check if level complete local coloredBoxes = {} for i=0,#boxes do @@ -126,7 +147,7 @@ function game:update(dt) time = time - dt if time <= 0 then -- TODO we need to pass an image of the screen and data about time of losing - Gamestate.push(lost, love.graphics.newScreenshot(), totalScore + score) + Gamestate.push(lost, love.graphics.newScreenshot(), totalScore + score, controls, session) -- call leave to clean up the gamestate game:leave() end @@ -199,20 +220,21 @@ end function game:keypressed(key, unicode) if input(key, controls.back) then + log("Leaving game...") Gamestate.switch(previousState) elseif input(key, controls.pause) then - Gamestate.push(paused, love.graphics.newScreenshot()) + Gamestate.push(paused, love.graphics.newScreenshot(), controls, session) end end function game:focus(isFocused) if not isFocused then - Gamestate.push(paused, love.graphics.newScreenshot()) + Gamestate.push(paused, love.graphics.newScreenshot(), controls, session) end end function game:leave() - --double check the correctness of this + -- clear the game upon any exit (except pause) level = 0 score = 0 totalScore = 0 diff --git a/src/gamestates/lost.lua b/src/gamestates/lost.lua index 48e44b4..9e426e4 100644 --- a/src/gamestates/lost.lua +++ b/src/gamestates/lost.lua @@ -1,11 +1,38 @@ +local input = require "util.input" + local lost = {} -local previousState, screenshot, score +local previousState, screenshot, score, controls +local pingTimer, session = 0, false -function lost:enter(previous, screenImageData, totalScore) +function lost:enter(previous, screenImageData, totalScore, gameControls, gamejoltSession) + log("Game lost.") previousState = previous screenshot = love.graphics.newImage(screenImageData) score = totalScore + controls = gameControls + session = gamejoltSession + -- ping our idle state immediately + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") + end + end +end + +function lost:update(dt) + -- ping every 30 seconds if in a session + pingTimer = pingTimer + dt + if pingTimer >= 30 then + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care + end + end + pingTimer = pingTimer - 30 + end end function lost:draw() @@ -23,30 +50,23 @@ function lost:draw() love.graphics.printf(string.format("Final Score: %.1f", score), 0, love.graphics.getHeight() / 2 - 25, love.graphics.getWidth(), "center") love.graphics.setNewFont(16) love.graphics.printf("(Press Esc to restart.)", 0, love.graphics.getHeight() * 3/4 - 8, love.graphics.getWidth(), "center") - --[[ - love.graphics.printf(string.format("Total Score: %.1f", totalScore), 0, 3, screenWidth / 2, "center") - --love.graphics.printf(string.format("Best Score: %.1f", bestScore), screenWidth / 2, 3, screenWidth / 2, "center") - - -- bottom of screen stuff - love.graphics.printf(string.format("Time: %.1f", time), 0, screenWidth / 2 + 25, screenWidth / 2, "center") - love.graphics.printf("Level: "..level, 0, screenWidth / 2 + 25, screenWidth, "center") - love.graphics.printf(string.format("Current Score: %.1f", score), screenWidth / 2, screenWidth / 2 + 25, screenWidth / 2, "center") - ]] end ---[[ function lost:mousepressed(x, y, button) - if button == "l" then + if input(button, controls.select) or input(button, controls.back) then + log("Restarting game...") Gamestate.pop("LOST") - --Gamestate.switch(previousState) end end --]] function lost:keypressed(key, unicode) - if key == " " then + if input(key, controls.pause) then + log("Restarting game...") Gamestate.pop("LOST") - elseif key == "escape" then + elseif input(key, controls.back) then + log("Restarting game...") Gamestate.pop("LOST") end end diff --git a/src/gamestates/menu.lua b/src/gamestates/menu.lua index aac38ad..31133ca 100644 --- a/src/gamestates/menu.lua +++ b/src/gamestates/menu.lua @@ -9,6 +9,7 @@ local menu = {} local screenWidth, screenHeight local settings, controls +local pingTimer, session = 0, false function menu:init() log("Initializing menu...") @@ -40,6 +41,7 @@ function menu:init() boxSize = 20 } } + -- TODO WRITE TO FILE end -- load or create controls if love.filesystem.isFile("controls.lua") then @@ -73,15 +75,38 @@ function menu:init() buttons = {} } } + -- TODO WRITE THE CONTROLS TO FILE end end -function menu:enter() +function menu:enter(previous, gamejoltSession) log("Entering menu...") + session = gamejoltSession + -- ping our idle state immediately + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") + end + end screenWidth = love.graphics.getWidth() screenHeight = love.graphics.getHeight() end +function menu:update(dt) + -- we want to ping every 30 seconds if connected + pingTimer = pingTimer + dt + if pingTimer >= 30 then + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care + end + end + pingTimer = pingTimer - 30 + end +end + function menu:draw() love.graphics.setNewFont(30 * screenWidth / 800) love.graphics.printf("RGB - The Color Chooser", 0, screenHeight / 7, screenWidth, "center") @@ -102,6 +127,9 @@ end function menu:keypressed(key, unicode) if input(key, controls.back) then log("Quitting.") + if session then + Gamejolt.closeSession() + end love.event.quit() end end diff --git a/src/gamestates/paused.lua b/src/gamestates/paused.lua index f58c5ad..8b73455 100644 --- a/src/gamestates/paused.lua +++ b/src/gamestates/paused.lua @@ -1,10 +1,37 @@ +local input = require "util.input" + local paused = {} -local previousState, screenshot +local previousState, screenshot, controls +local pingTimer, session = 0, false -function paused:enter(previous, screenImageData) +function paused:enter(previous, screenImageData, gameControls, gamejoltSession) + log("Game paused.") previousState = previous screenshot = love.graphics.newImage(screenImageData) + controls = gameControls + session = gamejoltSession + -- ping our idle state immediately + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") + end + end +end + +function paused:update(dt) + -- ping every 30 seconds if in a session + pingTimer = pingTimer + dt + if pingTimer >= 30 then + if session then + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care + end + end + pingTimer = pingTimer - 30 + end end function paused:draw() @@ -24,16 +51,16 @@ end ---[[ function paused:mousepressed(x, y, button) - if button == "l" then + if input(button, controls.select) or input(button, controls.back) then Gamestate.pop("UNPAUSED") end end --]] function paused:keypressed(key, unicode) - if key == " " then + if input(key, controls.pause) then Gamestate.pop("UNPAUSED") - elseif key == "escape" then + elseif input(key, controls.back) then Gamestate.pop("UNPAUSED") end end diff --git a/src/main.lua b/src/main.lua index 919777e..b3e1144 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,87 +1,53 @@ Gamestate = require "lib.gamestate" -GameJolt = require "lib.gamejolt" -debug = require "conf" -- a bit redundant but makes it obvious what is global - -local startDate = os.date("*t", os.time()) -local logFile = "logs/" .. startDate.year .. "." .. startDate.month .. "." .. startDate.day .. "-" .. startDate.hour .. "." .. startDate.min .. ".log" -function log(...) - --[[ - local strings = "" - if type(arg) == "table" then - for _,v in pairs(arg) do - strings = strings .. v - end - else - strings = arg - end - if love.filesystem.exists("logs") then - if not love.filesystem.isDirectory("logs") then - love.filesystem.remove() - love.filesystem.createDirectory("logs") - end - else - love.filesystem.createDirectory("logs") - end - local success, errorMsg = love.filesystem.append(logFile, strings) - if not success then - print("Failed to write to log file.", errorMsg) - end - --]] - if debug then print(...) end -end +Gamejolt = require "lib.gamejolt" local inifile = require "lib.inifile" local menu = require "gamestates.menu" function love.load() - log("Loading...") -- set custom window icon local icon = love.image.newImageData("icon.png") love.window.setIcon(icon) - log("Window icon set.") -- initialize Game Jolt - local initSuccess = GameJolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842") - if initSuccess then log("Game Jolt initialized.") end + Gamejolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842") -- load settings and change if needed - local gamejoltSessionOpen = false --set true if we get a session open + local gamejoltSession = false --whether or not we have an active session if love.filesystem.isFile("settings.ini") then - log("Loading settings...") local settings = inifile.parse("settings.ini") love.window.setMode(settings.display.width, settings.display.height, {fullscreen = settings.display.fullscreen, borderless = settings.display.borderless}) -- login if we have the data to do so if settings.gamejolt.username and settings.gamejolt.usertoken then - log("Logging in to Game Jolt.") - local authSuccess = GameJolt.authUser(settings.gamejolt.username, settings.gamejolt.usertoken) + local authSuccess = Gamejolt.authUser(settings.gamejolt.username, settings.gamejolt.usertoken) if authSuccess then -- check if the player has been banned - local userInfo = GameJolt.fetchUserByName(settings.gamejolt.username) + local userInfo = Gamejolt.fetchUserByName(settings.gamejolt.username) if userInfo.status == "Banned" then - log("Player has been banned from Game Jolt.") settings.gamejolt.username = false settings.gamejolt.usertoken = false inifile.save("settings.ini", settings) error("You have been banned from Game Jolt. Your login data has been deleted, re-open RGB to continue playing without Game Jolt account integration.") end - gamejoltSessionOpen = GameJolt.openSession() -- tell Game Jolt the user is playing + local sessionSuccess = Gamejolt.openSession() -- tell Game Jolt the user is playing if sessionSuccess then - -- we don't ping immediately, also the menu DOES ping immediately - gamejoltSessionOpen = true - log("Game Jolt session opened.") + --[[ -- we don't ping immediately, also the menu DOES ping immediately + local idleSuccess = Gamejolt.pingSession(false) + if not idleSuccess then + log("Couldn't ping Gamejolt session. Session may close.") --this is lazy but I don't care + end + --]] + gamejoltSession = true else - -- TODO make this known to user log("Couldn't open a session with Game Jolt.") end else - -- TODO make this better, also detect if online somehow log("Failed to log into Game Jolt. Please report this error (with a screenshot) to: paul.liverman.iii@gmail.com") end end end Gamestate.registerEvents() - log("Loaded, switching to main menu...") - Gamestate.switch(menu, gamejoltSessionOpen) + Gamestate.switch(menu, gamejoltSession) end