Class: OR2D::Cameras::PaddedCamera

Inherits:
OR2D::Camera show all
Defined in:
lib/or2d/cameras/padded.rb

Overview

A PaddedCamera is a camera that has padding. When the target <OR2D::Entity> reaches the edge of the padding, the camera will scroll.

Since:

  • 2023-04-26

Instance Attribute Summary

Attributes inherited from OR2D::Camera

#target, #viewport

Instance Method Summary collapse

Methods inherited from OR2D::Camera

#scroll_east, #scroll_north, #scroll_south, #scroll_west

Constructor Details

#initialize(padding_width = 512, padding_height = 512, target = nil) ⇒ PaddedCamera

Constructs a new PaddedCamera.

Parameters:

  • target (OR2D::Entity) (defaults to: nil)

    the target of the camera

  • padding_width (Integer) (defaults to: 512)

    the width of the padding

  • padding_height (Integer) (defaults to: 512)

    the height of the padding

Since:

  • 2023-04-26



8
9
10
11
12
13
# File 'lib/or2d/cameras/padded.rb', line 8

def initialize(padding_width = 512, padding_height = 512, target = nil)
  @padding_width = padding_width
  @padding_height = padding_height
  super(@padding_width, @padding_height, target, 1.scale)
  orientate
end

Instance Method Details

#orientateObject

Since:

  • 2023-04-26



60
61
62
63
# File 'lib/or2d/cameras/padded.rb', line 60

def orientate
  @target.screen_x = @viewport.screen_x + @viewport.width / 2
  @target.screen_y = @viewport.screen_y + @viewport.height / 2
end

#scrollObject

Since:

  • 2023-04-26



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/or2d/cameras/padded.rb', line 42

def scroll
  if should_scroll_east?
    super(:east, @speed, target: true)
    @viewport.screen_x += @speed
  elsif should_scroll_west?
    super(:west, @speed, target: true)
    @viewport.screen_x -= @speed
  end

  if should_scroll_north?
    super(:north, @speed, target: true)
    @viewport.screen_y -= @speed
  elsif should_scroll_south?
    super(:south, @speed, target: true)
    @viewport.screen_y += @speed
  end
end

#should_scroll?Boolean

Checks if the camera should scroll. This check is done by checking if the target <OR2D::Entity> is within the <OR2D::Cameras::PaddedCamera#boundary>.

Returns:

  • (Boolean)

    true if the camera should scroll, false otherwise

Since:

  • 2023-04-26



22
23
24
# File 'lib/or2d/cameras/padded.rb', line 22

def should_scroll?
  should_scroll_east? || should_scroll_west? || should_scroll_north? || should_scroll_south?
end

#should_scroll_east?Boolean

Returns:

  • (Boolean)

Since:

  • 2023-04-26



26
27
28
# File 'lib/or2d/cameras/padded.rb', line 26

def should_scroll_east?
  @target.screen_x >= (@viewport.screen_x + @viewport.width)
end

#should_scroll_north?Boolean

Returns:

  • (Boolean)

Since:

  • 2023-04-26



34
35
36
# File 'lib/or2d/cameras/padded.rb', line 34

def should_scroll_north?
  @target.screen_y <= (@viewport.screen_y - @viewport.height)
end

#should_scroll_south?Boolean

Returns:

  • (Boolean)

Since:

  • 2023-04-26



38
39
40
# File 'lib/or2d/cameras/padded.rb', line 38

def should_scroll_south?
  @target.screen_y >= (@viewport.screen_y + @viewport.height)
end

#should_scroll_west?Boolean

Returns:

  • (Boolean)

Since:

  • 2023-04-26



30
31
32
# File 'lib/or2d/cameras/padded.rb', line 30

def should_scroll_west?
  @target.screen_x <= (@viewport.screen_x - @viewport.width)
end

#updateObject

Updates the camera.

Since:

  • 2023-04-26



16
17
18
# File 'lib/or2d/cameras/padded.rb', line 16

def update
  scroll if should_scroll?
end