Jump to content

[Aporte]Fractal Trees MTA


Recommended Posts

Buenos como estaba aburrido y sigo en youtube a un programador de la universidad de NY decidí ver uno  de sus vídeos y me dije porque no hacerlo en mta así que aquí esta:
 

Os dejo el vídeo en el cual código me base para hacer esto.

Spoiler

 

 

 

Source

local sX, sY = guiGetScreenSize()
local w, h = 600, 700
local x, y = sX*0.5 - w*0.5, sY*0.5 - h*0.5
local len = 100
local angleOffset = 10
local separation = 20
local tick = getTickCount()
local baseX, baseY = x + w*.5, y + h
local updateLen = false

local nodes = {}

function setup()
	table.insert(nodes, {x = baseX, y = baseY - len, angle = 300, len = len, parent = 0, haveChildres = false})
end
addEventHandler("onClientResourceStart", resourceRoot, setup)

function renderWorkStation()
	--Render Background
	dxDrawRectangle(x, y, w, h, tocolor(50, 50, 50, 255))

	for i = 1, #nodes do
		branch(i)
	end

	--Create new Nodes
	if getTickCount() - tick > 500 then
		for i = 1, #nodes do
			local node = nodes[i]
			if not node.haveChildres and len > 2 then
					createChildrens(i)
					node.haveChildres = true
			end
		end
		tick = getTickCount()
		updateLen = true
	end

	if updateLen then
		len = len*0.67
		updateLen = false
	end
end
addEventHandler("onClientRender", root, renderWorkStation)

function getXYFromAngle(angle, len)
	return math.cos( math.rad( angle ) ) * len, math.sin( math.rad( angle ) ) * len
end

function createChildrens(index)
	local parent = nodes[index]
	local angle = parent.angle - angleOffset
	--Right
	local startX, startY = getXYFromAngle(angle, len)
	table.insert(nodes, {x = startX + parent.x, y = startY + parent.y, angle = angle + separation*2, len = len, parent = index})
	--outputChatBox("[Node] Creating node ID => "..#nodes.. " Angle => "..angle)
	--Left
	local startX, startY = getXYFromAngle(angle - separation*2, len)
	table.insert(nodes, {x = startX + parent.x, y = startY + parent.y, angle = angle - separation, len = len, parent = index})
	--outputChatBox("[Node] Creating node ID => "..#nodes.. " Angle => "..-angle)
end

function branch(index)
	local node = nodes[index]
	if index == 1 then
		--dxDrawRectangle(node.x - 2.5, node.y - 2.5, 5, 5, tocolor(255, 0, 0, 255))
		dxDrawLine(node.x, node.y, baseX, baseY)
	else

		--dxDrawRectangle(node.x - 2.5, node.y - 2.5, 5, 5, tocolor(255, 0, 0, 255))
		local color = node.len < 20 and tocolor(75, 200, 75) or tocolor(255, 255, 255)
		dxDrawLine(node.x, node.y, nodes[node.parent].x, nodes[node.parent].y, color)
	end
end

addCommandHandler("changeAngle", 
function(cmd, angle, sep)
	angleOffset = tonumber(angle)
	separation = tonumber(sep)
	len = 100
	nodes = {}
	setup()
end)

Esto lo hice porque me aburria así que no es un código ni bien organizado ni pensado para un servidor simplemente for-fun

  • Like 3
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...