Class: Patience::Pile
- Extended by:
- Forwardable
- Defined in:
- lib/patience/pile.rb
Overview
Direct Known Subclasses
Instance Attribute Summary collapse
-
#background ⇒ Object
Returns the value of attribute background.
-
#cards ⇒ Object
Returns the value of attribute cards.
Instance Method Summary collapse
-
#<<(other_card) ⇒ Object
Appends card to the pile considering position of that pile.
-
#draw_on(win) ⇒ Object
Draws pile in the window.
-
#hit?(mouse_pos) ⇒ Boolean
Returns true or card, if there was clicked background or card in the pile, respectively.
-
#initialize(cards = []) ⇒ Pile
constructor
A new instance of Pile.
-
#last_card?(card) ⇒ Boolean
Returns true if the given card is the last card in the pile Otherwise, returns false.
-
#overlaps?(other_card) ⇒ Boolean
If the pile is empty, returns the result of collision detection of the other_card and background of the pile.
-
#pos=(pos) ⇒ Object
Sets position of the pile.
-
#shuffle_off!(num) ⇒ Object
Throws off ‘num’ quantity of cards and returns the array of them.
Constructor Details
Instance Attribute Details
#background ⇒ Object
Returns the value of attribute background.
12 13 14 |
# File 'lib/patience/pile.rb', line 12 def background @background end |
#cards ⇒ Object
Returns the value of attribute cards.
12 13 14 |
# File 'lib/patience/pile.rb', line 12 def cards @cards end |
Instance Method Details
#<<(other_card) ⇒ Object
Appends card to the pile considering position of that pile.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/patience/pile.rb', line 32 def <<(other_card) bg_w = self.background.sub_rect.w bg_h = self.background.sub_rect.h sprite_w = other_card.sprite_width sprite_h = other_card.sprite_height other_card.pos = self.pos if bg_w > sprite_w && bg_h > sprite_h w = bg_w - sprite_w h = bg_h - sprite_h other_card.pos += [w/2, h/2] end cards << other_card end |
#draw_on(win) ⇒ Object
Draws pile in the window.
64 65 66 67 |
# File 'lib/patience/pile.rb', line 64 def draw_on(win) win.draw(background) cards.each { |card| card.draw_on(win) } end |
#hit?(mouse_pos) ⇒ Boolean
Returns true or card, if there was clicked background or card in the pile, respectively. Otherwise returns false or nil.
71 72 73 74 |
# File 'lib/patience/pile.rb', line 71 def hit?(mouse_pos) background.to_rect.contain?(mouse_pos) or cards.find { |card| card.hit?(mouse_pos) } end |
#last_card?(card) ⇒ Boolean
Returns true if the given card is the last card in the pile Otherwise, returns false.
59 60 61 |
# File 'lib/patience/pile.rb', line 59 def last_card?(card) card == cards.last end |
#overlaps?(other_card) ⇒ Boolean
If the pile is empty, returns the result of collision detection of the other_card and background of the pile. If the pile isn’t empty, tries to find a card, which overlaps other_card.
79 80 81 82 83 84 85 |
# File 'lib/patience/pile.rb', line 79 def overlaps?(other_card) if cards.empty? background.to_rect.collide?(other_card) else cards.find { |card| card.face_up? and card.overlaps?(other_card) } end end |
#pos=(pos) ⇒ Object
Sets position of the pile. Applies to the cards in the pile and background both.
52 53 54 55 |
# File 'lib/patience/pile.rb', line 52 def pos=(pos) background.pos = *pos cards.each { |card| card.pos = *pos } end |
#shuffle_off!(num) ⇒ Object
Throws off ‘num’ quantity of cards and returns the array of them.
27 28 29 |
# File 'lib/patience/pile.rb', line 27 def shuffle_off!(num) cards.slice!(0..num-1) end |