конч vec3 + option

This commit is contained in:
neckrat 2025-03-22 01:18:53 +03:00
commit 12fc900e54
6 changed files with 226 additions and 0 deletions

13
.vscode/settings.json vendored Normal file
View File

@ -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
}

80
main.lua Normal file
View File

@ -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

48
option/option.lua Normal file
View File

@ -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

55
option/option_test.lua Normal file
View File

@ -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)

BIN
player.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 77 KiB

30
vector/vec3.lua Normal file
View File

@ -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())