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