Sha256: 64ef325c1ad42b607165d1ad995a272c4ed3769a89ac7d969492a219a43c9b4f

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# Provides a mouse pointer usable through mouse or keyboard
module Lotu
  class Cursor < Actor
    attr_reader :clicked_x, :clicked_y
    attr_accessor :speed, :use_mouse

    def initialize(opts={})
      default_opts = {
        :use_mouse => true,
        :speed => 100
      }
      opts = default_opts.merge!(opts)
      super
      @clicked_x = @clicked_y = 0
      @speed = opts[:speed]
      @use_mouse = opts[:use_mouse]
      set_image(opts[:image]) unless opts[:image].nil?
      set_keys(opts[:keys]) unless opts[:keys].nil?
    end

    def update
      if @use_mouse
        @x = $window.mouse_x
        @y = $window.mouse_y
      end
    end

    # This is the method you want to call when a user press the
    # "click" key of your preference with something like:
    # set_keys Gosu::Button::MsLeft => :click
    # It'll yield the x, y coordinates of the clicked point
    def click
      @clicked_x = @x
      @clicked_y = @y
      fire(:click, @clicked_x, @clicked_y)
    end

    def adjust_mouse
      $window.mouse_y = @y
      $window.mouse_x = @x
    end

    def up
      @y -= @speed * dt
      adjust_mouse if use_mouse
    end

    def down
      @y += @speed * dt
      adjust_mouse if use_mouse
    end

    def left
      @x -= @speed * dt
      adjust_mouse if use_mouse
    end

    def right
      @x += @speed * dt
      adjust_mouse if use_mouse
    end

    def to_s
      "@pos(#{format('%.2f, %.2f', @x, @y)}) @clicked(#{format('%.2f, %.2f', @clicked_x, @clicked_y)})"
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lotu-0.1.4 lib/lotu/cursor.rb