main.lua, turtle.lua

This commit is contained in:
Nick Stokoe
2023-01-05 14:43:53 +00:00
parent c61b14375d
commit 980f401861
2 changed files with 120 additions and 0 deletions

80
turtle.lua Normal file
View File

@@ -0,0 +1,80 @@
local function fd(x, y, ang, steps, penDown)
local x2, y2 = x+math.sin(ang)*steps, y-math.cos(ang)*steps
if penDown then
love.graphics.line(x, y, x2, y2)
end
return x2, y2
end
local deg2rad = math.pi/180
function mkTurtle(x,y,degrees,orig)
local penDown = true
if x == nil then x = 0 end
if y == nil then y = 0 end
if degrees == nil then degrees = 0 end
local self
self = {
fd = function(steps)
x, y = fd(x, y, degrees*deg2rad, steps, penDown)
return self
end,
bk = function(steps)
x, y = fd(x, y, degrees*deg2rad, -steps, penDown)
return self
end,
rt = function(deg)
degrees = degrees + deg
return self
end,
lt = function(deg)
degrees = degrees - deg
return self
end,
pd = function(isDown)
penDown = isDown
return self
end,
col = function(color)
-- FIXME
return self
end,
dup = function(orig)
return mkTurtle(x, y, degrees, orig)
end,
parent = function()
return orig
end,
inline = function(env, func)
local stub = {}
local meta = {
__index = function(table, key)
if env[key] ~= nil then
return env[key]
end
if _ENV ~= nil and _ENV[key] ~= nil then
return _ENV[key]
end
return nil
end,
}
setmetatable(stub, meta)
setfenv(func, stub)
return func()
end,
}
return self
end
return mkTurtle