Jump to content

كود ~


xMr.SAAD

Recommended Posts

client 1  
---------
 
--[[
    Todo:
        Make proper transitions (old setting -> new setting) when updating the snow settings
        Spring clean
        Expose more settings to users? (eg: rotate true/false, jitter speed, alpha blending)
--]]
 
 
local snowflakes = {}
local snowing = false
 
local box_width, box_depth, box_height, box_width_doubled, box_depth_doubled = 4,4,4,8,8
local position = {0,0,0}
local flake_removal = nil
local snow_fadein = 10
local snow_id = 1
 
sx,sy = guiGetScreenSize()
sx2,sy2 = sx/2,sy/2
localPlayer = getLocalPlayer()
 
 
 
--local random = math.random
function random(lower,upper)
    return lower+(math.random()*(upper-lower))
end
 
function startSnow()
    if not snowing then
        snowflakes = {}
           
        local lx,ly,lz = getWorldFromScreenPosition(0,0,1)
        local rx,ry,rz = getWorldFromScreenPosition(sx,0,1)
        box_width = getDistanceBetweenPoints3D(lx,ly,lz,rx,ry,rz)+3 -- +1.5 each side of the screen
        box_depth = box_width
       
        box_width_doubled = box_width*2
        box_depth_doubled = box_depth*2
 
        lx,ly,lz = getWorldFromScreenPosition(sx2,sy2,box_depth)
        position = {lx,ly,lz}      
       
        -- let it snow
        for i=1, settings.density do
            local x,y,z = random(0,box_width*2),random(0,box_depth*2),random(0,box_height*2)
            createFlake(x-box_width,y-box_depth,z-box_height,0)
        end
       
    --  outputChatBox(string.format("Width/Depth: %.1f",box_width))
        addEventHandler("onClientRender",root,drawSnow)
        snowing = true
    --  outputChatBox("Snow started")
        return true
    else
    --  outputChatBox("Its already snowing")
        return false
    end
    return false
end
 
function toggleSnow()
    if snowing then
        stopSnow()
    else
        startSnow()
    end
end
 
addEventHandler("onClientResourceStart",resourceRoot,toggleSnow)
addCommandHandler("snow",function()
    if snowToggle then
        toggleSnow()
    else
        outputChatBox("Snow toggling is disabled.")
    end
end)
 
 
function showSnowToggleBind()
    if getKeyState("lctrl") or getKeyState("rctrl") then
        if snowToggle then
            toggleSnow()
        else
            outputChatBox("Snow toggling is disabled.")
        end
    end
end
addCommandHandler("Snow Toggle (hold ctrl)",showSnowToggleBind)
bindKey("s","down","Snow Toggle (hold ctrl)")
 
 
function stopSnow()
    if snowing then
        removeEventHandler("onClientRender",root,drawSnow)
        for i,flake in pairs(snowflakes) do
            snowflakes[i] = nil
        end
        snowflakes = nil
        flake_removal = nil
        snowing = false
    --  outputChatBox("Snow stopped")
        return true
    end
    return false
end
addEventHandler("onClientResourceStop",resourceRoot,stopSnow)
 
 
function updateSnowType(type)
    if type then
        settings.type = type
    --  outputChatBox("Snow type set to "..type)
        return true
    end
    return false
end
 
 
function updateSnowDensity(dense,blend,speed)
    if dense and tonumber(dense) then
        dense = tonumber(dense)
        if snowing then
            if blend then
                -- if we are blending in more flakes
                if dense > settings.density then
                    -- default speed
                    if not tonumber(speed) then
                        speed = 300
                    end
                    -- create 1/20 of the new amount every 'speed'ms for 20 iterations
                    setTimer(function(old,new)
                        for i=1, (new-old)/20, 1 do
                            local x,y = random(0,box_width*2),random(0,box_depth*2)
                            createFlake(x-box_width,y-box_depth,box_height,0)                          
                        end
                    end,tonumber(speed),20,settings.density,dense)
               
                -- if we are blending out existing flakes, just flag that we should stop recreating them and check in createFlake()
                elseif dense < settings.density then
                    if not tonumber(speed) then
                        speed = 10
                    end
                    flake_removal = {settings.density-dense,0,tonumber(speed)}
                end
               
                if not tonumber(speed) then
                    speed = 0
                end
            else
                speed = 0
                if dense > settings.density then
                    for i=settings.density+1, dense do
                        local x,y = random(0,box_width*2),random(0,box_depth*2)
                        createFlake(x-box_width,y-box_depth,box_height,0)              
                    end
                elseif dense < settings.density then
                    for i=density, dense+1, -1 do
                        table.remove(snowflakes,i)
                    end
                end
            end
        else
            speed = 0
        end
       
    --  outputChatBox("Snow density set to "..dense.." (b: "..((blend ~= nil) and "yes" or "no").." - "..((dense > settings.density) and "in" or "out").." at "..tonumber(speed).."ms : "..settings.density..","..dense..")")
        settings.density = tonumber(dense)
        return true
    end
    return false
end
 
 
function updateSnowWindDirection(xdir,ydir)
    if xdir and tonumber(xdir) and ydir and tonumber(ydir) then
        settings.wind_direction = {tonumber(xdir)/100,tonumber(ydir)/100}
    --  outputChatBox("Snow winddirection set to "..xdir..","..ydir)
        return true
    end
    return false
end
 
 
function updateSnowWindSpeed(speed)
    if speed and tonumber(speed) then
        settings.wind_speed = tonumber(speed)
    --  outputChatBox("Snow windspeed set to "..settings.wind_speed)
        return true
    end
    return false
end
 
 
function updateSnowflakeSize(min,max)
    if min and tonumber(min) and max and tonumber(max) then
        settings.snowflake_min_size = tonumber(min)
        settings.snowflake_max_size = tonumber(max)
    --  outputChatBox("Snowflake size set to "..min.." - "..max)
        return true
    end
    return false
end
 
 
function updateSnowFallSpeed(min,max)
    if min and tonumber(min) and max and tonumber(max) then
        settings.fall_speed_min = tonumber(min)
        settings.fall_speed_max = tonumber(max)
        return true
    end
    return false
end
 
 
function updateSnowAlphaFadeIn(alpha)
    if alpha and tonumber(alpha) then
        snow_fadein = tonumber(alpha)
    --  outputChatBox("Snow fade in alpha set to "..alpha)
        return true
    end
    return false
end
 
 
function updateSnowJitter(jit)
    settings.jitter = jit
end
 
 
function createFlake(x,y,z,alpha,i) 
    if flake_removal then
        if (flake_removal[2] % flake_removal[3]) == 0 then
            flake_removal[1] = flake_removal[1] - 1
            if flake_removal[1] == 0 then
                flake_removal = nil
            end
            table.remove(snowflakes,i)
            return
        else
            flake_removal[2] = flake_removal[2] + 1
        end
    end
   
    snow_id = (snow_id % 4) + 1
   
    if i then
       
Edited by Guest
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...