From 898eefc21d6d531ffa85b16cee6909ae893624a6 Mon Sep 17 00:00:00 2001 From: Paul Liverman Date: Wed, 11 Feb 2015 12:01:24 -0800 Subject: [PATCH] fixed GitHub glitch hopefully --- 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, 74 insertions(+), 137 deletions(-) diff --git a/src/gamestates/game.lua b/src/gamestates/game.lua index a8c76ea..f95b99b 100644 --- a/src/gamestates/game.lua +++ b/src/gamestates/game.lua @@ -15,7 +15,6 @@ 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 @@ -63,7 +62,7 @@ local function copyColor(A) return {A[1], A[2], A[3]} end -function game:enter(previous, settings, gameControls, gamejoltSession) +function game:enter(previous, settings, gameControls) log("Entering game...") -- save the state we came from previousState = previous @@ -75,14 +74,6 @@ function game:enter(previous, settings, gameControls, gamejoltSession) -- 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 @@ -106,18 +97,6 @@ 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 @@ -147,7 +126,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, controls, session) + Gamestate.push(lost, love.graphics.newScreenshot(), totalScore + score) -- call leave to clean up the gamestate game:leave() end @@ -220,21 +199,20 @@ 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(), controls, session) + Gamestate.push(paused, love.graphics.newScreenshot()) end end function game:focus(isFocused) if not isFocused then - Gamestate.push(paused, love.graphics.newScreenshot(), controls, session) + Gamestate.push(paused, love.graphics.newScreenshot()) end end function game:leave() - -- clear the game upon any exit (except pause) + --double check the correctness of this level = 0 score = 0 totalScore = 0 diff --git a/src/gamestates/lost.lua b/src/gamestates/lost.lua index 9e426e4..48e44b4 100644 --- a/src/gamestates/lost.lua +++ b/src/gamestates/lost.lua @@ -1,38 +1,11 @@ -local input = require "util.input" - local lost = {} -local previousState, screenshot, score, controls -local pingTimer, session = 0, false +local previousState, screenshot, score -function lost:enter(previous, screenImageData, totalScore, gameControls, gamejoltSession) - log("Game lost.") +function lost:enter(previous, screenImageData, totalScore) 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() @@ -50,23 +23,30 @@ 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 input(button, controls.select) or input(button, controls.back) then - log("Restarting game...") + if button == "l" then Gamestate.pop("LOST") + --Gamestate.switch(previousState) end end --]] function lost:keypressed(key, unicode) - if input(key, controls.pause) then - log("Restarting game...") + if key == " " then Gamestate.pop("LOST") - elseif input(key, controls.back) then - log("Restarting game...") + elseif key == "escape" then Gamestate.pop("LOST") end end diff --git a/src/gamestates/menu.lua b/src/gamestates/menu.lua index 31133ca..aac38ad 100644 --- a/src/gamestates/menu.lua +++ b/src/gamestates/menu.lua @@ -9,7 +9,6 @@ local menu = {} local screenWidth, screenHeight local settings, controls -local pingTimer, session = 0, false function menu:init() log("Initializing menu...") @@ -41,7 +40,6 @@ function menu:init() boxSize = 20 } } - -- TODO WRITE TO FILE end -- load or create controls if love.filesystem.isFile("controls.lua") then @@ -75,38 +73,15 @@ function menu:init() buttons = {} } } - -- TODO WRITE THE CONTROLS TO FILE end end -function menu:enter(previous, gamejoltSession) +function menu:enter() 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") @@ -127,9 +102,6 @@ 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 8b73455..f58c5ad 100644 --- a/src/gamestates/paused.lua +++ b/src/gamestates/paused.lua @@ -1,37 +1,10 @@ -local input = require "util.input" - local paused = {} -local previousState, screenshot, controls -local pingTimer, session = 0, false +local previousState, screenshot -function paused:enter(previous, screenImageData, gameControls, gamejoltSession) - log("Game paused.") +function paused:enter(previous, screenImageData) 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() @@ -51,16 +24,16 @@ end ---[[ function paused:mousepressed(x, y, button) - if input(button, controls.select) or input(button, controls.back) then + if button == "l" then Gamestate.pop("UNPAUSED") end end --]] function paused:keypressed(key, unicode) - if input(key, controls.pause) then + if key == " " then Gamestate.pop("UNPAUSED") - elseif input(key, controls.back) then + elseif key == "escape" then Gamestate.pop("UNPAUSED") end end diff --git a/src/main.lua b/src/main.lua index b3e1144..919777e 100644 --- a/src/main.lua +++ b/src/main.lua @@ -1,53 +1,87 @@ Gamestate = require "lib.gamestate" -Gamejolt = require "lib.gamejolt" +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 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 - Gamejolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842") + local initSuccess = GameJolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842") + if initSuccess then log("Game Jolt initialized.") end -- load settings and change if needed - local gamejoltSession = false --whether or not we have an active session + local gamejoltSessionOpen = false --set true if we get a session open 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 - local authSuccess = Gamejolt.authUser(settings.gamejolt.username, settings.gamejolt.usertoken) + log("Logging in to Game Jolt.") + 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 - local sessionSuccess = Gamejolt.openSession() -- tell Game Jolt the user is playing + gamejoltSessionOpen = GameJolt.openSession() -- tell Game Jolt the user is playing if sessionSuccess then - --[[ -- 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 + -- we don't ping immediately, also the menu DOES ping immediately + gamejoltSessionOpen = true + log("Game Jolt session opened.") 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() - Gamestate.switch(menu, gamejoltSession) + log("Loaded, switching to main menu...") + Gamestate.switch(menu, gamejoltSessionOpen) end