81 lines
1.4 KiB
Lua
81 lines
1.4 KiB
Lua
|
|
|
|
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
|
|
|