Class: HitSq
Overview
a pattern of hits to time a Snd
Instance Attribute Summary collapse
-
#hits ⇒ Object
Returns the value of attribute hits.
Instance Method Summary collapse
-
#count ⇒ Object
return number of hits.
- #delete_random(chance = 0.5) ⇒ Object
-
#eqly_spaced(possible_hits = 4, chance = 1, ignore_first = 0, ignore_last = 0) ⇒ Object
Adds into #hits.
-
#initialize ⇒ HitSq
constructor
A new instance of HitSq.
-
#move(val = 0.5) ⇒ Object
Shift all hits by val, no validation atm.
-
#persist ⇒ Object
(internal use only).
- #trim_both(portion = 0.25) ⇒ Object
- #trim_end(portion = 0.25) ⇒ Object
- #trim_start(portion = 0.25) ⇒ Object
Methods inherited from Api
Constructor Details
#initialize ⇒ HitSq
Returns a new instance of HitSq.
4 5 6 7 |
# File 'lib/api/hit_sq.rb', line 4 def initialize @hits=[] super end |
Instance Attribute Details
#hits ⇒ Object
Returns the value of attribute hits.
3 4 5 |
# File 'lib/api/hit_sq.rb', line 3 def hits @hits end |
Instance Method Details
#count ⇒ Object
return number of hits
117 118 119 |
# File 'lib/api/hit_sq.rb', line 117 def count hits.count end |
#delete_random(chance = 0.5) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/api/hit_sq.rb', line 42 def delete_random chance = 0.5 to_del = [] hits.each do |h| if rand < chance to_del << h end end to_del.each {|d| self.hits.delete d } persist self end |
#eqly_spaced(possible_hits = 4, chance = 1, ignore_first = 0, ignore_last = 0) ⇒ Object
Adds into #hits.
- possible_hits
-
number of hits that can occur. Must be int
- chance
-
chance a hit will be included. range: 0 to 1
- ignore_first
-
skip the first n possible hits
- ignore_first
-
skip the last n possible hits
e.g. disperse_hits(16,1,4,4) makes this pattern [-|-|-|-|+|+|+|+|+|+|+|+|-|-|-|-|]
107 108 109 110 111 112 113 114 115 |
# File 'lib/api/hit_sq.rb', line 107 def eqly_spaced(possible_hits = 4, chance = 1, ignore_first=0, ignore_last=0) possible_hits.times do |i| if ignore_first <= i && possible_hits - ignore_last > i delay = i/possible_hits.to_f @hits.push delay if (rand + chance >= 1) end end self end |
#move(val = 0.5) ⇒ Object
Shift all hits by val, no validation atm
121 122 123 124 125 126 127 128 |
# File 'lib/api/hit_sq.rb', line 121 def move(val=0.5) self.hits.collect! {|x| z = (x+val) z = (z*1000).round / 1000.0 # rounding x = z } # delay all self end |
#persist ⇒ Object
(internal use only)
98 99 100 |
# File 'lib/api/hit_sq.rb', line 98 def persist parent.dist.hits = hits if parent end |
#trim_both(portion = 0.25) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/api/hit_sq.rb', line 78 def trim_both portion=0.25 save = [] hits.count.times do |i| upto = i.to_f / hits.count if upto < 1.0 - portion save << hits[i] end end save2 = [] hits.count.times do |i| upto = i.to_f / hits.count if upto >= portion save2 << hits[i] end end self.hits = save&save2 persist end |
#trim_end(portion = 0.25) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/api/hit_sq.rb', line 66 def trim_end portion=0.25 save = [] hits.count.times do |i| upto = i.to_f / hits.count if upto < 1.0 - portion save << hits[i] end end self.hits = save persist end |
#trim_start(portion = 0.25) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/api/hit_sq.rb', line 54 def trim_start portion=0.25 save = [] hits.count.times do |i| upto = i.to_f / hits.count if upto >= portion save << hits[i] end end self.hits = save persist end |