Class: Patience::Cursor

Inherits:
Object show all
Extended by:
Forwardable
Defined in:
lib/patience/cursor.rb

Overview

Patience::Cursor is a high-level class, which deals with all events in the game. Cursor always knows current position of the mouse.

cursor = Cursor.new
always { @cursor.mouse_pos = mouse_pos }
cursor.clicked_something? #=> false
cursor.still_on_something? #=> false

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clickObject

Returns the value of attribute click.



13
14
15
# File 'lib/patience/cursor.rb', line 13

def click
  @click
end

#dragObject

Returns the value of attribute drag.



13
14
15
# File 'lib/patience/cursor.rb', line 13

def drag
  @drag
end

#dropObject

Returns the value of attribute drop.



13
14
15
# File 'lib/patience/cursor.rb', line 13

def drop
  @drop
end

#mouse_posObject

Returns the value of attribute mouse_pos.



13
14
15
# File 'lib/patience/cursor.rb', line 13

def mouse_pos
  @mouse_pos
end

Instance Method Details

#carrying_card?Boolean

Returns true if cursor clicked something different from nothing. And by “something different” I mean any card.

Returns:

  • (Boolean)


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

def carrying_card?
  click and card
end

#click!Object



15
16
17
# File 'lib/patience/cursor.rb', line 15

def click!
  @click.scenario.call
end

#clicked_something?Boolean

Checks whether cursor clicked something. If so, also checks, if the cursor is still in boundaries of that object at the time of mouse button release. Returns true, if this is true. Otherwise, returns false.

Returns:

  • (Boolean)


29
30
31
# File 'lib/patience/cursor.rb', line 29

def clicked_something?
  click and click.something? and still_on_something?
end

#drop!Object

Calls calculated scenario for the drop event and then refreshes click, drag and drop by setting them to nil.



21
22
23
24
# File 'lib/patience/cursor.rb', line 21

def drop!
  @drop.scenario.call
  @click, @drag, @drop = nil, nil, nil
end

#movable?Boolean Also known as: drawable?

Returns true if cursor clicked some card and that card is avaliable for dragging (e.g. its face is up). Otherwise, returns false.

Returns:

  • (Boolean)


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

def movable?
  carrying_card? and draggable?
end

#still_on_something?Boolean

Checks whether cursor is still over the clicked object by asking card or pile, if the mouse_pos is within the pale of them. Returns true, if this is true. Otherwise, returns false.

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
# File 'lib/patience/cursor.rb', line 36

def still_on_something?
  if carrying_card?
    card.hit?(mouse_pos)
  elsif pile
    pile.hit?(mouse_pos)
  else
   false
  end
end