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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
# coding: utf-8
# Copyright 2019 DragonRuby LLC
# MIT License
# numeric.rb has been released under MIT (*only this file*).
class Numeric
include ValueType
include NumericDeprecated
alias_method :gt, :>
alias_method :gte, :>=
alias_method :lt, :<
alias_method :lte, :<=
alias_method :__original_eq_eq__, :== unless Numeric.instance_methods.include? :__original_eq_eq__
def to_layout_row opts = {}
$layout.rect(row: self,
col: opts.col || 0,
w: opts.w || 0,
h: opts.h || 0).y
end
def to_layout_col opts = {}
$layout.rect(row: 0,
col: self,
w: opts.w || 0,
h: opts.h || 0).x
end
def to_layout_w
$layout.rect(row: 0, col: 0, w: self, h: 1).w
end
def to_layout_h
$layout.rect(row: 0, col: 0, w: 1, h: self).h
end
def to_layout_row_from_bottom opts = {}
($layout.row_max_index - self).to_layout_row opts
end
def to_layout_col_from_right opts = {}
($layout.col_max_index - self).to_layout_col opts
end
# Converts a numeric value representing seconds into frames.
#
# @gtk
def seconds
self * 60
end
# Divides the number by `2.0` and returns a `float`.
#
# @gtk
def half
self / 2.0
end
def third
self / 3.0
end
def quarter
self / 4.0
end
def to_byte
clamp(0, 255).to_i
end
def clamp *opts
min = (opts.at 0)
max = (opts.at 1)
return min if min && self < min
return max if max && self > max
return self
end
def clamp_wrap min, max
max, min = min, max if min > max
return self if self >= min && self <= max
return min if min == max
if self < min
overflow = min - self
return (max - overflow).clamp_wrap min, max
end
if self > max
overflow = self - max
return (min + overflow).clamp_wrap min, max
end
return self
end
def elapsed_time tick_count_override = nil
(tick_count_override || Kernel.tick_count) - self
end
def elapsed_time_percent duration
elapsed_time.percentage_of duration
end
def new?
elapsed_time == 0
end
# Returns `true` if the numeric value has passed a duration/offset number.
# `Kernel.tick_count` is used to determine if a number represents an elapsed
# moment in time.
#
# @gtk
def elapsed? offset = 0, tick_count_override = Kernel.tick_count
(self + offset) < tick_count_override
end
def frame_index *opts
frame_count_or_hash, hold_for, repeat, tick_count_override = opts
if frame_count_or_hash.is_a? Hash
frame_count = frame_count_or_hash[:count]
hold_for = frame_count_or_hash[:hold_for]
repeat = frame_count_or_hash[:repeat]
tick_count_override = frame_count_or_hash[:tick_count_override]
else
frame_count = frame_count_or_hash
end
tick_count_override ||= Kernel.tick_count
animation_frame_count = frame_count
animation_frame_hold_time = hold_for
animation_length = animation_frame_hold_time * animation_frame_count
return nil if Kernel.tick_count < self
if !repeat && (self + animation_length) < (tick_count_override - 1)
return nil
else
return self.elapsed_time.-(1).idiv(animation_frame_hold_time) % animation_frame_count
end
rescue Exception => e
raise <<-S
* ERROR:
#{opts}
#{e}
S
end
def zero?
self == 0
end
def zero
0
end
def one
1
end
def two
2
end
def five
5
end
def ten
10
end
alias_method :gt, :>
alias_method :above?, :>
alias_method :right_of?, :>
alias_method :lt, :<
alias_method :below?, :<
alias_method :left_of?, :<
def shift_right i
self + i
end
def shift_left i
shift_right(i * -1)
rescue Exception => e
raise_immediately e, :shift_left, i
end
def shift_up i
self + i
rescue Exception => e
raise_immediately e, :shift_up, i
end
def shift_down i
shift_up(i * -1)
rescue Exception => e
raise_immediately e, :shift_down, i
end
# This provides a way for a numeric value to be randomized based on a combination
# of two options: `:sign` and `:ratio`.
#
# @gtk
def randomize *definitions
result = self
if definitions.include?(:sign)
result = rand_sign
end
if definitions.include?(:ratio)
result = rand * result
elsif definitions.include?(:int)
result = (rand result)
end
result
end
def rand_sign
return self * -1 if rand > 0.5
self
end
def rand_ratio
self * rand
end
def remainder_of_divide n
mod n
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def ease_extended tick_count_override, duration, default_before, default_after, *definitions
GTK::Easing.ease_extended self,
tick_count_override,
self + duration,
default_before,
default_after,
*definitions
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def global_ease duration, *definitions
ease_extended Kernel.global_tick_count,
duration,
GTK::Easing.initial_value(*definitions),
GTK::Easing.final_value(*definitions),
*definitions
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def ease duration, *definitions
ease_extended Kernel.tick_count,
duration,
GTK::Easing.initial_value(*definitions),
GTK::Easing.final_value(*definitions),
*definitions
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def ease_spline_extended tick_count_override, duration, spline
GTK::Easing.ease_spline_extended self,
tick_count_override,
self + duration,
spline
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def global_ease_spline duration, spline
ease_spline_extended Kernel.global_tick_count,
duration,
spline
end
# Easing function progress/percentage for a specific point in time.
#
# @gtk
def ease_spline duration, spline
ease_spline_extended Kernel.tick_count,
duration,
spline
end
# Converts a number representing an angle in degrees to radians.
#
# @gtk
def to_radians
self * Math::PI.fdiv(180)
end
# Converts a number representing an angle in radians to degrees.
#
# @gtk
def to_degrees
self / Math::PI.fdiv(180)
end
# Given `self`, a rectangle primitive is returned.
#
# @example
# 5.to_square 100, 300 # returns [100, 300, 5, 5]
#
# @gtk
def to_square x, y, anchor_x = 0.5, anchor_y = nil
GTK::Geometry.to_square(self, x, y, anchor_x, anchor_y)
end
# Returns a normal vector for a number that represents an angle in degrees.
#
# @gtk
def vector max_value = 1
[vector_x(max_value), vector_y(max_value)]
end
# Returns the y component of a normal vector for a number that represents an angle in degrees.
#
# @gtk
def vector_y max_value = 1
max_value * Math.sin(self.to_radians)
end
# Returns the x component of a normal vector for a number that represents an angle in degrees.
#
# @gtk
def vector_x max_value = 1
max_value * Math.cos(self.to_radians)
end
def x_vector max_value = 1
vector_x max_value
end
def y_vector max_value = 1
vector_y max_value
end
def mod n
self % n
end
def mod_zero? *ns
ns.any? { |n| mod(n) == 0 }
end
def zmod? n
(self % n) == 0
end
def multiply n
self * n
end
def fmult n
self * n.to_f
end
def imult n
(self * n).to_i
end
def mult n
self * n
end
# @gtk
def fdiv n
self / n.to_f
end
# Divides `self` by a number `n` as a float, and converts it `to_i`.
#
# @gtk
def idiv n
(self / n.to_f).to_i
end
# Returns a numeric value that is a quantity `magnitude` closer to
#`self`. If the distance between `self` and `target` is less than
#the `magnitude` then `target` is returned.
#
# @gtk
def towards target, magnitude
return self if self == target
delta = (self - target).abs
return target if delta < magnitude
return self - magnitude if self > target
return self + magnitude
end
# Given `self` and a number representing `y` of a grid. This
# function will return a one dimensional array containing the value
# yielded by an implicit block.
#
# @example
# 3.map_with_ys 2 do |x, y|
# x * y
# end
# # x y x y x y x y x y x y
# # 0*0, 0*1 1*0 1*1 2*0 2*1
# # => [ 0, 0, 0, 1, 0, 2]
#
# @gtk
def map_with_ys ys, &block
self.times.flat_map do |x|
ys.map_with_index do |y|
yield x, y
end
end
rescue Exception => e
raise_immediately e, :map_with_ys, [self, ys]
end
def combinations other_int
self.numbers.product(other_int.numbers)
end
def percentage_of n
(self / n.to_f).cap_min_max(0, 1)
end
def cap i
return i if self > i
self
end
def cap_min_max min, max
return min if self < min
return max if self > max
self
end
def lesser other
return other if other < self
self
end
def greater other
return other if other > self
self
end
def subtract i
self - i
end
def minus i
self - i
end
def add i
self + i
end
def plus i
self + i
end
def numbers
(0..self).to_a
end
# @gtk
def map
unless block_given?
raise <<-S
* ERROR:
A block is required for Numeric#map.
S
end
self.to_i.times.map do
yield
end
end
def each
unless block_given?
raise <<-S
* ERROR:
A block is required for Numeric#each.
S
end
self.to_i.times do
yield
end
end
def times_with_index
unless block_given?
raise <<-S
* ERROR:
A block is required for Numeric#times_with_index.
S
end
self.to_i.times.with_index do |i|
yield i
end
end
def each_with_index
unless block_given?
raise <<-S
* ERROR:
A block is required for Numeric#each_with_index.
S
end
self.to_i.times.with_index do |i|
yield i
end
end
# @gtk
def map_with_index
unless block_given?
raise <<-S
* ERROR:
A block is required for Numeric#map.
S
end
self.to_i.times.map do |i|
yield i
end
end
def __raise_arithmetic_exception__ other, m, e
raise <<-S
* ERROR:
Attempted to invoke :#{m} on #{self} with the right hand argument of:
#{other}
The object above is not a Numeric.
#{e}
S
end
def serialize
self
end
def self.from_top n
return 720 - n unless $gtk
$gtk.args.grid.top - n
end
def from_top
Numeric.from_top self
end
def self.from_right n
return 1280 - n unless $gtk
$gtk.args.grid.right - n
end
def from_right
Numeric.from_right self
end
def self.clamp n, min, max
n.clamp min, max
end
def mid? l, r
(between? l, r) || (between? r, l)
end
end
class Fixnum
include ValueType
alias_method :__original_eq_eq__, :== unless Fixnum.instance_methods.include? :__original_eq_eq__
alias_method :__original_add__, :+ unless Fixnum.instance_methods.include? :__original_add__
alias_method :__original_subtract__, :- unless Fixnum.instance_methods.include? :__original_subtract__
alias_method :__original_multiply__, :* unless Fixnum.instance_methods.include? :__original_multiply__
alias_method :__original_divide__, :- unless Fixnum.instance_methods.include? :__original_divide__
# Returns `true` if the numeric value is evenly divisible by 2.
#
# @gtk
def even?
return (self % 2) == 0
end
# Returns `true` if the numeric value is *NOT* evenly divisible by 2.
#
# @gtk
def odd?
return !even?
end
# Returns `-1` if the number is less than `0`. `+1` if the number
# is greater than `0`. Returns `0` if the number is equal to `0`.
#
# @gtk
def sign
return -1 if self < 0
return 1 if self > 0
return 0
end
# Returns `true` if number is greater than `0`.
#
# @gtk
def pos?
sign > 0
end
# Returns `true` if number is less than `0`.
#
# @gtk
def neg?
sign < 0
end
# Returns the cosine of a represented in degrees (NOT radians).
#
# @gtk
def cos
Math.cos(self.to_radians)
end
# Returns the cosine of a represented in degrees (NOT radians).
#
# @gtk
def sin
Math.sin(self.to_radians)
end
def to_sf
"%.2f" % self
end
def ifloor int
(self.idiv int.to_i) * int.to_i
end
end
class Float
include ValueType
alias_method :__original_add__, :+ unless Float.instance_methods.include? :__original_add__
alias_method :__original_subtract__, :- unless Float.instance_methods.include? :__original_subtract__
alias_method :__original_multiply__, :* unless Float.instance_methods.include? :__original_multiply__
alias_method :__original_divide__, :- unless Float.instance_methods.include? :__original_divide__
def serialize
self
end
# @gtk
def sign
return -1 if self < 0
return 1 if self > 0
return 0
end
def replace_infinity scalar
return self if !scalar
return self unless self.infinite?
return -scalar if self < 0
return scalar if self > 0
end
def to_sf
"%.2f" % self
end
def ifloor int
(self.idiv int.to_i) * int.to_i
end
end
class Integer
alias_method :__original_round__, :round unless Integer.instance_methods.include? :__original_round__
alias_method :__original_add__, :+ unless Integer.instance_methods.include? :__original_add__
alias_method :__original_subtract__, :- unless Integer.instance_methods.include? :__original_subtract__
alias_method :__original_multiply__, :* unless Integer.instance_methods.include? :__original_multiply__
alias_method :__original_divide__, :- unless Integer.instance_methods.include? :__original_divide__
def round *args
__original_round__
end
def nan?
false
end
def center other
(self - other).abs.fdiv(2)
end
end
|