Separate config from state, cleanup

master
informatic 2017-01-08 22:24:13 +01:00
parent 82f85333ab
commit cac836c793
7 changed files with 108 additions and 66 deletions

11
config.lua Normal file
View File

@ -0,0 +1,11 @@
return {
transitionTime = 1,
cycleTime = 5,
screens = {
require 'screens.cube',
require 'screens.weather',
require 'screens.time',
require 'screens.misery',
require 'screens.screen1',
},
}

View File

@ -3,24 +3,10 @@ local inspect = require 'vendor.inspect'
local push = require 'vendor.push'
local lurker = require 'vendor.lurker'
local config = require('config')
environment = os.getenv('ENV')
screens = {
require 'screens.weather',
require 'screens.time',
require 'screens.misery',
require 'screens.screen1',
}
state = {
currentScreen = 1,
state = 'running',
cycleTime = 3,
transitionTime = 1,
transitioning = false,
stateCounter = 0,
}
local gameWidth, gameHeight = 1280, 720
local windowWidth, windowHeight = love.window.getDesktopDimensions()
windowWidth, windowHeight = windowWidth*.5, windowHeight*.5 --make the window a bit smaller than the screen itself
@ -37,6 +23,12 @@ function love.resize(w, h)
end
function love.load()
state = {
currentScreen = 1,
transitioning = false,
stateCounter = 0,
}
love.mouse.setVisible( false )
secondaryCanvas = love.graphics.newCanvas(push:getWidth(), push:getHeight())
@ -44,7 +36,7 @@ function love.load()
fpsGraph = debugGraph:new('fps', 0, 0)
memGraph = debugGraph:new('mem', 0, 30)
for key, node in ipairs(screens) do
for key, node in ipairs(config.screens) do
node.load()
end
@ -52,8 +44,6 @@ function love.load()
end
function lurker.preswap(f)
print(f)
if f:match('_thread.lua') then
print("Preventing _thread update!")
return false
@ -70,11 +60,11 @@ function love.draw()
oldw, oldh = love.graphics.getWidth, love.graphics.getHeight
love.graphics.getWidth, love.graphics.getHeight = getw, geth
screens[state.currentScreen].render()
cofnig.screens[state.currentScreen].render()
if state.transitioning then
-- Render next screen into canvas and fade accordingly
secondaryCanvas:renderTo(screens[state.currentScreen % #screens + 1].render)
secondaryCanvas:renderTo(config.screens[state.currentScreen % #config.screens + 1].render)
love.graphics.setColor(255, 255, 255, 255 * (state.stateCounter / state.transitionTime)) -- red, green, blue, opacity (this would be white with 20% opacity)
love.graphics.draw(secondaryCanvas, 0, 0)
end
@ -93,22 +83,22 @@ function love.draw()
end
function love.update(dt)
screens[state.currentScreen].update(dt)
config.screens[state.currentScreen].update(dt)
if state.transitioning then
screens[state.currentScreen % #screens + 1].update(dt)
config.screens[state.currentScreen % #config.screens + 1].update(dt)
end
state.stateCounter = state.stateCounter + dt
if state.transitioning then
if state.stateCounter >= state.transitionTime then
if state.stateCounter >= config.transitionTime then
state.stateCounter = 0
state.transitioning = false
state.currentScreen = (state.currentScreen % #screens) + 1
state.currentScreen = (state.currentScreen % #config.screens) + 1
end
else
if state.stateCounter >= state.cycleTime then
if state.stateCounter >= config.cycleTime then
state.stateCounter = 0
state.transitioning = true
end

65
screens/cube.lua Normal file
View File

@ -0,0 +1,65 @@
local node = {}
local papa = love.graphics.newImage("papa.png")
local h = 25.0
local v = {
{-h, -h, -h},
{ h, -h, -h},
{ h, h, -h},
{-h, h, -h},
{-h, -h, h},
{ h, -h, h},
{ h, h, h},
{-h, h, h}
}
local c = {
{0, 1, 31},
{1, 2, 31},
{2, 3, 31},
{3, 0, 31},
{0, 4, 34},
{1, 5, 34},
{2, 6, 34},
{3, 7, 34},
{4, 5, 32},
{5, 6,32},
{6, 7,32},
{7, 4,32}
}
local E = 100 * math.tan(2*math.pi/3)
function to2d(p) return p[1] * E / (p[3]+E), p[2] * E / (p[3]+E) end
function node.load() end
function node.render()
love.graphics.setColor( 0, 0, 0 )
love.graphics.rectangle("fill", 0, 0, love.graphics.getWidth(), love.graphics.getHeight())
love.graphics.setColor( 255, 255, 255 )
local w = love.graphics.getWidth() / 2
local h = love.graphics.getHeight() / 2
local scl = 6
for _, p in ipairs(c) do
x1, y1 = to2d(v[p[1]+1])
x2, y2 = to2d(v[p[2]+1])
love.graphics.line(x1 * scl + w, y1 * scl + h, x2 * scl + w, y2 * scl + h)
end
end
function node.update(dt)
for _, vec in ipairs(v) do
angle = -dt/3
nv0 = math.cos(angle) * vec[1] + math.sin(angle) * vec[3]
nv2 = -math.sin(angle) * vec[1] + math.cos(angle) * vec[3]
vec[1] = nv0
vec[3] = nv2
angle = dt
nv1 = math.cos(angle) * vec[2] - math.sin(angle) * vec[3]
nv2 = math.sin(angle) * vec[2] + math.cos(angle) * vec[3]
vec[2] = nv1
vec[3] = nv2
end
end
return node

View File

@ -9,11 +9,8 @@ local updateInterval = 2 * 60
local lastUpdate = 0
local state = nil
function node.load()
end
function node.unload()
end
function node.load() end
function node.unload() end
function node.render()
love.graphics.setColor( 0, 0, 0 )
@ -22,11 +19,12 @@ function node.render()
if state then
love.graphics.setColor( 255, 255, 255 )
love.graphics.setFont(textFont)
love.graphics.printf(state.text, 50, 180, love.graphics.getWidth() - 100, 'center')
love.graphics.printf(state.entry, 50, 180, love.graphics.getWidth() - 100, 'center')
love.graphics.setColor( 255, 255, 255, 200 )
love.graphics.setFont(smallFont)
love.graphics.printf(state.description, 200, love.graphics.getHeight() - 100, love.graphics.getWidth() - 400, 'center')
local description = 'added by ' .. state.author .. ' on ' .. os.date('%Y/%m/%d %X', state.added)
love.graphics.printf(description, 200, love.graphics.getHeight() - 100, love.graphics.getWidth() - 400, 'center')
else
love.graphics.setColor( 255, 255, 255, 80 )

View File

@ -3,32 +3,11 @@ local http = require("socket.http")
local json = require("vendor.json")
local lume = require("vendor.lume")
local miseryURL = 'http://oodviewer.q3k.me/term/_,'
math.randomseed( os.time() )
function unescape(str)
str = string.gsub( str, '&lt;', '<' )
str = string.gsub( str, '&gt;', '>' )
str = string.gsub( str, '&quot;', '"' )
str = string.gsub( str, '&apos;', "'" )
str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
return str
end
local miseryURL = 'http://oodviewer.q3k.me/randomterm.json/_,'
local r, c, h = http.request(miseryURL)
if c == 200 then
local data = {}
for item, desc in string.gmatch(r, '<li>([^\n]*) <i>([^<]*)</i>') do
data[#data + 1] = {
text = unescape(item:gsub('<[^>]*>', '')),
description = unescape(desc),
}
end
love.thread.getChannel('misery'):push(lume.randomchoice(data))
love.thread.getChannel('misery'):push(json.decode(r))
print("Update finished")
else
print("Update failed")

View File

@ -1,11 +1,9 @@
local node = {}
local socket = require("socket")
local textFont = love.graphics.newFont('fonts/Lato-Thin.ttf', 400)
local smallFont = love.graphics.newFont('fonts/Lato-Light.ttf', 60)
function node.load()
end
function node.load() end
function node.render()
love.graphics.setColor( 0, 0, 0 )
@ -14,7 +12,7 @@ function node.render()
love.graphics.setColor( 255, 255, 255 )
love.graphics.setFont(textFont);
love.graphics.printf(os.date('%H:%M'), 0, 120, love.graphics.getWidth(), 'center');
love.graphics.printf(os.date('%H:%M'), 0, 100, love.graphics.getWidth(), 'center');
love.graphics.setFont(smallFont);
love.graphics.printf(os.date('%Y/%m/%d'), 0, 575, love.graphics.getWidth(), 'center');

View File

@ -10,17 +10,15 @@ local weatherGlyphs = {
snow = "",
mist = "",
clear = "",
clouds = "",
}
local updateInterval = 5 * 60
local lastUpdate = 0
local state = nil
function node.load()
end
function node.unload()
end
function node.load() end
function node.unload() end
function node.render()
love.graphics.setColor( 0, 0, 0 )
@ -31,7 +29,10 @@ function node.render()
if weatherGlyphs[state.weather] then
love.graphics.setFont(weatherFont)
love.graphics.print(weatherGlyphs[state.weather], 150, 35)
love.graphics.print(weatherGlyphs[state.weather], 120, 35)
else
love.graphics.setFont(smallFont)
love.graphics.print(state.weather, 150, love.graphics.getHeight()/2 - 60)
end
love.graphics.setFont(textFont)