diff --git a/lua/darkrp_modules/levels/cl_hud.lua b/lua/darkrp_modules/levels/cl_hud.lua index a13ead4..2ea8dbd 100644 --- a/lua/darkrp_modules/levels/cl_hud.lua +++ b/lua/darkrp_modules/levels/cl_hud.lua @@ -68,35 +68,46 @@ local OldXP = 0 local xp_bar = Material("vrondakis/xp_bar.png","noclamp smooth") local function HUDPaint() if not LevelSystemConfiguration then return end - local PlayerLevel = LocalPlayer():getDarkRPVar("level") - local PlayerXP = LocalPlayer():getDarkRPVar("xp") + local PlayerLevel = LocalPlayer():getDarkRPVar("level") or 1 + local PlayerXP = LocalPlayer():getDarkRPVar("xp") or 0 + local MaxPlayerXP = (((10+(((PlayerLevel)*((PlayerLevel)+1)*90))))*LevelSystemConfiguration.XPMult) - local percent = ((PlayerXP or 0)/(((10+(((PlayerLevel or 1)*((PlayerLevel or 1)+1)*90))))*LevelSystemConfiguration.XPMult)) // Gets the accurate level up percentage + + local percent = PlayerXP / MaxPlayerXP // Gets the accurate level up percentage local drawXP = Lerp(8*FrameTime(),OldXP,percent) OldXP = drawXP - local percent2 = percent*100 - percent2 = math.Round(percent2) - percent2 = math.Clamp(percent2, 0, 99) //Make sure it doesn't round past 100% + + local xpBarText = "" + + if LevelSystemConfiguration.BarTextPercentage then + xpBarText = percent * 100 + xpBarText = math.Round(xpBarText) + xpBarText = math.Clamp(xpBarText, 0, 99) + + xpBarText = xpBarText .. "%" + else + xpBarText = PlayerXP .. " / " .. MaxPlayerXP + end - if LevelSystemConfiguration.EnableBar then + if LevelSystemConfiguration.EnableBar and not LevelSystemConfiguration.AlternativeBar then // Draw the XP Bar surface.SetDrawColor(0,0,0,200) - surface.DrawRect(ScrW()/2-300,(LevelSystemConfiguration.XPBarYPos or 0),580,25) + surface.DrawRect(ScrW()/2-300,ScrH() * (LevelSystemConfiguration.XPBarYPos or 0),580,25) // Draw the XP Bar before the texture - surface.SetDrawColor(LevelSystemConfiguration.LevelBarColor[1],LevelSystemConfiguration.LevelBarColor[2],LevelSystemConfiguration.LevelBarColor[3],255) - surface.DrawRect(ScrW()/2-300,(LevelSystemConfiguration.XPBarYPos or 0),580*drawXP,25) + surface.SetDrawColor(LevelSystemConfiguration.LevelBarColor.r,LevelSystemConfiguration.LevelBarColor.g,LevelSystemConfiguration.LevelBarColor.b,255) + surface.DrawRect(ScrW()/2-300,ScrH() * (LevelSystemConfiguration.XPBarYPos or 0),580*drawXP,25) //Render the texture surface.SetMaterial(xp_bar) surface.SetDrawColor(255,255,255,255) - surface.DrawTexturedRect( ScrW()/2-371, 0+(LevelSystemConfiguration.XPBarYPos or 0), 742,46) + surface.DrawTexturedRect( ScrW()/2-371, ScrH() * (LevelSystemConfiguration.XPBarYPos or 0), 742,46) end // Render the text - if LevelSystemConfiguration.BarText then - draw.DrawText("Level "..(LocalPlayer():getDarkRPVar("level") or 0).." - "..percent2 .."%", "HeadBar", ScrW()/2,7+(LevelSystemConfiguration.XPBarYPos or 0),(LevelSystemConfiguration.XPTextColor or Color(255,255,255,255)), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) + if LevelSystemConfiguration.BarText and not LevelSystemConfiguration.AlternativeBar then + draw.DrawText("Level "..(LocalPlayer():getDarkRPVar("level") or 0).." - "..xpBarText, "HeadBar", ScrW()/2,7+(LevelSystemConfiguration.XPBarYPos or 0),(LevelSystemConfiguration.XPTextColor or Color(255,255,255,255)), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) end if LevelSystemConfiguration.LevelText then @@ -108,4 +119,50 @@ local function HUDPaint() end hook.Add("HUDPaint", "manolis:MVLevels:HUDPaintA", HUDPaint) // IS THAT UNIQUE ENOUGH FOR YOU, FUCKING GMOD HOOKING BULLSHIT. +local function CreateAlternativeXPBar(oldXP, newXP, maxXP, currentLevel) + vrondakis_xp_bar = vgui.Create("Vrondakis.XPBar") + vrondakis_xp_bar:SetSize(ScrW() * 0.4, ScrH() * 0.095) + vrondakis_xp_bar:SetPos(0, ScrH() * LevelSystemConfiguration.AlternativeXPBarYPos) + vrondakis_xp_bar:CenterHorizontal() + vrondakis_xp_bar:SetData(oldXP, newXP, maxXP, currentLevel) + + return vrondakis_xp_bar +end + +net.Receive("Vrondakis.ShowXPBar", function() + --[[local data = util.Decompress(net.ReadData()) + data = util.JSONToTable(data)--]] + if not LevelSystemConfiguration.EnableBar or not LevelSystemConfiguration.AlternativeBar then return end + local oldXP = net.ReadInt(20) + local newXP = net.ReadInt(20) + local maxXP = net.ReadInt(20) + local currentLevel = LocalPlayer():getDarkRPVar("level") or 0 + + if IsValid(vrondakis_xp_bar) then + vrondakis_xp_bar:ResetViewingTime() + vrondakis_xp_bar:SetData(oldXP, newXP, maxXP, currentLevel) -- update data + else + local xp_bar = CreateAlternativeXPBar(oldXP, newXP, maxXP, currentLevel) + xp_bar:SetViewingTime(5) + end +end) + +-- Context menu + +hook.Add("ContextMenuOpened", "Vrondakis.show_alternative_xp_bar", function() + if not LevelSystemConfiguration.AlternativeBar then return end + + local oldXP = LocalPlayer():getDarkRPVar("xp") or 0 + local newXP = LocalPlayer():getDarkRPVar("xp") or 0 + local currentLevel = LocalPlayer():getDarkRPVar("level") or 0 + local maxXP = (((10+(((currentLevel)*((currentLevel)+1)*90))))*LevelSystemConfiguration.XPMult) + + local xp_bar = CreateAlternativeXPBar(oldXP, newXP, maxXP, currentLevel) +end) + +hook.Add("ContextMenuClosed", "Vrondakis.remove_alternative_xp_bar", function() + if LevelSystemConfiguration.AlternativeBar and IsValid(vrondakis_xp_bar) then + vrondakis_xp_bar:Remove() + end +end) \ No newline at end of file diff --git a/lua/darkrp_modules/levels/cl_vrondakis_xp_bar.lua b/lua/darkrp_modules/levels/cl_vrondakis_xp_bar.lua new file mode 100644 index 0000000..a3afe2e --- /dev/null +++ b/lua/darkrp_modules/levels/cl_vrondakis_xp_bar.lua @@ -0,0 +1,106 @@ +surface.CreateFont("Vrondakis.XPBarLevelFont", { + font = "Roboto", + size = ScreenScale(12) +}) + +surface.CreateFont("Vrondakis.XPBarLevelFont", { + font = "Roboto", + size = ScreenScale(12) +}) + +surface.CreateFont("Vrondakis.XPBarText", { + font = "Roboto", + size = ScreenScale(5.75) +}) + +local PANEL = {} + +function PANEL:Init() + -- Animiation init + --self.startBarAnim = 0 + + self.viewingTime = 0 + --self:SetViewingTime(self.viewingTime) + + -- Box area where the level will reside + + self.levelArea = self:Add("DPanel") + self.levelArea:Dock(LEFT) + self.levelArea:SetWide(100) + + self.levelArea.Paint = function(me, w, h) + draw.RoundedBox(8, 0, 0, w, h, LevelSystemConfiguration.AlternativeBarBGColor) + + if not self.currentLevel then return end + draw.SimpleText(self.currentLevel, "Vrondakis.XPBarLevelFont", w / 2, h / 2, Color(210, 210, 210), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) + end + + -- The actual xp bar + + self.xpBar = self:Add("DPanel") + self.xpBar:Dock(FILL) + self.xpBar:DockMargin(0, self.levelArea:GetTall() * 1.8, 0, self.levelArea:GetTall() * 1.8) + + self.xpBar.Paint = function(me, w, h) + draw.RoundedBoxEx(4, 0, 0, w, h, LevelSystemConfiguration.AlternativeBarBGColor, false, true, false, true) + + if not self.oldXP or not self.newXP or not self.maxXP then return end + + local xpWidthMultiplier = math.Clamp(self.newXP / self.maxXP, 0, 1) + local barColor = Color(LevelSystemConfiguration.LevelBarColor.r,LevelSystemConfiguration.LevelBarColor.g,LevelSystemConfiguration.LevelBarColor.b) + draw.RoundedBoxEx(4, 0, 0, w * xpWidthMultiplier, h, barColor, false, false, false, false) + + if LevelSystemConfiguration.BarText then + local text = "" + + if LevelSystemConfiguration.BarTextPercentage then + local percent = xpWidthMultiplier * 100 + percent = math.Round(percent) + percent = math.Clamp(percent, 0, 99) + + text = percent .. "%" + else + text = self.newXP .. " / " .. self.maxXP + end + + draw.SimpleText(text, "Vrondakis.XPBarText", w / 2, h / 2, LevelSystemConfiguration.XPTextColor, TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) + end + end +end + +function PANEL:SetData(oldXP, newXP, maxXP, currentLevel) + self.oldXP = oldXP + self.newXP = newXP + self.maxXP = maxXP + self.currentLevel = currentLevel + + -- Animation stuff + --[[self.barPercent = math.Clamp(self.newXP / self.maxXP, 0, 1) + self.barPercent = Lerp(8 * FrameTime(), self.startBarAnim, self.barPercent) + self.startBarAnim = self.barPercent + + print(self.barPercent)--]] +end + +function PANEL:SetViewingTime(time) + time = time or 5 + self.viewingTime = time + self.removeTime = CurTime() + time +end + +function PANEL:ResetViewingTime() + self:SetViewingTime(self.viewingTime) +end + +function PANEL:Think() + if self.viewingTime <= 0 then return end + + if self.removeTime and CurTime() > self.removeTime then + self:Remove() + end +end + +function PANEL:Paint() +end + +vgui.Register("Vrondakis.XPBar", PANEL, "DPanel") \ No newline at end of file diff --git a/lua/darkrp_modules/levels/sh_config.lua b/lua/darkrp_modules/levels/sh_config.lua index a6b9eb0..15d95c0 100644 --- a/lua/darkrp_modules/levels/sh_config.lua +++ b/lua/darkrp_modules/levels/sh_config.lua @@ -11,11 +11,15 @@ LevelSystemConfiguration.Language = "EN" -- (available: FR, EN, PL, RU, zh-CN) //Hud settings LevelSystemConfiguration.EnableBar = true -- Is the XP Bar enabled? +LevelSystemConfiguration.AlternativeBar = false -- Should we use the alternative xp bar? (New look to xp bar) +LevelSystemConfiguration.AlternativeBarBGColor = Color(40, 45, 56) -- What should the main colour of the alternative xp bar be? (This will not change the xp bar itself, go to LevelBarColor for that) LevelSystemConfiguration.BarText = true -- Is the bar text enabled? -LevelSystemConfiguration.XPTextColor = Color(255,255,255,255) -- The color of the XP percentage HUD element. -LevelSystemConfiguration.LevelBarColor = {6,116,255} -- The color of the XP bar. (Sorry this one is different. It is still {R,G,B}) +LevelSystemConfiguration.BarTextPercentage = true -- Should the bar text be a percentage or specific amount +LevelSystemConfiguration.XPTextColor = Color(210,210,210,255) -- The color of the XP percentage HUD element. +LevelSystemConfiguration.LevelBarColor = Color(41, 128, 185) -- The color of the XP bar LevelSystemConfiguration.XPBarYPos = 0 -- Y position of the XP bar -LevelSystemConfiguration.LevelText = true -- Enable the white text on left bottom? +LevelSystemConfiguration.AlternativeXPBarYPos = 0.85 -- Y position of the alternative XP bar +LevelSystemConfiguration.LevelText = false -- Enable the white text on left bottom? LevelSystemConfiguration.LevelColor = Color(255,255,255,255) -- The color of the "Level: 1" HUD element. White looks best. (This setting is nullified if you have the prestige system) LevelSystemConfiguration.LevelTextPos = {1.5, 180.0} -- The position of the LevelText. Y starts from bottom. Fiddle with it LevelSystemConfiguration.DisplayLevel = true -- Show player levels when you look at them @@ -25,20 +29,20 @@ LevelSystemConfiguration.GreenAllBars = true -- Are the green bars at the bottom //Kill settings LevelSystemConfiguration.KillModule = true -- Give XP + Money for kills! -- Next 2 settings control this. LevelSystemConfiguration.Friendly = true -- Only take away money / give XP if the killer is a lower level/same level than the victim. (Recommended:true) -LevelSystemConfiguration.TakeAwayMoneyAmount = 100 -- How much money to take away from players when they are killed and add to the killer. You can change this to 0 if none. The XP amount is dynamic. +LevelSystemConfiguration.TakeAwayMoneyAmount = 250 -- How much money to take away from players when they are killed and add to the killer. You can change this to 0 if none. The XP amount is dynamic. LevelSystemConfiguration.NPCXP = true -- Give XP when an NPC is killed? -LevelSystemConfiguration.NPCXPAmount = 10 -- Amount of XP to give when an NPC is killed +LevelSystemConfiguration.NPCXPAmount = 100 -- Amount of XP to give when an NPC is killed //Timer settings -LevelSystemConfiguration.TimerModule = true -- Give XP to everybody every howeverlong +LevelSystemConfiguration.TimerModule = false -- Give XP to everybody every howeverlong LevelSystemConfiguration.Timertime = 120 -- How much time (in seconds) until everybody gets given XP LevelSystemConfiguration.TimerXPAmount = 50 -- How much XP to give each time it goes off LevelSystemConfiguration.TimerXPAmountVip = 100 -- How much XP to give for vip players each time it goes off -LevelSystemConfiguration.TimerXPVipGroups = {"vip", "premium"} -- The vip groups +LevelSystemConfiguration.TimerXPVipGroups = {"vip"} -- The vip groups //XP settings LevelSystemConfiguration.XPMult = 1 -- How hard it is to level up. 2 would require twice as much XP, ect. -LevelSystemConfiguration.MaxLevel = 99 -- The max level +LevelSystemConfiguration.MaxLevel = 100 -- The max level LevelSystemConfiguration.ContinueXP = false -- If remaining XP continues over to next levels. I recommend this to be false. Seriously. What if a level 1 gets 99999999 XP somehow? He is level 99 so quickly. LevelSystemConfiguration.BoughtXP = true -- Does the player gain xp from buying something (shipment/entity) diff --git a/lua/darkrp_modules/levels/sv_hud.lua b/lua/darkrp_modules/levels/sv_hud.lua new file mode 100644 index 0000000..3a53197 --- /dev/null +++ b/lua/darkrp_modules/levels/sv_hud.lua @@ -0,0 +1,16 @@ +util.AddNetworkString("Vrondakis.ShowXPBar") + +hook.Add("PlayerXPChanged", "DisplayXPBarOnChange", function(ply, oldXP, newXP) + --[[local data = { + oldXP = oldXP, + newXP = newXP + } + + data = util.Compress(util.TableToJSON(data))--]] + + net.Start("Vrondakis.ShowXPBar") + net.WriteInt(oldXP, 20) + net.WriteInt(newXP, 20) + net.WriteInt(ply:getMaxXP(), 20) + net.Send(ply) +end) \ No newline at end of file diff --git a/lua/darkrp_modules/levels/sv_levels.lua b/lua/darkrp_modules/levels/sv_levels.lua index cf6989d..1936352 100644 --- a/lua/darkrp_modules/levels/sv_levels.lua +++ b/lua/darkrp_modules/levels/sv_levels.lua @@ -9,6 +9,9 @@ end function meta:setXP(xp) if not (xp or self:IsPlayer()) then return end + local oldXP = self:getXP() + + hook.Call("PlayerXPChanged", nil, self, oldXP, xp) return self:setDarkRPVar("xp", xp) end