Oh, sorry, I forgot the important parts!
main.lua
Code: Select all
function love.load()
require "game"
require "menu"
scene=menu
love.graphics.setBackgroundColor(150,50,110)
end
function love.update(dt)
scene:update(dt)
end
function love.draw()
scene:draw()
end
function love.mousepressed(x,y,button)
scene:onClick(x,y,button)
end
function love.keypressed(key)
scene:keypressed(key)
end
game.lua
Code: Select all
game = {}
game.lineWidth = 10
game.font = love.graphics.newFont(16)
game.planet = {x=love.graphics.getWidth()/2,y=love.graphics.getHeight()/2,r=100,color={190,170,170}}
game.lines = {count=10,r=50,speed=math.pi,a=0}
function game:update(dt)
--updating line angle
self.lines.a = self.lines.a + self.lines.speed * dt
if self.lines.a > math.pi then self.lines.a = self.lines.a - math.pi*2 end
end
function game:draw()
love.graphics.setLineWidth(self.lineWidth)
--planet
love.graphics.setColor(self.planet.color)
love.graphics.circle("line",self.planet.x,self.planet.y,self.planet.r,self.planet.r*3)
--lines
for count = 1,self.lines.count do
local a = self.lines.a + count / self.lines.count * math.pi*2
love.graphics.line(self.planet.x+math.cos(a)*self.planet.r,self.planet.y+math.sin(a)*self.planet.r,self.planet.x+math.cos(a)*(self.planet.r+self.lines.r),self.planet.y+math.sin(a)*(self.planet.r+self.lines.r))
end
--text
love.graphics.setColor(180,80,140)
love.graphics.setFont(self.font)
love.graphics.print("This isn't really a game, sorry. Press ESCAPE to return to menu.")
end
function game:onClick(x,y,button)
end
function game:keypressed(key)
scene=menu
end
menu.lua
Code: Select all
menu = {}
menu.buttons = {
{text="PLAY",x=love.graphics.getWidth()/2,y=love.graphics.getHeight()*1/4,color={220,190,190},hoverColor={0,0,0},onClick=function() scene=game end},
{text="EXIT",x=love.graphics.getWidth()/2,y=love.graphics.getHeight()*3/4,color={220,190,190},hoverColor={0,0,0},onClick=function() love.event.push("quit") end},
}
menu.font = love.graphics.newFont(24)
function menu:update(dt)
local x,y = love.mouse:getX(),love.mouse:getY()
self.clickable = nil
--mouse-button collision
for i,v in ipairs(self.buttons) do
if x > v.x - self.font:getWidth(v.text)/2 and
x < v.x + self.font:getWidth(v.text)/2 and
y > v.y - self.font:getHeight()/2 and
y < v.y + self.font:getHeight()/2 then
self.clickable = v
end
end
end
function menu:draw()
--set font
love.graphics.setFont(self.font)
for i,v in ipairs(self.buttons) do
--set color
if v == self.clickable then love.graphics.setColor(v.hoverColor)
else love.graphics.setColor(v.color) end
--draw button
love.graphics.print(v.text,v.x,v.y,0,1,1,self.font:getWidth(v.text)/2,self.font:getHeight()/2)
end
end
function menu:onClick(x,y,button)
if button == "l" and self.clickable then
self.clickable.onClick()
end
end
function menu:keypressed(key)
if key == "escape" then
love.event.push("quit")
end
end
Though I realised, that there are no benefits in this over the usual game state way. I don't know what he **** I was thinking yesterday. Sorry for this inconvinience!