Code: Select all
library DynamicCamera initializer Init
globals
location CamLoc=Location(0,0)
//Use the x values as the angle of attack values. Y values are the according distance/offset values.
//Example:
//DISTANCEX1 = 345
//DISTANCEY1 = 300
//DISTANCEX2 = 295
//DISTANCEY2 = 600
//This means you want the camera distance to be 300 at an angle of 345 and 600 at an angle of 295.
//The values inbetween will be calculated automatically.
//ZOFFSET is the basic camera offset:
constant real DISTANCEX1 = 345
constant real DISTANCEY1 = 300
constant real DISTANCEX2 = 295
constant real DISTANCEY2 = 600
constant real OFFSETX1 = 345
constant real OFFSETY1 = 0
constant real OFFSETX2 = 295
constant real OFFSETY2 = 200
constant real ZOFFSET = 100
//These are intern values for the dynamic functions GetDynamicDistance and GetDynamicOffset:
real DistanceM
real DistanceT
real OffsetM
real OffsetT
//Gloabs used in the example usage
unit array Hero
boolean array Key_Up
boolean array Key_Down
boolean array Key_Right
boolean array Key_Left
boolean array Locked
boolean array Rotate
real array ControlableAoA
real array ControlableRot
endglobals
//private function GetFunctionM takes real x1, real y1, real x2, real y2 returns real
// return (y2-y1)/(x2-x1)
//endfunction
//private function GetFunctionT takes real x1, real y1, real x2, real y2 returns real
// return y1-x1*GetFunctionM(x1, y1, x2, y2)
//endfunction
//You can replace this function if you already have a similar one in your map
private function ProjectX takes real x, real inc, real a returns real
return x+inc*Cos(a*bj_DEGTORAD)
endfunction
//You can replace this function if you already have a similar one in your map
private function ProjectY takes real y, real inc, real a returns real
return y+inc*Sin(a*bj_DEGTORAD)
endfunction
//You can replace this function if you already have a similar one in your map
private function DistanceBetweenXY takes real x1, real y1, real x2, real y2 returns real
return SquareRoot((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1))
endfunction
//Dynamic functions for distance and offset. Basically these are linear mathematical functions y = mx+t
private function GetDynamicDistance takes real angleOfAttack returns real
return DistanceM*angleOfAttack+DistanceT
endfunction
private function GetDynamicOffset takes real angleOfAttack returns real
return OffsetM*angleOfAttack+OffsetT
endfunction
//Camera actions, lots of stuff. All working :)
function UseDynamicCam takes unit whichUnit, boolean locked, boolean rotate returns nothing
local integer p=GetPlayerId(GetLocalPlayer())
local real unitx=GetUnitX(whichUnit)
local real unity=GetUnitY(whichUnit)
local real camx=GetCameraEyePositionX()
local real camy=GetCameraEyePositionY()
local real camz
local real camrot=GetCameraField(CAMERA_FIELD_ROTATION)
local real camzoff=GetCameraField(CAMERA_FIELD_ZOFFSET)
local real angleofattack=GetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK)*bj_RADTODEG
local real offsetx=camx+350*Cos(camrot+bj_PI)
local real offsety=camy+350*Sin(camrot+bj_PI)
local real offsetz
local real unitangle=GetUnitFacing(whichUnit)
local real panx=ProjectX(unitx, GetDynamicOffset(angleofattack), unitangle)
local real pany=ProjectY(unity, GetDynamicOffset(angleofattack), unitangle)
local real anticlipangle
local real distance
if angleofattack>345 then
set panx=unitx
set pany=unity
endif
if locked then
call PanCameraToTimed(panx, pany, 0.5)
endif
if rotate then
call SetCameraField(CAMERA_FIELD_ROTATION, unitangle+ControlableRot[p], 0.75)
endif
call MoveLocation(CamLoc, camx, camy)
set camz=GetLocationZ(CamLoc)+GetUnitFlyHeight(whichUnit)
call MoveLocation(CamLoc, offsetx, offsety)
set offsetz=GetLocationZ(CamLoc)
set distance=DistanceBetweenXY(panx, pany, camx, camy)
set anticlipangle=357-bj_RADTODEG*Atan((offsetz-camz-camzoff)/distance)
if ControlableAoA[p]>anticlipangle then
call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, anticlipangle, 0.5)
else
call SetCameraField(CAMERA_FIELD_ANGLE_OF_ATTACK, ControlableAoA[p], 0.5)
endif
call SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, GetDynamicDistance(angleofattack), 0.5)
call SetCameraField(CAMERA_FIELD_ZOFFSET, camzoff-GetCameraTargetPositionZ()+camz+ZOFFSET, 0.5)
call SetCameraField(CAMERA_FIELD_FIELD_OF_VIEW, 120, 0)
endfunction
//===========================================================================
private function Init takes nothing returns nothing
local integer i=0
set DistanceM=(DISTANCEY2-DISTANCEY1)/(DISTANCEX2-DISTANCEX1)
set DistanceT=DISTANCEY1-DISTANCEX1*DistanceM
set OffsetM=(OFFSETY2-OFFSETY1)/(OFFSETX2-OFFSETX1)
set OffsetT=OFFSETY1-OFFSETX1*OffsetM
//set DistanceM=GetFunctionM(DISTANCEX1, DISTANCEY1, DISTANCEX2, DISTANCEY2)
//set DistanceT=GetFunctionT(DISTANCEX1, DISTANCEY1, DISTANCEX2, DISTANCEY2)
//set OffsetM=GetFunctionM(OFFSETX1, OFFSETY1, OFFSETX2, OFFSETY2)
//set OffsetT=GetFunctionT(OFFSETX1, OFFSETY1, OFFSETX2, OFFSETY2)
//This sets the defult values for the variables
loop
exitwhen i==12 //Change this to the max number of human controled players
set ControlableAoA[i]=340
set ControlableRot[i]=0
set Locked[i]=true
set Rotate[i]=true
set Hero[i]=CreateUnit(Player(i), 'hrif', GetRandomReal(-1000, 1000), GetRandomReal(-1000, 1000), GetRandomReal(0, 360))
set i=i+1
endloop
endfunction
endlibrary