Class: Sunniesnow::Charter::TipPointStart

Inherits:
Object
  • Object
show all
Defined in:
lib/sscharter.rb

Instance Method Summary collapse

Constructor Details

#initialize(x = 0, y = 0, relative_time = 0.0, relative: true, speed: nil, relative_beat: nil, beat_speed: nil) ⇒ TipPointStart

Returns a new instance of TipPointStart.



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/sscharter.rb', line 223

def initialize x = 0, y = 0, relative_time = 0.0, relative: true, speed: nil,
    relative_beat: nil, beat_speed: nil
  @x = x
  @y = y
  @relative_time = relative_time
  @relative = relative
  @speed = speed
  @relative_beat = relative_beat
  @beat_speed = beat_speed
  check
end

Instance Method Details

#checkObject



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/sscharter.rb', line 235

def check
  if !@x.is_a?(Numeric) || !@y.is_a?(Numeric)
    raise ArgumentError, 'x and y must be numbers'
  end
  @x = @x.to_f
  @y = @y.to_f
  i[@relative_time @speed @relative_beat @beat_speed].each do |key|
    value = instance_variable_get key
    raise ArgumentError, "cannot specify both #@time_key and #{key}" if @time_key && value&.!=(0)
    @time_key = key if value&.!=(0)
  end
  @time_key ||= :@relative_time
end

#get_start_placeholder(start_event) ⇒ Object

Raises:

  • (ArgumentError)


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/sscharter.rb', line 249

def get_start_placeholder start_event
  raise ArgumentError, "start_event is not tip-pointable" unless start_event.tip_pointable?
  result = Event.new :placeholder, start_event.beat, start_event.bpm_changes
  if @relative
    result[:x] = start_event[:x] + @x
    result[:y] = start_event[:y] + @y
  else
    result[:x] = @x
    result[:y] = @y
  end
  case @time_key
  when :@relative_time
    raise ArgumentError, "relative_time must be a number" unless @relative_time.is_a? Numeric
    raise ArgumentError, "relative_time must be non-negative" if @relative_time < 0
    result.offset = -@relative_time.to_f
  when :@speed
    raise ArgumentError, "speed must be a number" unless @speed.is_a? Numeric
    raise ArgumentError, "speed must be positive" if @speed <= 0
    result.offset = -Math.hypot(result[:x] - start_event[:x], result[:y] - start_event[:y]) / @speed
  when :@relative_beat
    raise ArgumentError, "relative_beat must be a number" unless @relative_beat.is_a? Numeric
    raise ArgumentError, "relative_beat must be non-negative" if @relative_beat < 0
    warn "Rational is recommended over Float for relative_beat" if @relative_beat.is_a? Float
    result.beat -= @relative_beat.to_r
  when :@beat_speed
    raise ArgumentError, "beat_speed must be a number" unless @beat_speed.is_a? Numeric
    raise ArgumentError, "beat_speed must be positive" if @beat_speed <= 0
    delta_beat = Math.hypot(result[:x] - start_event[:x], result[:y] - start_event[:y]) / @beat_speed
    result.beat -= delta_beat.to_r # a little weird, but fine
  end
  result[:tip_point] = start_event[:tip_point]
  result
end