commit 12fc900e54fb8251a37853faeebc717731978359 Author: neckrat Date: Sat Mar 22 01:18:53 2025 +0300 конч vec3 + option diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..af27022 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,13 @@ +{ + "Lua.diagnostics.globals": [ + "love" + ], + "Lua.workspace.library": [ + "${addons}/love2d/module/library" + ], + "Lua.runtime.version": "LuaJIT", + "Lua.runtime.special": { + "love.filesystem.load": "loadfile" + }, + "Lua.workspace.checkThirdParty": false +} \ No newline at end of file diff --git a/main.lua b/main.lua new file mode 100644 index 0000000..54c353b --- /dev/null +++ b/main.lua @@ -0,0 +1,80 @@ +local Player = {} + +local function vec2(x, y) + return function() + return { x = x, y = y } + end +end + +local function vec2_length(v) + return math.sqrt(v().x * v().x + v().y * v().y) +end + +local function vec2_add(v1, v2) + return vec2(v1().x + v2().x, v1().y + v2().y) +end + +local function vec2_normalize(v) + local length = vec2_length(v) + if (length == 0) then return vec2(0, 0) end + return vec2(v().x / length, v().y / length) +end + +local function vec2_scale(v, factor) + return vec2(v().x * factor, v().y * factor) +end + +local function vec2_to_string(v) + return "{" .. v().x .. ", " .. v().y .. "}" +end + +function Player:init() + self.position = vec2(0, 0) + self.velocity = vec2(0, 0) + + self.sprite = love.graphics.newImage("player.png") +end + +function Player:update(dt) + local speed = 30 + local friction = 0.05 + local acc = vec2(0, 0) + + if (love.keyboard.isDown('w')) then + acc = vec2_add(acc, vec2(0, -1)) + end + if (love.keyboard.isDown('a')) then + acc = vec2_add(acc, vec2(-1, 0)) + end + if (love.keyboard.isDown('d')) then + acc = vec2_add(acc, vec2(1, 0)) + end + if (love.keyboard.isDown('s')) then + acc = vec2_add(acc, vec2(0, 1)) + end + + acc = vec2_scale(vec2_normalize(acc), speed * dt) + self.velocity = vec2_add(self.velocity, acc) + self.position = vec2_add(self.position, self.velocity) + + + self.velocity = vec2_scale(self.velocity, 1 - friction) + + -- print(vec2_length(self.velocity)) +end + +function love.load() + Player:init() +end + +function love.update(dt) + Player:update(dt) +end + +function love.draw() + love.graphics.draw(Player.sprite, Player.position().x, Player.position().y) +end + +function love.conf(t) + t.console = true +end \ No newline at end of file diff --git a/option/option.lua b/option/option.lua new file mode 100644 index 0000000..03b712b --- /dev/null +++ b/option/option.lua @@ -0,0 +1,48 @@ +Option = { + some = nil, + none = nil, + + __tostring = function() + return "Option" + end +} +None = setmetatable({}, { + __index = Option, + __tostring = function() + return "None" + end +}) + +function Some(value) + local opt = { + some = function() + return value + end + } + setmetatable(opt, { + __index = Option, + __tostring = function() + return "Some(" .. value .. ")" + end + }) + + return opt +end + +function Option:is_none() + return self.some == nil +end + +function Option:is_some() + return self.some ~= nil +end + +function Option:unwrap(default) + if self.some then return self.some() end + return default +end + +function Option:try(fn) + if self:is_none() then return self end + return fn(self.some()) +end diff --git a/option/option_test.lua b/option/option_test.lua new file mode 100644 index 0000000..6a9d7cf --- /dev/null +++ b/option/option_test.lua @@ -0,0 +1,55 @@ +--- Tests +local none = None +local none1 = None +local some = Some(42) +local some1 = Some('foo') + +assert(none:is_none()) +assert(none1 == none) + +assert(some:is_some()) +assert(some ~= some1) + +assert(some:unwrap(43) == 42) +assert(some:unwrap() == 42) +assert(none:unwrap(43) == 43) + +assert(some:try( + function(x) + return x + 1 + end +) == 43) +assert(none:try( + function(x) + return x + 1 + end +) == None) + +assert(tostring(some) == "Some(42)") +assert(tostring(none) == "None") + +--- Usage +local function safe_div(a, b) + if (b == 0) then return None end + return Some(a / b) +end + +local res = safe_div(10, 2) +assert(res:is_some()) + +local res1 = safe_div(10, 0) +assert(res1:is_none()) + +local chain = safe_div(10, 2):try( + function(x) + return safe_div(x, 5) + end +) +assert(chain:unwrap(0) == 1) + +local chain1 = safe_div(10, 0):try( + function(x) + return safe_div(x, 5) + end +) +assert(chain1:unwrap(0) == 0) diff --git a/player.png b/player.png new file mode 100644 index 0000000..759ade7 Binary files /dev/null and b/player.png differ diff --git a/vector/vec3.lua b/vector/vec3.lua new file mode 100644 index 0000000..0584fc7 --- /dev/null +++ b/vector/vec3.lua @@ -0,0 +1,30 @@ +__Vec3 = { + x = 0, + y = 0, + z = 0 +} + +function Vec3(vec) + return setmetatable({ + x = vec[1] or 0, + y = vec[2] or 0, + z = vec[3] or 0, + }, {__index = __Vec3, + }) +end + +function __Vec3:add(v1, v2) + return Vec3 { v1.x + v2.x, v1.y + v2.y, v1.z + v2.z } +end + +function __Vec3:length() + return math.sqrt(self.x ^ 2 + self.y ^ 2 + self.z ^ 2) +end + +function __Vec3:to_string() + return "{".. self.x .. ", " .. self.y .. ", " .. self.z .. "}" +end + +local v = Vec3 {1, 2, 3} + +print(v:to_string()) \ No newline at end of file