DIESER CODE WURDE NICHT AUSGIEBIG GETESTET!
ERSTELLUNGSDAUER: 15 Minuten unter Zeitdruck
Außerdem wurde kein Wert auf Übersichtlichkeit gelegt.
Hier geht es um die reine Funktionsanwendung in Assoziation mit anderen Funktionen und Events.
Gewisse Dinge hätte man besser lösen können.. bezüglich der Performance und Anwendbarkeit, jedoch wäre das für einen gebürtigen Anfänger etwas zu tricky
P.S. Ich wollte das eigentlich objektorientiert schreiben, jedoch ist das Tutorial mehr für Anfänger gedacht..
Außerdem sind die Skripts mehr oder weniger modular aufgebaut.
main_c.lua
global = {}
global.width, global.height = guiGetScreenSize()
local main = {}
main.canStreamedElements = {"player", "vehicle"}
main.streamedElements = {}
main.boxWidth = 130
addEventHandler("onClientResourceStart", resourceRoot, function() --resourceRoot wird mit root substituiert, da sonst dieses Event beim Starten jeder Ressource getriggert werden würde.
for _, _type in ipairs(main.canStreamedElements) do
for i, element in ipairs(getElementsByType(_type, root, true)) do
main.streamedElements[element] = _type
end
end
end)
addEventHandler("onClientRender", root, function()
for element, value in pairs(main.streamedElements) do
if element ~= localPlayer then
local x, y, z = getElementPosition(element)
local cx, cy, cz = getCameraMatrix()
if isLineOfSightClear(cx, cy, cz, x, y, z, true, true, true, true, true, true, true, element) then
local sx, sy = getScreenFromWorldPosition(x, y, z)
if sx and sy then
createElementInfoBox(sx - (main.boxWidth / 2), sy, main.boxWidth, element)
end
end
end
end
end)
addEventHandler("onClientElementStreamIn", root, function()
main.streamedElements[source] = getElementType(source)
end)
addEventHandler("onClientElementStreamOut", root, function()
if main.streamedElements[source] then
main.streamedElements[source] = nil
end
end)
addEventHandler("onClientElementDestroy", root, function()
if main.streamedElements[source] then
main.streamedElements[source] = nil
end
end)
Alles anzeigen
main_s.lua
addEventHandler("onResourceStart", resourceRoot, function() --resourceRoot wird mit root substituiert, da sonst dieses Event beim Starten jeder Ressource getriggert werden würde.
local doBind = function(player)
bindKey(player, "o", "down", function(keyPresser, key)
createPedInFront(keyPresser, 5)
end)
end
for i, player in ipairs(getElementsByType("player")) do
doBind(player)
end
addEventHandler("onPlayerJoin", root, function()
doBind(source)
end)
end)
function createPedInFront(player, distance)
local x, y, z = getElementPosition(player) -- die Position des keyPressers
local rx, ry, rz = getElementRotation(player) -- die Rotation des keyPressers
local nx, ny = math.getPointFromDistance2D(x, y, rz, distance or 5) -- die Position des keyPressers in 5m Entfernung (vor sich selber) (distance or 0 ist eine optionale Variable)
createPed(math.random(9, 100), nx, ny, z, math.findRotation(nx, ny, x, y)) -- Erstellung des Peds (vor mir, er schaut mich an)
outputDebugString("PED wurde erstellt.") -- Bekanntgabe in Debugscript
end
addEvent("server:createPedInFront", true)
addEventHandler("server:createPedInFront", root, createPedInFront)
Alles anzeigen
drawing_c.lua
function createElementInfoBox(x, y, width, element)
assert(x and y and width and element, "Bad Argument @ createElementInfoBox")
local font = "default-bold"
local health = math.round(getElementHealth(element))
local maxHealth = (getElementType(element) == "vehicle" and 1000 or 100)
local alpha = math.round(getElementAlpha(element))
local dimension = getElementDimension(element)
local elementType = getElementType(element)
local content = string.format([[Health: %s / %s
Alpha: %s
Dimension: %s
Type: %s
]], health, maxHealth, alpha, dimension, elementType)
if health > 0 then
local healthBorderSize = (width / 4) * (1 - (health / maxHealth))
local fontHeight = dxGetFontHeight(1, font) * 4 -- kann man ruhig machen, da die Zeilen hier fest definiert sind
dxDrawRectangle(x, y, width, fontHeight, tocolor(0, 0, 0, 100))
dxDrawRectangleBorder(x, y, width, fontHeight, 1, tocolor(255, 255, 255, 100), true)
dxDrawRectangleBorder(x, y, width, fontHeight, healthBorderSize, tocolor(150 + math.calc(255 - 150, health, maxHealth), 80, 0, 150 - math.calc(150, health, maxHealth)), false)
dxDrawText(content, x, y, x + width, y + fontHeight, tocolor(255, 255, 255, 255), 1, "bold", "center")
end
end
Alles anzeigen