diff --git a/README.md b/README.md index e33220a..29297b3 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,4 @@ A dungeon crawler game made with LoveDOS for the DOSember Game Jam https://itch. - [Click.wav by frosty ham](https://opengameart.org/content/click-0) - [B&W Ornamental Cursor by qubodup](https://opengameart.org/content/bw-ornamental-cursor-19x19) - [Dogs of Cyberspace by congusbongus](https://modarchive.org/index.php?request=view_by_moduleid&query=207032) +- [Microchip texture by ZeptoBARS](https://opengameart.org/content/microchip-texture) diff --git a/imgs/cpu.png b/imgs/cpu.png new file mode 100644 index 0000000..19adaf4 Binary files /dev/null and b/imgs/cpu.png differ diff --git a/main.lua b/main.lua index 5bf8aaf..ee63d47 100644 --- a/main.lua +++ b/main.lua @@ -1,46 +1,120 @@ ----@diagnostic disable: undefined-global -Mousex = 0 -Mousey = 0 -Cursor = nil -Bgm = nil -Click = nil -Bckg = nil +------------------------------------------------------------------------------ +-- Imports +------------------------------------------------------------------------------ +local love = require 'love' +local game_states = require 'src.states' + + +------------------------------------------------------------------------------ +-- Variables +------------------------------------------------------------------------------ + +Current_state = 1 +Fade_in = 0.0 +Fade_out = 1.0 +Change_state = false + + +------------------------------------------------------------------------------ +-- Game methods +------------------------------------------------------------------------------ + +local function lerp(a, b, t) + -- Linear interpolation between two values a and b, by a t between 0.0 and 1.0 + return (a * t) + (b * (1.0 - t)) +end + + +------------------------------------------------------------------------------ +-- Game methods +------------------------------------------------------------------------------ function love.load() - Cursor = love.graphics.newImage('imgs/pointer.png') - Bckg = love.graphics.newImage('imgs/bckg.png') + game_states[Current_state]:load() +end - Bgm = love.audio.newSource('bgm/dogs.wav') - Bgm:setLooping(true) - Bgm:play() +function love.update(dt) + local new_state = game_states[Current_state]:update(dt) - Click = love.audio.newSource('snd/click.wav') + -- If the game state changed then trigger a fade out. + if new_state ~= Current_state and Fade_out >= 1.0 then + Fade_out = 0.0 + Change_state = true + end + + -- If there is a fade in going then update it. + if Fade_in < 1.0 then + Fade_in = Fade_in + (dt / 2) + end + + -- If there is a fade out going then update it. + if Fade_out < 1.0 then + Fade_out = Fade_out + (dt / 2) + end + + -- Update the game state. + if game_states[new_state] == nil then + -- If the new state doesn't exist then wait for the fade out to end and quit. + if Fade_out >= 1.0 then + game_states[Current_state]:unload(dt) + love.event.quit() + end + else + -- If the new state exists then unload it's data and set the new state. + if Change_state and Fade_out >= 1.0 then + game_states[Current_state]:unload(dt) + Current_state = new_state + game_states[Current_state]:load() + Change_state = false + end + end end function love.draw() - love.graphics.draw(Bckg, 0, 0) - love.graphics.draw(Cursor, Mousex , Mousey) + -- Reset the drawing color and clear the screen. + love.graphics.clear() + + -- Render the current game state. + game_states[Current_state]:draw() + + -- If there is a fade in going then render it. + if Fade_in < 1.0 then + love.graphics.setColor(0, 0, 0) + love.graphics.circle('fill', -320, love.graphics.getHeight() / 2, lerp(0, 400 * 2, Fade_in)) + love.graphics.setColor() + end + + -- If there is a fade out going then render it. + if Fade_out < 1.0 then + love.graphics.setColor(0, 0, 0) + love.graphics.circle('fill', -320, love.graphics.getHeight() / 2, lerp(400 * 2, 0, Fade_out)) + love.graphics.setColor() + end end function love.keypressed(key) - if key == "escape" then - love.event.quit() - end + -- Send events to the active game state if there is no fade active. + if Fade_in >= 1.0 and Fade_out >= 1.0 then + game_states[Current_state]:keypressed(key) + end end function love.mousemoved(x, y) - Mousex = x - Mousey = y + -- Send events to the active game state if there is no fade active. + if Fade_in >= 1.0 and Fade_out >= 1.0 then + game_states[Current_state]:mousemoved(x, y) + end end -function love.mousepressed(x, y, button) - if button == 1 then - Click:play() - end +function love.mousepressed(x, y, btn) + -- Send events to the active game state if there is no fade active. + if Fade_in >= 1.0 and Fade_out >= 1.0 then + game_states[Current_state]:mousemoved(x, y, btn) + end end diff --git a/src/classes.lua b/src/classes.lua new file mode 100644 index 0000000..0923b10 --- /dev/null +++ b/src/classes.lua @@ -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 diff --git a/src/gstate.lua b/src/gstate.lua new file mode 100644 index 0000000..6523738 --- /dev/null +++ b/src/gstate.lua @@ -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 diff --git a/src/intro.lua b/src/intro.lua new file mode 100644 index 0000000..c039805 --- /dev/null +++ b/src/intro.lua @@ -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 diff --git a/src/states.lua b/src/states.lua new file mode 100644 index 0000000..700e4f2 --- /dev/null +++ b/src/states.lua @@ -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