Wc3C.net

Wc3C.net (http://www.wc3c.net/forums.php)
-   Scripts (http://www.wc3c.net/forumdisplay.php?f=737)
-   -   Unit Fade System (http://www.wc3c.net/showthread.php?t=102012)

Anitarf 08-12-2008 10:09 PM

Unit Fade System
 
This system requires ARGB and Table. With it, you can fade units over time by calling a single function, thanks to ARGB you don't need to pass a million parameters to it.

Collapse JASS:
library UnitFadeSystem initializer Init requires ARGB, Table

//*****************************************************************
//*  UNIT FADE SYSTEM
//*
//*  written by: Anitarf
//*  requires: -ARGB
//*            -Table
//*
//*  A system that makes gradual modification of unit's vertex
//*  colouring, also called fading, an easy affair. The functions
//*  you can use are:
//*
//*  FadeUnitStart - Fades a unit from the start colour to the end
//*                  colour over a given duration.
//*  FadeUnitStop  - If a unit is already fading, then it stops
//*                  and the function returns it's current color;
//*                  else, the function returns 0.
//*  IsUnitFading  - Returns true if the unit is already fading.
//*****************************************************************

    private keyword fadingUnit

    globals
        //this value determines how much time elapses between unit colour updates
        private constant real FADE_PERIOD = 0.04


// END OF CALIBRATION SECTION    
// ================================================================

        private fadingUnit array fuList
        private integer fuListMax = 0
        private HandleTable cache //initialized in Init
    endglobals

    private struct fadingUnit
        integer list
        unit u
        ARGB start = 0
        ARGB end = 0
        real time = 0.0
        real endtime = FADE_PERIOD

        static method check takes unit u returns boolean
            return cache.exists(u)
        endmethod
        static method get takes unit u returns fadingUnit
            local fadingUnit fu = fadingUnit(cache[u])
            if fu==0 then
                set fu = fadingUnit.create()
                set cache[u]=integer(fu)
                set fu.u = u
                set fuList[fuListMax]=fu
                set fu.list = fuListMax
                set fuListMax=fuListMax+1
            endif
            return fu
        endmethod
        method onDestroy takes nothing returns nothing
            call cache.flush(.u)
            set this.u = null
            set fuListMax=fuListMax-1
            set fuList[this.list]=fuList[fuListMax]
            set fuList[this.list].list=this.list
        endmethod
    endstruct

    private function FadeUnits takes nothing returns nothing
        local integer i = 0
        local fadingUnit fu
        loop
            exitwhen i>=fuListMax
            set fu=fuList[i]
            set fu.time=fu.time+FADE_PERIOD
            if fu.time>=fu.endtime then
                set fu.time=fu.endtime
                call fu.destroy()
            else
                set i = i+1
            endif
            call ARGB.mix(fu.start, fu.end, fu.time/fu.endtime).recolorUnit(fu.u)
        endloop
    endfunction

    private function Init takes nothing returns nothing
        local timer t = CreateTimer()
        call TimerStart(t, FADE_PERIOD, true, function FadeUnits)
        set cache = HandleTable.create()
    endfunction

// ================================================================

    function FadeUnitStart takes unit u, ARGB start, ARGB end, real duration returns nothing
        local fadingUnit fu = fadingUnit.get(u)
        if duration < FADE_PERIOD then
            set duration = FADE_PERIOD
        endif
        set fu.start = start
        set fu.end = end
        set fu.time = 0.0
        set fu.endtime = duration
    endfunction

    function FadeUnitStop takes unit u returns ARGB
        local fadingUnit fu = fadingUnit.get(u)
        call fu.destroy()
        return ARGB.mix(fu.start, fu.end, fu.time/fu.endtime)
    endfunction

    function IsUnitFading takes unit u returns boolean
        return fadingUnit.check(u)
    endfunction

endlibrary

Rising_Dusk 08-12-2008 10:17 PM

From what I can tell, this looks good, solid, and useful. I particularly like that you're building off of other in-database scripts. If you've tested it to make sure it works (It seems like it should be okay, but I might be overlooking something) then I will move it on its way.

Toadcop 08-14-2008 02:34 PM

Quote:

local trigger t = CreateTrigger()
call TriggerRegisterTimerEventPeriodic(t, FADE_PERIOD)
call TriggerAddAction(t, function FadeUnits)
does it make sense ?

Quote:

thanks to ARGB you don't need to pass a million parameters to it.
1 vs 4 ? ...

Anitarf 08-14-2008 02:50 PM

Quote:

Originally Posted by Toadcop
does it make sense ?

Fixed.
Quote:

1 vs 4 ? ...
No, 2 vs 8.

Vexorian 08-15-2008 04:26 PM

It is rather nice. Tested and seems to work.

kentchow75 02-02-2009 10:11 AM

Sorry but I'm too noob in Jass.
I was searching all over the planet for a fade system and I found here.

The problem is,
"FadeUnitStart takes unit u, ARGB start, ARGB end, real duration returns nothing"

What perimeter should I put in the "start" and "end" ?

akolyt0r 02-02-2009 11:14 AM

something like:
Quote:

0xFFFF0000
0xFFFF5B3D



All times are GMT. The time now is 12:56 AM.

Powered by vBulletin (Copyright ©2000 - 2019, Jelsoft Enterprises Ltd).
Hosted by www.OICcam.com
IT Support and Services provided by Executive IT Services