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

[JASS] Move Unit Backwards

Status
Not open for further replies.
Level 24
Joined
Feb 28, 2007
Messages
3,480
Hey all, I have what I believe is a relatively simple question to ask.
Basically what I want is that when a unit enters a certain region, he is automatically moved backwards a certain range. Like I said, backwards, meaning that I'd prefer it if the guy was facing upwards when entering the region, he'd also face upwards when going backwards. I'd prefer a vJass answer, thanks in advance.
 
Level 30
Joined
Dec 6, 2007
Messages
2,228
JASS:
scope Sample initializer GetStarted
    globals
        private constant rect TriggerRect = gg_rct_Enter
        private constant string Effect = "Abilities\\Weapons\\AncientProtectorMissile\\AncientProtectorMissile.mdl"
        private constant real Offset = 300
        private constant real Speed = 5
    endglobals
//*****************End of the Settings*********************
    globals
        private integer i = 0
        private real x
        private real y
        private region TriggerRegion = CreateRegion()
    endglobals

    //!***********************************
    // Polar Projection Function
    // call PolarProjection (x, y, distance, angle)

    private function PolarProjection takes real rx, real ry, real dist, real angle returns nothing
        set x = rx + dist * Cos(angle * bj_DEGTORAD)
        set y = ry + dist * Sin(angle * bj_DEGTORAD)
    endfunction
    
    //!***********************************
    // Get Angle Function
    // call GetAngle (x1,y1,x2,y2)
    
    private function GetAngle takes real x1, real y1, real x2, real y2 returns real
        return bj_RADTODEG*Atan2(y2-y1,x2-x1)
    endfunction
    
    //!***********************************
    // Main Part
    
    private struct Spell
        private unit      u
        private real      ReachedDistance
        
        private static Spell array indx
        private static integer     counter = 0
        private static timer       time    = CreateTimer()      
            
            static method Execution takes nothing returns nothing
                local Spell d
                set i = 0
                loop
                    exitwhen i >= Spell.counter
                    set d = Spell.indx[i]                    
                    if d.ReachedDistance < Offset then
                        call PolarProjection(GetUnitX(d.u),GetUnitY(d.u),Speed,GetAngle(GetRectCenterX(TriggerRect),GetRectCenterY(TriggerRect),GetUnitX(d.u),GetUnitY(d.u)))
                        call SetUnitX(d.u,x)
                        call SetUnitY(d.u,y)
                        call DestroyEffect(AddSpecialEffect(Effect,x,y))
                        set d.ReachedDistance = d.ReachedDistance + Speed
                    else                 
                        set d.u = null
                        call d.destroy()                        
                        set Spell.counter = Spell.counter - 1
                        set Spell.indx[i] = d.indx[Spell.counter]                        
                        set i = i - 1    
                    endif
                    set i = i + 1
                endloop
                set i = 0
                if Spell.counter == 0 then
                    call PauseTimer(Spell.time)
                endif
            endmethod
            
            static method GetValues takes unit u returns nothing
                local Spell d = Spell.allocate()
                set d.u = u
                set d.ReachedDistance = 0
                if Spell.counter == 0 then
                    call TimerStart(Spell.time,0.01,true, function Spell.Execution)
                endif
                set Spell.indx[Spell.counter] = d
                set Spell.counter = Spell.counter + 1
            endmethod
    endstruct

    private function Input takes nothing returns nothing
        call Spell.GetValues(GetTriggerUnit())
    endfunction

    private function GetStarted takes nothing returns nothing
        local trigger Trig = CreateTrigger()
        call RegionAddRect(TriggerRegion,TriggerRect)
        call TriggerRegisterEnterRegion(Trig,TriggerRegion,null)
        call TriggerAddAction(Trig, function Input)
        call Preload(Effect)
        call PreloadStart()
    endfunction
endscope
 
Status
Not open for further replies.
Top