Class: Lotu::Cursor

Inherits:
Actor show all
Defined in:
lib/lotu/cursor.rb

Instance Attribute Summary collapse

Attributes inherited from Actor

#angle, #center_x, #center_y, #color, #factor_x, #factor_y, #height, #image, #mode, #parent, #width, #x, #y, #z

Instance Method Summary collapse

Methods inherited from Actor

#adjust_width_and_height, #calc_zoom, #die, #draw, #draw_box, #draw_debug, #dt, #factor=, #parse_options, #rand_color, #set_gosu_image, #set_image

Methods included from Behavior

#behave_like, #inherited

Methods included from Helpers::Util

#class_debug_info, #instance_debug_info, #parse_cli_options

Methods inherited from GameEntity

#draw

Constructor Details

#initialize(opts = {}) ⇒ Cursor

Returns a new instance of Cursor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lotu/cursor.rb', line 9

def initialize(opts={})
  default_opts = {
    :use_mouse => true,
    :speed => 300,
    :x => $lotu.width/2,
    :y => $lotu.height/2
  }
  opts = default_opts.merge!(opts)
  super
  $lotu.mouse_x = opts[:x]
  $lotu.mouse_y = opts[:y]
  @clicked_x = @clicked_y = 0
  @speed = opts[:speed]
  @use_mouse = opts[:use_mouse]
end

Instance Attribute Details

#clicked_xObject (readonly)

Returns the value of attribute clicked_x.



6
7
8
# File 'lib/lotu/cursor.rb', line 6

def clicked_x
  @clicked_x
end

#clicked_yObject (readonly)

Returns the value of attribute clicked_y.



6
7
8
# File 'lib/lotu/cursor.rb', line 6

def clicked_y
  @clicked_y
end

#speedObject

Returns the value of attribute speed.



7
8
9
# File 'lib/lotu/cursor.rb', line 7

def speed
  @speed
end

#use_mouseObject

Returns the value of attribute use_mouse.



7
8
9
# File 'lib/lotu/cursor.rb', line 7

def use_mouse
  @use_mouse
end

Instance Method Details

#adjust_mouseObject



43
44
45
46
# File 'lib/lotu/cursor.rb', line 43

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

#clickObject

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



37
38
39
40
41
# File 'lib/lotu/cursor.rb', line 37

def click
  @clicked_x = @x
  @clicked_y = @y
  fire(:click, @clicked_x, @clicked_y)
end

#downObject



53
54
55
56
# File 'lib/lotu/cursor.rb', line 53

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

#leftObject



58
59
60
61
# File 'lib/lotu/cursor.rb', line 58

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

#rightObject



63
64
65
66
# File 'lib/lotu/cursor.rb', line 63

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

#to_sObject



68
69
70
71
# File 'lib/lotu/cursor.rb', line 68

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

#upObject



48
49
50
51
# File 'lib/lotu/cursor.rb', line 48

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

#updateObject



25
26
27
28
29
30
31
# File 'lib/lotu/cursor.rb', line 25

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