Class: RedBird::Camera

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

Overview

Because most of the time the tilemap used as the scenario is larger than the screen, you need to choose an entity to use as a reference. A Camera control the scenario offset and makes the position of the scene rendered in the screen be relative to an entity.

Author:

  • Frederico Linhares

Instance Method Summary collapse

Constructor Details

#initialize(focus, scenario) ⇒ Camera

Returns a new instance of Camera.

Parameters:

Author:

  • Frederico Linhares



15
16
17
18
19
20
# File 'lib/red_bird/camera.rb', line 15

def initialize(focus, scenario)
  @scenario = scenario
  @max_hor_offset = @scenario.total_width - @scenario.width
  @max_ver_offset = @scenario.total_height - @scenario.height
  self.focus = focus
end

Instance Method Details

#callObject

Updates scenario offset according to entity position.

Author:

  • Frederico Linhares



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/red_bird/camera.rb', line 34

def call
  hor_offset = (@focus.relative_pos_x - @hor_center).to_i
  if hor_offset < 0 then
    @scenario.hor_offset = 0
  elsif hor_offset > @max_hor_offset then
    @scenario.hor_offset = @max_hor_offset
  else
    @scenario.hor_offset = hor_offset
  end

  ver_offset = (@focus.relative_pos_y - @ver_center).to_i
  if ver_offset < 0 then
    @scenario.ver_offset = 0
  elsif ver_offset > @max_ver_offset then
    @scenario.ver_offset = @max_ver_offset
  else
    @scenario.ver_offset = ver_offset
  end
end

#focus=(focus) ⇒ Object

Change the camere focus.

Parameters:

Author:

  • Frederico Linhares



26
27
28
29
# File 'lib/red_bird/camera.rb', line 26

def focus=(focus)
  @focus = focus
  set_center
end