Class: OR2D::Camera
- Inherits:
-
Object
- Object
- OR2D::Camera
- Defined in:
- lib/or2d/camera.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#target ⇒ OR2D::Entity
readonly
The target of the camera.
-
#viewport ⇒ OR2D::Entity
readonly
The viewport of the camera.
Instance Method Summary collapse
-
#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
constructor
Constructs a new Camera object.
- #scroll(direction, amount, target: false) ⇒ Object
-
#scroll_east(delta_x, target) ⇒ Object
Simulates an eastern camera scroll by moving all of the visible entities on the screen to the west.
-
#scroll_north(delta_y, target) ⇒ Object
Simulates a northern camera scroll by moving all of the visible entities on the screen to the south.
-
#scroll_south(delta_y, target) ⇒ Object
Simulates a southern camera scroll by moving all of the visible entities on the screen to the north.
-
#scroll_west(delta_x, target) ⇒ Object
Simulates a western camera scroll by moving all of the visible entities on the screen to the east.
- #update ⇒ Object
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.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/or2d/camera.rb', line 16 def initialize( = OR2D.game.screen_width, = 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) - , y: (OR2D.game.screen_height / 2) + , width: , height: , show: false }) end |
Instance Attribute Details
#target ⇒ OR2D::Entity (readonly)
The target of the camera.
6 7 8 |
# File 'lib/or2d/camera.rb', line 6 def target @target end |
#viewport ⇒ OR2D::Entity (readonly)
The viewport of the camera.
10 11 12 |
# File 'lib/or2d/camera.rb', line 10 def @viewport end |
Instance Method Details
#scroll(direction, amount, target: false) ⇒ Object
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.
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.
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.
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.
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 |
#update ⇒ Object
31 |
# File 'lib/or2d/camera.rb', line 31 def update; end |