Class: Chingu::Viewport
- Inherits:
-
Object
- Object
- Chingu::Viewport
- Defined in:
- lib/chingu/viewport.rb
Overview
A basic viewport class. Coordinates X and Y are relative to game area that can be specified by any arguments acceptable by Chingu::Rect#new method. By default, the game_area is the same as window and thus the vieport has no effect.
TODO: Implement use of viewports angle, center_x, center_y
Instance Attribute Summary collapse
-
#factor_x ⇒ Object
Returns the value of attribute factor_x.
-
#factor_y ⇒ Object
Returns the value of attribute factor_y.
-
#game_area ⇒ Object
Returns the value of attribute game_area.
-
#x ⇒ Object
Returns the value of attribute x.
-
#x_lag ⇒ Object
Returns the value of attribute x_lag.
-
#x_target ⇒ Object
Returns the value of attribute x_target.
-
#y ⇒ Object
Returns the value of attribute y.
-
#y_lag ⇒ Object
Returns the value of attribute y_lag.
-
#y_target ⇒ Object
Returns the value of attribute y_target.
Instance Method Summary collapse
-
#apply(&block) ⇒ Object
Apply the X/Y viewport-translation, used by trait “viewport”.
-
#center_around(object) ⇒ Object
Center the viewport around the given object (it must respont to x/y) Center will fail if object is in the corners of the game area.
-
#game_area_object=(game_object) ⇒ Object
Set a game world by giving it a game object The game objects image will be the rectangle the viewport can move within.
-
#initialize(options = {}) ⇒ Viewport
constructor
A new instance of Viewport.
-
#inside?(object, y = nil) ⇒ Boolean
Returns true if object is inside view port, false if outside TODO: add view port height and width! (and use clip_to when painting?).
-
#inside_game_area?(object) ⇒ Boolean
Returns true object is inside the game area The “game area” is the full map/world/area from which the viewport shows a slice The viewport can't show anything outside the game area.
-
#lag=(lag) ⇒ Object
Set x_lag and y_lag to value 'lag'.
-
#move_towards_target ⇒ Object
Modify viewports x and y from target_x / target_y and x_lag / y_lag Use this to have the viewport “slide” after the player.
-
#outside?(object, y = nil) ⇒ Boolean
Returns true object is outside the view port.
-
#outside_game_area?(object) ⇒ Boolean
Returns true object is outside the game area.
- #to_s ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Viewport
Returns a new instance of Viewport.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/chingu/viewport.rb', line 36 def initialize( = {}) @x = [:x] || 0 @y = [:y] || 0 @x_target = [:x_target] || @x @y_target = [:y_target] || @y @x_lag = [:x_lag] || 0 @y_lag = [:y_lag] || 0 @factor_x = [:factor_x] || 1 @factor_y = [:factor_y] || 1 @game_area = Chingu::Rect.new([:game_area] || [@x, @y, $window.width, $window.height]) end |
Instance Attribute Details
#factor_x ⇒ Object
Returns the value of attribute factor_x
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def factor_x @factor_x end |
#factor_y ⇒ Object
Returns the value of attribute factor_y
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def factor_y @factor_y end |
#game_area ⇒ Object
Returns the value of attribute game_area
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def game_area @game_area end |
#x ⇒ Object
Returns the value of attribute x
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def x @x end |
#x_lag ⇒ Object
Returns the value of attribute x_lag
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def x_lag @x_lag end |
#x_target ⇒ Object
Returns the value of attribute x_target
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def x_target @x_target end |
#y ⇒ Object
Returns the value of attribute y
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def y @y end |
#y_lag ⇒ Object
Returns the value of attribute y_lag
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def y_lag @y_lag end |
#y_target ⇒ Object
Returns the value of attribute y_target
34 35 36 |
# File 'lib/chingu/viewport.rb', line 34 def y_target @y_target end |
Instance Method Details
#apply(&block) ⇒ Object
Apply the X/Y viewport-translation, used by trait “viewport”
156 157 158 159 160 |
# File 'lib/chingu/viewport.rb', line 156 def apply(&block) $window.translate(-@x.to_i, -@y.to_i) do $window.scale(@factor_x, @factor_y, 0, 0, &block) end end |
#center_around(object) ⇒ Object
Center the viewport around the given object (it must respont to x/y) Center will fail if object is in the corners of the game area
TODO: Add support for x,y here!
61 62 63 64 |
# File 'lib/chingu/viewport.rb', line 61 def center_around(object) self.x = object.x * @factor_x - $window.width / 2 self.y = object.y * @factor_y - $window.height / 2 end |
#game_area_object=(game_object) ⇒ Object
Set a game world by giving it a game object The game objects image will be the rectangle the viewport can move within.
78 79 80 |
# File 'lib/chingu/viewport.rb', line 78 def game_area_object=(game_object) @game_area = Rect.new(0, 0, game_object.width, game_object.height) end |
#inside?(object, y = nil) ⇒ Boolean
Returns true if object is inside view port, false if outside TODO: add view port height and width! (and use clip_to when painting?)
This is a very flawed implementation, it Should take inte account objects height,width,factor_x,factor_y,center_x,center_y as well…
89 90 91 92 93 |
# File 'lib/chingu/viewport.rb', line 89 def inside?(object, y = nil) x, y = y ? [object,y] : [object.x, object.y] x >= @x && x <= (@x + $window.width) && y >= @y && y <= (@y + $window.height) end |
#inside_game_area?(object) ⇒ Boolean
Returns true object is inside the game area The “game area” is the full map/world/area from which the viewport shows a slice The viewport can't show anything outside the game area
105 106 107 108 |
# File 'lib/chingu/viewport.rb', line 105 def inside_game_area?(object) object.x >= @game_area.x && object.x <= @game_area.width && object.y >= @game_area.x && object.y <= @game_area.height end |
#lag=(lag) ⇒ Object
Set x_lag and y_lag to value 'lag'
51 52 53 |
# File 'lib/chingu/viewport.rb', line 51 def lag=(lag) @x_lag = @y_lag = lag end |
#move_towards_target ⇒ Object
Modify viewports x and y from target_x / target_y and x_lag / y_lag Use this to have the viewport “slide” after the player
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/chingu/viewport.rb', line 119 def move_towards_target if @x_target && @x != @x_target x_step = @x_target * @factor_x - @x self.x = @x + x_step * (1.0 - @x_lag) end if @y_target && @y != @y_target y_step = @y_target * @factor_y - @y self.y = @y + y_step * (1.0 - @y_lag) end end |
#outside?(object, y = nil) ⇒ Boolean
Returns true object is outside the view port
96 97 98 |
# File 'lib/chingu/viewport.rb', line 96 def outside?(object, y = nil) not inside?(object, y) end |
#outside_game_area?(object) ⇒ Boolean
Returns true object is outside the game area
111 112 113 |
# File 'lib/chingu/viewport.rb', line 111 def outside_game_area?(object) not inside_game_area?(object) end |
#to_s ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/chingu/viewport.rb', line 162 def to_s a = @game_area %/ Vieport Position: #{@x}, #{@y} Game area: #{a.x},#{a.y},#{a.width},#{a.height}" Target: #{@target_x}, #{@target_y} / end |