session stuff done
(todo, move session stuff into a required function or two)
This commit is contained in:
parent
04278fd6f6
commit
cbd6f2daee
@ -15,6 +15,7 @@ local boxes = {}
|
|||||||
local score, totalScore = 0, 0
|
local score, totalScore = 0, 0
|
||||||
local level, time, startingTime = 0, 0, 0
|
local level, time, startingTime = 0, 0, 0
|
||||||
local previousState, gameSettings, controls
|
local previousState, gameSettings, controls
|
||||||
|
local pingTimer, session = 0, false
|
||||||
--these are defined on each entry to this gamestate
|
--these are defined on each entry to this gamestate
|
||||||
local screenWidth, screenHeight --defines where things are rendered
|
local screenWidth, screenHeight --defines where things are rendered
|
||||||
local boxColumns, boxRows --defines how many boxes
|
local boxColumns, boxRows --defines how many boxes
|
||||||
@ -62,7 +63,7 @@ local function copyColor(A)
|
|||||||
return {A[1], A[2], A[3]}
|
return {A[1], A[2], A[3]}
|
||||||
end
|
end
|
||||||
|
|
||||||
function game:enter(previous, settings, gameControls)
|
function game:enter(previous, settings, gameControls, gamejoltSession)
|
||||||
log("Entering game...")
|
log("Entering game...")
|
||||||
-- save the state we came from
|
-- save the state we came from
|
||||||
previousState = previous
|
previousState = previous
|
||||||
@ -74,6 +75,14 @@ function game:enter(previous, settings, gameControls)
|
|||||||
-- save the settings for later use
|
-- save the settings for later use
|
||||||
gameSettings = settings or gameSettings
|
gameSettings = settings or gameSettings
|
||||||
controls = gameControls or controls
|
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
|
-- set how to play the game based on settings
|
||||||
boxSize = gameSettings.boxSize
|
boxSize = gameSettings.boxSize
|
||||||
colorStep = gameSettings.colorStep
|
colorStep = gameSettings.colorStep
|
||||||
@ -97,6 +106,18 @@ function game:resume(previous, action)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function game:update(dt)
|
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
|
-- check if level complete
|
||||||
local coloredBoxes = {}
|
local coloredBoxes = {}
|
||||||
for i=0,#boxes do
|
for i=0,#boxes do
|
||||||
@ -126,7 +147,7 @@ function game:update(dt)
|
|||||||
time = time - dt
|
time = time - dt
|
||||||
if time <= 0 then
|
if time <= 0 then
|
||||||
-- TODO we need to pass an image of the screen and data about time of losing
|
-- 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
|
-- call leave to clean up the gamestate
|
||||||
game:leave()
|
game:leave()
|
||||||
end
|
end
|
||||||
@ -199,20 +220,21 @@ end
|
|||||||
|
|
||||||
function game:keypressed(key, unicode)
|
function game:keypressed(key, unicode)
|
||||||
if input(key, controls.back) then
|
if input(key, controls.back) then
|
||||||
|
log("Leaving game...")
|
||||||
Gamestate.switch(previousState)
|
Gamestate.switch(previousState)
|
||||||
elseif input(key, controls.pause) then
|
elseif input(key, controls.pause) then
|
||||||
Gamestate.push(paused, love.graphics.newScreenshot())
|
Gamestate.push(paused, love.graphics.newScreenshot(), controls, session)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function game:focus(isFocused)
|
function game:focus(isFocused)
|
||||||
if not isFocused then
|
if not isFocused then
|
||||||
Gamestate.push(paused, love.graphics.newScreenshot())
|
Gamestate.push(paused, love.graphics.newScreenshot(), controls, session)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function game:leave()
|
function game:leave()
|
||||||
--double check the correctness of this
|
-- clear the game upon any exit (except pause)
|
||||||
level = 0
|
level = 0
|
||||||
score = 0
|
score = 0
|
||||||
totalScore = 0
|
totalScore = 0
|
||||||
|
@ -1,11 +1,38 @@
|
|||||||
|
local input = require "util.input"
|
||||||
|
|
||||||
local lost = {}
|
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
|
previousState = previous
|
||||||
screenshot = love.graphics.newImage(screenImageData)
|
screenshot = love.graphics.newImage(screenImageData)
|
||||||
score = totalScore
|
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
|
end
|
||||||
|
|
||||||
function lost:draw()
|
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.printf(string.format("Final Score: %.1f", score), 0, love.graphics.getHeight() / 2 - 25, love.graphics.getWidth(), "center")
|
||||||
love.graphics.setNewFont(16)
|
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("(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
|
end
|
||||||
|
|
||||||
---[[
|
---[[
|
||||||
function lost:mousepressed(x, y, button)
|
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.pop("LOST")
|
||||||
--Gamestate.switch(previousState)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
function lost:keypressed(key, unicode)
|
function lost:keypressed(key, unicode)
|
||||||
if key == " " then
|
if input(key, controls.pause) then
|
||||||
|
log("Restarting game...")
|
||||||
Gamestate.pop("LOST")
|
Gamestate.pop("LOST")
|
||||||
elseif key == "escape" then
|
elseif input(key, controls.back) then
|
||||||
|
log("Restarting game...")
|
||||||
Gamestate.pop("LOST")
|
Gamestate.pop("LOST")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,6 +8,7 @@ local menu = {}
|
|||||||
|
|
||||||
local screenWidth, screenHeight
|
local screenWidth, screenHeight
|
||||||
local settings, controls
|
local settings, controls
|
||||||
|
local pingTimer, session = 0, false
|
||||||
|
|
||||||
function menu:init()
|
function menu:init()
|
||||||
log("Initializing menu...")
|
log("Initializing menu...")
|
||||||
@ -39,6 +40,7 @@ function menu:init()
|
|||||||
boxSize = 20
|
boxSize = 20
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
-- TODO WRITE TO FILE
|
||||||
end
|
end
|
||||||
-- load or create controls
|
-- load or create controls
|
||||||
if love.filesystem.isFile("controls.lua") then
|
if love.filesystem.isFile("controls.lua") then
|
||||||
@ -72,15 +74,38 @@ function menu:init()
|
|||||||
buttons = {}
|
buttons = {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
-- TODO WRITE THE CONTROLS TO FILE
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function menu:enter()
|
function menu:enter(previous, gamejoltSession)
|
||||||
log("Entering menu...")
|
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()
|
screenWidth = love.graphics.getWidth()
|
||||||
screenHeight = love.graphics.getHeight()
|
screenHeight = love.graphics.getHeight()
|
||||||
end
|
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()
|
function menu:draw()
|
||||||
love.graphics.setNewFont(30 * screenWidth / 800)
|
love.graphics.setNewFont(30 * screenWidth / 800)
|
||||||
love.graphics.printf("RGB - The Color Chooser", 0, screenHeight / 7, screenWidth, "center")
|
love.graphics.printf("RGB - The Color Chooser", 0, screenHeight / 7, screenWidth, "center")
|
||||||
@ -101,6 +126,9 @@ end
|
|||||||
function menu:keypressed(key, unicode)
|
function menu:keypressed(key, unicode)
|
||||||
if input(key, controls.back) then
|
if input(key, controls.back) then
|
||||||
log("Quitting.")
|
log("Quitting.")
|
||||||
|
if session then
|
||||||
|
Gamejolt.closeSession()
|
||||||
|
end
|
||||||
love.event.quit()
|
love.event.quit()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -1,10 +1,37 @@
|
|||||||
|
local input = require "util.input"
|
||||||
|
|
||||||
local paused = {}
|
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
|
previousState = previous
|
||||||
screenshot = love.graphics.newImage(screenImageData)
|
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
|
end
|
||||||
|
|
||||||
function paused:draw()
|
function paused:draw()
|
||||||
@ -24,16 +51,16 @@ end
|
|||||||
|
|
||||||
---[[
|
---[[
|
||||||
function paused:mousepressed(x, y, button)
|
function paused:mousepressed(x, y, button)
|
||||||
if button == "l" then
|
if input(button, controls.select) or input(button, controls.back) then
|
||||||
Gamestate.pop("UNPAUSED")
|
Gamestate.pop("UNPAUSED")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
--]]
|
--]]
|
||||||
|
|
||||||
function paused:keypressed(key, unicode)
|
function paused:keypressed(key, unicode)
|
||||||
if key == " " then
|
if input(key, controls.pause) then
|
||||||
Gamestate.pop("UNPAUSED")
|
Gamestate.pop("UNPAUSED")
|
||||||
elseif key == "escape" then
|
elseif input(key, controls.back) then
|
||||||
Gamestate.pop("UNPAUSED")
|
Gamestate.pop("UNPAUSED")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
59
src/main.lua
59
src/main.lua
@ -1,90 +1,51 @@
|
|||||||
Gamestate = require "lib.gamestate"
|
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 inifile = require "lib.inifile"
|
||||||
|
|
||||||
local menu = require "gamestates.menu"
|
local menu = require "gamestates.menu"
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
log("Loading...")
|
|
||||||
-- set custom window icon
|
-- set custom window icon
|
||||||
local icon = love.image.newImageData("icon.png")
|
local icon = love.image.newImageData("icon.png")
|
||||||
love.window.setIcon(icon)
|
love.window.setIcon(icon)
|
||||||
log("Window icon set.")
|
|
||||||
|
|
||||||
-- initialize Game Jolt
|
-- initialize Game Jolt
|
||||||
local initSuccess = GameJolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842")
|
Gamejolt.init(48728, "b8e4a0eae1509d3edef3d8451bae1842")
|
||||||
if initSuccess then log("Game Jolt initialized.") end
|
|
||||||
|
|
||||||
-- load settings and change if needed
|
-- 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
|
if love.filesystem.isFile("settings.ini") then
|
||||||
log("Loading settings...")
|
|
||||||
local settings = inifile.parse("settings.ini")
|
local settings = inifile.parse("settings.ini")
|
||||||
love.window.setMode(settings.display.width, settings.display.height, {fullscreen = settings.display.fullscreen, borderless = settings.display.borderless})
|
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
|
-- login if we have the data to do so
|
||||||
if settings.gamejolt.username and settings.gamejolt.usertoken then
|
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
|
if authSuccess then
|
||||||
-- check if the player has been banned
|
-- 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
|
if userInfo.status == "Banned" then
|
||||||
log("Player has been banned from Game Jolt.")
|
|
||||||
settings.gamejolt.username = false
|
settings.gamejolt.username = false
|
||||||
settings.gamejolt.usertoken = false
|
settings.gamejolt.usertoken = false
|
||||||
inifile.save("settings.ini", settings)
|
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.")
|
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
|
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
|
if sessionSuccess then
|
||||||
log("Game Jolt session opened.")
|
local idleSuccess = Gamejolt.pingSession(false)
|
||||||
local idleSuccess = GameJolt.pingSession(false)
|
|
||||||
if not idleSuccess then
|
if not idleSuccess then
|
||||||
-- TODO make a better system for checking this
|
log("Couldn't ping Gamejolt session. Session may close.") --this is lazy but I don't care
|
||||||
log("Couldn't ping Game Jolt session. Session may close.") --this is lazy but I don't care
|
|
||||||
end
|
end
|
||||||
|
gamejoltSession = true
|
||||||
else
|
else
|
||||||
-- TODO make this known to user
|
|
||||||
log("Couldn't open a session with Game Jolt.")
|
log("Couldn't open a session with Game Jolt.")
|
||||||
end
|
end
|
||||||
else
|
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")
|
log("Failed to log into Game Jolt. Please report this error (with a screenshot) to: paul.liverman.iii@gmail.com")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Gamestate.registerEvents()
|
Gamestate.registerEvents()
|
||||||
log("Loaded, switching to main menu...")
|
Gamestate.switch(menu, gamejoltSession)
|
||||||
Gamestate.switch(menu, gamejoltSessionOpen)
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user