Class: Klondike
- Inherits:
-
Scene
show all
- Defined in:
- lib/rubysketch/solitaire/klondike.rb
Defined Under Namespace
Classes: ColumnPlace, MarkPlace, NextsPlace
Instance Attribute Summary
Attributes inherited from Scene
#name, #parent
Instance Method Summary
collapse
Methods inherited from Scene
#activated, #active?, #add, #deactivated, #emitParticle, #mouseDragged, #mouseMoved, #mousePressed, #mouseReleased, #particle, #remove, #transition, #update
Constructor Details
#initialize(hash = nil) ⇒ Klondike
Returns a new instance of Klondike.
7
8
9
10
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 7
def initialize(hash = nil)
super()
hash ? load(hash) : ready
end
|
Instance Method Details
#canDrop?(card) ⇒ Boolean
51
52
53
54
55
56
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 51
def canDrop?(card)
case card.place
when *columns then card.opened?
else card.last?
end
end
|
#cardClicked(card) ⇒ Object
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 58
def cardClicked(card)
if card.closed? && card.place == deck
deckClicked
elsif card.opened? && place = findPlaceToGo(card)
moveCard card, place, 0.3
elsif card.opened?
shake card, vector: createVector(5, 0)
noopSound.play gain: 0.5
end
end
|
#cardDropped(x, y, card, prevPlace) ⇒ Object
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 77
def cardDropped(x, y, card, prevPlace)
if place = getPlaceAccepts(x, y, card)
moveCard card, place, 0.2
elsif prevPlace
history.disable do
prevPos, prevTime = card.pos.xy, now
moveCard card, prevPlace, 0.15, add: false, ease: :quadIn do |t, finished|
pos, time = card.pos.xy, now
vel = (pos - prevPos) / (time - prevTime)
prevPos, prevTime = pos, time
backToPlace card, vel if finished
end
end
end
end
|
#deckClicked ⇒ Object
69
70
71
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 69
def deckClicked()
deck.empty? ? refillDeck : drawNexts
end
|
#difficulty ⇒ Object
16
17
18
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 16
def difficulty()
@difficulty ||= :normal
end
|
#draw ⇒ Object
35
36
37
38
39
40
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 35
def draw()
sprite *places.map(&:sprite)
sprite *cards.sort {|a, b| a.z <=> b.z}.map(&:sprite)
sprite *interfaces
super
end
|
#focusChanged(focus) ⇒ Object
47
48
49
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 47
def focusChanged(focus)
focus ? resume : pause if ios?
end
|
#inspect ⇒ Object
139
140
141
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 139
def inspect()
"#<Klondike:#{object_id}>"
end
|
#load(hash) ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 106
def load(hash)
raise 'Unknown state version' unless hash['version'] == 1
all = places + cards
findAll = -> id { all.find {|obj| obj .id == id} or raise "No object '#{id}'"}
findCard = -> id {cards.find {|card| card.id == id} or raise "No card '#{id}'"}
@difficulty = hash['difficulty'].intern
self.history = History.load hash['history'] do |id|
(id.respond_to?('=~') && id =~ /^id:/) ? findAll[id] : nil
end
self.score.load hash['score']
@elapsedTime = hash['elapsedTime']
@moveCount = hash['moveCount']
places.each do |place|
place.clear
ids = hash['places'][place.name.to_s] or raise "No place '#{place.name}'"
place.add *ids.map {|id| findCard[id]}
end
hash['openeds'].each do |id|
findCard[id].open
end
raise "Failed to restore state" unless
places.reduce([]) {|a, place| a + place.cards}.size == 52
start!
end
|
#nextsClicked ⇒ Object
73
74
75
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 73
def nextsClicked()
drawNexts if nexts.empty?
end
|
#pause ⇒ Object
20
21
22
23
24
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 20
def pause()
super
@prevTime = nil
stopTimer :save
end
|
#resized(w, h) ⇒ Object
42
43
44
45
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 42
def resized(w, h)
super
updateLayout w, h
end
|
#resume ⇒ Object
26
27
28
29
30
31
32
33
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 26
def resume()
super
return if !started? || completed?
@prevTime = now
startInterval :save, 1, now: true do
save
end
end
|
#save ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 93
def save()
settings['state'] = {
version: 1,
difficulty: difficulty,
history: history.to_h {|o| o.id if o.respond_to? :id},
score: score.to_h,
elapsedTime: elapsedTime,
moveCount: @moveCount,
places: places.map {|place| [place.name, place.cards.map(&:id)]}.to_h,
openeds: cards.select {|card| card.opened?}.map(&:id)
}
end
|
#sprites ⇒ Object
12
13
14
|
# File 'lib/rubysketch/solitaire/klondike.rb', line 12
def sprites()
super + [*cards, *places].map(&:sprite) + interfaces
end
|