Class Joyau::Viewport
In: ruby/site_ruby/joyau/viewport.rb
Parent: Joyau::Drawable

When writing game, we might have to think about hero’s position, obstacle’s position, … but this can be confusing.

When a viewport is drawed, it’s object are moved, drawed, and then, their moves are cancelled. So, you can think about the position in the world without considering the PSP’s screen.

Methods

<<   abs2rel   center_on   draw   new   rel2abs  

Attributes

objects  [R] 

Public Class methods

Creates a new Viewport.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 32
    def initialize
      @objects = []
    end

Public Instance methods

Inserts a new object in the Viewport. This method raises an exception if the given object is not a Drawable.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 40
    def <<(obj)
      raise TypeError, "obj has to be a Drawable." unless obj.is_a? Drawable
      @objects << obj

      return self
    end

Converts an aboslute position (given as a point) to a relative position.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 63
    def abs2rel(point)
      return Point.new(point.x - self.x, point.y - self.y)
    end

Centers the view on point, which is relative to the view.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 77
    def center_on(point)
      p = rel2abs(point)
      move(240 - p.x, 136 - p.y)
    end

Draws all the objects in the Viewport.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 50
    def draw
      @objects.each { |i|
        old_pos = Point.new(i.x, i.y)
        i.pos = Point.new(x + i.x, y + i.y)
        i.draw
        i.pos = old_pos
        i.clearMove
      }
    end

Converts a relative position (given as a point) to an absolute position.

[Source]

# File ruby/site_ruby/joyau/viewport.rb, line 70
    def rel2abs(point)
      return Point.new(point.x + self.x, point.y + self.y)
    end

[Validate]