main.lua, turtle.lua
This commit is contained in:
40
main.lua
Normal file
40
main.lua
Normal file
@@ -0,0 +1,40 @@
|
||||
mkTurtle = require "turtle"
|
||||
local screen = {
|
||||
width = love.graphics.getWidth(),
|
||||
height = love.graphics.getHeight(),
|
||||
}
|
||||
local o = mkTurtle(screen.width/2, screen.height*3/4)
|
||||
|
||||
local t = 0;
|
||||
function love.update(dt)
|
||||
t = t+ dt
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
function love.draw()
|
||||
tt = o:dup()
|
||||
|
||||
-- local fd, lt, rt, bk, pd = tt.fd, tt.lt, tt.rt, tt.bk, tt.pd
|
||||
-- tt.rt(45).fd(10)
|
||||
|
||||
local amultl = 0.8 + math.cos(t*5)/2
|
||||
local amultr = 0.8 + math.cos(t*7)/2
|
||||
local lmultl = 0.7 + math.cos(t*3)/2
|
||||
local lmultr = 0.7 + math.cos(t*4.5)/2
|
||||
local iangle = math.cos(t*2.5)*40-20
|
||||
function trunk(tt, level, length, angle)
|
||||
tt.fd(length)
|
||||
if level > 0 then
|
||||
tt.lt(iangle)
|
||||
trunk(tt:dup().lt(angle), level-1, length*lmultl, angle*amultl)
|
||||
trunk(tt:dup().rt(angle), level-1, length*lmultr, angle*amultr)
|
||||
end
|
||||
end
|
||||
|
||||
trunk(tt, 10, 60, math.sin(t)*180)
|
||||
|
||||
|
||||
end
|
||||
|
||||
80
turtle.lua
Normal file
80
turtle.lua
Normal 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
|
||||
|
||||
Reference in New Issue
Block a user