ehb_game_lib 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e2dd328f0f04eb106f0d5583057e3327c7e2ed15
4
+ data.tar.gz: 635f13624ba391b24b80a9403ae987db25d53d80
5
+ SHA512:
6
+ metadata.gz: 95094355b73f2229f9c976314f12b2ae364f9640e05e32e289bf05731b2bd736382ed508af0876be125d9953dc4eead42e4b3ffda5f2ea503474a682e536334c
7
+ data.tar.gz: 763dd1a5d99ee835996d3faf697c7668c0bb95fbf6b612794116aa7e0dd5673a0f1907ac44fdb2ca0222896ac130a3a3f36ee88b7745ece671c1f4091ea21d13
@@ -0,0 +1,24 @@
1
+ require 'active_support'
2
+ require 'active_support/core_ext'
3
+ require 'chingu'
4
+ require 'ehb_game_lib/version'
5
+ require 'ehb_game_lib/globals'
6
+ require 'ehb_game_lib/gfx'
7
+ require 'ehb_game_lib/math/circle'
8
+ require 'ehb_game_lib/math/line'
9
+ require 'ehb_game_lib/math/line_segment'
10
+ require 'ehb_game_lib/math/quadratic_equation'
11
+ require 'ehb_game_lib/math/rectable_object'
12
+ require 'ehb_game_lib/math/vector'
13
+ require 'ehb_game_lib/math/intersection/circle_line'
14
+ require 'ehb_game_lib/math/intersection/circle_line_segment'
15
+ require 'ehb_game_lib/nes/all_colors_palette'
16
+ require 'ehb_game_lib/nes/color'
17
+ require 'ehb_game_lib/nes/palette'
18
+ require 'ehb_game_lib/patches/chingu/basic_game_object'
19
+ require 'ehb_game_lib/patches/gosu/font'
20
+ require 'ehb_game_lib/traits/bounding_line_segment'
21
+ require 'ehb_game_lib/utils/cursor'
22
+
23
+ module EhbGameLib
24
+ end
@@ -0,0 +1,36 @@
1
+ module EhbGameLib
2
+ class Gfx
3
+ class << self
4
+ def fill_circle(cx, cy, r, color, z)
5
+ x1 = 0
6
+ y1 = -r
7
+ circ = 2 * ::Math::PI * r
8
+ step = 360 / circ
9
+ step.step(45, step) do |a|
10
+ x2 = Gosu.offset_x(a, r)
11
+ y2 = Gosu.offset_y(a, r)
12
+ EhbGameLib::Globals.window.draw_quad \
13
+ cx + x1, cy + y1, color, cx + x2, cy + y2, color,
14
+ cx - x2, cy + y2, color, cx - x1, cy + y1, color, z
15
+ EhbGameLib::Globals.window.draw_quad \
16
+ cx - x1, cy - y1, color, cx - x2, cy - y2, color,
17
+ cx + x2, cy - y2, color, cx + x1, cy - y1, color, z
18
+ EhbGameLib::Globals.window.draw_quad \
19
+ cx + y1, cy + x1, color, cx + y2, cy + x2, color,
20
+ cx - y2, cy + x2, color, cx - y1, cy + x1, color, z
21
+ EhbGameLib::Globals.window.draw_quad \
22
+ cx - y1, cy - x1, color, cx - y2, cy - x2, color,
23
+ cx + y2, cy - x2, color, cx + y1, cy - x1, color, z
24
+ x1 = x2
25
+ y1 = y2
26
+ end
27
+ EhbGameLib::Globals.window.draw_quad \
28
+ cx + x1, cy + y1, color, cx - y1, cy - x1, color,
29
+ cx + y1, cy - x1, color, cx - x1, cy + y1, color, z
30
+ EhbGameLib::Globals.window.draw_quad \
31
+ cx - x1, cy - y1, color, cx + y1, cy + x1, color,
32
+ cx - y1, cy + x1, color, cx + x1, cy - y1, color, z
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,10 @@
1
+ module EhbGameLib
2
+ # No-global variables bridge to Chingu global variables.
3
+ class Globals
4
+ class << self
5
+ def window
6
+ $window
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,38 @@
1
+ module EhbGameLib
2
+ module Math
3
+ class Circle
4
+ attr_reader :center, :radius
5
+
6
+ def initialize(cx, cy, radius)
7
+ @center = Vector.new(cx, cy)
8
+ @radius = radius
9
+ end
10
+
11
+ def to_s
12
+ "C: #{center}, R: #{radius}"
13
+ end
14
+
15
+ def ys(x)
16
+ QuadraticEquation.new(1, -2 * cy, ys_c(x)).roots
17
+ end
18
+
19
+ private
20
+
21
+ def ys_c(x)
22
+ x.abs2 - 2 * x * cx + cx.abs2 + cy.abs2 - r.abs2
23
+ end
24
+
25
+ def cx
26
+ center.x
27
+ end
28
+
29
+ def cy
30
+ center.y
31
+ end
32
+
33
+ def r
34
+ radius
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,76 @@
1
+ module EhbGameLib
2
+ module Math
3
+ module Intersection
4
+ class CircleLine
5
+ def initialize(circle, line)
6
+ @circle = circle
7
+ @line = line
8
+ end
9
+
10
+ def intersection
11
+ @intersection ||= intersection_uncached
12
+ end
13
+
14
+ def intersect?
15
+ !intersection.empty?
16
+ end
17
+
18
+ private
19
+
20
+ def intersection_uncached
21
+ xs.flat_map do |x|
22
+ @circle.ys(x).map { |y| Vector.new(x, y) }
23
+ end
24
+ end
25
+
26
+ def xs
27
+ QuadraticEquation.new(xs_a, xs_b, xs_c).roots
28
+ end
29
+
30
+ def xs_a
31
+ b.abs2 + a.abs2
32
+ end
33
+
34
+ def xs_b
35
+ -2 * cx * b.abs2 + 2 * a * (c + cy * b)
36
+ end
37
+
38
+ def xs_c
39
+ xs_c0 + xs_c1
40
+ end
41
+
42
+ def xs_c0
43
+ b.abs2 * (cx.abs2 + cy.abs2 - r.abs2)
44
+ end
45
+
46
+ def xs_c1
47
+ c * (c + 2 * cy * b)
48
+ end
49
+
50
+ def a
51
+ @line.a
52
+ end
53
+
54
+ def b
55
+ @line.b
56
+ end
57
+
58
+ def c
59
+ @line.c
60
+ end
61
+
62
+ def r
63
+ @circle.radius
64
+ end
65
+
66
+ def cx
67
+ @circle.center.x
68
+ end
69
+
70
+ def cy
71
+ @circle.center.y
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,24 @@
1
+ module EhbGameLib
2
+ module Math
3
+ module Intersection
4
+ class CircleLineSegment
5
+ def initialize(circle, line_segment)
6
+ @circle = circle
7
+ @line_segment = line_segment
8
+ end
9
+
10
+ def intersection
11
+ circle_line.intersection.select do |point|
12
+ @line_segment.point_in_area?(point)
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def circle_line
19
+ @circle_line ||= CircleLine.new(@circle, @line_segment.line)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ module EhbGameLib
2
+ module Math
3
+ class Line
4
+ class << self
5
+ def new_by_coordinates(x0, y0, x1, y1)
6
+ new(
7
+ y1 - y0,
8
+ x0 - x1,
9
+ y0 * x1 + x0 * y0 - x0 * y1 - y0 * x0
10
+ )
11
+ end
12
+ end
13
+
14
+ attr_reader :a, :b, :c
15
+
16
+ def initialize(a, b, c)
17
+ @a = a
18
+ @b = b
19
+ @c = c
20
+ end
21
+
22
+ def point_in?(vector)
23
+ (vector.x * a + vector.y * b + c).zero?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ module EhbGameLib
2
+ module Math
3
+ class LineSegment
4
+ attr_reader :p0, :p1
5
+
6
+ def initialize(x0, y0, x1, y1)
7
+ @p0 = Vector.new(x0, y0)
8
+ @p1 = Vector.new(x1, y1)
9
+ end
10
+
11
+ def line
12
+ @line ||= Line.new_by_coordinates(p0.x, p0.y, p1.x, p1.y)
13
+ end
14
+
15
+ def point_in_area?(vector)
16
+ x_interval.include?(vector.x) && y_interval.include?(vector.y)
17
+ end
18
+
19
+ private
20
+
21
+ def x_interval
22
+ @x_interval ||= interval(p0.x, p1.x)
23
+ end
24
+
25
+ def y_interval
26
+ @y_interval ||= interval(p0.y, p1.y)
27
+ end
28
+
29
+ def interval(c0, c1)
30
+ i = [c0, c1].sort
31
+ (i[0]..i[1])
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,28 @@
1
+ module EhbGameLib
2
+ module Math
3
+ class QuadraticEquation
4
+ attr_reader :a, :b, :c
5
+
6
+ def initialize(a, b, c)
7
+ @a = a
8
+ @b = b
9
+ @c = c
10
+ end
11
+
12
+ def roots
13
+ return [root(1), root(-1)] if delta > 0.0
14
+ return [root(1)] if delta == 0.0
15
+ []
16
+ end
17
+
18
+ def delta
19
+ @delta ||= b**2 - 4 * a * c
20
+ end
21
+
22
+ def root(signal)
23
+ signal = signal < 0 ? -1 : 1
24
+ (-b + signal * ::Math.sqrt(delta)) / (2 * a)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,98 @@
1
+ module EhbGameLib
2
+ module Math
3
+ module RectableObject
4
+ H_AXIS = { name: :ax, coord: :x, size: :width, floor: :left, mean: :center,
5
+ ceil: :right }.freeze
6
+ V_AXIS = { name: :ay, coord: :y, size: :height, floor: :top, mean: :middle,
7
+ ceil: :bottom }.freeze
8
+
9
+ [H_AXIS, V_AXIS].each do |a|
10
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
11
+ def #{a[:name]}
12
+ @#{a[:name]} ||= Axis.new(
13
+ -> { #{a[:coord]} },
14
+ -> (v) { self.#{a[:coord]} = v },
15
+ -> { #{a[:size]} }
16
+ )
17
+ end
18
+ RUBY_EVAL
19
+
20
+ %i[floor mean ceil].each do |m|
21
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
22
+ def #{a[m]}
23
+ #{a[:name]}.#{m}
24
+ end
25
+
26
+ def #{a[m]}=(v)
27
+ #{a[:name]}.#{m} = v
28
+ end
29
+ RUBY_EVAL
30
+ end
31
+ end
32
+
33
+ def rect
34
+ ::Chingu::Rect.new(left, top, width, height)
35
+ end
36
+
37
+ class Axis
38
+ attr_accessor :anchor
39
+
40
+ def initialize(coord_get, coord_set, size_get)
41
+ @coord_get = coord_get
42
+ @coord_set = coord_set
43
+ @size_get = size_get
44
+ end
45
+
46
+ def coord
47
+ @coord_get.call
48
+ end
49
+
50
+ def coord=(v)
51
+ @coord_set.call(v)
52
+ end
53
+
54
+ def size
55
+ @size_get.call
56
+ end
57
+
58
+ %w[floor mean ceil].each do |m|
59
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
60
+ def #{m}
61
+ coord + #{m}_delta
62
+ end
63
+
64
+ def #{m}=(v)
65
+ self.coord = v - #{m}_delta
66
+ end
67
+ RUBY_EVAL
68
+ end
69
+
70
+ private
71
+
72
+ def floor_delta
73
+ case anchor
74
+ when :mean then - size / 2
75
+ when :ceil then - size
76
+ else 0
77
+ end
78
+ end
79
+
80
+ def mean_delta
81
+ case anchor
82
+ when :mean then 0
83
+ when :ceil then - size / 2
84
+ else size / 2
85
+ end
86
+ end
87
+
88
+ def ceil_delta
89
+ case anchor
90
+ when :mean then size / 2
91
+ when :ceil then 0
92
+ else size
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
98
+ end
@@ -0,0 +1,52 @@
1
+ module EhbGameLib
2
+ module Math
3
+ class Vector
4
+ attr_reader :x, :y
5
+
6
+ def initialize(x, y)
7
+ @x = x
8
+ @y = y
9
+ end
10
+
11
+ def to_s
12
+ "#{x}, #{y}"
13
+ end
14
+
15
+ def equal?(other)
16
+ x == other.x && y == other.y
17
+ end
18
+
19
+ def ==(other)
20
+ equal?(other)
21
+ end
22
+
23
+ def +(other)
24
+ self.class.new(x + other.x, y + other.y)
25
+ end
26
+
27
+ def -(other)
28
+ self.class.new(x - other.x, y - other.y)
29
+ end
30
+
31
+ def *(other)
32
+ self.class.new(x * other, y * other)
33
+ end
34
+
35
+ def /(other)
36
+ self.class.new(x / other, y / other)
37
+ end
38
+
39
+ def dot(other)
40
+ x * other.x + y * other.y
41
+ end
42
+
43
+ def length
44
+ @length ||= ::Math.sqrt(x * x + y * y)
45
+ end
46
+
47
+ def normalize
48
+ self / length
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,29 @@
1
+ module EhbGameLib
2
+ module Nes
3
+ class AllColorsPalette
4
+ def initialize(pal_file)
5
+ @colors = []
6
+ File.open(pal_file) do |file|
7
+ while (b = file.read(3))
8
+ b = b.bytes.to_a
9
+ @colors << Gosu::Color.rgba(b[0], b[1], b[2], 0xFF)
10
+ end
11
+ end
12
+ @colors.freeze
13
+ end
14
+
15
+ def gosu_color(i)
16
+ @colors[i]
17
+ end
18
+
19
+ def draw(x, y)
20
+ s = 32
21
+ @colors.each_with_index do |c, i|
22
+ cx = x + (i % 16) * s
23
+ cy = y + (i / 16) * s
24
+ Gosu.draw_rect(cx, cy, s, s, c)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ module EhbGameLib
2
+ module Nes
3
+ class Color
4
+ attr_accessor :color_index
5
+
6
+ BLACK = 0x0F
7
+ YELLOW = 0x28
8
+ RED = 0x05
9
+ WHITE = 0x30
10
+
11
+ def initialize(all_colors, color_index)
12
+ @all_colors = all_colors
13
+ @color_index = color_index
14
+ end
15
+
16
+ def gosu_color
17
+ @all_colors.gosu_color(color_index)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,19 @@
1
+ module EhbGameLib
2
+ module Nes
3
+ class Palette
4
+ COLORS_COUNT = 16
5
+
6
+ def initialize(all_colors)
7
+ @colors = Array.new(COLORS_COUNT) { |i| ::EhbGameLib::Nes::Color.new(all_colors, i) }
8
+ end
9
+
10
+ def [](index)
11
+ @colors[index % @colors.length]
12
+ end
13
+
14
+ def []=(index, value)
15
+ @colors[index % @colors.length].color_index = value
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module EhbGameLib
2
+ module Patches
3
+ module Chingu
4
+ module BasicGameObject
5
+ def destroy
6
+ super
7
+ @alive = false
8
+ end
9
+
10
+ def alive?
11
+ @alive.nil? ? true : false
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ unless ::Chingu::BasicGameObject.included_modules.include?(
19
+ ::EhbGameLib::Patches::Chingu::BasicGameObject
20
+ )
21
+ ::Chingu::BasicGameObject.prepend(::EhbGameLib::Patches::Chingu::BasicGameObject)
22
+ end
@@ -0,0 +1,29 @@
1
+ module EhbGameLib
2
+ module Patches
3
+ module Gosu
4
+ module Font
5
+ # Draw text with line breaks.
6
+ def drawm(text, x, y, z, scale_x = 1, scale_y = 1, color = 0xff_ffffff, mode = :default)
7
+ text.each_line do |line|
8
+ draw(line.delete("\n"), x, y, z, scale_x, scale_y, color, mode)
9
+ y += height
10
+ end
11
+ end
12
+
13
+ def text_height(text)
14
+ text.lines.count * height
15
+ end
16
+
17
+ def text_width(text)
18
+ text.lines.map { |l| super(l) }.max
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ unless ::Gosu::Font.included_modules.include?(
26
+ ::EhbGameLib::Patches::Gosu::Font
27
+ )
28
+ ::Gosu::Font.prepend(::EhbGameLib::Patches::Gosu::Font)
29
+ end
@@ -0,0 +1,56 @@
1
+ module EhbGameLib
2
+ module Traits
3
+ # Interface required:
4
+ # attr_reader :last_x, :last_y
5
+ module BoundingLineSegment
6
+ def nearest_circle_collision(klasses)
7
+ return [nil, nil] unless line_segment?
8
+ min_obj = min_p = min_dist = nil
9
+ each_circle_collision(klasses) do |object, p|
10
+ dist = Gosu.distance(last_x, last_y, p.x, p.y)
11
+ next unless min_dist.nil? || dist < min_dist
12
+ min_dist = dist
13
+ min_obj = object
14
+ min_p = p
15
+ end
16
+ [min_obj, min_p]
17
+ end
18
+
19
+ private
20
+
21
+ def each_circle_collision(klasses)
22
+ each_game_object(klasses) do |object|
23
+ object_intersection(object).intersection.each do |p|
24
+ yield(object, p)
25
+ end
26
+ end
27
+ end
28
+
29
+ def each_game_object(klasses)
30
+ Array(klasses).each do |klass|
31
+ (klass.respond_to?(:all) ? klass.all : Array(klass)).each do |object|
32
+ yield(object)
33
+ end
34
+ end
35
+ end
36
+
37
+ def line_segment?
38
+ last_x.nil? || last_y.nil? || x.nil? || y.nil? ? false : true
39
+ end
40
+
41
+ def line_segment
42
+ ::EhbGameLib::Math::LineSegment.new(last_x, last_y, x, y)
43
+ end
44
+
45
+ def object_circle(object)
46
+ ::EhbGameLib::Math::Circle.new(object.x, object.y, object.radius)
47
+ end
48
+
49
+ def object_intersection(object)
50
+ ::EhbGameLib::Math::Intersection::CircleLineSegment.new(
51
+ object_circle(object), line_segment
52
+ )
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,58 @@
1
+ module EhbGameLib
2
+ module Utils
3
+ class Cursor
4
+ def initialize(set)
5
+ @set = set.to_a
6
+ @index = nil
7
+ end
8
+
9
+ delegate :count, :each, :inject, :map, to: :set
10
+
11
+ def select_last
12
+ self.index = @set.length - 1
13
+ end
14
+
15
+ def next_element
16
+ next_previous_element(1)
17
+ end
18
+
19
+ def previous_element
20
+ next_previous_element(-1)
21
+ end
22
+
23
+ def current
24
+ index.nil? ? nil : @set[index]
25
+ end
26
+
27
+ def index
28
+ fix_index
29
+ @index
30
+ end
31
+
32
+ private
33
+
34
+ def fix_index
35
+ if @set.empty?
36
+ @index = nil
37
+ elsif @index.nil?
38
+ @index = 0
39
+ elsif @index >= @set.count
40
+ @index = @set.count - 1
41
+ elsif @index < 0
42
+ @index = 0
43
+ end
44
+ end
45
+
46
+ def index=(value)
47
+ @index = value
48
+ fix_index
49
+ end
50
+
51
+ attr_reader :set
52
+
53
+ def next_previous_element(steps)
54
+ self.index = (index + steps) % @set.count
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module EhbGameLib
2
+ VERSION = '0.0.5'.freeze
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ class CircleTest < Test::Unit::TestCase
6
+ test 'ys' do
7
+ circle = Circle.new(0, 0, 1)
8
+ assert_equal [], circle.ys(-1.1)
9
+ assert_equal [0], circle.ys(-1)
10
+ assert_equal [1, -1], circle.ys(0)
11
+ assert_equal [0], circle.ys(1)
12
+ assert_equal [], circle.ys(1.1)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ module Intersection
6
+ class CircleLineSegmentTest < Test::Unit::TestCase
7
+ test 'intersect' do
8
+ @circle = Circle.new(0, 0, 1)
9
+ assert_equal [], intersection(-1.1, -1, -1.1, 1)
10
+ assert_equal [Vector.new(-1.0, 0)], intersection(-1.0, -1, -1.0, 1)
11
+ assert_equal [Vector.new(0, -1)], intersection(0, -1, 0, 0)
12
+ assert_equal [Vector.new(0, 1), Vector.new(0, -1)], intersection(0, -1, 0, 1)
13
+ assert_equal [Vector.new(1, 0), Vector.new(-1, 0)], intersection(-2, 0, 2, 0)
14
+
15
+ @circle = Circle.new(320, 120, 12)
16
+ assert_equal [Vector.new(320, 132), Vector.new(320, 108)],
17
+ intersection(320.0, 215.0, 320.0, -4785.0)
18
+
19
+ @circle = Circle.new(320, 240, 12)
20
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
21
+ intersection(100, 240, 500, 240)
22
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
23
+ intersection(455.0, 240, -4545.0, 240)
24
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
25
+ intersection(455.0, 240.00000000012759, -4545.0, 239.9999999235764)
26
+ end
27
+
28
+ private
29
+
30
+ def intersection(seg_x0, seg_y0, seg_x1, seg_y1)
31
+ CircleLineSegment.new(
32
+ @circle,
33
+ ::EhbGameLib::Math::LineSegment.new(seg_x0, seg_y0, seg_x1, seg_y1)
34
+ ).intersection
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ module Intersection
6
+ class CircleLineTest < Test::Unit::TestCase
7
+ test 'intersect' do
8
+ @circle = Circle.new(0, 0, 1)
9
+ assert_equal [], intersection(-1.1, -1, -1.1, 1)
10
+ assert_equal [Vector.new(-1.0, 0)], intersection(-1.0, -1, -1.0, 1)
11
+ assert_equal [Vector.new(0, 1), Vector.new(0, -1)], intersection(0, -1, 0, 0)
12
+ assert_equal [Vector.new(0, 1), Vector.new(0, -1)], intersection(0, -1, 0, 1)
13
+ assert_equal [Vector.new(1, 0), Vector.new(-1, 0)], intersection(-2, 0, 2, 0)
14
+
15
+ @circle = Circle.new(320, 120, 12)
16
+ assert_equal [Vector.new(320, 132), Vector.new(320, 108)],
17
+ intersection(320.0, 215.0, 320.0, -4785.0)
18
+
19
+ @circle = Circle.new(320, 240, 12)
20
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
21
+ intersection(100, 240, 500, 240)
22
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
23
+ intersection(455.0, 240, -4545.0, 240)
24
+ assert_equal [Vector.new(332, 240), Vector.new(308, 240)],
25
+ intersection(455.0, 240.00000000012759, -4545.0, 239.9999999235764)
26
+ end
27
+
28
+ private
29
+
30
+ def intersection(seg_x0, seg_y0, seg_x1, seg_y1)
31
+ CircleLine.new(@circle, line(seg_x0, seg_y0, seg_x1, seg_y1)).intersection
32
+ end
33
+
34
+ def line(seg_x0, seg_y0, seg_x1, seg_y1)
35
+ ::EhbGameLib::Math::Line.new_by_coordinates(seg_x0, seg_y0, seg_x1, seg_y1)
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,27 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ class LineSegmentTest < Test::Unit::TestCase
6
+ test 'point_in_area?' do
7
+ line = LineSegment.new(-1, -1, -1, 1)
8
+ assert_equal true, line.point_in_area?(Vector.new(-1, -1))
9
+ assert_equal true, line.point_in_area?(Vector.new(-1, 1))
10
+ assert_equal true, line.point_in_area?(Vector.new(-1, 0))
11
+ assert_equal false, line.point_in_area?(Vector.new(-1, 2))
12
+ assert_equal false, line.point_in_area?(Vector.new(-1.1, 2))
13
+
14
+ line = LineSegment.new(-1, -1, 1, -1)
15
+ assert_equal true, line.point_in_area?(Vector.new(-1, -1))
16
+ assert_equal true, line.point_in_area?(Vector.new(1, -1))
17
+ assert_equal true, line.point_in_area?(Vector.new(0, -1))
18
+ assert_equal false, line.point_in_area?(Vector.new(2, -1))
19
+ assert_equal false, line.point_in_area?(Vector.new(2, -1.1))
20
+
21
+ line = LineSegment.new(455.0, 240.00000000012759, -4545.0, 239.9999999235764)
22
+ assert_equal true, line.point_in_area?(Vector.new(332.0, 240.0))
23
+ assert_equal true, line.point_in_area?(Vector.new(308.0, 240.0))
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ class LineTest < Test::Unit::TestCase
6
+ test 'new_by_coordinates' do
7
+ l = Line.new_by_coordinates(100, 240, 500, 240)
8
+ assert_equal(0, l.a)
9
+ assert_equal(-400, l.b)
10
+ assert_equal(96_000, l.c)
11
+
12
+ l = Line.new_by_coordinates(455.0, 240, -4545.0, 240)
13
+ assert_equal(0, l.a)
14
+ assert_equal(5000, l.b)
15
+ assert_equal(-1_200_000, l.c)
16
+
17
+ l = Line.new_by_coordinates(455.0, 240.00000000012759, -4545.0, 239.9999999235764)
18
+ assert_equal(-7.65511742883973e-08, l.a)
19
+ assert_equal(5000, l.b)
20
+ assert_equal(-1_199_999.9999658072, l.c)
21
+ end
22
+
23
+ test 'point_in?' do
24
+ line = Line.new_by_coordinates(-1, -1, -1, 1)
25
+ assert_equal true, line.point_in?(Vector.new(-1, -1))
26
+ assert_equal true, line.point_in?(Vector.new(-1, 1))
27
+ assert_equal true, line.point_in?(Vector.new(-1, 0))
28
+ assert_equal true, line.point_in?(Vector.new(-1, 2))
29
+ assert_equal false, line.point_in?(Vector.new(-1.1, 2))
30
+
31
+ line = Line.new_by_coordinates(-1, -1, 1, -1)
32
+ assert_equal true, line.point_in?(Vector.new(-1, -1))
33
+ assert_equal true, line.point_in?(Vector.new(1, -1))
34
+ assert_equal true, line.point_in?(Vector.new(0, -1))
35
+ assert_equal true, line.point_in?(Vector.new(2, -1))
36
+ assert_equal false, line.point_in?(Vector.new(2, -1.1))
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ class QuadraticEquationTest < Test::Unit::TestCase
6
+ test 'delta' do
7
+ assert_equal(-8, QuadraticEquation.new(1, 2, 3).delta)
8
+ assert_equal(0, QuadraticEquation.new(1, 8, 16).delta)
9
+ assert_equal(16, QuadraticEquation.new(1, 2, -3).delta)
10
+ end
11
+
12
+ test 'roots' do
13
+ assert_equal([], QuadraticEquation.new(1, 2, 3).roots)
14
+ assert_equal([-4], QuadraticEquation.new(1, 8, 16).roots)
15
+ assert_equal([1, -3], QuadraticEquation.new(1, 2, -3).roots)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,77 @@
1
+ require 'test_helper'
2
+
3
+ module EhbGameLib
4
+ module Math
5
+ class RectableObjectTest < Test::Unit::TestCase
6
+ class Stub
7
+ include ::EhbGameLib::Math::RectableObject
8
+
9
+ attr_accessor :x, :y
10
+
11
+ def width
12
+ 10
13
+ end
14
+
15
+ def height
16
+ 10
17
+ end
18
+ end
19
+
20
+ setup do
21
+ @o = Stub.new
22
+ end
23
+
24
+ test 'x anchor' do
25
+ @o.x = 20
26
+ assert_x_values(20, 20, 25, 30)
27
+
28
+ @o.ax.anchor = :mean
29
+ assert_x_values(20, 15, 20, 25)
30
+
31
+ @o.ax.anchor = :ceil
32
+ assert_x_values(20, 10, 15, 20)
33
+ end
34
+
35
+ test 'y anchor' do
36
+ @o.y = 20
37
+ assert_y_values(20, 20, 25, 30)
38
+
39
+ @o.ay.anchor = :mean
40
+ assert_y_values(20, 15, 20, 25)
41
+
42
+ @o.ay.anchor = :ceil
43
+ assert_y_values(20, 10, 15, 20)
44
+ end
45
+
46
+ test 'left set' do
47
+ @o.ax.anchor = :floor
48
+ @o.left = 20
49
+ assert_x_values(20, 20, 25, 30)
50
+
51
+ @o.ax.anchor = :mean
52
+ @o.left = 30
53
+ assert_x_values(35, 30, 35, 40)
54
+
55
+ @o.ax.anchor = :ceil
56
+ @o.left = 40
57
+ assert_x_values(50, 40, 45, 50)
58
+ end
59
+
60
+ private
61
+
62
+ def assert_x_values(x, left, center, right)
63
+ assert_equal x, @o.x, 'x'
64
+ assert_equal left, @o.left, 'left'
65
+ assert_equal center, @o.center, 'center'
66
+ assert_equal right, @o.right, 'right'
67
+ end
68
+
69
+ def assert_y_values(y, top, middle, bottom)
70
+ assert_equal y, @o.y, 'y'
71
+ assert_equal top, @o.top, 'top'
72
+ assert_equal middle, @o.middle, 'middle'
73
+ assert_equal bottom, @o.bottom, 'bottom'
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,2 @@
1
+ require 'ehb_game_lib'
2
+ require 'test/unit'
metadata ADDED
@@ -0,0 +1,154 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ehb_game_lib
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - Eduardo H. Bogoni
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: chingu
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.8.1
41
+ - !ruby/object:Gem::Dependency
42
+ name: gosu
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.13.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.13.1
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '='
74
+ - !ruby/object:Gem::Version
75
+ version: 0.42.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '='
81
+ - !ruby/object:Gem::Version
82
+ version: 0.42.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description:
98
+ email:
99
+ - eduardobogoni@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - lib/ehb_game_lib.rb
105
+ - lib/ehb_game_lib/gfx.rb
106
+ - lib/ehb_game_lib/globals.rb
107
+ - lib/ehb_game_lib/math/circle.rb
108
+ - lib/ehb_game_lib/math/intersection/circle_line.rb
109
+ - lib/ehb_game_lib/math/intersection/circle_line_segment.rb
110
+ - lib/ehb_game_lib/math/line.rb
111
+ - lib/ehb_game_lib/math/line_segment.rb
112
+ - lib/ehb_game_lib/math/quadratic_equation.rb
113
+ - lib/ehb_game_lib/math/rectable_object.rb
114
+ - lib/ehb_game_lib/math/vector.rb
115
+ - lib/ehb_game_lib/nes/all_colors_palette.rb
116
+ - lib/ehb_game_lib/nes/color.rb
117
+ - lib/ehb_game_lib/nes/palette.rb
118
+ - lib/ehb_game_lib/patches/chingu/basic_game_object.rb
119
+ - lib/ehb_game_lib/patches/gosu/font.rb
120
+ - lib/ehb_game_lib/traits/bounding_line_segment.rb
121
+ - lib/ehb_game_lib/utils/cursor.rb
122
+ - lib/ehb_game_lib/version.rb
123
+ - test/lib/ehb_game_lib/math/circle_test.rb
124
+ - test/lib/ehb_game_lib/math/intersection/circle_line_segment_test.rb
125
+ - test/lib/ehb_game_lib/math/intersection/circle_line_test.rb
126
+ - test/lib/ehb_game_lib/math/line_segment_test.rb
127
+ - test/lib/ehb_game_lib/math/line_test.rb
128
+ - test/lib/ehb_game_lib/math/quadratic_equation_test.rb
129
+ - test/lib/ehb_game_lib/math/rectable_object_test.rb
130
+ - test/test_helper.rb
131
+ homepage:
132
+ licenses: []
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.6.14
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: EHB's game library for Ruby
154
+ test_files: []