Module: Chingu::Traits::Viewport

Defined in:
lib/chingu/traits/viewport.rb

Overview

A chingu trait providing velocity and acceleration logic. Adds parameters: velocity_x/y, acceleration_x/y and modifies self.x / self.y Also keeps previous_x and previous_y which is the x, y before modification. Can be useful for example collision detection

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#viewportObject

Returns the value of attribute viewport



31
32
33
# File 'lib/chingu/traits/viewport.rb', line 31

def viewport
  @viewport
end

Instance Method Details

#drawObject

Override game states default draw that draws objects relative to the viewport. It only draws game objects inside the viewport. (GOSU does no such optimizations)



71
72
73
74
# File 'lib/chingu/traits/viewport.rb', line 71

def draw
  #self.game_objects.draw_relative(-@viewport.x, -@viewport.y)
  @viewport.apply { super }
end

#inside_viewport?(object) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/chingu/traits/viewport.rb', line 49

def inside_viewport?(object)
  puts "Deprecated, use self.viewport.inside?() instead"
  object.x >= @viewport.x && object.x <= (@viewport.x + $window.width) &&
  object.y >= @viewport.y && object.y <= (@viewport.y + $window.height)
end

#outside_viewport?(object) ⇒ Boolean

Returns true object is outside the view port

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/chingu/traits/viewport.rb', line 56

def outside_viewport?(object)
  puts "Deprecated, use self.viewport.outside?() instead"
  not inside_viewport?(object)
end

#setup_trait(options) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/chingu/traits/viewport.rb', line 39

def setup_trait(options)
  @viewport_options = {:debug => false}.merge(options)
  
  @viewport = Chingu::Viewport.new()
  @viewport.x = options[:viewport_x] || 0
  @viewport.y = options[:viewport_y] || 0
  
  super
end

#update_traitObject

Take care of laggy viewport movements



62
63
64
65
# File 'lib/chingu/traits/viewport.rb', line 62

def update_trait
        @viewport.move_towards_target
        super
end