CameraControllingEntity
Introduction
The CameraControllingEntity is an object which provides convenient camera positioning and zooming functionality. It is included by default in the Top Down and Platformer projects when using the New Project Wizard.
Typically an instance of the CameraControllingEntity is created in the FRB Editor in GameScreen. For more information on working with the CameraControllingEntity in the FRB editor, see the FRB Editor CameraControllingEntity page.
Target
The Target property controls the desired location of the CameraControllingEntity. If a Target is assigned, the CameraControllingEntity changes its position to move towards the target according to the other properties assigned such as the Map and TargetApproachStyle.
Target can be assigned in code to change the desired position. The following code shows how to change between two targets by pressing the 1 and 2 keys on the keyboard:
Most games use a Target which is an Entity or Targets for multiple objects (as shown below). You can also create PositionedObjects to control the movement of the CameraControllingEntity as shown in the following code:
Targets
The Targets property (plural of Target) can be used to give the CameraControllingEntity multiple targets. This is typically used when the camera should follow a list of players in a mutiplayer game. The following code can be used to switch between following one of two targets individually or the list of targets. Notice that the CameraControllingEntity is positioned so that both targets are visible using their average X and Y values.
Moving Immediately to the Target
By default the CameraControllingEntity interpolates from its current position to the target. This is normally desired as it can make the camera move smoothly. At times the Camera may need to be moved immediately to its target.
The following code results in the CameraControllingEntity moving immediately.
Keep in mind that setting the Camera.Main's position to the target location will not immediately move the camera to the target position. If using a CameraControllingEntity, then the Camera's position becomes read only, so any changes to the camera position must be done through the CameraControllingEntity.
Additionally, when calling ApplyTarget, the value that is passed in should be obtained from the GetTarget method. The GetTarget method considers the bounds of the Map and the Camera size. Setting a value explicitly (such as by defining a Vector2 with constant X and Y values) may result in the CameraControllingEntity temporarily showing a region that is outside of the Map bounds.
Map
The Map object can be used to define the bounds for the CameraControllingEntity. If a Map is null, then the CameraControllingEntity does not stop on any bounds. If a Map is set, the CameraControllingEntity attempts to keep the Camera within the bounds. If the Target is set such that the Camera would view outside of the Map, the Camera moves smoothly to the edge of the bounds.
If the Map is smaller than the Camera's viewable area, the Camera centers itself within the Map.
A typical FlatRedBall game uses a LayeredTileMap (loaded from a TMX file) as its Map, but manual bounds can be set as well.
The following code shows how to manually create an AxisAlignedRectangle in code and use it as the Map to set bounds. Note that the following code only includes the relevant code to assign the Map property: