Class: OR2D::Camera

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

Overview

Since:

  • 2023-04-26

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(viewport_width = OR2D.game.screen_width, viewport_height = OR2D.game.screen_height, target = OR2D::Entity.new(:square, { size: 1, x: 0, y: 0, show: false }), speed = 1) ⇒ Camera

Constructs a new Camera object.

Parameters:

  • viewport_width (Integer) (defaults to: OR2D.game.screen_width)

    the width of the viewport

  • viewport_height (Integer) (defaults to: OR2D.game.screen_height)

    the height of the viewport

  • target (OR2D::Entity) (defaults to: OR2D::Entity.new(:square, { size: 1, x: 0, y: 0, show: false }))

    the target of the camera

Since:

  • 2023-04-26



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/or2d/camera.rb', line 16

def initialize(viewport_width = OR2D.game.screen_width,
               viewport_height = OR2D.game.screen_height,
               target = OR2D::Entity.new(:square, { size: 1, x: 0, y: 0, show: false }),
               speed = 1)
  @target = target
  @speed = speed
  @viewport = OR2D::Entity.new(:rectangle, {
    x: (OR2D.game.screen_width / 2) - viewport_width,
    y: (OR2D.game.screen_height / 2) + viewport_height,
    width: viewport_width,
    height: viewport_height,
    show: false
  })
end

Instance Attribute Details

#targetOR2D::Entity (readonly)

The target of the camera.

Returns:

Since:

  • 2023-04-26



6
7
8
# File 'lib/or2d/camera.rb', line 6

def target
  @target
end

#viewportOR2D::Entity (readonly)

The viewport of the camera.

Returns:

Since:

  • 2023-04-26



10
11
12
# File 'lib/or2d/camera.rb', line 10

def viewport
  @viewport
end

Instance Method Details

#scroll(direction, amount, target: false) ⇒ Object

Since:

  • 2023-04-26



33
34
35
36
37
38
39
40
41
# File 'lib/or2d/camera.rb', line 33

def scroll(direction, amount, target: false)
  case direction
  when :north then scroll_north(amount, target)
  when :south then scroll_south(amount, target)
  when :east then scroll_east(amount, target)
  when :west then scroll_west(amount, target)
  else raise ArgumentError, "Invalid direction: #{direction}"
  end
end

#scroll_east(delta_x, target) ⇒ Object

Simulates an eastern camera scroll by moving all of the visible entities on the screen to the west.

Parameters:

  • delta_x (Integer)

    the amount to scroll each entity

  • target (Boolean)

    whether or not to scroll the target

Since:

  • 2023-04-26



68
69
70
71
72
73
74
# File 'lib/or2d/camera.rb', line 68

def scroll_east(delta_x, target)
  OR2D.game.entities.each_value do |entity|
    next if !target && entity == @target

    entity.screen_x -= delta_x
  end
end

#scroll_north(delta_y, target) ⇒ Object

Simulates a northern camera scroll by moving all of the visible entities on the screen to the south.

Parameters:

  • delta_y (Integer)

    the amount to scroll each entity

  • target (Boolean)

    whether or not to scroll the target

Since:

  • 2023-04-26



46
47
48
49
50
51
52
# File 'lib/or2d/camera.rb', line 46

def scroll_north(delta_y, target)
  OR2D.game.entities.each_value do |entity|
    next if !target && entity == @target

    entity.screen_y += delta_y
  end
end

#scroll_south(delta_y, target) ⇒ Object

Simulates a southern camera scroll by moving all of the visible entities on the screen to the north.

Parameters:

  • delta_y (Integer)

    the amount to scroll each entity

  • target (Boolean)

    whether or not to scroll the target

Since:

  • 2023-04-26



57
58
59
60
61
62
63
# File 'lib/or2d/camera.rb', line 57

def scroll_south(delta_y, target)
  OR2D.game.entities.each_value do |entity|
    next if !target && entity == @target

    entity.screen_y -= delta_y
  end
end

#scroll_west(delta_x, target) ⇒ Object

Simulates a western camera scroll by moving all of the visible entities on the screen to the east.

Parameters:

  • delta_x (Integer)

    the amount to scroll each entity

  • target (Boolean)

    whether or not to scroll the target

Since:

  • 2023-04-26



79
80
81
82
83
84
85
# File 'lib/or2d/camera.rb', line 79

def scroll_west(delta_x, target)
  OR2D.game.entities.each_value do |entity|
    next if !target && entity == @target

    entity.screen_x += delta_x
  end
end

#updateObject

Since:

  • 2023-04-26



31
# File 'lib/or2d/camera.rb', line 31

def update; end