Class: Chingu::Viewport

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Viewport



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/chingu/viewport.rb', line 36

def initialize(options = {})
  @x = options[:x] || 0
  @y = options[:y] || 0 
  @x_target = options[:x_target] || @x
  @y_target = options[:y_target] || @y
  @x_lag = options[:x_lag] || 0
  @y_lag = options[:y_lag] || 0
  @factor_x = options[:factor_x] || 1
  @factor_y = options[:factor_y] || 1
  @game_area = Chingu::Rect.new(options[:game_area] || [@x, @y, $window.width, $window.height])       
end

Instance Attribute Details

#factor_xObject

Returns the value of attribute factor_x



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def factor_x
  @factor_x
end

#factor_yObject

Returns the value of attribute factor_y



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def factor_y
  @factor_y
end

#game_areaObject

Returns the value of attribute game_area



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def game_area
  @game_area
end

#xObject

Returns the value of attribute x



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def x
  @x
end

#x_lagObject

Returns the value of attribute x_lag



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def x_lag
  @x_lag
end

#x_targetObject

Returns the value of attribute x_target



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def x_target
  @x_target
end

#yObject

Returns the value of attribute y



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def y
  @y
end

#y_lagObject

Returns the value of attribute y_lag



34
35
36
# File 'lib/chingu/viewport.rb', line 34

def y_lag
  @y_lag
end

#y_targetObject

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_targetObject

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_sObject



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