• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Creating trees with slowed birth animation

Status
Not open for further replies.
Level 10
Joined
Jun 6, 2007
Messages
392
I'm making a simple library for creating growing trees. create tree function takes the growth duration as a parameter. The problem is, that when I use long growth times (e.g. 10 seconds), the animation "jumps" to the end. Why, and how could it be fixed? Here's my code:
JASS:
library TreeLib

    private struct Data
        destructable tree
        integer executions
        boolean invul = false
    endstruct

    globals
        constant integer TREE_ASHENVALE = 'ATtr'
        
        private constant real TIMEOUT = 0.04
        private constant real ANIM_SPEED = 3
        
        private timer T = CreateTimer()
        private integer Count = 0
        private Data array TreeList
    endglobals
    
    private function periodic takes nothing returns nothing
        local Data dat
        local integer i = 0
        loop
            exitwhen i >= Count
            
            set dat = TreeList[i]
            set dat.executions = dat.executions - 1
            if dat.executions == 0 then
                call SetDestructableAnimation(dat.tree, "stand")
                if dat.invul then
                    call SetDestructableInvulnerable(dat.tree, false)
                endif
                call dat.destroy()
                set Count = Count - 1
                set TreeList[i] = TreeList[Count]
            endif
            if Count == 0 then
                call PauseTimer(T)
            endif
            set i = i + 1
        endloop
    endfunction
    
    function createTree takes integer treeType, real x, real y, real angle, real growthDuration, boolean invWhenGrowing returns nothing
        local Data dat = Data.create()
        set dat.tree = CreateDestructable(treeType, x, y, angle, 1, 1)
        call SetDestructableAnimation(dat.tree, "birth")
        call SetDestructableAnimationSpeed(dat.tree, ANIM_SPEED/growthDuration)
        if invWhenGrowing then
            call SetDestructableInvulnerable(dat.tree, true)
            set dat.invul = true
        endif
        
        set dat.executions = R2I(growthDuration/TIMEOUT)
        set TreeList[Count] = dat
        set Count = Count + 1
        if Count == 1 then
            call TimerStart(T, TIMEOUT, true, function periodic)
        endif
    endfunction

endlibrary
And a simple test trigger:
[/
JASS:
function Trig_Untitled_Trigger_001_Actions takes nothing returns nothing
    local real x = GetCameraTargetPositionX() 
    local real y = GetCameraTargetPositionY()
    
    call createTree(TREE_ASHENVALE, x, y, 0, 7.0, true)
endfunction

//===========================================================================
function InitTrig_Untitled_Trigger_001 takes nothing returns nothing
    set gg_trg_Untitled_Trigger_001 = CreateTrigger(  )
    call TriggerRegisterPlayerChatEvent( gg_trg_Untitled_Trigger_001, Player(0), "tree", true )
    call TriggerAddAction( gg_trg_Untitled_Trigger_001, function Trig_Untitled_Trigger_001_Actions )
endfunction
 
It's animation, yo, not really your fault. Just give a new stand animation in queue to refresh the duration in periodic event:

JASS:
if dat.executions == 0 then
    call SetDestructableAnimation(dat.tree, "stand")
    if dat.invul then
        call SetDestructableInvulnerable(dat.tree, false)
    endif
    call dat.destroy()
    set Count = Count - 1
    set TreeList[i] = TreeList[Count]
endif
->
JASS:
if dat.executions == 0 then
    call SetDestructableAnimation(dat.tree, "stand")
    if dat.invul then
        call SetDestructableInvulnerable(dat.tree, false)
    endif
    call dat.destroy()
    set Count = Count - 1
    set TreeList[i] = TreeList[Count]
else
    call QueueDestructableAnimation(dat.tree, "stand")
endif

Moved to Triggers&Scripts.
 
Status
Not open for further replies.
Top