Class: CardPlace

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, HasSprite
Defined in:
lib/rubysketch/solitaire/places.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasSprite

#hit?

Constructor Details

#initialize(game, name, linkCards: false) ⇒ CardPlace

Returns a new instance of CardPlace.



11
12
13
14
# File 'lib/rubysketch/solitaire/places.rb', line 11

def initialize(game, name, linkCards: false)
  @game, @name, @linkCards = game, name.intern, linkCards
  @cards = []
end

Instance Attribute Details

#cardsObject (readonly)

Returns the value of attribute cards.



16
17
18
# File 'lib/rubysketch/solitaire/places.rb', line 16

def cards
  @cards
end

#nameObject (readonly)

Returns the value of attribute name.



16
17
18
# File 'lib/rubysketch/solitaire/places.rb', line 16

def name
  @name
end

Instance Method Details

#accept?(x, y, card) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/rubysketch/solitaire/places.rb', line 50

def accept?(x, y, card)
  false
end

#add(*cards, updatePos: true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/rubysketch/solitaire/places.rb', line 20

def add(*cards, updatePos: true)
  cards.map(&:to_a).flatten.each do |card|
    card.place&.pop card
    @cards.push card
    card.next  = nil
    card.pos   = posFor card if updatePos
    card.place = self
  end
  @cards.each_cons(2) {|prev, it| prev.next = @linkCards ? it : nil}
end

#idObject



46
47
48
# File 'lib/rubysketch/solitaire/places.rb', line 46

def id()
  @id ||= "id:#{name}"
end

#inspectObject



77
78
79
# File 'lib/rubysketch/solitaire/places.rb', line 77

def inspect()
  "#<CardPlace #{name}>"
end

#pop(card = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rubysketch/solitaire/places.rb', line 31

def pop(card = nil)
  return nil if @cards.empty?
  card  ||= @cards.last
  index   = @cards.index card
  poppeds =
    if index
      @cards[index..-1].tap {@cards[index..-1] = []}
    else
      [@cards.pop]
    end
  @cards.last&.next = nil
  poppeds.each_cons(2) {|prev, it| prev.next = it}
  poppeds.first.tap {|first| first.place = nil}
end

#posFor(card, index = nil) ⇒ Object



54
55
56
57
# File 'lib/rubysketch/solitaire/places.rb', line 54

def posFor(card, index = nil)
  index ||= indexFor card
  createVector *pos.to_a(2), self.z + index + 1
end

#spriteObject



67
68
69
70
71
72
73
74
75
# File 'lib/rubysketch/solitaire/places.rb', line 67

def sprite()
  @sprite ||= Sprite.new(0, 0, *skin.cardSpriteSize).tap do |sp|
    sp.draw do
      noStroke
      fill *skin.translucentBackgroundColor
      rect 0, 0, sp.w, sp.h, 4
    end
  end
end

#updateCards(seconds = 0.2, excludes: []) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/rubysketch/solitaire/places.rb', line 59

def updateCards(seconds = 0.2, excludes: [])
  cards.each.with_index do |card, index|
    next if excludes.include? card
    pos = posFor card, index
    move card, pos, seconds if pos != card.pos
  end
end