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

[vJASS] Camera System...

Status
Not open for further replies.
Level 29
Joined
Mar 10, 2009
Messages
5,016
Guys Im posting this for testing purposes and hopefully to get some feedbacks...

Im making a camera system for my map that can rotate left/right/zoom in/out...

Any suggestions please provide, thanks...

JASS:
library MyCam initializer init

globals
    private constant hashtable                 HASH = InitHashtable()
    private constant integer             MAXPLAYERS = 3 //starting with "0" coz Player 1 is Player(0)
    private constant real        CAMERA_ANGLE_SPEED = 2.0
    private constant real        ZOOM_ADJUST_OFFSET = 20.
    private constant real                     DELAY = 0.03
    private constant boolean          ENABLE_LAUNCH = true //set to false to set arrow keys manually via GUI or another code
    private constant boolean       ENABLE_UNIT_LOCK = true //recommended setting
    private constant boolean            ENABLE_LOCK = true //enables lock Zoom and Angle
    private real                       CAMERA_ANGLE
    private real                               ZOOM
    private unit array                     DUMMYCAM
    private integer array                     DUMID
    private timer array                    CAMTIMER
endglobals

//===CAMERA MOVEMENTS: DO NOT TOUCH THIS
private function CamOn takes nothing returns nothing
    local timer t = GetExpiredTimer()
    local integer tID = GetHandleId(t)
    local player p = LoadPlayerHandle(HASH, tID, 1)
    local integer ID = GetPlayerId(p)    
    if GetLocalPlayer()==Player(ID) and LoadBoolean(HASH, DUMID[ID], 3)==true then
        
        if LoadStr(HASH, tID, 2)=="up" then
            set ZOOM = LoadReal(HASH, DUMID[ID], 1)
            call SaveReal(HASH, DUMID[ID], 1, ZOOM - ZOOM_ADJUST_OFFSET)      
            set ZOOM = LoadReal(HASH, DUMID[ID], 1)
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, ZOOM, 0.03) 
            
        elseif LoadStr(HASH, tID, 2)=="down" then
            set ZOOM = LoadReal(HASH, DUMID[ID], 1)
            call SaveReal(HASH, DUMID[ID], 1, ZOOM + ZOOM_ADJUST_OFFSET)      
            set ZOOM = LoadReal(HASH, DUMID[ID], 1)
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, ZOOM, 0.03) 
        
        elseif LoadStr(HASH, tID, 2)=="left" then
            set CAMERA_ANGLE = LoadReal(HASH, DUMID[ID], 2)
            call SaveReal(HASH, DUMID[ID], 2, CAMERA_ANGLE - CAMERA_ANGLE_SPEED)      
            set CAMERA_ANGLE = LoadReal(HASH, DUMID[ID], 2)
            call SetCameraField(CAMERA_FIELD_ROTATION, CAMERA_ANGLE, 0.03)
            
        elseif LoadStr(HASH, tID, 2)=="right" then
            set CAMERA_ANGLE = LoadReal(HASH, DUMID[ID], 2)
            call SaveReal(HASH, DUMID[ID], 2, CAMERA_ANGLE + CAMERA_ANGLE_SPEED)      
            set CAMERA_ANGLE = LoadReal(HASH, DUMID[ID], 2)
            call SetCameraField(CAMERA_FIELD_ROTATION, CAMERA_ANGLE, 0.03)
        endif
    else
        call SaveBoolean(HASH, DUMID[ID], 4, true)
        call PauseTimer(CAMTIMER[ID])    
    endif
    set t = null
endfunction

function CamOff takes nothing returns boolean
    local integer i = 0 
    loop
        if GetTriggerPlayer()==Player(i) then
            call SaveBoolean(HASH, DUMID[i], 3, false)
        endif
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    return false
endfunction

private function LockUnit takes nothing returns boolean
    local integer i = 0 
    loop
        if GetLocalPlayer()==Player(i) then
            call SetCameraTargetController(GetTriggerUnit(), 0, 0, true)
            call SaveUnitHandle(HASH, DUMID[i], 4, GetTriggerUnit())
        endif
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    return false
endfunction

//============================================================
//===CAMERA LEFT:
function CamLEFT_Cond takes nothing returns boolean
    local integer i = GetPlayerId(GetTriggerPlayer())
    call SaveBoolean(HASH, DUMID[i], 3, true)
    call SaveBoolean(HASH, DUMID[i], 4, false)
    call SavePlayerHandle(HASH, GetHandleId(CAMTIMER[i]), 1, Player(i)) 
    call SaveStr(HASH, GetHandleId(CAMTIMER[i]), 2, "left")
    call TimerStart(CAMTIMER[i], DELAY, true, function CamOn) 
    return false
endfunction
//============================================================
//===CAMERA RIGHT:
function CamRIGHT_Cond takes nothing returns boolean
    local integer i = GetPlayerId(GetTriggerPlayer())
    call SaveBoolean(HASH, DUMID[i], 3, true)
    call SaveBoolean(HASH, DUMID[i], 4, false)
    call SavePlayerHandle(HASH, GetHandleId(CAMTIMER[i]), 1, Player(i)) 
    call SaveStr(HASH, GetHandleId(CAMTIMER[i]), 2, "right")
    call TimerStart(CAMTIMER[i], DELAY, true, function CamOn) 
    return false
endfunction
//============================================================
//===CAMERA UP:
function CamUP_Cond takes nothing returns boolean
    local integer i = GetPlayerId(GetTriggerPlayer())
    call SaveBoolean(HASH, DUMID[i], 3, true)
    call SaveBoolean(HASH, DUMID[i], 4, false)
    call SavePlayerHandle(HASH, GetHandleId(CAMTIMER[i]), 1, Player(i)) 
    call SaveStr(HASH, GetHandleId(CAMTIMER[i]), 2, "up")
    call TimerStart(CAMTIMER[i], DELAY, true, function CamOn) 
    return false
endfunction
//============================================================
//===CAMERA DOWN:
function CamDOWN_Cond takes nothing returns boolean
    local integer i = GetPlayerId(GetTriggerPlayer())
    call SaveBoolean(HASH, DUMID[i], 3, true)
    call SaveBoolean(HASH, DUMID[i], 4, false)
    call SavePlayerHandle(HASH, GetHandleId(CAMTIMER[i]), 1, Player(i)) 
    call SaveStr(HASH, GetHandleId(CAMTIMER[i]), 2, "down")
    call TimerStart(CAMTIMER[i], DELAY, true, function CamOn) 
    return false
endfunction
//============================================================
//============================================================
//============================================================
private function LockDownAll takes nothing returns boolean
    local integer i = 0
    loop
        if GetLocalPlayer()==Player(i) and LoadBoolean(HASH, DUMID[i], 4)==true then
            set ZOOM = LoadReal(HASH, DUMID[i], 1)
            set CAMERA_ANGLE = LoadReal(HASH, DUMID[i], 2)
            call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, ZOOM, 0.03) 
            call SetCameraField(CAMERA_FIELD_ROTATION, CAMERA_ANGLE, 0.03)        
        endif
        set i = i+1
        exitwhen i > MAXPLAYERS
        endloop
    return false
endfunction

//============================================================
private function Launch takes nothing returns nothing
    local trigger t1 = CreateTrigger()//UP         
    local trigger t2 = CreateTrigger()//DOWN
    local trigger t3 = CreateTrigger()//LEFT
    local trigger t4 = CreateTrigger()//RIGHT
    local trigger t5 = CreateTrigger()//CamOff
    local integer i
    
    call TriggerAddCondition(t1, Condition(function CamUP_Cond))
    call TriggerAddCondition(t2, Condition(function CamDOWN_Cond))
    call TriggerAddCondition(t3, Condition(function CamLEFT_Cond))
    call TriggerAddCondition(t4, Condition(function CamRIGHT_Cond))
    call TriggerAddCondition(t5, Condition(function CamOff))
    
    //==PRESSING UP:
    set i = 0
    loop
        call TriggerRegisterPlayerEvent(t1, Player(i), EVENT_PLAYER_ARROW_UP_DOWN)
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
    //==PRESSING DOWN:
    set i = 0
    loop
        call TriggerRegisterPlayerEvent(t2, Player(i), EVENT_PLAYER_ARROW_DOWN_DOWN)
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
     //==PRESSING LEFT:
    set i = 0
    loop
        call TriggerRegisterPlayerEvent(t3, Player(i), EVENT_PLAYER_ARROW_LEFT_DOWN)
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
     //==PRESSING RIGHT:
    set i = 0
    loop
        call TriggerRegisterPlayerEvent(t4, Player(i), EVENT_PLAYER_ARROW_RIGHT_DOWN)
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
    //==CAMERA OFF:
    set i = 0
    loop
        call TriggerRegisterPlayerEvent(t5, Player(i), EVENT_PLAYER_ARROW_LEFT_UP)
        call TriggerRegisterPlayerEvent(t5, Player(i), EVENT_PLAYER_ARROW_RIGHT_UP)
        call TriggerRegisterPlayerEvent(t5, Player(i), EVENT_PLAYER_ARROW_UP_UP)
        call TriggerRegisterPlayerEvent(t5, Player(i), EVENT_PLAYER_ARROW_DOWN_UP)
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
    set t1 = null
    set t2 = null
    set t3 = null
    set t4 = null    
    set t5 = null 
endfunction

private function init takes nothing returns nothing
    local trigger t1 = CreateTrigger()
    local trigger t2 = CreateTrigger()
    local integer i = 0
    loop
        set DUMMYCAM[i] = CreateUnit(Player(i), 'hpea', 0,0,0)
        set DUMID[i] = GetHandleId(DUMMYCAM[i])
        call UnitAddAbility(DUMMYCAM[i], 'Amrf')
        call UnitAddAbility(DUMMYCAM[i], 'Aloc')
        call SetUnitPathing(DUMMYCAM[i], false)
        call ShowUnit(DUMMYCAM[i], false)
        call SaveReal(HASH, DUMID[i], 1, 2000.) //Distance
        call SaveReal(HASH, DUMID[i], 2, 90.) //Angle        
        call SaveBoolean(HASH, DUMID[i], 3, false) //Checking
        call SaveBoolean(HASH, DUMID[i], 4, true) //Checking Lock
        set CAMTIMER[i] = CreateTimer()
        set i = i+1
        exitwhen i > MAXPLAYERS
    endloop
    
    //===UNIT LOCK:
    if ENABLE_UNIT_LOCK==true then
        set i = 0
        loop
            call TriggerRegisterPlayerUnitEvent(t1, Player(i), EVENT_PLAYER_UNIT_SELECTED, null)   
            set i = i+1
            exitwhen i > MAXPLAYERS
        endloop 
        call TriggerAddCondition(t1, Condition(function LockUnit))
    endif
    
    //===LAUNCH TRIGGER:
    if ENABLE_LAUNCH==true then
        call Launch() 
    endif
    
    //LOCK ZOOM and ANGLE:
    if ENABLE_LOCK==true then
        call TriggerRegisterTimerEvent(t2, 0.03, true)
        call TriggerAddCondition(t2, Condition(function LockDownAll))        
    endif
    
    set t1 = null
    set t2 = null
endfunction

endlibrary
 
Last edited:
Status
Not open for further replies.
Top