• 🏆 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!

[Snippet] SetUnitLookAtAngle

Kazeon

Hosted Project: EC
Level 34
Joined
Oct 12, 2011
Messages
3,449
JASS:
library SetUnitLook /* v1.0
    
    A library to permanently keep a unit body part facing certain angle. Normally
    you can only set a unit body part facing to another unit. Then I found that
    sometimes we need to keep it facing to certain angle, and that's what this
    library do.
    
    
    API
    
    LookAtAngle.Set (unit whichUnit, string whichBone, real angle, real zOffset) returns nothing
    
    LookAtAngle.Release (unit whichUnit) returns nothing
    

*/
    globals
    
        // Dummy id on Object Editor
        private constant integer DUMMY_ID = 'h000'
        
        // Accuracy
        private constant real ACCURACY = .03
        
        // Furthermore, edit them under your own risk
        private integer Total = -1
        private integer array This
        private player Passive = Player(PLAYER_NEUTRAL_PASSIVE)
        private timer Timer = CreateTimer()
        
    endglobals
    
    struct LookAtAngle
        
        private real Angle
        private real X
        private real Y
        
        private unit Unit
        private unit Dummy
        
        static method Release takes unit whichUnit returns nothing
        
            local thistype this
            local integer i = 0
            
            loop
                exitwhen i > Total
                set this = This[i]
                if whichUnit == .Unit then
                    call RemoveUnit(.Dummy)
                    call ResetUnitLookAt(.Unit)
                    call .destroy()
                    set This[i] = This[Total]
                    set Total = Total - 1
                    if Total < 0 then
                        call PauseTimer(Timer)
                    endif
                    return
                endif
                set i = i + 1
            endloop
            
        endmethod
        
        private static method onPeriodic takes nothing returns nothing
        
            local thistype this
            local integer i = 0
            
            loop
                exitwhen i > Total
                set this = This[i]
                if IsUnitType(.Unit, UNIT_TYPE_DEAD) then
                    call Release(.Unit)
                elseif GetUnitX(.Unit) != .X or GetUnitY(.Unit) != .Y then
                    set .X = GetUnitX(.Unit)
                    set .Y = GetUnitY(.Unit)
                    call SetUnitX(.Dummy, .X + 50. * Cos(.Angle))
                    call SetUnitY(.Dummy, .Y + 50. * Sin(.Angle))
                endif
                set i = i + 1
            endloop
            
        endmethod
        
        static method Set takes unit whichUnit, string whichBone, real angle, real zOffset returns nothing
            
            local thistype this
            local integer i = 0
            
            loop
                exitwhen i > Total
                set this = This[i]
                if whichUnit == .Unit then
                    set .Angle = angle * bj_DEGTORAD
                    call ResetUnitLookAt(whichUnit)
                    call SetUnitLookAt(whichUnit, whichBone, .Dummy, 0., 0., zOffset)
                    return
                endif
                set i = i + 1
            endloop
            set this = allocate()
            set Total = Total + 1
            set This[Total] = this
            set .Unit = whichUnit
            set .Angle = angle * bj_DEGTORAD
            set .X = 0.
            set .Y = 0.
            set .Dummy = CreateUnit(Passive, DUMMY_ID, 0., 0., 0.)
            call UnitRemoveAbility(.Dummy, 'Amov')
            call SetUnitLookAt(whichUnit, whichBone, .Dummy, 0., 0., zOffset)
            if Total == 0 then
                call TimerStart(Timer, ACCURACY, true, function LookAtAngle.onPeriodic)
            endif
        endmethod
        
    endstruct

endlibrary
 
Top