Background:
In an effort to organize and clean up my Pong project I've searched and researched (see what I did there?) about handling game states and am attempting to write my own way of doing so. I've created a game.lua which is my base class as well, it containts load(), draw(), and update(), and now a title.lua which, as you may have guessed, will handle all my title screen business by using its own version of the three functions. All my game functions are working, as I can see my console notifications, but it doesn't seem to want to run my title code.
Problem:
I found this thread: http://love2d.org/forums/viewtopic.php? ... tle+screen and it is right up my alley. Yet the OP doesn't show me how the instuctions.lua would look like.
So in relation to mine, I have main.lua which requires game.lua. And game.lua which requires title.lua. But title doesn't work, obviously, because it doesn't know game exists. If I require both, I get a loop error, duh! And if I reverse them, title isn't being called anyway cause I suspect game doesn't know it is there...it seems I have a catch-22.
So my relatively easy question to you is, What kind of wizardy do I need to use to make, at least my file system let alone my code, work with each other. Or should I just bite the bullet and learn/utilize a library like Pölygamy?
Code: Select all
----------------------------------------------------------------------------
-- project: Pong Revised
-- file: main.lua
-- author: Michael Groll
-- version: 0.8.0
-- gitHub: https://github.com/Shanulu/pongRevised
--
-- description: Love2D's base file, contains most of the initializing,
-- drawing, and updating of the Pong components. Also
-- contains some of the keyboard inputs as well as my
-- customSort function.
----------------------------------------------------------------------------
require 'ball' --contains our ball class and collision deteciton
require 'paddle' --contains our paddle class
require 'interface' --contains our screen and buttons
require 'blocks' --contains our blocks
require 'game'
function love.load()
Game:load()
-- variables
deleted = { order = math.huge } --a table used to for comparing later
height = love.graphics.getHeight() --commonly used
width = love.graphics.getWidth()
gameState = "title" --title, pregame, options, live, pause, endgame
difficulty = 100 --ai max speed movement
preGame = 5 --timer for pregame
--we call Paddle:load() in interface.lua, so we can set difficulty
end
function love.draw()
Game:draw()
end
function love.update(dt)
Game:update(dt)
end
Code: Select all
----------------------------------------------------------------------------
-- project: Pong Revised
-- file: game.lua
-- author: Michael Groll
-- gitHub: https://github.com/Shanulu/pongRevised
--
-- description:
----------------------------------------------------------------------------
require 'title'
game = {}
Game = {}
Game.__index = Game
function Game:new()
local game = setmetatable({}, Game)
game.isActive = false
game.beenLoaded = false
return game
end
function Game:load()
for _, v in ipairs(game) do
if v.beenLoaded == false then
v:load()
v.beenLoaded = true
end
end
print("game load")
end
function Game:draw()
for _, v in ipairs(game) do
if v.isActive == true then
v:draw()
end
end
print("game draw")
end
function Game:update(dt)
for _, v in ipairs(game) do
if v.isActive == true then
v:update()
end
end
print("game update")
end
function Game:activate(i) --send this an index like "title" or "live"
for _, v in ipairs(game) do
if _ == i then
v.isActive = true
else
v.isActive = false
end
end
end
Code: Select all
----------------------------------------------------------------------------
-- project: Pong Revised
-- file: title.lua
-- author: Michael Groll
-- gitHub: https://github.com/Shanulu/pongRevised
--
-- description:
----------------------------------------------------------------------------
require 'button'
--@LOVE FORUM GOERS:
--not sure if theres a better way to do this (reference title to its corresponding table entry, or if it even works
--my code doesn't run yet so its hard to test.
game["title"] = Game:new()
game["title"].isActive = true
title = game["title"]
function Game:load() --not sure if this should be Game:load() or title:load or even game["title"].load()
--This is copied and works
print("title loading")
-- Graphics, Images, Font
love.graphics.setBackgroundColor(0, 0, 0) --Black
titleFont = love.graphics.newFont("Fonts/Digital_tech.otf", 56)--draw our title screen
love.graphics.setFont(titleFont)
love.graphics.setColorMode("modulate") --be sure to change it to replace when drawing buttons
Button:load() --load the buttons
-- Sound and Music
music = love.audio.newSource("Sounds/bgm.ogg")
music:setLooping(true)
BGM = true --options setting
sound = true --options setting
love.audio.play(music)
Button:load()
end
function Game:draw()
love.graphics.print("Pong 2.0", width/2 - 50, 20)
Button:draw()
end