Started implementing game states and intro.

This commit is contained in:
2025-09-28 02:14:41 -04:00
parent 6fdad4e408
commit 56366e4db9
7 changed files with 291 additions and 24 deletions

46
src/classes.lua Normal file
View File

@@ -0,0 +1,46 @@
------------------------------------------------------------------------------
-- Methods
------------------------------------------------------------------------------
-- Single inheritance version of the sample code from
-- http://lua-users.org/wiki/ObjectOrientationTutorial
local function make_class(BaseClass)
-- "cls" is the new class
local cls = {}
-- copy base class contents into the new class
if BaseClass ~= nil then
for k, v in pairs(BaseClass) do
cls[k] = v
end
end
-- set the class's __index, and start filling an "is_a" table that contains this class and all of its bases
-- so you can do an "instance of" check using my_instance.is_a[MyClass]
cls.__index, cls.is_a = cls, {[cls] = true}
if BaseClass ~= nil then
cls.is_a[BaseClass] = true
end
-- the class's __call metamethod
setmetatable(cls, {__call = function (c, ...)
local instance = setmetatable({}, c)
-- run the init method if it's there
local init = instance._init
if init then init(instance, ...) end
return instance
end})
-- return the new class table, that's ready to fill with methods
return cls
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return make_class

58
src/gstate.lua Normal file
View File

@@ -0,0 +1,58 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local make_class = require 'src.classes'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local GameState = make_class()
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function GameState:_init(name, index)
self.name = name
self.index = index
self.valid = false
end
function GameState:load()
end
function GameState:update(_)
return self.index
end
function GameState:draw()
end
function GameState:unload()
end
function GameState:keypressed(_)
end
function GameState:mousemoved(_, _)
end
function GameState:mousepressed(_, _, _)
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return GameState

66
src/intro.lua Normal file
View File

@@ -0,0 +1,66 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local love = require 'love'
local make_class = require 'src.classes'
local GameState = require 'src.gstate'
------------------------------------------------------------------------------
-- Class definitions
------------------------------------------------------------------------------
local Intro = make_class(GameState)
------------------------------------------------------------------------------
-- Class methods
------------------------------------------------------------------------------
function Intro:_init(name, index)
-- Call super-class constructor.
GameState._init(self, name, index)
-- Attribute definitions
self.skip = false
end
function Intro:load()
self.background = love.graphics.newImage('imgs/cpu.png')
self.valid = true
end
function Intro:update(_)
if not self.skip then return self.index else return 0 end
end
function Intro:draw()
if self.valid then
love.graphics.draw(self.background, 0, 0)
love.graphics.print(string.format('OS: %s', love.system.getOS()), 5, 5)
love.graphics.print(string.format('Love version: %s', love.getVersion()), 5, 15)
love.graphics.print(string.format('Memory usage: %s KiB', love.system.getMemUsage()), 5, 25)
end
end
function Intro:unload()
self.background = nil
self.valid = false
end
function Intro:keypressed(_)
self.skip = true
end
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return Intro

22
src/states.lua Normal file
View File

@@ -0,0 +1,22 @@
------------------------------------------------------------------------------
-- Imports
------------------------------------------------------------------------------
local Intro = require 'src.intro'
------------------------------------------------------------------------------
-- Variables
------------------------------------------------------------------------------
-- Table of all valid game states.
local states = {
Intro('intro', 1),
}
------------------------------------------------------------------------------
-- Module return
------------------------------------------------------------------------------
return states