Console::Camera

console 0.2.0 · app/console/camera.rb · 230 lines · view source

Console::Camera -- a 2D scroll/zoom camera.

The camera owns the world -> screen offset used by every draw call inside camera.apply. UI drawn outside the block stays screen-locked, which is the usual split (world in apply, HUD after).

camera.look_at(entity)                 # follow an entity
camera.within(100)                     # 100px of the world stays visible
camera.shake(6, 12)                    # magnitude 6, 12 ticks
camera.apply do
  draw.sprite(x: entity.x, y: entity.y, path: :hero)
end

Offsets are computed so the focused point lands at the screen's centre, and the view is clamped so you never see past the world bounds unless you opt out with camera.unbounded.

def initialize(args)
line 22
def args
line 37
def grid
line 41
def screen_center_x

Centre of the screen. Named screen_center_* so it cannot be confused with Geom.center_x, which takes a rect.

def screen_center_y
line 51

configuration

def bounds=(value)
line 57
def within(margin)

Set the world rectangle the camera is allowed to show.

def unbounded
line 67
def zoom=(value)
line 73

focus

def follow(entity)
line 79
def stop_following
line 84
def look_at(thing)

Point the camera at a world position (or an entity) for one frame.

def shake(magnitude, ticks = 10)
line 98

per-frame

def update
line 106
def shake_offset_x
line 126
def shake_offset_y
line 131
def shaking?
line 136

transform

def offset_x

Current total offset, including shake.

def offset_y
line 147
def to_screen(wx, wy)

Convert a world point to screen space.

def to_world(sx, sy)

Convert a screen point to world space (for mouse input).

def pointer_world

Pointer position in world space, accounting for camera and shake.

drawing

def apply

Run the block with the camera applied to all coordinates.

Coordinates are transformed by pushing a translate offset that Draw reads.

def active?
line 183
def current_offset
line 187
def visible_rect

Screen bounds of the visible world area (for culling / minimaps).

def visible?(r)

True when a world rect overlaps the visible area (with padding).

def clamp_to_bounds
line 206