library CameraSys initializer Init
//Camera System by The_Witcher
//
// This is a very basic camera system which adjusts the camera
// distance to the target so the camera isn't looking through houses etc.
// useful for RPGs and that stuff
//
// To bind the camera to a unit use
// SetCameraUnit( unit )
//
// if you want to have your normal camera again use
// ReleaseCamUnit( unit )
//
// in case you want to know which unit is bound to the camera for player xy use
// GetCameraUnit( player )
//
// SETUP PART
globals
// The max. distance the camera can have to the target
private constant real maxrange = 1000
// the timer interval (0.01 is best but can lagg in huge maps with many of these short intervals)
private constant real interval = 0.01
// the time the camera will need to adjust
private constant real delay = 0.25
// the angle of attack of the camera
private constant real AngleOfAttack = 340
endglobals
// SETUP END
// don't modify the code below!
globals
private real distance = 50
private item ite
private unit array CamUnit
private timer tim = CreateTimer()
endglobals
function SetCameraUnit takes unit u returns nothing
set CamUnit[GetPlayerId(GetOwningPlayer(u))] = u
endfunction
function ReleaseCameraUnit takes unit u returns nothing
set CamUnit[GetPlayerId(GetOwningPlayer(u))] = null
call ResetToGameCameraForPlayer(GetOwningPlayer(u),0)
call CameraSetSmoothingFactor(0)
endfunction
function GetCameraUnit takes player pl returns unit
return CamUnit[GetPlayerId(pl)]
endfunction
private function IsCoordPathable takes real x, real y returns boolean
call SetItemVisible(ite,true)
call SetItemPosition(ite,x,y)
set x = GetItemX( ite) - x
set y = GetItemY( ite) - y
call SetItemVisible(ite,false)
if x < 1 and x > -1 and y < 1 and y > -1 then
return true
endif
return false
endfunction
private function SetCameraZ takes player changedPlayer, real newCameraZ returns nothing
if GetLocalPlayer() == changedPlayer then
set newCameraZ = GetCameraField(CAMERA_FIELD_ZOFFSET)+newCameraZ-GetCameraTargetPositionZ()
endif
call SetCameraField(CAMERA_FIELD_ZOFFSET,newCameraZ,- 0.01)
call SetCameraField(CAMERA_FIELD_ZOFFSET,newCameraZ,0.01)
endfunction
private function Actions takes nothing returns nothing
local real x
local real y
local real r
local real w
local integer i = 0
local integer b
local real d = distance
loop
exitwhen i >= bj_MAX_PLAYERS
if CamUnit[i] != null then
set r = 0
set x = GetUnitX(CamUnit[i])
set y = GetUnitY(CamUnit[i])
call SetCameraTargetControllerNoZForPlayer(Player(i),CamUnit[i],0,0,false)
set b = 1
set w = GetUnitFacing(CamUnit[i]) - 180
set d = distance
loop
set x = x + d * Cos(w * bj_DEGTORAD)
set y = y + d * Sin(w * bj_DEGTORAD)
set r = r + d
if not IsCoordPathable(x,y)then
set b = 0
endif
if b == 0 and d == distance then
set r = r - d
set x = x - d * Cos(w * bj_DEGTORAD)
set y = y - d * Sin(w * bj_DEGTORAD)
set b = 1
set d = 1
endif
exitwhen (b == 0 and d == 1) or r > maxrange
endloop
if GetLocalPlayer() == Player(i) then
call CameraSetSmoothingFactor(1)
call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, r, delay)
call SetCameraField(CAMERA_FIELD_ROTATION, w+180, delay)
call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, AngleOfAttack, delay)
endif
call SetCameraZ(Player(i),100)
endif
set i = i + 1
endloop
endfunction
private function Init takes nothing returns nothing
local integer i = 0
call TimerStart(tim,interval,true,function Actions)
loop
exitwhen i >= bj_MAX_PLAYERS
set CamUnit[i] = null
set i = i + 1
endloop
set ite = CreateItem( 'wolg', 0,0 )
call SetItemVisible(ite,false)
endfunction
endlibrary