Class: Patience::Pile

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

Overview

Patience::Pile is aimed to hold cards in the pile :surprise:. Every pile has its own background sprite and set of cards.

cards = [Card.new(1, 1), Card.new(1, 2), Card.new(1, 3)]
random_pile = Pile.new(cards)
random_pile.background = [10, 10]

Direct Known Subclasses

Deck

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards = []) ⇒ Pile

Returns a new instance of Pile.



14
15
16
17
18
# File 'lib/patience/pile.rb', line 14

def initialize(cards=[])
  @cards = cards
  pile_background = 'patience/sprites/pile_background.png'
  @background = Ray::Sprite.new path_of(pile_background)
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



12
13
14
# File 'lib/patience/pile.rb', line 12

def background
  @background
end

#cardsObject

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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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