diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 2740a4e..835ff3a 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -3,10 +3,12 @@ name: CI on: workflow_dispatch: push: - paths: - - 'src/*' - - 'tests/*' - - '.github/workflows/CI.yml' + branches: + - main + pull_request: + branches: + - main + - develop jobs: test: diff --git a/.github/workflows/DOCS.yml b/.github/workflows/DOCS.yml new file mode 100644 index 0000000..36139e7 --- /dev/null +++ b/.github/workflows/DOCS.yml @@ -0,0 +1,31 @@ +name: DOCS + +env: + CLOUDFLARE_PROJECT_DOXYGEN: doxygen-engine + +on: + workflow_dispatch: + push: + branches: + - main + pull_request: + branches: + - main + - develop + +jobs: + doxygen: + runs-on: ubuntu-latest + steps: + - + uses: actions/checkout@master + - + run: | + docker run --rm -v $(pwd):/app -w /app rodrigodornelles/doxygen:lua doxygen + - + uses: cloudflare/pages-action@v1 + with: + apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} + accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} + projectName: ${{ env.CLOUDFLARE_PROJECT_DOXYGEN }} + directory: html diff --git a/.gitignore b/.gitignore index cb3080b..4f1656b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ luacov.* vendor/ -dist/ \ No newline at end of file +dist/ +html/ +latex/ \ No newline at end of file diff --git a/Doxyfile b/Doxyfile new file mode 100644 index 0000000..8ae6916 --- /dev/null +++ b/Doxyfile @@ -0,0 +1,41 @@ +# Custom Configs +DOXYFILE_ENCODING = UTF-8 +PROJECT_NAME = "Engine" +PROJECT_NUMBER = 0.0.2 +PROJECT_BRIEF = "Game engine in lua" +PROJECT_LOGO = +OUTPUT_DIRECTORY = . +RECURSIVE = YES +CREATE_SUBDIRS = NO +STRIP_CODE_COMMENTS = NO +CALL_GRAPH = YES +HAVE_DOT = YES +EXTRACT_ALL = YES +USE_MATHJAX = YES +EXTRACT_STATIC = NO +SHORT_NAMES = YES +INLINE_GROUPED_CLASSES = YES +INLINE_SIMPLE_STRUCTS = YES +TYPEDEF_HIDES_STRUCT = YES +SOURCE_BROWSER = YES +VERBATIM_HEADERS = NO +PREDEFINED = DOXYGEN= +PLANTUML_JAR_PATH = $(PLANTUML_JAR_PATH) +PLANTUML_CFG_FILE = +PLANTUML_INCLUDE_PATH = +OUTPUT_LANGUAGE = English +FILE_PATTERNS = *.lua +FILTER_PATTERNS = *.lua=lua2dox +EXTENSION_MAPPING = .lua=C++ +INPUT = README.md SUPPORT.md src examples +USE_MDFILE_AS_MAINPAGE = README.md +MARKDOWN_ID_STYLE = GITHUB +ALIASES += startebnf=@startuml{ebnf} +ALIASES += endebnf=@enduml +# Theme: https://jothepro.github.io/doxygen-awesome-css/ +GENERATE_TREEVIEW = YES +DISABLE_INDEX = NO +FULL_SIDEBAR = NO +HTML_EXTRA_STYLESHEET = $(DOXYGEN_AWESOME_PATH)/doxygen-awesome.css \ + $(DOXYGEN_AWESOME_PATH)/doxygen-awesome-sidebar-only.css +HTML_COLORSTYLE = LIGHT diff --git a/examples/asteroids/game.lua b/examples/asteroids/game.lua index 750d3d9..4b6605a 100644 --- a/examples/asteroids/game.lua +++ b/examples/asteroids/game.lua @@ -11,7 +11,7 @@ --! state 8 as "menu" --! --! [*] -> 1 ---! 1 --> 2 +--! 1 ---> 2 --! 1 --> 3 --! 2 --> 1 --! 3 --> 4 diff --git a/src/cli/init.lua b/src/cli/init.lua index 0485294..d708435 100644 --- a/src/cli/init.lua +++ b/src/cli/init.lua @@ -1,16 +1,18 @@ local os = require('os') -local args = require('src/shared/args') +local zeebo_args = require('src/shared/args') -local run = args.has(arg, 'run') -local coverage = args.has(arg, 'coverage') -local game = args.get(arg, 'game', './examples/pong') -local core = args.get(arg, 'core', 'ginga') -local screen = args.get(arg, 'screen', '1280x720') -local command = args.param(arg, {'game', 'core', 'screen'}, 1, 'help') +--! @cond +local run = zeebo_args.has(arg, 'run') +local coverage = zeebo_args.has(arg, 'coverage') +local game = zeebo_args.get(arg, 'game', './examples/pong') +local core = zeebo_args.get(arg, 'core', 'ginga') +local screen = zeebo_args.get(arg, 'screen', '1280x720') +local command = zeebo_args.param(arg, {'game', 'core', 'screen'}, 1, 'help') if command == 'test-self' then coverage = coverage and '-lluacov' or '' - local ok = os.execute('lua '..coverage..' ./tests/*.lua') + local ok = os.execute('lua '..coverage..' ./tests/test_lib_common_math.lua') + local ok = ok and os.execute('lua '..coverage..' ./tests/test_shared_args.lua') if #coverage > 0 then os.execute('luacov src') end @@ -27,8 +29,9 @@ elseif command == 'build' then -- move common lib os.execute('cp src/lib/common/*.lua dist') + os.execute('cp src/object/application.lua dist/src_object_application.lua') os.execute('cp src/object/game.lua dist/src_object_game.lua') - os.execute('cp src/object/keys.lua dist/src_object_keys.lua') + os.execute('cp src/object/std.lua dist/src_object_std.lua') os.execute('cp src/shared/*.lua dist') -- move engine @@ -57,3 +60,5 @@ else print('command not found: '..command) os.exit(1) end + +--! @endcond diff --git a/src/lib/common/lib_math.lua b/src/lib/common/lib_math.lua index 2ac2d04..6f982fb 100644 --- a/src/lib/common/lib_math.lua +++ b/src/lib/common/lib_math.lua @@ -1,8 +1,13 @@ -local function clamp(value, min, max) - if value < min then - return min - elseif value > max then - return max +--! @defgroup std +--! @{ +--! @defgroup math +--! @{ + +local function clamp(value, value_min, value_max) + if value < value_min then + return value_min + elseif value > value_max then + return value_max else return value end @@ -30,27 +35,27 @@ local function abs(value) return value end -local function saw(x) - if x < 0.25 then - return x * 4 - elseif x < 0.50 then - return 1 - ((x - 0.25) * 4) - elseif x < 0.75 then - return ((x - 0.50) * 4) * (-1) +local function saw(value) + if value < 0.25 then + return value * 4 + elseif value < 0.50 then + return 1 - ((value - 0.25) * 4) + elseif value < 0.75 then + return ((value - 0.50) * 4) * (-1) end - return ((x - 0.75) * 4) - 1 + return ((value - 0.75) * 4) - 1 end local function lerp(a, b, alpha) return a + alpha * ( b - a ) end -local function map(x, in_min, in_max, out_min, out_max) - return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min +local function map(value, in_min, in_max, out_min, out_max) + return (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min end -local function clamp2(value, min, max) - return (value - min) % (max - min + 1) + min +local function clamp2(value, value_min, value_max) + return (value - value_min) % (value_max - value_min + 1) + value_min end local function cycle(passed, duration) @@ -79,6 +84,9 @@ local function max(...) return max_value end +--! @} +--! @} + local P = { cycle=cycle, clamp=clamp, @@ -88,7 +96,8 @@ local P = { map=map, dis=dis, saw=saw, - max=max + max=max, + dir=dir } return P; diff --git a/src/lib/ginga/core.lua b/src/lib/ginga/core.lua index 9f755f9..ce51a84 100644 --- a/src/lib/ginga/core.lua +++ b/src/lib/ginga/core.lua @@ -1,12 +1,18 @@ -local mathstd = require('math') -local game = require('game') -local math = require('lib_math') +local math = require('math') +local application = require('game') +local zeebo_math = require('lib_math') local decorators = require('decorators') +local game = require('src_object_game') +local std = require('src_object_std') +local fixture190 = '' + +--! @short nclua:canvas +--! @li local canvas = canvas + +--! @short nclua:event +--! @li local event = event -local game_obj = {meta={}, config={}, callbacks={}} -local std = {draw={},key={press={}},game={}} -local fixture190 = '' -- key mappings local key_bindings={ @@ -37,25 +43,25 @@ _ENV = nil local function std_draw_fps(x, y) canvas:attrColor('yellow') - if game_obj.fps_show >= 1 then + if game.fps_show >= 1 then canvas:drawRect('fill', x, y, 40, 24) end - if game_obj.fps_show >= 2 then + if game.fps_show >= 2 then canvas:drawRect('fill', x + 48, y, 40, 24) end canvas:attrColor('black') canvas:attrFont('Tiresias', 16) - if game_obj.fps_show >= 1 then + if game.fps_show >= 1 then canvas:drawText(x + 2, y, fps_obj.total) end - if game_obj.fps_show >= 1 then - canvas:drawText(x + 50, y, game_obj.fps_max) + if game.fps_show >= 1 then + canvas:drawText(x + 50, y, game.fps_max) end end local function std_draw_clear(color) canvas:attrColor(color) - canvas:drawRect('fill', 0, 0, game_obj.width, game_obj.height) + canvas:drawRect('fill', 0, 0, game.width, game.height) end local function std_draw_color(color) @@ -86,17 +92,17 @@ local function std_draw_line(x1, y1, x2, y2) end local function std_game_reset() - if game.callbacks.exit then - game.callbacks.exit(std, game_obj) + if application.callbacks.exit then + application.callbacks.exit(std, game) end - if game.callbacks.init then - game.callbacks.init(std, game_obj) + if application.callbacks.init then + application.callbacks.init(std, game) end end local function std_game_exit() - if game.callbacks.exit then - game.callbacks.exit(std, game_obj) + if application.callbacks.exit then + application.callbacks.exit(std, game) end event.post({class="ncl", type="stop"}) end @@ -105,7 +111,7 @@ local function event_loop(evt) if evt.class ~= 'key' then return end if not key_bindings[evt.key] then return end - -- https://github.com/TeleMidia/ginga/issues/190 + --! @li https://github.com/TeleMidia/ginga/issues/190 if #fixture190 == 0 and evt.key ~= 'ENTER' then fixture190 = evt.type end @@ -119,32 +125,32 @@ end local function fixed_loop() -- internal clock - game_obj.milis = event.uptime() - game_obj.fps = fps_obj.total - game_obj.dt = fps_obj.delta - if not fps.counter(game_obj.fps_max, fps_obj, game_obj.milis) then - game_obj.fps_max = fps_dropper[game_obj.fps_max] + game.milis = event.uptime() + game.fps = fps_obj.total + game.dt = fps_obj.delta + if not fps.counter(game.fps_max, fps_obj, game.milis) then + game.fps_max = fps_dropper[game.fps_max] end -- game loop - game.callbacks.loop(std, game_obj) + application.callbacks.loop(std, game) -- game render canvas:attrColor(0, 0, 0, 0) canvas:clear() - game.callbacks.draw(std, game_obj) + application.callbacks.draw(std, game) std_draw_fps(8,8) canvas:flush() -- internal loop - event.timer(fps_limiter[game_obj.fps_max], fixed_loop) + event.timer(fps_limiter[game.fps_max], fixed_loop) end local function setup(evt) if evt.class ~= 'ncl' or evt.action ~= 'start' then return end local w, h = canvas:attrSize() - std.math=math - std.math.random = mathstd.random + std.math=zeebo_math + std.math.random = math.random std.draw.clear=std_draw_clear std.draw.color=std_draw_color std.draw.rect=std_draw_rect @@ -152,26 +158,15 @@ local function setup(evt) std.draw.font=std_draw_font std.draw.line=std_draw_line std.draw.poly=decorators.poly(0, nil, std_draw_line) - std.key.press.up=0 - std.key.press.down=0 - std.key.press.left=0 - std.key.press.right=0 - std.key.press.red=0 - std.key.press.green=0 - std.key.press.yellow=0 - std.key.press.blue=0 - std.key.press.enter=0 std.game.reset=std_game_reset std.game.exit=std_game_exit - game_obj.width=w - game_obj.height=h - game_obj.milis=0 - game_obj.fps=0 - game_obj.fps_max = game.config and game.config.fps_max or 100 - game_obj.fps_show = game.config and game.config.fps_max or 0 - fps_obj.drop_time = game.config and game.config.fps_time or 1 - fps_obj.drop_count = game.config and game.config.fps_drop or 2 - game.callbacks.init(std, game_obj) + game.width=w + game.height=h + game.fps_max = application.config and application.config.fps_max or 100 + game.fps_show = application.config and application.config.fps_max or 0 + fps_obj.drop_time = application.config and application.config.fps_time or 1 + fps_obj.drop_count = application.config and application.config.fps_drop or 2 + application.callbacks.init(std, game) event.register(event_loop) event.timer(1, fixed_loop) event.unregister(setup) diff --git a/src/lib/love2d/core.lua b/src/lib/love2d/core.lua index 37c24f9..0d388e2 100644 --- a/src/lib/love2d/core.lua +++ b/src/lib/love2d/core.lua @@ -1,11 +1,10 @@ local os = require('os') -local game = require('game') -local math = require('lib_math') +local application = require('game') +local zeebo_math = require('lib_math') local decorators = require('decorators') -local arglib = require('args') -local game_obj = {} -local std = {draw={},key={press={}},game={}} -local started = false +local zeebo_arg = require('args') +local game = require('src_object_game') +local std = require('src_object_std') local key_bindings = { up='up', left='left', @@ -20,7 +19,7 @@ local key_bindings = { local function std_draw_clear(color) love.graphics.setColor(0, 0, 0) - love.graphics.rectangle('fill', 0, 0, game_obj.width, game_obj.height) + love.graphics.rectangle('fill', 0, 0, game.width, game.height) end local function std_draw_color(color) @@ -54,30 +53,30 @@ local function std_draw_font(a,b) end local function std_game_reset() - if game.callbacks.exit then - game.callbacks.exit(std, game_obj) + if application.callbacks.exit then + application.callbacks.exit(std, game) end - if game.callbacks.init then - game.callbacks.init(std, game_obj) + if application.callbacks.init then + application.callbacks.init(std, game) end end local function std_game_exit() - if game.callbacks.exit then - game.callbacks.exit(std, game_obj) + if application.callbacks.exit then + application.callbacks.exit(std, game) end love.event.quit() end function love.draw() - game.callbacks.draw(std, game_obj) + application.callbacks.draw(std, game) end function love.update(dt) - game_obj.dt = dt * 1000 - game_obj.milis = love.timer.getTime() * 1000 - game_obj.fps = love.timer.getFPS() - game.callbacks.loop(std, game_obj) + game.dt = dt * 1000 + game.milis = love.timer.getTime() * 1000 + game.fps = love.timer.getFPS() + application.callbacks.loop(std, game) end function love.keypressed(key) @@ -93,18 +92,18 @@ function love.keyreleased(key) end function love.resize(w, h) - game_obj.width = w - game_obj.height = h + game.width = w + game.height = h end function love.load(args) - local screen = arglib.get(args, 'screen') + local screen = zeebo_arg.get(args, 'screen') local w, h = love.graphics.getDimensions() if screen then w, h = screen:match('(%d+)x(%d+)') love.window.setMode(w, h, {resizable=true}) end - std.math=math + std.math=zeebo_math std.math.random = love.math.random std.draw.clear=std_draw_clear std.draw.color=std_draw_color @@ -113,20 +112,10 @@ function love.load(args) std.draw.font=std_draw_font std.draw.line=std_draw_line std.draw.poly=decorators.poly(0, love.graphics.polygon) - std.key.press.up=0 - std.key.press.down=0 - std.key.press.left=0 - std.key.press.right=0 - std.key.press.red=0 - std.key.press.green=0 - std.key.press.yellow=0 - std.key.press.blue=0 - std.key.press.enter=0 std.game.reset=std_game_reset std.game.exit=std_game_exit - game_obj.width=w - game_obj.height=h - game_obj.dt = 0 - love.window.setTitle(game.meta.title..' - '..game.meta.version) - game.callbacks.init(std, game_obj) + game.width=w + game.height=h + love.window.setTitle(application.meta.title..' - '..application.meta.version) + application.callbacks.init(std, game) end diff --git a/src/lib/repl/core.lua b/src/lib/repl/core.lua index b36f959..a987566 100644 --- a/src/lib/repl/core.lua +++ b/src/lib/repl/core.lua @@ -1,15 +1,18 @@ +--! @file src/lib/repl/core.lua +--! @short Read Eval Print Loop +--! @brief an interpreter to debuging the game via stdio. +--! @par Extended Backus-Naur Form --! @startebnf ---! line= [frame_skip], variable, ["=", value (* assignment *)]; +--! line = exit | frame_skip | [frame_skip], variable, ["=", value (* assignment *)]; --! frame_skip = [digit], "!" ; --! digit = { ? 0 - 9 ? }- ; +--! exit = "?" ; --! @endebnf package.path=package.path..';dist/?.lua' -- TODO make more smarth solution -local game_obj = require('src_object_game') -local key_obj = require('src_object_keys') -local mathstd = require('math') -local math = require('lib_math') - -local function mock() return end +local game = require('src_object_game') +local std = require('src_object_std') +local math = require('math') +local zeebo_math = require('lib_math') local function line_skip_frames(line_src) local frames, line = line_src:match('(%d+)!(.*)') @@ -31,13 +34,13 @@ local function line_assignment(line) return line, '' end -local function evaluate(var, assign, std_lib, game_obj) +local function evaluate(var, assign, std, game, application) local script = '' if assign and #assign > 0 then - script = 'return function(std, game)\n'..var..'=('..assign..')\n return ('..var..')\nend' + script = 'return function(std, game, application)\n'..var..'=('..assign..')\n return ('..var..')\nend' elseif var and #var > 0 then - script = 'return function(std, game)\nreturn ('..var..')\nend' + script = 'return function(std, game, application)\nreturn ('..var..')\nend' end if script and #script > 0 then @@ -45,7 +48,7 @@ local function evaluate(var, assign, std_lib, game_obj) local func, err = load(script) if func then local result = func() - return result(std_lib, game_obj) + return result(std, game, application) else error(err) end @@ -62,29 +65,23 @@ local function main() local frames = 0 local variable = '' local assignment = '' - local file_name = arg[1] or 'dist/game.lua' + local file_name = arg[1] or './dist/src_object_application.lua' local file_src = io.open(file_name, "r") - local game = file_src and load(file_src:read('*all')) - local stdlib = {draw = {}} + local apploader = file_src and load(file_src:read('*all')) local started = false - if not file_src or not game then - error('game not found!') + if not file_src then + error('game not found!'..file_name) + end + if not apploader then + error('game error!') end -- init the game - game = game() - stdlib.math = math - stdlib.math.random = mathstd.random - stdlib.draw.clear=mock - stdlib.draw.color=mock - stdlib.draw.rect=mock - stdlib.draw.text=mock - stdlib.draw.font=mock - stdlib.draw.line=mock - stdlib.draw.poly=mock - stdlib.key = key_obj - game.callbacks.init(stdlib, game_obj) + application = apploader() + std.math = zeebo_math + std.math.random = math.random + application.callbacks.init(std, game) while true do local index = 1 @@ -98,7 +95,7 @@ local function main() variable, assignment = line_assignment(line) frames = tonumber(frames) - local ok, output = evaluate(variable, assignment, stdlib, game_obj) + local ok, output = evaluate(variable, assignment, std, game, application) if ok then print(output) else @@ -106,21 +103,23 @@ local function main() print('\n') end - if not started and frames > 0 and game.callbacks.init then - game.callbacks.init(stdlib, game_obj) + if not started and frames > 0 and application.callbacks.init then + application.callbacks.init(std, game) started = true end while index <= frames do - if game.callbacks.loop then - game.callbacks.loop(stdlib, game_obj) + if application.callbacks.loop then + application.callbacks.loop(std, game) end - if game.callbacks.draw then - game.callbacks.draw(stdlib, game_obj) + if application.callbacks.draw then + application.callbacks.draw(std, game) end index = index + 1 end end end -main() +if not package.loaded['modulename'] then + main() +end diff --git a/src/object/application.lua b/src/object/application.lua new file mode 100644 index 0000000..9dda146 --- /dev/null +++ b/src/object/application.lua @@ -0,0 +1,25 @@ +--! @file src/object/application.lua +--! @short application object +--! @brief metatags, configs and code. + +local P = { + meta={ + title='', + description='', + version='' + }, + config = { + fps_max = 100, + fps_show = 0, + fps_drop = 2, + fps_time = 1 + }, + callbacks={ + init=function () end, + loop=function () end, + draw=function () end, + exit=function () end + } +} + +return P; diff --git a/src/object/game.lua b/src/object/game.lua index 6138805..cab3b73 100644 --- a/src/object/game.lua +++ b/src/object/game.lua @@ -1,3 +1,7 @@ +--! @file src/object/game.lua +--! @short game object +--! @brief a table to put anything related to developer game. + local P = { dt = 0, fps = 0, diff --git a/src/object/keys.lua b/src/object/keys.lua deleted file mode 100644 index efb1aff..0000000 --- a/src/object/keys.lua +++ /dev/null @@ -1,15 +0,0 @@ -local P = { - press = { - up=0, - down=0, - left=0, - right=0, - red=0, - green=0, - yellow=0, - blue=0, - enter=0 - } -} - -return P; diff --git a/src/object/std.lua b/src/object/std.lua new file mode 100644 index 0000000..942a5be --- /dev/null +++ b/src/object/std.lua @@ -0,0 +1,37 @@ +--! @file src/object/std.lua +--! @short standard library object +--! @brief can be used as mock + +local P = { + math = { + + }, + draw = { + clear = function () end, + color = function () end, + rect = function () end, + text = function () end, + font = function () end, + line = function () end, + poly = function () end + }, + game = { + reset = function () end, + exit = function () end + }, + key = { + press = { + up=0, + down=0, + left=0, + right=0, + red=0, + green=0, + yellow=0, + blue=0, + enter=0 + } + } +} + +return P; diff --git a/src/shared/args.lua b/src/shared/args.lua index 4ca14d8..df8fd0d 100644 --- a/src/shared/args.lua +++ b/src/shared/args.lua @@ -1,10 +1,17 @@ -local function shared_args_get(args, arg, default) +--! @brief get value of a compound flag +--! @param[in] args list of arguments +--! @param[in] key single char flag +--! @return string +--! @retval value when found +--! @retval default when not found +--! @retval NULL when found and not set default +local function shared_args_get(args, key, default) local index = 1 local value = default while index <= #args do local param = args[index] local nextparam = args[index + 1] - if param:sub(1, 2) == '--' and param:sub(3, #param) == arg and nextparam and nextparam:sub(1, 2) ~= '--' then + if param:sub(1, 2) == '--' and param:sub(3, #param) == key and nextparam and nextparam:sub(1, 2) ~= '--' then value = nextparam end index = index + 1 @@ -12,11 +19,17 @@ local function shared_args_get(args, arg, default) return value end -local function shared_args_has(args, arg) +--! @brief verify if exist a flag +--! @param[in] args list of arguments +--! @param[in] key single char flag +--! @return boolean +--! @retval true when found +--! @retval false when not found +local function shared_args_has(args, key) local index = 1 while index <= #args do local param = args[index] - if param:sub(1, 2) == '--' and param:sub(3, #param) == arg then + if param:sub(1, 2) == '--' and param:sub(3, #param) == key then return true end index = index + 1 @@ -24,14 +37,23 @@ local function shared_args_has(args, arg) return false end -local function shared_args_param(args, args_get, position, default) +--! @brief get a ordered param +--! @see take care when using @ref shared_args_get +--! @param[in] args list of arguments +--! @param[in] ignore_keys a list of compound flags to ignore +--! @param[in] position order of parameter started by 1 +--! @return string +--! @retval value when found +--! @retval default when not found +--! @retval NULL when found and not set default +local function shared_args_param(args, ignore_keys, position, default) local index = 1 local count = 1 - local args_get2 = {} + local ignore_keys2 = {} local value = default - while index <= #args_get do - args_get2[args_get[index]] = true + while index <= #ignore_keys do + ignore_keys2[ignore_keys[index]] = true index = index + 1 end @@ -40,7 +62,7 @@ local function shared_args_param(args, args_get, position, default) local arg = args[index] local param = args[index - 1] if arg:sub(1, 2) ~= '--' then - if index <= 1 or (param and not (param:sub(1, 2) == '--' and args_get2[param:sub(3, #param)])) then + if index <= 1 or (param and not (param:sub(1, 2) == '--' and ignore_keys2[param:sub(3, #param)])) then if position == count then value = arg end diff --git a/tests/test_lib_common_math.lua b/tests/test_lib_common_math.lua new file mode 100644 index 0000000..723df55 --- /dev/null +++ b/tests/test_lib_common_math.lua @@ -0,0 +1,61 @@ +local luaunit = require('luaunit') +local zeebo_math = require('src/lib/common/lib_math') + +function test_clamp() + luaunit.assertEquals(zeebo_math.clamp(10, 1, 5), 5) + luaunit.assertEquals(zeebo_math.clamp(-10, -5, 5), -5) + luaunit.assertEquals(zeebo_math.clamp(3, 1, 5), 3) +end + +function test_dir() + luaunit.assertEquals(zeebo_math.dir(2, 1), 1) + luaunit.assertEquals(zeebo_math.dir(-2, 1), -1) + luaunit.assertEquals(zeebo_math.dir(0, 1), 0) +end + +function test_dis() + luaunit.assertEquals(zeebo_math.dis(0, 0, 3, 4), 5) +end + +function test_abs() + luaunit.assertEquals(zeebo_math.abs(-5), 5) + luaunit.assertEquals(zeebo_math.abs(10), 10) +end + +function test_saw() + luaunit.assertAlmostEquals(zeebo_math.saw(0.1), 0.4, 1e-6) + luaunit.assertAlmostEquals(zeebo_math.saw(0.4), 0.4, 1e-6) + luaunit.assertAlmostEquals(zeebo_math.saw(0.6), -0.4, 1e-6) + luaunit.assertAlmostEquals(zeebo_math.saw(0.9), -0.4, 1e-6) +end + +function test_lerp() + luaunit.assertEquals(zeebo_math.lerp(0, 10, 0.5), 5) + luaunit.assertEquals(zeebo_math.lerp(1, 5, 0.25), 2) +end + +function test_map() + luaunit.assertEquals(zeebo_math.map(5, 0, 10, 0, 100), 50) + luaunit.assertEquals(zeebo_math.map(2, 0, 5, 0, 50), 20) +end + +function test_clamp2() + luaunit.assertEquals(zeebo_math.clamp2(10, 1, 5), 5) + luaunit.assertEquals(zeebo_math.clamp2(-10, -5, 5), 1) + luaunit.assertEquals(zeebo_math.clamp2(10, -5, 5), -1) + luaunit.assertEquals(zeebo_math.clamp2(3, 1, 5), 3) +end + +function test_cycle() + luaunit.assertEquals(zeebo_math.cycle(5, 10), 0.5) + luaunit.assertEquals(zeebo_math.cycle(15, 10), 0.5) +end + +function test_max() + luaunit.assertEquals(zeebo_math.max(1, 2, 3, 4, 5), 5) + luaunit.assertEquals(zeebo_math.max(10, -5, 3, 0), 10) + luaunit.assertEquals(zeebo_math.max(-1, -2, -3), -1) + luaunit.assertEquals(zeebo_math.max({1, 2, 3, 4, 5}), 5) +end + +os.exit(luaunit.LuaUnit.run())