- inifile added, beginning Game Jolt integreation
- gamestates moved into their own directory
- paused state that is auto-fired when window loses focus
This commit is contained in:
Paul Liverman
2015-02-10 00:18:45 -08:00
parent 361247ccac
commit 46bad9539d
9 changed files with 296 additions and 75 deletions

212
src/gamestates/game.lua Normal file
View File

@@ -0,0 +1,212 @@
love.math.setRandomSeed(os.time())
-- Gamestates
--local won = require "gamestates.won" --TODO MAKE THIS
local lost = require "gamestates.lost"
local paused = require "gamestates.paused"
-- This Gamestate
local game = {}
-- Locals
local boxes = {}
local score, totalScore = 0, 0
local level, time, startingTime = 0, 0, 0
local previousState, gameSettings
--these are defined on each entry to this gamestate
local screenWidth, screenHeight --defines where things are rendered
local boxColumns, boxRows --defines how many boxes
--these are loaded from values passed on entry to this gamestate
local boxSize, colorStep, timeLimit = 20, 80, 60 --default values just in case
local function nextLevel()
totalScore = totalScore + score
score = 0
level = level + 1
time = time + timeLimit --your remaining time is added on to the next level
startingTime = time --save where you started on this level for scoring
-- (re)create black boxes
boxes = {}
for i=0,boxColumns do
boxes[i] = {}
for j=0,boxRows do
boxes[i][j] = {0, 0, 0}
end
end
-- assign a random set of boxes random colors
for i=1,math.floor(math.pow(level, 1.07) * 1.03 + 2) do --(level * 1.5 + 2)
local x, y = love.math.random(0, #boxes), love.math.random(0, #boxes[1])
boxes[x][y] = {love.math.random(0, 255), love.math.random(0, 255), love.math.random(0, 255)}
boxes[x][y][1] = boxes[x][y][1] - boxes[x][y][1] % colorStep --is this right?
boxes[x][y][2] = boxes[x][y][2] - boxes[x][y][2] % colorStep
boxes[x][y][3] = boxes[x][y][3] - boxes[x][y][3] % colorStep
end
end
local function colorsEqual(A, B)
if A[1] == B[1] and A[2] == B[2] and A[3] == B[3] then
return true
end
return false
end
local function copyColor(A)
return {A[1], A[2], A[3]}
end
function game:enter(previous, settings)
-- save the state we came from
previousState = previous
-- set locals based on screen size
screenWidth = love.graphics.getWidth()
screenHeight = love.graphics.getHeight()
boxColumns = math.floor(screenWidth / boxSize) - 1
boxRows = math.floor(screenHeight / boxSize) - 5
-- save the settings for later use
gameSettings = settings or gameSettings
-- set how to play the game based on settings
boxSize = gameSettings.boxSize
colorStep = gameSettings.colorStep
timeLimit = gameSettings.timeLimit
-- set the font we're going to use
love.graphics.setNewFont(28)
-- this is nextLevel shit
nextLevel()
end
function game:resume(previous, action)
if action == "LOST" then
game:enter(previousState) --we want to keep the old values
totalScore = 0 --this should have happened in game:leave() but does not for an unknown reason
end
if action == "UNPAUSED" then
love.graphics.setNewFont(28) -- fix our font!
end
end
function game:update(dt)
-- check if level complete
local coloredBoxes = {}
for i=0,#boxes do
for j=0,#boxes[i] do
if not colorsEqual(boxes[i][j], {0, 0, 0}) then
table.insert(coloredBoxes, boxes[i][j])
end
end
end
local won, color
if #coloredBoxes >= 2 then
won = true
color = copyColor(coloredBoxes[1])
for i=2,#coloredBoxes do
if not colorsEqual(color, coloredBoxes[i]) then
won = false
end
end
end
if won then
-- TODO we need a brief push/pop of gamestate to display a winning message
nextLevel()
end
-- else decrement time, and check if out of time
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)
-- call leave to clean up the gamestate
game:leave()
end
-- update the current score
score = #coloredBoxes / math.pow(startingTime - time, 0.02) * colorStep
end
function game:draw()
--boxes
for i=0,#boxes do
for j=0,#boxes[i] do
love.graphics.setColor(boxes[i][j])
love.graphics.rectangle("fill", i * boxSize, j * boxSize + boxSize * 2, boxSize, boxSize)
end
end
--lines
-- vertical
love.graphics.setColor(255, 255, 255)
for i=0,boxColumns+1 do
love.graphics.line(i * boxSize, 0 + boxSize * 2, i * boxSize, screenHeight - boxSize * 2)
end
-- horizontal
for j=0,boxRows+1 do
love.graphics.line(0, j * boxSize + boxSize * 2, screenWidth, j * boxSize + boxSize * 2)
end
-- Info Overlays
--love.graphics.setNewFont(28) --purposely stays same no matter screen res
love.graphics.setColor(255, 255, 255)
-- top of screen stuff
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 game:mousepressed(x, y, button)
-- new x/y adjusted for where boxes actually are
local nx = math.floor(x / boxSize)
local ny = math.floor((y - boxSize * 2) / boxSize)
-- check if we are actually over a box first
if boxes[nx][ny] then
-- left, red
if button == "l" then
boxes[nx][ny][1] = boxes[nx][ny][1] + colorStep
if boxes[nx][ny][1] > 255 then
boxes[nx][ny][1] = 0
end
-- middle, green
elseif button == "m" or button == "wu" or button == "wd" then
boxes[nx][ny][2] = boxes[nx][ny][2] + colorStep
if boxes[nx][ny][2] > 255 then
boxes[nx][ny][2] = 0
end
-- right, blue
elseif button == "r" then
boxes[nx][ny][3] = boxes[nx][ny][3] + colorStep
if boxes[nx][ny][3] > 255 then
boxes[nx][ny][3] = 0
end
end
end
end
function game:leave()
--double check the correctness of this
level = 0
score = 0
totalScore = 0
time = 0
startingTime = 0
end
function game:keypressed(key, unicode)
if key == " " then
Gamestate.push(paused, love.graphics.newScreenshot())
elseif key == "escape" then
Gamestate.switch(previousState)
end
end
function game:focus(isFocused)
if not isFocused then
Gamestate.push(paused, love.graphics.newScreenshot())
end
end
return game

54
src/gamestates/lost.lua Normal file
View File

@@ -0,0 +1,54 @@
local lost = {}
local previousState, screenshot, score
function lost:enter(previous, screenImageData, totalScore)
previousState = previous
screenshot = love.graphics.newImage(screenImageData)
score = totalScore
end
function lost:draw()
-- draw the screenshot
love.graphics.setColor(255, 255, 255)
love.graphics.draw(screenshot)
-- draw a partial transparency black to fade out the screenshot
love.graphics.setColor(0, 0, 0, 200)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
-- print info
love.graphics.setNewFont(40)
love.graphics.setColor(255, 255, 255)
love.graphics.printf("Game Over", 0, love.graphics.getHeight() / 4 - 20, love.graphics.getWidth(), "center")
love.graphics.setNewFont(50)
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
Gamestate.pop("LOST")
--Gamestate.switch(previousState)
end
end
--]]
function lost:keypressed(key, unicode)
if key == " " then
Gamestate.pop("LOST")
elseif key == "escape" then
Gamestate.pop("LOST")
end
end
return lost

37
src/gamestates/menu.lua Normal file
View File

@@ -0,0 +1,37 @@
local Gamestate = require "lib.gamestate"
local game = require "gamestates.game"
local menu = {}
local screenWidth, screenHeight
function menu:enter()
screenWidth = love.graphics.getWidth()
screenHeight = love.graphics.getHeight()
end
function menu:draw()
love.graphics.setNewFont(30 * screenWidth / 800)
love.graphics.printf("RGB - The Color Chooser", 0, screenHeight / 7, screenWidth, "center")
love.graphics.setNewFont(16 * screenWidth / 800)
love.graphics.printf("1. Left click to cycle red.\n2. Middle click or scroll to cycle green.\n3. Right click to cycle blue.", 0, screenHeight / 3, screenWidth, "center")
love.graphics.printf("Your goal is to get every panel that is not black to be the same color.", 0, screenHeight / 1.75, screenWidth, "center")
love.graphics.printf("Click to begin.", 0, screenHeight / 1.3, screenWidth, "center")
love.graphics.setNewFont(12 * screenWidth / 800)
love.graphics.printf("(Esc exits the game.)", 0, screenHeight - 20 * screenHeight / 400, screenWidth, "center")
end
function menu:mousepressed(x, y, button)
if button == "l" then
-- TODO replace constructed settings object with actual loaded settings
Gamestate.switch(game, {boxSize = 20, colorStep = 80, timeLimit = 10})
end
end
function menu:keypressed(key, unicode)
if key == "escape" then
love.event.quit()
end
end
return menu

41
src/gamestates/paused.lua Normal file
View File

@@ -0,0 +1,41 @@
local paused = {}
local previousState, screenshot
function paused:enter(previous, screenImageData)
previousState = previous
screenshot = love.graphics.newImage(screenImageData)
end
function paused:draw()
-- draw the screenshot
love.graphics.setColor(255, 255, 255)
love.graphics.draw(screenshot)
-- draw a partial transparency black to fade out the screenshot
love.graphics.setColor(0, 0, 0, 200)
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
-- print info
love.graphics.setNewFont(40)
love.graphics.setColor(255, 255, 255)
love.graphics.printf("Paused", 0, love.graphics.getHeight() * 2/5 - 20, love.graphics.getWidth(), "center")
love.graphics.setNewFont(20)
love.graphics.printf("(Press Esc to resume.)", 0, love.graphics.getHeight() * 3/5 - 10, love.graphics.getWidth(), "center")
end
---[[
function paused:mousepressed(x, y, button)
if button == "l" then
Gamestate.pop("UNPAUSED")
end
end
--]]
function paused:keypressed(key, unicode)
if key == " " then
Gamestate.pop("UNPAUSED")
elseif key == "escape" then
Gamestate.pop("UNPAUSED")
end
end
return paused