From e47a1e5544d00c26aa08ae41287a525617e494b8 Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 15 Nov 2021 16:46:12 +0800 Subject: *misc --- .../BuiltIn/Libraries/GameLab/Engine/Math/init.lua | 11 ++ Data/BuiltIn/Libraries/GameLab/Engine/init.lua | 2 + Data/BuiltIn/Libraries/GameLab/Package.lua | 3 + Data/BuiltIn/Libraries/GameLab/Path/init.lua | 5 + Data/BuiltIn/Libraries/GameLab/Threading/init.lua | 5 + Data/BuiltIn/Libraries/GameLab/Utils/init.lua | 25 +++- Data/BuiltIn/Libraries/GameLab/init.lua | 1 - Data/BuiltIn/Libraries/coil/LICENSE | 20 +++ Data/BuiltIn/Libraries/coil/README.md | 120 +++++++++++++++ Data/BuiltIn/Libraries/coil/coil.lua | 165 +++++++++++++++++++++ Data/Libraries/GameLab/Editor/reload.lua | 0 Data/Scripts/defines.lua | 2 + Data/Scripts/macro.lua | 3 - Data/boot.lua | 2 +- 14 files changed, 358 insertions(+), 6 deletions(-) create mode 100644 Data/BuiltIn/Libraries/GameLab/Path/init.lua create mode 100644 Data/BuiltIn/Libraries/GameLab/Threading/init.lua create mode 100644 Data/BuiltIn/Libraries/coil/LICENSE create mode 100644 Data/BuiltIn/Libraries/coil/README.md create mode 100644 Data/BuiltIn/Libraries/coil/coil.lua create mode 100644 Data/Libraries/GameLab/Editor/reload.lua create mode 100644 Data/Scripts/defines.lua delete mode 100644 Data/Scripts/macro.lua (limited to 'Data') diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/Math/init.lua b/Data/BuiltIn/Libraries/GameLab/Engine/Math/init.lua index 5997f7d..e7c0164 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/Math/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/Math/init.lua @@ -25,4 +25,15 @@ math.Clamp = function(v, min, max) return math.Min(math.Max(v, min), max) end +math.Sign = function(num) + local retval = 0 + if(num > 0) then + retval = 1 + end + if(num < 0) then + retval = -1 + end + return retval +end + return math \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Engine/init.lua b/Data/BuiltIn/Libraries/GameLab/Engine/init.lua index ac0073a..55a437f 100644 --- a/Data/BuiltIn/Libraries/GameLab/Engine/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Engine/init.lua @@ -1,4 +1,6 @@ local engine = GameLab.Package("GameLab.Engine") local import = GameLab.Import(...) + + return engine \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Package.lua b/Data/BuiltIn/Libraries/GameLab/Package.lua index 5eba68b..dbc2264 100644 --- a/Data/BuiltIn/Libraries/GameLab/Package.lua +++ b/Data/BuiltIn/Libraries/GameLab/Package.lua @@ -1,3 +1,6 @@ + +-- GameLab模块 + local package = function (name) local t = _G local pkgs = string.gmatch(name, "%.*(%w+)%.*") diff --git a/Data/BuiltIn/Libraries/GameLab/Path/init.lua b/Data/BuiltIn/Libraries/GameLab/Path/init.lua new file mode 100644 index 0000000..10dbf7a --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/Path/init.lua @@ -0,0 +1,5 @@ +local m = GameLab.Package("GameLab.Path") + + + +return m \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Threading/init.lua b/Data/BuiltIn/Libraries/GameLab/Threading/init.lua new file mode 100644 index 0000000..c044a3c --- /dev/null +++ b/Data/BuiltIn/Libraries/GameLab/Threading/init.lua @@ -0,0 +1,5 @@ +local m = GameLab.Package("GameLab.Threading") + + + +return m \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/Utils/init.lua b/Data/BuiltIn/Libraries/GameLab/Utils/init.lua index 51f6938..86bf90e 100644 --- a/Data/BuiltIn/Libraries/GameLab/Utils/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/Utils/init.lua @@ -1,3 +1,26 @@ local utils = GameLab.Package("GameLab.Utils") -return uitls \ No newline at end of file +utils.Clone = function(obj) + local lut = {} + + local function copy(object) + if type(object) ~= "table" then + return object + elseif lut[object] then + return lut[object] + end + + local newtable = {} + lut[object] = newtable + + for k, v in pairs(object) do + newtable[copy(k)] = copy(value) + end + + return setmetatable(newtable, getmetatable(object)) + end + + return copy(obj) +end + +return utils \ No newline at end of file diff --git a/Data/BuiltIn/Libraries/GameLab/init.lua b/Data/BuiltIn/Libraries/GameLab/init.lua index 33f8f2e..89a3efb 100644 --- a/Data/BuiltIn/Libraries/GameLab/init.lua +++ b/Data/BuiltIn/Libraries/GameLab/init.lua @@ -1,7 +1,6 @@ GameLab = GameLab or {} GameLab.VERSION = "0.0.1" -GameLab.AUTHOR = "chai" -- methods -- 用于模块的init.lua中加载模块内的脚本 diff --git a/Data/BuiltIn/Libraries/coil/LICENSE b/Data/BuiltIn/Libraries/coil/LICENSE new file mode 100644 index 0000000..03b6555 --- /dev/null +++ b/Data/BuiltIn/Libraries/coil/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2014 rxi + + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Data/BuiltIn/Libraries/coil/README.md b/Data/BuiltIn/Libraries/coil/README.md new file mode 100644 index 0000000..bec60a0 --- /dev/null +++ b/Data/BuiltIn/Libraries/coil/README.md @@ -0,0 +1,120 @@ +# coil +A tiny cooperative threading module for Lua. Coil is based around coroutines, +allowing many cooperative threads to run simultaneously. + +## Usage +The [coil.lua](coil.lua) file should be dropped into an existing project and +required by it. +```lua +coil = require "coil" +``` +At the start of each frame `coil.update()` should be called and given the delta +time since the last call as its argument. +```lua +coil.update(deltatime) +``` +Coil refers to each cooperative thread as a *task*; new tasks can be created by +using the `coil.add()` function. +```lua +-- prints the word "hello" each second for 5 seconds +coil.add(function() + for i = 1, 5 do + print("hello") + coil.wait(1) + end +end) +``` + +### coil.wait() +As tasks are cooperative, they must be manually yielded to allow other parts of +the program to continue. This is done by calling the `coil.wait()` function. +The function can be used to wait for different events before continuing. + +* If wait is called with a number as its argument then it waits for that number + of seconds before resuming. +* If wait is called with a callback created by coil.callback() it will wait + until that callback function is called before resuming; it will return the + arguments passed to the callback when it was called. +* If wait is called with no arguments it waits until the next frame + (`coil.update()` call) before resuming; it returns the time that has passed + since the last frame. + +### coil.callback() +The `coil.callback()` function can be used to create a special callback which, +if passed to the `coil.wait()` function, will cause the task to wait until the +callback is called. +```lua +-- Prints "Hello world" when `cb()` is called. +coil.add(function() + cb = coil.callback() + coil.wait(cb) + print("Hello world") +end) +``` + +### coil.update(dt) +This should be called at the start of each frame. Updates all the tasks, +running those which aren't waiting or paused. `dt` should be the amount of time +in seconds which has passed since the function was last called. + +### coil.add(fn) +Adds a new task, the task will begin running on the next call to +`coil.update()`. +```lua +-- prints "hello world" every 2 seconds +coil.add(function() + while 1 do + print("hello world") + coil.wait(2) + end +end) +``` + +## Stopping a task +A task can be stopped and removed at any point by calling its `:stop()` method. +To do this the task must be assigned to a variable when it is created. +```lua +-- Adds a new task +local t = coil.add(function() + coil.wait(1) + print("hello") +end) + +-- Removes the task before it has a chance to run +t:stop() +``` + +## Groups +Coil provides the ability to create task groups; these are objects which can +have tasks added to them, and which are in charge of updating and handling +their contained tasks. A group is created by calling the `coil.group()` +function. +```lua +local group = coil.group() +``` + +Once a group is created it acts independently of the `coil` object, and must +be updated each frame using its own update method. +```lua +group:update(deltatime) +``` + +To add a task to a group, the group's `add()` method should be used. +```lua +group:add(function() + coil.wait(10) + print("10 seconds have passed") +end) +``` + +A good example of where groups are useful is for games where you may have a set +of tasks which effect objects in the game world and which you want to pause +when the game is paused. A group's tasks can be paused by simply neglecting +to call its `update()` method; when a group is destroyed its tasks are also +destroyed. + + +## License +This library is free software; you can redistribute it and/or modify it under +the terms of the MIT license. See [LICENSE](LICENSE) for details. + diff --git a/Data/BuiltIn/Libraries/coil/coil.lua b/Data/BuiltIn/Libraries/coil/coil.lua new file mode 100644 index 0000000..fbd20c9 --- /dev/null +++ b/Data/BuiltIn/Libraries/coil/coil.lua @@ -0,0 +1,165 @@ +-- +-- coil +-- +-- Copyright (c) 2014 rxi +-- +-- This library is free software; you can redistribute it and/or modify it +-- under the terms of the MIT license. See LICENSE for details. +-- + +local coil = { _version = "0.1.0" } +coil.__index = coil +coil.tasks = {} + + +local unpack = unpack or table.unpack + +local _assert = function(cond, msg, lvl) + if cond then return cond, msg, lvl end + error(msg, lvl + 1) +end + +local callback_mt = { + __call = function(t, ...) + t.args = {...} + t.ready = true + end} + + +local task = {} +task.__index = task + + +function task.new(fn, parent) + local self = setmetatable({}, task) + self.routine = coroutine.wrap(fn) + self.parent = parent + self.pausecount = 0 + return self +end + + +function task:pause() + self.pausecount = self.pausecount + 1 +end + + +function task:resume() + _assert(self.pausecount > 0, "unbalanced resume()", 2) + self.pausecount = self.pausecount - 1 +end + + +function task:stop() + coil.remove(self.parent, self) +end + + + +function coil:update(dt) + if #self == 0 then return end + coil.deltatime = dt + for i = #self, 1, -1 do + local task = self[i] + if task.wait then + -- Handle wait + if type(task.wait) == "number" then + -- Handle numerical wait + task.wait = task.wait - dt + if task.wait <= 0 then + task.waitrem = task.wait + task.wait = nil + end + elseif type(task.wait) == "table" then + -- Handle callback object + if task.wait.ready then + task.wait = nil + end + end + end + if not task.wait and task.pausecount == 0 then + -- Run task + coil.current = task + if not task.routine() then + coil.remove(self, i) + end + end + end + coil.current = nil +end + + +function coil:add(fn) + local t = task.new(fn, self) + table.insert(self, t) + return t +end + + +function coil:remove(t) + if type(t) == "number" then + self[t] = self[#self] + table.remove(self) + return + end + for i, task in ipairs(self) do + if task == t then + return coil.remove(self, i) + end + end +end + + +function coil.wait(x, y) + -- Discard first argument if its a coil group + x = getmetatable(x) == coil and y or x + local c = coil.current + _assert(c, "wait() called from outside a coroutine", 2) + if type(x) == "number" then + -- Handle numerical wait + c.wait = (c.waitrem or 0) + x + if c.wait <= 0 then + c.waitrem = c.wait + return + else + c.waitrem = nil + end + else + -- Handle next-frame / callback wait + _assert(x == nil or getmetatable(x) == callback_mt, + "wait() expected number, callback object or nothing as argument", + 2) + c.waitrem = nil + c.wait = x + end + coroutine.yield(true) + -- Return args if wait was a callback object + if type(x) == "table" then + return unpack(x.args) + end + -- Return delta time if wait had no args + if x == nil then + return coil.deltatime + end +end + + +function coil.callback() + return setmetatable({ ready = false }, callback_mt) +end + + +function coil.group() + return setmetatable({}, coil) +end + + + +local bound = { + update = function(...) return coil.update(coil.tasks, ...) end, + add = function(...) return coil.add(coil.tasks, ...) end, + remove = function(...) return coil.remove(coil.tasks, ...) end, +} +setmetatable(bound, coil) + +return bound diff --git a/Data/Libraries/GameLab/Editor/reload.lua b/Data/Libraries/GameLab/Editor/reload.lua new file mode 100644 index 0000000..e69de29 diff --git a/Data/Scripts/defines.lua b/Data/Scripts/defines.lua new file mode 100644 index 0000000..cec592c --- /dev/null +++ b/Data/Scripts/defines.lua @@ -0,0 +1,2 @@ +GAMELAB_PROFILE = true +GAMELAB_DEBUG = true diff --git a/Data/Scripts/macro.lua b/Data/Scripts/macro.lua deleted file mode 100644 index c8a4895..0000000 --- a/Data/Scripts/macro.lua +++ /dev/null @@ -1,3 +0,0 @@ --- "macros" -GAMELAB_PROFILE = true -GAMELAB_DEBUG = true diff --git a/Data/boot.lua b/Data/boot.lua index 4b931aa..566468a 100644 --- a/Data/boot.lua +++ b/Data/boot.lua @@ -10,7 +10,7 @@ local engineCLibs = "./BuiltIn/Libraries/?.dll" .. ";./BuiltIn/Plugins/?.dll" local editorCLibs = "./Libraries/?.dll" .. "./Plugins/?.dll" package.cpath=package.cpath .. ";" .. engineCLibs .. ";" .. editorCLibs -require("macro") +require("defines") require("settings") -- debugging -- cgit v1.1-26-g67d0