Sha256: a72f3d13b70d769aff540289bf7022e2e3f60fed7aa21f3e8d1a207e664ce2b8

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Vedeu

  # Converts a position into an index for the terminal.
  class PositionIndex

    # Convenience constructor for Vedeu::Position.
    #
    # @param (see #initialize)
    def self.[](y, x)
      new(y, x).[]
    end

    # Returns a new instance of Vedeu::PositionIndex.
    #
    # @param y [Fixnum]
    # @param x [Fixnum]
    # @return [Vedeu::PositionIndex]
    def initialize(y, x)
      @y = y
      @x = x
    end

    # Returns a tuple containing the y and x coordinates.
    #
    # @return [Array<Fixnum>]
    def []
      [y, x]
    end

    # @param other [Vedeu::PositionIndex]
    # @return [Boolean]
    def eql?(other)
      self.class == other.class && (x == other.x && y == other.y)
    end
    alias_method :==, :eql?

    # @return [Vedeu::Position]
    def to_position
      Vedeu::Position.new(y, x)
    end

    # @return [Fixnum]
    def x
      @_x ||= ((@x - 1) <= 1) ? 0 : (@x - 1)
    end
    alias_method :last, :x

    # @return [Fixnum]
    def y
      @_y ||= ((@y - 1) <= 1) ? 0 : (@y - 1)
    end
    alias_method :first, :y

  end # PositionIndex

end # Vedeu

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
vedeu-0.4.21 lib/vedeu/geometry/position_index.rb
vedeu-0.4.20 lib/vedeu/geometry/position_index.rb
vedeu-0.4.19 lib/vedeu/geometry/position_index.rb