Jump to content

problem with spectator


Kenix

Recommended Posts

When a player dies and goes to spectator can't spectate to another player .For example 2 players alive ( players in spectator can spectate players ( who get "Status" == "Playing" ) ), but sometimes when you switch the button( on keyboard ) "arrows" does not work to switch to another player but he is alive and have is in the table alivePlayers .

Help guys.

Server:

alivePlayers = {}  
spec = {}  
local g_CurrentSpectated = 1 
  
function spec.already( thePlayer ) 
    local bool = false 
    for k,i in ipairs ( alivePlayers ) do 
        if ( i == thePlayer ) then 
            bool = true 
        end 
    end 
    return bool 
end 
  
function sizeof( t )  
    local s = 0 
    for _ in pairs( t ) do 
        s = s + 1 
    end 
    return s 
end 
  
addEvent("spec.some",true)  
function spec.some(  ) 
    if spec.get( ) >= 1 then 
       triggerEvent("spec.next",source) 
    end  
end 
addEventHandler("spec.some",root,spec.some)  
  
addEvent("spec.get",true) 
function spec.get() 
    return sizeof(alivePlayers)  
end 
addEventHandler("spec.get",root,spec.get) 
  
  
addEvent("spec.add",true) 
function spec.add(  ) 
    if not ( spec.already( source ) ) then  
        table.insert(alivePlayers, source) 
        return true  
    end 
    return false 
end 
addEventHandler("spec.add",root,spec.add) 
  
addEvent("spec.remove",true) 
function spec.remove(  ) 
    local bool = false 
    for i, val in ipairs(alivePlayers) do 
        if (val == source) then 
            table.remove(alivePlayers, i) 
            bool = true 
        end 
    end 
    return bool 
end 
addEventHandler("spec.remove", root,spec.remove) 
  
  
addEvent("spec.next",true) 
function spec.next( ) 
    if g_CurrentSpectated >= sizeof(alivePlayers) then 
        g_CurrentSpectated = 1 
    else 
        g_CurrentSpectated = g_CurrentSpectated + 1 
    end 
    setCameraTarget(source,alivePlayers[g_CurrentSpectated]) 
end 
addEventHandler("spec.next",root,spec.next) 
  
addEvent("spec.prev",true) 
function spec.prev( ) 
    if g_CurrentSpectated <= 1 then 
        g_CurrentSpectated = sizeof(alivePlayers) 
    else 
        g_CurrentSpectated = g_CurrentSpectated - 1 
    end 
    setCameraTarget(source,alivePlayers[g_CurrentSpectated]) 
end 
addEventHandler("spec.prev",root,spec.prev) 
  
addEventHandler("onPlayerWasted",root, 
    function() 
        if source then 
            triggerEvent("spec.remove",source) 
        end 
    end 
) 
  
addEventHandler("onPlayerQuit",root,spec.remove) 
  
setTimer(  
 function() 
  for k,i in ipairs ( getElementsByType( "player" ) ) do 
   if ( getElementData( i, "Status" ) == "Spectator" ) then 
    local target = getCameraTarget( i ) 
    if ( getElementType( target ) == "player" ) then 
     setElementData( i, "Specting", target ) 
    else 
     setElementData( i, "Specting", false ) 
    end 
   else 
    setElementData( i, "Specting", false ) 
   end 
  end 
 end 
, 1000, 0) 

Utils server:

function callServerfunction(funcname, ...) 
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do arg[key] = tonumber(value) or value end 
    end 
    loadstring("return "..funcname)()(unpack(arg)) 
end 
addEvent("onClientCallsServerFunction", true) 
addEventHandler("onClientCallsServerFunction", resourceRoot , callServerfunction) 
  
function callClientfunction(client, funcname, ...) 
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do 
            if (type(value) == "number") then arg[key] = tostring(value) end 
        end 
    end 
    triggerClientEvent(client, "onServerCallsClientFunction", resourceRoot, funcname, unpack(arg or {})) 
end  
  

Client:

local spec = {  }  
local Spectator_text = { Rus = {},Eng = {}, Pl = {},Sp = {} }  
  
Spectator_text.Rus.text = "Следят за тобой:\n" 
Spectator_text.Eng.text = "People spectating you:\n" 
Spectator_text.Pl.text = "Ludzie widzem ci:\n" 
Spectator_text.Sp.text = "La gente te espectador:\n" 
  
Spectator_text.Rus.more = " И больше ..." 
Spectator_text.Eng.more = " more ..." 
Spectator_text.Pl.more = " wiecej ..." 
Spectator_text.Sp.more = " mas ..." 
  
local spectate = {}  
local me = getLocalPlayer() 
local Spector_image = {} 
local g_CurrentSpectated = 0 
local x, y = guiGetScreenSize () 
  
local spectatorSettings = { 
    count = 7,  
    charLimit = 19,  
    xOffset = 200,  
    yOffset = (y / 2) - 40,  
    alwaysShow = true  
} 
  
addEvent( "spec.show",true) 
function spec.show( bool ) 
    callServerfunction("setElementData" , source,"Status","Spectator") 
    callServerfunction("setElementData" , source,"showHud","n") 
    bindKey("arrow_l", "down", spectate.prev) 
    bindKey("arrow_r", "down", spectate.next) 
     
    triggerServerEvent("spec.some",me) 
end 
addEventHandler("spec.show",root,spec.show) 
  
  
addEvent( "spec.hide",true) 
function spec.hide(  ) 
    if getElementData(me,"Status") == "Joined" then return end 
    callServerfunction("setElementData" , me, "Status","Playing") 
    callServerfunction("setElementData" , me, "show","none") 
    unbindKey("arrow_l", "down", spectate.prev) 
    unbindKey("arrow_r", "down", spectate.next) 
end 
addEventHandler("spec.hide",root,spec.hide) 
  
addEventHandler("onClientRender",root, 
    function() 
        if getElementData(me,"Status") == "Spectator" then  
            dxDrawRelativeImage(416.0,762.0,134.0,121.0,"img/prv2.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
            dxDrawRelativeImage(722.0,762.0,134.0,121.0,"img/next2.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
            if getElementData(me,"show") == "image1" then 
                dxDrawRelativeImage(416.0,762.0,134.0,121.0,"img/prv.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
            end 
            if getElementData(me,"show") == "image2" then 
                dxDrawRelativeImage(722.0,762.0,134.0,121.0,"img/next.png",0.0,0.0,0.0,tocolor(255,255,255,255),false) 
            end 
        end  
    end 
) 
  
addEventHandler("onClientPlayerWasted",me, 
    function( killer, weapon, bodypart ) 
        if source then 
            triggerEvent("spec.show",source) 
        end 
    end 
) 
  
function spec.check ( elem ) 
    if elem then 
        if isElement (elem) then 
            if (getElementType (elem) == 'player') then 
                return true 
            end 
        end 
    end 
    return false 
end 
  
function spec.getSpecMe(  ) 
    local tableSpec = {} 
    for i,v in ipairs(getElementsByType("player")) do 
        if getElementData( v ,"Status" ) == "Spectator" then 
            table.insert( tableSpec, v ) 
        else 
            table.remove( tableSpec, i ) 
        end 
    end 
    return tableSpec 
end 
  
function spectate.prev() 
    if getElementData(me,"Status") ~= "Spectator" then return end 
    setElementData(me,"show","image1") 
    setTimer(setElementData,200,1,me,"show","none")  
     
    triggerServerEvent("spec.prev",me) 
end 
  
function spectate.next() 
    if getElementData(me,"Status") ~= "Spectator" then return end 
    setElementData(me,"show","image2") 
    setTimer(setElementData,200,1,me,"show","none") 
     
    triggerServerEvent("spec.next",me) 
end 
  
function updatePlayerSpec() 
    if spec.spectating( ) then 
        return 
    else     
        triggerEvent("hideSpector",me) 
    end 
end 
setTimer(updatePlayerSpec,50,0) 
  
function spec.showSpectators(  ) 
    local spectators = spec.getSpecMe( )  
    local textX = x - spectatorSettings.xOffset 
    local textY = spectatorSettings.yOffset 
     
    if (not isPlayerDead(me)) and getElementData( me,"Status") == "Playing" then 
        local s_Spectators = Spectator_text[ getElementData( me,"Lang" ) ].text   
        if (#spectators > 0) then 
            for i, v in ipairs(spectators) do 
                if spec.check (v) then 
                    local target = getElementData( v, "Specting" ) 
                    if ( target == getLocalPlayer() ) then 
                        local name = string.gsub( getPlayerName(v), "#%x%x%x%x%x%x", "" )  
                        if (string.len(name) > spectatorSettings.charLimit) then 
                            name = string.sub(name, 0, spectatorSettings.charLimit)..'..' 
                        end 
                        if (i > spectatorSettings.count) then 
                            s_Spectators = s_Spectators..tostring(#spectators - spectatorSettings.count)..''..Spectator_text[ getElementData( me,"Lang" ) ].more 
                            break 
                        else     
                            s_Spectators = s_Spectators..name..'\n' 
                        end 
                    else 
                        s_Spectators = '' 
                    end 
                end  
            end 
        else 
            s_Spectators = ''        
        end 
        dxDrawRelativeText (s_Spectators, textX, textY, x, y, tocolor(255, 255, 255, 255), 1.3, 'arial') 
    end 
end 
addEventHandler('onClientRender', root, spec.showSpectators) 

Utils client:

function dxDrawRelativeText(text,posX,posY,right,bottom,color,scale,font,alignX,alignY,clip,wordBreak,postGUI) 
    local resolutionX = 1280  
    local resolutionY = 1024  
    local sWidth,sHeight = guiGetScreenSize() 
    dxDrawText(tostring(text),(posX/resolutionX)*sWidth,(posY/resolutionY)*sHeight,(right/resolutionX)*sWidth,(bottom/resolutionY)*sHeight,color,(sWidth/resolutionX)*scale,font,alignX,alignY,clip,wordBreak,postGUI) 
end 
  
  
function dxDrawRelativeImage( posX, posY, width, height, path,rotation,rotationCenterOffsetX,rotationCenterOffsetY,color,postGUI) 
    local resolutionX = 1280  
    local resolutionY = 1024  
    local sWidth,sHeight = guiGetScreenSize() 
    dxDrawImage ( (posX/resolutionX)*sWidth, (posY/resolutionY)*sHeight, (width/resolutionX)*sWidth, (height/resolutionY)*sHeight, tostring(path) , rotation, rotationCenterOffsetX, rotationCenterOffsetY, color, postGUI ) 
end 
  
function callServerFunction(funcname, ...) 
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do 
            if (type(value) == "number") then arg[key] = tostring(value) end 
        end 
    end 
    triggerServerEvent("onClientCallsServerFunction", resourceRoot , funcname, unpack(arg)) 
end 
  
function callClientFunction(funcname, ...) 
    local arg = { ... } 
    if (arg[1]) then 
        for key, value in next, arg do arg[key] = tonumber(value) or value end 
    end 
    loadstring("return "..funcname)()(unpack(arg)) 
end 
addEvent("onServerCallsClientFunction", true) 
addEventHandler("onServerCallsClientFunction", resourceRoot, callClientFunction) 
  

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...