// ======= Copyright © 2003-2011, Unknown Worlds Entertainment, Inc. All rights reserved. ======= // // lua\GUIPlayerNames.lua // // Created by: Charlie Cleveland (charlie@unknownworlds.com) // // Draw names of players above their heads for commanders. // // ========= For more information, visit us at http://www.unknownworlds.com ===================== class 'GUIPlayerNames' (GUIScript) GUIPlayerNames.kMaxNames = kMaxPlayers/2 GUIPlayerNames.kMarineTextColor = Color(.30, .69, 1, .75) GUIPlayerNames.kAlienTextColor = Color(1, .79, .227, .75) GUIPlayerNames.kFontSize = 16 // Colors to interpolate between starting from no health to full health. kHealthColors = { Color(0.8, 0, 0), Color(0.5, 0, 0.5), Color(0, 0, 0.7) } kNumberHealthColors = table.maxn(kHealthColors) function GUIPlayerNames:Initialize() self.playerNameItemList = {} self.playerIdList = {} for i = 1, GUIPlayerNames.kMaxNames do local playerNameItem = GUIManager:CreateTextItem() playerNameItem:SetFontSize(GUIPlayerNames.kFontSize) playerNameItem:SetFontIsBold(true) playerNameItem:SetTextAlignmentX(GUIItem.Align_Center) playerNameItem:SetTextAlignmentY(GUIItem.Align_Min) playerNameItem:SetIsVisible(false) playerNameItem:SetColor(color) table.insert(self.playerNameItemList, playerNameItem) end end function GUIPlayerNames:Uninitialize() for i, playerNameItem in ipairs(self.playerNameItemList) do GUI.DestroyItem(playerNameItem) end self.playerNameItemList = nil self.playerIdList = nil end function GUIPlayerNames:Update(deltaTime) local player = Client.GetLocalPlayer() if not player then return end // Every so often, update player id list and names of players if (self.timeOfLastNameIdUpdate == nil) or (Shared.GetTime() > self.timeOfLastNameIdUpdate + .75) then self.playerIdList = {} local players = GetEntitiesForTeam("Player", player:GetTeamNumber()) for index, currentPlayer in ipairs(players) do if currentPlayer:GetIsAlive() and not currentPlayer:isa("Commander") then table.insert(self.playerIdList, currentPlayer:GetId()) end end self.timeOfLastNameIdUpdate = Shared.GetTime() end local numVis = 0 // Every tick, update position of player name to be where player is for index, playerId in ipairs(self.playerIdList) do // Offset below player a tad local player = Shared.GetEntity(playerId) if player ~= nil then local position = Client.WorldToScreen(player:GetOrigin() - Vector(.5, 0, 0)) //START - Hackepeter local nameBuffer = player:GetName() //Add health and armor under the players name local hp = math.ceil(player:GetHealth() / player:GetMaxHealth() * 100) local ap = math.ceil(player:GetArmor() / player:GetMaxArmor() * 100) local color = ColorArrayToInt(GetHealthColor(hp / 100)) //only show it if the player is hurt and set the text color if hp < 100 or ap < 100 then nameBuffer = string.format("%s\n(%d%%/%d%%)", nameBuffer, hp, ap) end //Add weapon information local weapon = player:GetActiveWeapon() if weapon ~= nil then //add name for current weapon in a new line if it takes ammo if weapon:isa("ClipWeapon") then //check type and generate short name local weaponName local weaponIsGL = false if weapon:isa("GrenadeLauncher") then weaponName = "GL" weaponIsGL = true elseif weapon:isa("Rifle") then weaponName = "R" elseif weapon:isa("Pistol") then weaponName = "P" elseif weapon:isa("Shotgun") then weaponName = "SG" elseif weapon:isa("Flamethrower") then weaponName = "FT" end //generate weapon info string nameBuffer = string.format("%s\n%s: %d/%d", nameBuffer, weaponName, weapon:GetClip(), weapon:GetAmmo()) //add grenade info if it is a GL if weaponIsGL then nameBuffer = string.format("%s-%d", nameBuffer, weapon:GetAuxClip()+1) end end end //apply to player self.playerNameItemList[index]:SetText(nameBuffer) //change color if parasited to yellow local isParasited = player:GetGameEffectMask(kGameEffect.Parasite) color = ConditionalValue(isParasited, GUIPlayerNames.kAlienTextColor, color) self.playerNameItemList[index]:SetColor(color) //END - Hackepeter //self.playerNameItemList[index]:SetText(ToString(player:GetName())) self.playerNameItemList[index]:SetPosition(Vector(position.x, position.y, 0)) self.playerNameItemList[index]:SetIsVisible(true) numVis = numVis + 1 else self.playerNameItemList[index]:SetIsVisible(false) end end // Set the rest invisible for index = table.count(self.playerIdList) + 1, GUIPlayerNames.kMaxNames do self.playerNameItemList[index]:SetIsVisible(false) end end