Class: OR2D::Cameras::PaddedCamera
Overview
A PaddedCamera is a camera that has padding. When the target <OR2D::Entity> reaches the edge of the padding, the camera will scroll.
Instance Attribute Summary
Attributes inherited from OR2D::Camera
#target, #viewport
Instance Method Summary
collapse
#scroll_east, #scroll_north, #scroll_south, #scroll_west
Constructor Details
#initialize(padding_width = 512, padding_height = 512, target = nil) ⇒ PaddedCamera
Constructs a new PaddedCamera.
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
#orientate ⇒ Object
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
|
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
|
Checks if the camera should scroll. This check is done by checking if the target <OR2D::Entity> is within the <OR2D::Cameras::PaddedCamera#boundary>.
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
|
26
27
28
|
# File 'lib/or2d/cameras/padded.rb', line 26
def should_scroll_east?
@target.screen_x >= (@viewport.screen_x + @viewport.width)
end
|
34
35
36
|
# File 'lib/or2d/cameras/padded.rb', line 34
def should_scroll_north?
@target.screen_y <= (@viewport.screen_y - @viewport.height)
end
|
38
39
40
|
# File 'lib/or2d/cameras/padded.rb', line 38
def should_scroll_south?
@target.screen_y >= (@viewport.screen_y + @viewport.height)
end
|
30
31
32
|
# File 'lib/or2d/cameras/padded.rb', line 30
def should_scroll_west?
@target.screen_x <= (@viewport.screen_x - @viewport.width)
end
|
#update ⇒ Object
16
17
18
|
# File 'lib/or2d/cameras/padded.rb', line 16
def update
scroll if should_scroll?
end
|