Class: Lotu::Cursor

Inherits:
Actor
  • Object
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, #systems, #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, #parse_options, #rand_color, #set_gosu_image, #set_image

Methods included from Controllable

#set_keys

Methods included from SystemUser

#use

Constructor Details

#initialize(opts = {}) ⇒ Cursor

Returns a new instance of Cursor.



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

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.



4
5
6
# File 'lib/lotu/cursor.rb', line 4

def clicked_x
  @clicked_x
end

#clicked_yObject (readonly)

Returns the value of attribute clicked_y.



4
5
6
# File 'lib/lotu/cursor.rb', line 4

def clicked_y
  @clicked_y
end

#speedObject

Returns the value of attribute speed.



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

def speed
  @speed
end

#use_mouseObject

Returns the value of attribute use_mouse.



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

def use_mouse
  @use_mouse
end

Instance Method Details

#adjust_mouseObject



41
42
43
44
# File 'lib/lotu/cursor.rb', line 41

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



35
36
37
38
39
# File 'lib/lotu/cursor.rb', line 35

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

#downObject



51
52
53
54
# File 'lib/lotu/cursor.rb', line 51

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

#leftObject



56
57
58
59
# File 'lib/lotu/cursor.rb', line 56

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

#rightObject



61
62
63
64
# File 'lib/lotu/cursor.rb', line 61

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

#to_sObject



66
67
68
69
# File 'lib/lotu/cursor.rb', line 66

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

#upObject



46
47
48
49
# File 'lib/lotu/cursor.rb', line 46

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

#updateObject



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

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