• 🏆 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]Keyboard WSA&D/Movement W&S

Status
Not open for further replies.
Level 14
Joined
Oct 19, 2014
Messages
187
JASS:
//*********************************************************************************************
//*
//*  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
JASS:
//*********************************************************************************************
//*
//*  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
 

Attachments

  • Keyboard WSAD.w3x
    22.1 KB · Views: 53
Last edited:
Level 5
Joined
Apr 26, 2006
Messages
160
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.
 
Status
Not open for further replies.
Top