session stuff done

(todo, move session stuff into a required function or two)
This commit is contained in:
Paul Liverman 2015-02-11 10:21:36 -08:00
parent 04278fd6f6
commit cbd6f2daee
5 changed files with 133 additions and 75 deletions

View File

@ -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

View File

@ -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

View File

@ -8,6 +8,7 @@ local menu = {}
local screenWidth, screenHeight
local settings, controls
local pingTimer, session = 0, false
function menu:init()
log("Initializing menu...")
@ -39,6 +40,7 @@ function menu:init()
boxSize = 20
}
}
-- TODO WRITE TO FILE
end
-- load or create controls
if love.filesystem.isFile("controls.lua") then
@ -72,15 +74,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")
@ -101,6 +126,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

View File

@ -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

View File

@ -1,90 +1,51 @@
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
log("Game Jolt session opened.")
local idleSuccess = GameJolt.pingSession(false)
local idleSuccess = Gamejolt.pingSession(false)
if not idleSuccess then
-- TODO make a better system for checking this
log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care
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