local doorUiBase = {}
local doorUi = {}
doorUi[DS_FOR_SALE] = {}
doorUi[DS_UNOWNED] = {}
doorUi[DS_OWNED] = {}
local doors = {}
function DoorUiBase(identifier, Render)
return {
identifier = identifier,
Render = Render
}
end
function DoorUi(identifier, base, pos, scale)
local result = {
identifier = identifier,
base = nil,
pos = pos,
scale = scale
}
for i = 1, #doorUiBase do
if doorUiBase[i].identifier == base then
result.base = doorUiBase[i]
return result
end
end
return nil
end
function AddDoorUiBase(ui)
for i = 1, #doorUiBase do
if doorUiBase[i].identifier == ui.identifier then
return false
end
end
table.insert(doorUiBase, ui)
return true
end
function AddDoorUi(ui, status)
for i = 1, #doorUi do
if doorUi[i].identifier == ui.identifier then
return false
end
end
if base then
for i = 1, #doorUiBase do
if doorUiBase[i].identifier == ui.base then
ui.base = doorUiBase[i]
end
end
end
table.insert(doorUi[status], ui)
return true
end
include("ui.lua")
local function CursorPos_Door(pos, angle)
local p = util.IntersectRayWithPlane(LocalPlayer():EyePos(), LocalPlayer():GetAimVector(), pos, angle:Up())
if !p then
return nil
end
if WorldToLocal(LocalPlayer():GetShootPos(), Angle(0, 0, 0), pos, angle).z < 0 then
return nil
end
local curP = WorldToLocal(p, Angle(0, 0, 0), pos, angle)
return Vector(curP.x, -curP.y)
end
local function InitDoorUi(door)
local tbl = {
id = door:EntIndex(),
ui = {}
}
table.Add(tbl.ui, doorUi[GetDoorStatus(LocalPlayer(), door)])
table.insert(doors, tbl)
end
hook.Add("PostDrawTranslucentRenderables", "HRP_DoorUiRender", function (bDepth, bSkybox)
if bSkybox then
return
end
local ent = ents.FindByClass("prop_door_rotating")
for i = 1, #ent do
local dis = LocalPlayer():GetPos():Distance(ent[i]:GetPos())
if dis <= 250 then
local ang = ent[i]:GetForward():Dot((ent[i]:GetPos() - LocalPlayer():GetPos()):GetNormalized())
local pos = Matrix()
local rot = Matrix()
if ang > 0 then
pos:Translate(Vector(-2, 37, 15))
rot:Rotate(Angle(0, -90, 90))
else
pos:Translate(Vector(2, 8, 15))
rot:Rotate(Angle(0, 90, 90))
end
local mat = ent[i]:GetWorldTransformMatrix()
mat = mat * pos * rot
local curPos = CursorPos_Door(mat:GetTranslation(), mat:GetAngles())
if curPos then
cam.Start3D2D(mat:GetTranslation(), mat:GetAngles(), 0.05)
local found = false
for d = 1, #doors do
if doors[d].id == ent[i]:EntIndex() then
found = true
for u = 1, #doors[d].ui do
doors[d].ui[u].base.Render(ent[i], doors[d].ui[u], curPos / 0.05)
end
end
end
if !found then
InitDoorUi(ent[i])
end
cam.End3D2D()
end
end
end
end)
hook.Add("InitPostEntity", "HRP_DoorOwnership", function ()
local doors = ents.FindByClass("prop_door_rotating")
for i = 1, #doors do
doors[i]:SetNWString("owner", "")
local other = ents.FindByName(doors[i]:GetName())
if #other > 1 then
for o = 1, #other do
if doors[i] != other[o] then
doors[i]:SetNWInt("other", other[o]:EntIndex())
break
end
end
else
doors[i]:SetNWInt("other", "")
end
end
end)
DS_FOR_SALE = 1
DS_UNOWNED = 2
DS_OWNED = 3
function GetDoorStatus(ply, door)
local status = DS_FOR_SALE
local owner = door:GetNWString("owner", "")
if #owner > 0 then
if owner == ply:SteamID64() then
status = DS_OWNED
else
status = DS_UNOWNED
end
end
return status
end
AddDoorUiBase(DoorUiBase("Button",
function (door, ui, curPos)
if curPos.x >= ui.pos.x && curPos.x <= ui.pos.x + ui.scale.x && curPos.y >= ui.pos.y && curPos.y <= ui.pos.y + ui.scale.y then
ui.hovering = true
else
ui.hovering = false
end
if ui.activator && ui.Activated then
if ui.hovering then
if IsKeyDown(ui.activator) then
ui.down = true
else
ui.down = false
end
if WasKeyPressed(ui.activator) then
ui.Activated(door)
end
end
end
surface.SetDrawColor(0, 0, 0)
surface.DrawRect(ui.pos.x, ui.pos.y, ui.scale.x, ui.scale.y)
local color = ui.color || Color(255, 255, 255)
if ui.hovering then
if ui.down then
color.r = color.r / 4
color.g = color.g / 4
color.b = color.b / 4
else
color.r = color.r / 2
color.g = color.g / 2
color.b = color.b / 2
end
end
surface.SetDrawColor(color)
surface.DrawRect(ui.pos.x + 2, ui.pos.y + 2, ui.scale.x - 4, ui.scale.y - 4)
surface.SetFont("DoorUi")
surface.SetTextColor(ui.textColor || Color(0, 0, 0))
local x, y = surface.GetTextSize(ui.identifier)
surface.SetTextPos(ui.pos.x + ui.scale.x / 2 - x / 2, ui.pos.y + ui.scale.y / 2 - y / 2)
surface.DrawText(ui.identifier)
end))
AddDoorUiBase(DoorUiBase("Text",
function (door, ui, curPos)
if !ui.text then
return
end
surface.SetFont(ui.font || "DoorUi")
surface.SetTextColor(ui.textColor || Color(255, 255, 255))
if ui.scale.x > 0 && ui.scale.y > 0 then
local x, y = surface.GetTextSize(ui.text)
surface.SetTextPos(ui.pos.x + ui.scale.x / 2 - x / 2, ui.pos.y + ui.scale.y / 2 - y / 2)
else
surface.SetTextPos(ui.pos.x, ui.pos.y)
end
surface.DrawText(ui.text)
end))
local cost = DoorUi("Cost", "Text", Vector(170, 0), Vector(250, 80))
cost.textColor = Color(0, 100, 0)
cost.font = "DoorUiMoney"
cost.text = "$400"
AddDoorUi(cost, DS_FOR_SALE);
local buyButt = DoorUi("Buy", "Button", Vector(170, 100), Vector(250, 80))
buyButt.activator = KEY_Z
buyButt.Activated = function (door)
chat.AddText("You have bought the door!")
end
AddDoorUi(buyButt, DS_FOR_SALE)