1. Head to the 33rd Modeling Contest Poll and drink to your heart's desire.
    Dismiss Notice
  2. Choose your means of doom in the 17th Mini Mapping Contest Poll.
    Dismiss Notice
  3. A slave to two rhythms, the 22nd Terraining Contest is here.
    Dismiss Notice
  4. The heavens smile on the old faithful. The 16th Techtree Contest has begun.
    Dismiss Notice
  5. The die is cast - the 6th Melee Mapping Contest results have been announced. Onward to the Hive Cup!
    Dismiss Notice
  6. The glory of the 20th Icon Contest is yours for the taking!
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Jass]Keyboard WSA&D/Movement W&S

Discussion in 'The Lab' started by JC Helas, Jan 19, 2020.

  1. JC Helas

    JC Helas

    Joined:
    Oct 19, 2014
    Messages:
    126
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Code (vJASS):

    //*********************************************************************************************
    //*
    //*  Keyboard WSA&D v1.0.1
    //*
    //*    You can now aware if a player has pressed and released the
    //*    key W,S,A and D in a very fast way.
    //*
    //*  API
    //*
    //*    WSAD_W[Player Id] == if true the player is pressing the key W while releasing if false.
    //*    WSAD_S[Player Id] == if true the player is pressing the key S while releasing if false.
    //*    WSAD_A[Player Id] == if true the player is pressing the key A while releasing if false.
    //*    WSAD_D[Player Id] == if true the player is pressing the key D while releasing if false.
    //*
    //*    Take note that in jass format player id starts in 0==Player 1.
    //*
    //*********************************************************************************************
    library WSAD initializer Init
        //! textmacro CreateFunc takes os
        globals
            public boolean array $os$
        endglobals
        private function $os$up takes nothing returns boolean
            set $os$[GetPlayerId(GetTriggerPlayer())]=false
            return false
        endfunction
        private function $os$down takes nothing returns boolean
            set $os$[GetPlayerId(GetTriggerPlayer())]=true
            return false
        endfunction
        //! endtextmacro
       
        //! runtextmacro CreateFunc("W")
        //! runtextmacro CreateFunc("S")
        //! runtextmacro CreateFunc("A")
        //! runtextmacro CreateFunc("D")
       
        //! textmacro RegisterKey takes os
            set i=0
            set up=CreateTrigger()
            set down=CreateTrigger()
            loop
                exitwhen i>23
                call BlzTriggerRegisterPlayerKeyEvent(up,Player(i),OSKEY_$os$,0,false)
                call BlzTriggerRegisterPlayerKeyEvent(down,Player(i),OSKEY_$os$,0,true)
                set $os$[i]=false
                set i=i+1
            endloop
            call TriggerAddCondition(up,Filter(function $os$up))
            call TriggerAddCondition(down,Filter(function $os$down))
        //! endtextmacro
        private function Init takes nothing returns nothing
            local integer i
            local trigger up
            local trigger down
            //! runtextmacro RegisterKey("W")
            //! runtextmacro RegisterKey("S")
            //! runtextmacro RegisterKey("A")
            //! runtextmacro RegisterKey("D")
        endfunction
    endlibrary
     

    Code (vJASS):

    //*********************************************************************************************
    //*
    //*  Movement W&S 1.0.1
    //*    
    //*    System allows player to move forward and backward  a unit
    //*    by the key of W/forward and S/backward base on player's
    //*    mouse position.
    //*
    //*  API
    //*
    //*    call MovementsWSAD_AddPlayerUnit(unit,player,integer animation)
    //*
    //*********************************************************************************************
    library MovementsWSAD initializer Init uses WSAD
        public struct data extends array
            unit pu//Player's Unit
            real speed
            real angle
            real mouseX
            real mouseY
            boolean mouseout
            boolean walking
            integer animation
            private static method MouseMoved takes nothing returns boolean
                local data i=GetPlayerId(GetTriggerPlayer())
                set i.mouseX=BlzGetTriggerPlayerMouseX()
                set i.mouseY=BlzGetTriggerPlayerMouseY()
                if i.mouseX+i.mouseY==0.0 then
                    set i.mouseout=false
                else
                    set i.mouseout=true
                endif
                return false
            endmethod
            private static method onInit takes nothing returns nothing
                local data i=0
                local trigger t=CreateTrigger()
                loop
                    exitwhen i==24
                    set i.pu=null
                    set i.speed=0.0
                    set i.angle=0.0
                    set i.mouseX=0.0
                    set i.mouseY=0.0
                    set i.walking=false
                    set i.mouseout=false
                    set i.animation=0
                    call TriggerRegisterPlayerEvent(t,Player(i),EVENT_PLAYER_MOUSE_MOVE)
                    set i=i+1
                endloop
                call TriggerAddCondition(t,Filter(function data.MouseMoved))
            endmethod
        endstruct
        globals
            private constant timer tmr=CreateTimer()
            private constant real periodic=0.031250
        private item itm
        endglobals
        private function CheckPath takes real x,real  y returns boolean
        local real ix
        local real iy
        call SetItemPosition(itm,x,y)
        set ix=GetItemX(itm)
        set iy=GetItemY(itm)
        call SetItemVisible(itm,false)
        return ((ix-x)*(ix-x)+(iy-y)*(iy-y))<100.0 and not IsTerrainPathable(ix,iy,PATHING_TYPE_WALKABILITY)
        endfunction
        private function Loop takes nothing returns nothing
            local data i=0
            local real spd
            local real unitX
            local real unitY
            loop
                exitwhen i==24
                if i.pu!=null and GetPlayerController(Player(i))==MAP_CONTROL_USER then
                    set spd=GetUnitMoveSpeed(i.pu)
                    set unitX=GetUnitX(i.pu)
                    set unitY=GetUnitY(i.pu)
                    if i.mouseout then
                        set i.angle=bj_RADTODEG*Atan2(i.mouseY-unitY,i.mouseX-unitX)
                        if i.angle<0.0 then
                            set i.angle=i.angle+360.0
                        endif
                        call SetUnitFacing(i.pu,i.angle)
                    endif
                    if (WSAD_W[i] or WSAD_S[i])/*
                    */
    and not(WSAD_W[i] and WSAD_S[i]) then
                        if i.speed<(spd/(2.0/periodic)) and i.speed>-((spd/(2.0/periodic))/2.0)then
                            if WSAD_W[i] and not WSAD_S[i] then
                   if i.speed<0.0 then
                                    set i.speed=0.0
                   endif
                                set i.speed=i.speed+(spd/(1.0/periodic))
                            endif
                if WSAD_S[i] and not WSAD_W[i] then
                   if i.speed>0.0 then
                                    set i.speed=0.0
                   endif
                                set i.speed=i.speed-(spd/(1.0/periodic))
                            endif
                        endif
                    elseif i.speed!=0.0 then
                        set i.speed=0.0
                        /*if i.speed<0.0 then
                            set i.speed=i.speed+(spd/(0.25/periodic))
                            if i.speed>0.0 then
                                set i.speed=0.0
                            endif
                        else
                            set i.speed=i.speed-(spd/(0.25/periodic))
                            if i.speed<0.0 then
                                set i.speed=0.0
                            endif
                        endif*/

                    endif
              set unitX=unitX+i.speed*Cos(i.angle*bj_DEGTORAD)
            set unitY=unitY+i.speed*Sin(i.angle*bj_DEGTORAD)
                    if i.speed!=0.0 and not i.walking then
                        set i.walking=true
                        call SetUnitAnimationByIndex(i.pu,i.animation)
                    elseif i.walking and i.speed==0.0 then
                        set i.walking=false
                        call SetUnitAnimationByIndex(i.pu,1)
                        call SetUnitTimeScale(i.pu,1.0)
                    endif
                    if i.walking and CheckPath(unitX,unitY) then
                        call SetUnitTimeScale(i.pu,(1.0/10.0)*i.speed)
                        //call SetUnitX(i.pu,unitX)
                        //call SetUnitY(i.pu,unitY)
                        call SetUnitPosition(i.pu,unitX,unitY)
            elseif i.walking then
               set i.walking=false
                    endif
                endif
                set i=i+1
            endloop
        endfunction
        public function AddPlayerUnit takes unit u,player p,integer anim returns nothing
            local data i=GetPlayerId(p)
            set i.pu=u
            set i.speed=0.0
            set i.angle=0.0
            set i.walking=false
            set i.animation=anim
            call SetCameraTargetControllerNoZForPlayer(p,u,0,0,false)
        endfunction
        private function Init takes nothing returns nothing
            call TimerStart(tmr,periodic,true,function Loop)
        set itm=CreateItem('afac',0,0)
        call SetItemVisible(itm,false)
        endfunction
    endlibrary
     
     

    Attached Files:

    Last edited: Jan 20, 2020
  2. JC Helas

    JC Helas

    Joined:
    Oct 19, 2014
    Messages:
    126
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    Criticism please
     
  3. Kanaru

    Kanaru

    Joined:
    Apr 26, 2006
    Messages:
    160
    Resources:
    0
    Resources:
    0
    How can I modify this so it doesn't rotate the character all the time to look at the cursor? It gets problematic if I want to move the cursor around the UI or something in combat for example.
     
  4. Pyrogasm

    Pyrogasm

    Joined:
    Feb 27, 2007
    Messages:
    3,788
    Resources:
    1
    Spells:
    1
    Resources:
    1
    Comment out this line in the Loop function:
    call SetUnitFacing(i.pu,i.angle)
    . JASS comments are //
     
  5. Kanaru

    Kanaru

    Joined:
    Apr 26, 2006
    Messages:
    160
    Resources:
    0
    Resources:
    0
    Thanks!