-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin_generator.rb
64 lines (49 loc) · 1.17 KB
/
coin_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class CoinGenerator
attr_accessor :storage
def initialize(path)
@storage = []
@path = path
@options = {
clip_width: 50,
time: 300,
loop: true,
}
end
def generate(snake_storage)
free_pos = random_free_pos(snake_storage)
@options[:x] = normlize(free_pos[:x])
@options[:y] = normlize(free_pos[:y])
@coin = Sprite.new(
@path,
@options,
)
@coin.play
@storage << @coin
end
def random_free_pos(snake_storage)
x = rand(10)
y = rand(10)
coins_pos = storage_to_hash
combined_storage = snake_storage + coins_pos
is_matched = combined_storage.find do |i|
(simplify(i[:x]) == x) && (simplify(i[:y]) == y)
end
# @TODO: define count of the empty positions for handling of satck overflow
return {x: x, y: y} unless is_matched
random_free_pos(snake_storage)
end
def storage_to_hash
@storage.map { |i| {x: i.x, y: i.y} }
end
def remove(eaten_coin)
@storage = @storage.filter { |i| i.data != eaten_coin.data }
eaten_coin.remove
end
private
def simplify(value)
return value / 50
end
def normlize(value)
return value * 50
end
end