Class: Camera

Inherits:
Object
  • Object
show all
Defined in:
lib/fantasy/camera.rb

Overview

Represents Coordinates of the camera and it affects the screen position of all Actors and Backgrounds. There is only one active Camera and it is acceded by Camera.main. This Camera is initialized automatically and it is already accessible. It can also be changed by another Camera instance.`

Examples:

Set the camera position

Camera.main.position = Coordinates.new(0, 100)

Camera follows player vertically

on_game do
  player = Actor.new("image")

  on_loop do
    Camera.main.position.y = player.position.y - (SCREEN_HEIGHT / 2)
  end
end

Switching between cameras

camera_1 = Camera.new
camera_2 = Camera.new(position: Coordinates.new(0, 10))
Camera.main = camera_1
Camera.main = camera_2

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position: Coordinates.zero) ⇒ Camera

Generates a Camera with all the default attribute values

Examples:

Generate an Camera

camera = Camera.new
camera.position # => Coordinates.zero
camera.direction # => Coordinates.zero
camera.speed # => 0

Parameters:

  • position (Coordinates) (defaults to: Coordinates.zero)

    the initial position of the camera. Default Coordinates.zero



74
75
76
77
78
79
# File 'lib/fantasy/camera.rb', line 74

def initialize(position: Coordinates.zero)
  @position = position
  @direction = Coordinates.zero
  @speed = 0
  @on_after_move_callback = nil
end

Class Attribute Details

.mainCamera

Returns The active Camera.

Returns:

  • (Camera)

    The active Camera



103
104
105
# File 'lib/fantasy/camera.rb', line 103

def main
  @main
end

Instance Attribute Details

#directionCoordinates

Note:

The the pixels per second is represented by the @speed attribute

Controls the direction in which the Camera will move in the next frame.

Default Coordinates.zero.

Examples:

Set direction

Camera.main.direction = Coordinates.right

Returns:



51
52
53
# File 'lib/fantasy/camera.rb', line 51

def direction
  @direction
end

#positionCoordinates

Coordinates object where x and y represent the position of the Camera in the World (no necessarily in the Screen).

Default Coordinates.zero.

Examples:

Setting position

Camera.main.position = Coordinates.new(10, 20)
Camera.main.position.x # => 10

Modify position

Camera.main.position.x +=  1
Camera.main.position.x # => 11

Returns:



39
40
41
# File 'lib/fantasy/camera.rb', line 39

def position
  @position
end

#speedFloat

Note:

The the direction is represented by the @direction attribute

Controls the pixels per second which the Camera will move in the next frame.

Default 0.

Examples:

Set speed

Camera.main.speed = 10

Returns:

  • (Float)

    the actual speed



63
64
65
# File 'lib/fantasy/camera.rb', line 63

def speed
  @speed
end

Instance Method Details

#on_after_move(&block) ⇒ Object

Triggered on every frame after the move has been executed

Examples:

Avoid Camera to move over limits

Camera.main.on_after_move do
  if Camera.main.position.x > 100
    Camera.main.position.x = 100
  end
end


97
98
99
# File 'lib/fantasy/camera.rb', line 97

def on_after_move(&block)
  @on_after_move_callback = block
end