From 1061e4822fdd163e54a1ab2738a9c7e68147c180 Mon Sep 17 00:00:00 2001 From: Kazhnuz Date: Thu, 11 Apr 2019 17:23:39 +0200 Subject: [PATCH] gamecore: better comments and organization --- gamecore/init.lua | 15 +++++++++++++++ gamecore/input.lua | 12 ++++++++++++ gamecore/lang.lua | 16 +++++++++++----- gamecore/options.lua | 11 +++++++++++ gamecore/scenemanager.lua | 18 +++++++++++++++--- gamecore/screen.lua | 12 ++++++++++++ 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/gamecore/init.lua b/gamecore/init.lua index 101f49c..e78c59f 100644 --- a/gamecore/init.lua +++ b/gamecore/init.lua @@ -42,6 +42,9 @@ local Screen = require(cwd .. "screen") local Lang = require(cwd .. "lang") local SceneManager = require(cwd .. "scenemanager") +-- INIT FUNCTIONS +-- Initialize and configure the core object + function CoreSystem:new() self.debug = DebugSystem(self) self.options = Options(self) @@ -50,6 +53,9 @@ function CoreSystem:new() self.scenemanager = SceneManager(self) end +-- MOUSE FUNCTIONS +-- Initialize and configure the core object + function CoreSystem:mousemoved(x, y, dx, dy) local x, y = self.screen:project(x, y) local dx, dy = self.screen:project(dx, dy) @@ -61,6 +67,9 @@ function CoreSystem:mousepressed( x, y, button, istouch ) self.scenemanager:mousepressed( x, y, button, istouch ) end +-- UPDATE FUNCTIONS +-- Load every sytem every update functions of the scene and objects + function CoreSystem:update(dt) self.debug:update(dt) self.input:update(dt) @@ -68,10 +77,16 @@ function CoreSystem:update(dt) self.scenemanager:update(dt) end +-- DRAW FUNCTIONS +-- Draw the whole game + function CoreSystem:draw() self.scenemanager:draw() end +-- EXIT FUNCTIONS +-- Quit the game + function CoreSystem:exit() self.options:save() love.event.quit() diff --git a/gamecore/input.lua b/gamecore/input.lua index 752eefb..1027a28 100644 --- a/gamecore/input.lua +++ b/gamecore/input.lua @@ -24,6 +24,9 @@ local InputManager = Object:extend() +-- INIT FUNCTIONS +-- Initialize and configure the controller system + function InputManager:new(controller) self.controller = controller self.data = self.controller.options:getInputData() @@ -38,6 +41,9 @@ function InputManager:initKeys() self.fakesources = self:getSources() end +-- INFO FUNCTIONS +-- Get functions from the controller object + function InputManager:isDown(sourceid, padkey) local isdown = false @@ -84,6 +90,9 @@ function InputManager:getKey(sourceid, padkey) return key end +-- KEY MANAGEMENT FUNCTIONS +-- Manage pressed keys + function InputManager:flushKeys() for i,v in ipairs(self.sources) do self:flushSourceKeys(sourceid) @@ -129,6 +138,9 @@ function InputManager:checkKeys(sourceid) end end +-- UPDATE FUNCTIONS +-- Check every step pressed keys + function InputManager:update(dt) for i,v in ipairs(self.sources) do self:checkKeys(i) diff --git a/gamecore/lang.lua b/gamecore/lang.lua index 6c6a69f..83ba1f5 100644 --- a/gamecore/lang.lua +++ b/gamecore/lang.lua @@ -24,12 +24,23 @@ local LanguageManager = Object:extend() +-- INIT FUNCTIONS +-- Initialize and configure the translation system + function LanguageManager:new(controller) self.controller = controller self:setLang(self.controller.options.data.language) self:getTranslationData() end +function LanguageManager:setLang(lang) + self.controller.options.data.language = lang + self.lang = self.controller.options.data.language +end + +-- INFO FUNCTIONS +-- Get informations from the translation manager + function LanguageManager:getTranslationData() local _path = "datas/languages/init.lua" local fileinfo = love.filesystem.getInfo(_path) @@ -55,9 +66,4 @@ function LanguageManager:getCurrentLangName() return langnames[self.lang] end -function LanguageManager:setLang(lang) - self.controller.options.data.language = lang - self.lang = self.controller.options.data.language -end - return LanguageManager diff --git a/gamecore/options.lua b/gamecore/options.lua index e087b42..5b8b81f 100644 --- a/gamecore/options.lua +++ b/gamecore/options.lua @@ -27,6 +27,9 @@ local OptionsManager = Object:extend() local cwd = (...):gsub('%.options$', '') .. "." local binser = require(cwd .. "libs.binser") +-- INIT FUNCTIONS +-- Initialize and configure the game options + function OptionsManager:new() -- We begin by creating an empty data table before reading the data. self.data = {} @@ -53,6 +56,9 @@ function OptionsManager:reset() self.data.audio.sfx = 100 end +-- INFO FUNCTIONS +-- Get informations from the option managers + function OptionsManager:getFile(absolute) local dir = "" if absolute then @@ -96,6 +102,11 @@ function OptionsManager:getInputData() return self.data.input end +-- DATA HANDLING FUNCTIONS +-- Save and get data from the savefile + +-- FIXME: maybe subclass a special module for that ? + function OptionsManager:write() local data = self:getData() diff --git a/gamecore/scenemanager.lua b/gamecore/scenemanager.lua index dd24e66..a1041c5 100644 --- a/gamecore/scenemanager.lua +++ b/gamecore/scenemanager.lua @@ -25,6 +25,9 @@ local SceneManager = Object:extend() +-- INIT FUNCTIONS +-- Initialize and configure the scene manager + function SceneManager:new(controller) self.controller = controller self.currentScene = nil @@ -52,6 +55,13 @@ function SceneManager:clearStorage() self.storage = {} end +function SceneManager:clearScene() + self.currentScene = nil +end + +-- UPDATE FUNCTIONS +-- Update the current scene and its subobjects + function SceneManager:update(dt) if (self.currentScene ~= nil) then self.currentScene:setKeys() @@ -61,6 +71,9 @@ function SceneManager:update(dt) end end +-- MOUSE FUNCTIONS +-- Send pointer data to the scene + function SceneManager:mousemoved(x, y, dx, dy) if (self.currentScene ~= nil) then self.currentScene.mouse.x, @@ -77,9 +90,8 @@ function SceneManager:mousepressed( x, y, button, istouch ) end end -function SceneManager:clearScene() - self.currentScene = nil -end +-- DRAW FUNCTIONS +-- Draw the current scene function SceneManager:draw() self.controller.screen:apply() diff --git a/gamecore/screen.lua b/gamecore/screen.lua index 24a3c4b..ec3e6e0 100644 --- a/gamecore/screen.lua +++ b/gamecore/screen.lua @@ -27,6 +27,9 @@ local ScreenManager = Object:extend() local cwd = (...):gsub('%.screen$', '') .. "." local CScreen = require(cwd .. "libs.cscreen") +-- INIT FUNCTIONS +-- Initialize and configure the screen manager + function ScreenManager:new(controller) self.controller = controller self.data = self.controller.options.data.video @@ -52,6 +55,9 @@ function ScreenManager:applySettings() CScreen.update(width, height) end +-- POINTER FUNCTIONS +-- Translate the pointer according to the screen coordinates + function ScreenManager:project(x, y) return CScreen.project(x, y) end @@ -60,10 +66,16 @@ function ScreenManager:getMousePosition() return CScreen.project(love.mouse.getX(), love.mouse.getY()) end +-- INFO FUNCTIONS +-- Get screen informations + function ScreenManager:getDimensions() return self.width, self.height end +-- DRAW FUNCTIONS +-- Apply draw functions to the scene + function ScreenManager:apply() CScreen.apply() end