API Camera - shmellyorc/Box GitHub Wiki

Camera

Namespace: Box.Systems

Represents a game camera used for panning, following entities, shaking, and clamping within a viewport. Each screen has its own Camera instance, accessible via its Camera property.


Constructors

No public constructors – cameras are created internally per Screen.


Properties

Name Type Description
Rect2 Clamp Rect2 Rectangle within which the camera position is constrained.
Vect2 Offset Vect2 Positional offset applied after following an entity or updating position.
Rect2 Bounds Rect2 World-space rectangle representing the camera’s viewable area (with culling padding).
Rect2 Area Rect2 Exact world-space rectangle of the viewport (without culling padding).
bool IsFollowing bool True if currently following an entity via Follow(...).
EaseType CameraEaseType EaseType Interpolation method used when following an entity.
Vect2 DefaultScale Vect2 Scale factor mapping viewport units to window pixels (Window/Viewport).
float Zoom float Camera zoom level. Setting adjusts internal view size.
float Speed float Follow interpolation speed (units per second).
Vect2 Position Vect2 Camera center position. Cannot set while following an entity.

Methods

Signature Description
void Reset() Reset zoom to default and update view rectangle to match Position.
bool InViewport(Rect2 rectangle) Check if a world-space rectangle intersects this camera’s view.
bool InViewport(Entity entity) Check if an entity’s bounds intersect the camera’s view.
void Follow(Entity entity, bool teleportToEntity) Begin following entity. If teleportToEntity is true, jump instantly to its position.
void UnFollow() Stop following any entity, allowing manual positioning via Position.
void Shake(float magnitude, float duration) Apply a screen shake effect of given magnitude (units) over duration seconds.

Usage Example

// Assume inside a Screen or system where 'Screen.Camera' is available
var camera = ScreenManager.Instance.ActiveScreen.Camera;

// Pan to a point manually
camera.Position = new Vect2(100, 50);

// Follow the player smoothly
camera.Follow(playerEntity, teleportToEntity: true);

// Later, stop following
camera.UnFollow();

// Zoom in
camera.Zoom = 2.0f;

// Shake camera on explosion
audioManager.PlaySound("explosion");
camera.Shake(magnitude: 5f, duration: 0.7f);

// Check visibility
if (camera.InViewport(enemy.Bounds))
    enemy.Render();