cyberarm_engine 0.13.0 → 0.13.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +8 -8
- data/.travis.yml +5 -5
- data/Gemfile +6 -6
- data/LICENSE.txt +21 -21
- data/README.md +73 -43
- data/Rakefile +10 -10
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/cyberarm_engine.gemspec +36 -36
- data/lib/cyberarm_engine.rb +49 -47
- data/lib/cyberarm_engine/animator.rb +53 -53
- data/lib/cyberarm_engine/background.rb +175 -175
- data/lib/cyberarm_engine/bounding_box.rb +149 -149
- data/lib/cyberarm_engine/common.rb +96 -96
- data/lib/cyberarm_engine/config_file.rb +46 -0
- data/lib/cyberarm_engine/engine.rb +101 -101
- data/lib/cyberarm_engine/game_object.rb +256 -256
- data/lib/cyberarm_engine/game_state.rb +88 -88
- data/lib/cyberarm_engine/gosu_ext/circle.rb +8 -8
- data/lib/cyberarm_engine/ray.rb +55 -55
- data/lib/cyberarm_engine/shader.rb +398 -262
- data/lib/cyberarm_engine/text.rb +146 -146
- data/lib/cyberarm_engine/timer.rb +22 -22
- data/lib/cyberarm_engine/transform.rb +272 -272
- data/lib/cyberarm_engine/ui/border_canvas.rb +100 -100
- data/lib/cyberarm_engine/ui/dsl.rb +98 -98
- data/lib/cyberarm_engine/ui/element.rb +275 -275
- data/lib/cyberarm_engine/ui/elements/button.rb +66 -66
- data/lib/cyberarm_engine/ui/elements/check_box.rb +58 -58
- data/lib/cyberarm_engine/ui/elements/container.rb +176 -176
- data/lib/cyberarm_engine/ui/elements/edit_line.rb +171 -171
- data/lib/cyberarm_engine/ui/elements/flow.rb +16 -16
- data/lib/cyberarm_engine/ui/elements/image.rb +51 -51
- data/lib/cyberarm_engine/ui/elements/label.rb +49 -49
- data/lib/cyberarm_engine/ui/elements/progress.rb +49 -49
- data/lib/cyberarm_engine/ui/elements/stack.rb +12 -12
- data/lib/cyberarm_engine/ui/elements/toggle_button.rb +55 -55
- data/lib/cyberarm_engine/ui/event.rb +46 -46
- data/lib/cyberarm_engine/ui/gui_state.rb +134 -134
- data/lib/cyberarm_engine/ui/style.rb +36 -36
- data/lib/cyberarm_engine/ui/theme.rb +120 -120
- data/lib/cyberarm_engine/vector.rb +289 -202
- data/lib/cyberarm_engine/version.rb +4 -4
- metadata +3 -2
@@ -1,37 +1,37 @@
|
|
1
|
-
module Gosu
|
2
|
-
class Color
|
3
|
-
def _dump(level)
|
4
|
-
[
|
5
|
-
"%02X" % self.alpha,
|
6
|
-
"%02X" % self.red,
|
7
|
-
"%02X" % self.green,
|
8
|
-
"%02X" % self.blue
|
9
|
-
].join
|
10
|
-
end
|
11
|
-
|
12
|
-
def self._load(hex)
|
13
|
-
argb(hex.to_i(16))
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
module CyberarmEngine
|
19
|
-
class Style
|
20
|
-
def initialize(hash = {})
|
21
|
-
@hash = Marshal.load(Marshal.dump(hash))
|
22
|
-
end
|
23
|
-
|
24
|
-
def method_missing(method, *args, &block)
|
25
|
-
if method.to_s.end_with?("=")
|
26
|
-
raise "Did not expect more than 1 argument" if args.size > 1
|
27
|
-
return @hash[method.to_s.sub("=", "").to_sym] = args.first
|
28
|
-
|
29
|
-
elsif args.size == 0
|
30
|
-
return @hash[method]
|
31
|
-
|
32
|
-
else
|
33
|
-
raise ArgumentError, "Did not expect arguments"
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
1
|
+
module Gosu
|
2
|
+
class Color
|
3
|
+
def _dump(level)
|
4
|
+
[
|
5
|
+
"%02X" % self.alpha,
|
6
|
+
"%02X" % self.red,
|
7
|
+
"%02X" % self.green,
|
8
|
+
"%02X" % self.blue
|
9
|
+
].join
|
10
|
+
end
|
11
|
+
|
12
|
+
def self._load(hex)
|
13
|
+
argb(hex.to_i(16))
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module CyberarmEngine
|
19
|
+
class Style
|
20
|
+
def initialize(hash = {})
|
21
|
+
@hash = Marshal.load(Marshal.dump(hash))
|
22
|
+
end
|
23
|
+
|
24
|
+
def method_missing(method, *args, &block)
|
25
|
+
if method.to_s.end_with?("=")
|
26
|
+
raise "Did not expect more than 1 argument" if args.size > 1
|
27
|
+
return @hash[method.to_s.sub("=", "").to_sym] = args.first
|
28
|
+
|
29
|
+
elsif args.size == 0
|
30
|
+
return @hash[method]
|
31
|
+
|
32
|
+
else
|
33
|
+
raise ArgumentError, "Did not expect arguments"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
37
|
end
|
@@ -1,120 +1,120 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
module Theme
|
3
|
-
def default(*args)
|
4
|
-
value = @options
|
5
|
-
args.each do |arg|
|
6
|
-
value = value.dig(arg)
|
7
|
-
end
|
8
|
-
|
9
|
-
value
|
10
|
-
end
|
11
|
-
|
12
|
-
def theme_defaults(options)
|
13
|
-
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
14
|
-
_theme = THEME
|
15
|
-
_theme = _theme.merge(options[:theme]) if options[:theme]
|
16
|
-
_theme.delete(:theme) if options[:theme]
|
17
|
-
|
18
|
-
hash = {}
|
19
|
-
class_names = self.class.ancestors
|
20
|
-
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
21
|
-
|
22
|
-
class_names.each do |klass|
|
23
|
-
next unless data = _theme.dig(klass)
|
24
|
-
data.each do |key, value|
|
25
|
-
hash.merge!(data)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
deep_merge(hash, options)
|
30
|
-
end
|
31
|
-
|
32
|
-
# Derived from Rails Hash#deep_merge!
|
33
|
-
# Enables passing partial themes through Element options without issue
|
34
|
-
def deep_merge(original, intergrate, &block)
|
35
|
-
hash = original.merge(intergrate) do |key, this_val, other_val|
|
36
|
-
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
37
|
-
deep_merge(this_val, other_val, &block)
|
38
|
-
elsif block_given?
|
39
|
-
block.call(key, this_val, other_val)
|
40
|
-
else
|
41
|
-
other_val
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
return hash
|
46
|
-
end
|
47
|
-
|
48
|
-
THEME = {
|
49
|
-
Element: {
|
50
|
-
x: 0,
|
51
|
-
y: 0,
|
52
|
-
z: 30,
|
53
|
-
|
54
|
-
width: nil,
|
55
|
-
height: nil,
|
56
|
-
color: Gosu::Color::WHITE,
|
57
|
-
background: Gosu::Color::NONE,
|
58
|
-
margin: 0,
|
59
|
-
padding: 0,
|
60
|
-
border_thickness: 0,
|
61
|
-
border_color: Gosu::Color::NONE,
|
62
|
-
border_radius: 0,
|
63
|
-
},
|
64
|
-
|
65
|
-
Button: { # < Label
|
66
|
-
margin: 1,
|
67
|
-
padding: 4,
|
68
|
-
border_thickness: 1,
|
69
|
-
border_color: ["ffd59674".hex, "ffff8746".hex],
|
70
|
-
border_radius: 0,
|
71
|
-
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
72
|
-
|
73
|
-
hover: {
|
74
|
-
color: Gosu::Color.rgb(200,200,200),
|
75
|
-
background: ["ffB23E41".to_i(16), "ffFF7C00".to_i(16)],
|
76
|
-
},
|
77
|
-
|
78
|
-
active: {
|
79
|
-
color: Gosu::Color::BLACK,
|
80
|
-
background: ["ffB23E41".to_i(16)]
|
81
|
-
}
|
82
|
-
},
|
83
|
-
|
84
|
-
EditLine: { # < Button
|
85
|
-
type: :text,
|
86
|
-
width: 200,
|
87
|
-
password_character: "•",
|
88
|
-
caret_width: 2,
|
89
|
-
caret_color: Gosu::Color::WHITE,
|
90
|
-
caret_interval: 500,
|
91
|
-
selection_color: Gosu::Color::GREEN,
|
92
|
-
},
|
93
|
-
|
94
|
-
Image: { # < Element
|
95
|
-
retro: false
|
96
|
-
},
|
97
|
-
|
98
|
-
Label: { # < Element
|
99
|
-
text_size: 28,
|
100
|
-
text_shadow: false,
|
101
|
-
font: "Arial",
|
102
|
-
margin: 0,
|
103
|
-
padding: 2
|
104
|
-
},
|
105
|
-
|
106
|
-
ToggleButton: { # < Button
|
107
|
-
checkmark: "√"
|
108
|
-
},
|
109
|
-
|
110
|
-
Progress: { # < Element
|
111
|
-
width: 250,
|
112
|
-
height: 36,
|
113
|
-
background: 0xff111111,
|
114
|
-
fraction_background: [0xffc75e61, 0xffe26623],
|
115
|
-
border_thickness: 1,
|
116
|
-
border_color: [0xffd59674, 0xffff8746]
|
117
|
-
}
|
118
|
-
}.freeze
|
119
|
-
end
|
120
|
-
end
|
1
|
+
module CyberarmEngine
|
2
|
+
module Theme
|
3
|
+
def default(*args)
|
4
|
+
value = @options
|
5
|
+
args.each do |arg|
|
6
|
+
value = value.dig(arg)
|
7
|
+
end
|
8
|
+
|
9
|
+
value
|
10
|
+
end
|
11
|
+
|
12
|
+
def theme_defaults(options)
|
13
|
+
raise "Error" unless self.class.ancestors.include?(CyberarmEngine::Element)
|
14
|
+
_theme = THEME
|
15
|
+
_theme = _theme.merge(options[:theme]) if options[:theme]
|
16
|
+
_theme.delete(:theme) if options[:theme]
|
17
|
+
|
18
|
+
hash = {}
|
19
|
+
class_names = self.class.ancestors
|
20
|
+
class_names = class_names[0..class_names.index(CyberarmEngine::Element)].map! {|c| c.to_s.split("::").last.to_sym}.reverse!
|
21
|
+
|
22
|
+
class_names.each do |klass|
|
23
|
+
next unless data = _theme.dig(klass)
|
24
|
+
data.each do |key, value|
|
25
|
+
hash.merge!(data)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
deep_merge(hash, options)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Derived from Rails Hash#deep_merge!
|
33
|
+
# Enables passing partial themes through Element options without issue
|
34
|
+
def deep_merge(original, intergrate, &block)
|
35
|
+
hash = original.merge(intergrate) do |key, this_val, other_val|
|
36
|
+
if this_val.is_a?(Hash) && other_val.is_a?(Hash)
|
37
|
+
deep_merge(this_val, other_val, &block)
|
38
|
+
elsif block_given?
|
39
|
+
block.call(key, this_val, other_val)
|
40
|
+
else
|
41
|
+
other_val
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
return hash
|
46
|
+
end
|
47
|
+
|
48
|
+
THEME = {
|
49
|
+
Element: {
|
50
|
+
x: 0,
|
51
|
+
y: 0,
|
52
|
+
z: 30,
|
53
|
+
|
54
|
+
width: nil,
|
55
|
+
height: nil,
|
56
|
+
color: Gosu::Color::WHITE,
|
57
|
+
background: Gosu::Color::NONE,
|
58
|
+
margin: 0,
|
59
|
+
padding: 0,
|
60
|
+
border_thickness: 0,
|
61
|
+
border_color: Gosu::Color::NONE,
|
62
|
+
border_radius: 0,
|
63
|
+
},
|
64
|
+
|
65
|
+
Button: { # < Label
|
66
|
+
margin: 1,
|
67
|
+
padding: 4,
|
68
|
+
border_thickness: 1,
|
69
|
+
border_color: ["ffd59674".hex, "ffff8746".hex],
|
70
|
+
border_radius: 0,
|
71
|
+
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
|
72
|
+
|
73
|
+
hover: {
|
74
|
+
color: Gosu::Color.rgb(200,200,200),
|
75
|
+
background: ["ffB23E41".to_i(16), "ffFF7C00".to_i(16)],
|
76
|
+
},
|
77
|
+
|
78
|
+
active: {
|
79
|
+
color: Gosu::Color::BLACK,
|
80
|
+
background: ["ffB23E41".to_i(16)]
|
81
|
+
}
|
82
|
+
},
|
83
|
+
|
84
|
+
EditLine: { # < Button
|
85
|
+
type: :text,
|
86
|
+
width: 200,
|
87
|
+
password_character: "•",
|
88
|
+
caret_width: 2,
|
89
|
+
caret_color: Gosu::Color::WHITE,
|
90
|
+
caret_interval: 500,
|
91
|
+
selection_color: Gosu::Color::GREEN,
|
92
|
+
},
|
93
|
+
|
94
|
+
Image: { # < Element
|
95
|
+
retro: false
|
96
|
+
},
|
97
|
+
|
98
|
+
Label: { # < Element
|
99
|
+
text_size: 28,
|
100
|
+
text_shadow: false,
|
101
|
+
font: "Arial",
|
102
|
+
margin: 0,
|
103
|
+
padding: 2
|
104
|
+
},
|
105
|
+
|
106
|
+
ToggleButton: { # < Button
|
107
|
+
checkmark: "√"
|
108
|
+
},
|
109
|
+
|
110
|
+
Progress: { # < Element
|
111
|
+
width: 250,
|
112
|
+
height: 36,
|
113
|
+
background: 0xff111111,
|
114
|
+
fraction_background: [0xffc75e61, 0xffe26623],
|
115
|
+
border_thickness: 1,
|
116
|
+
border_color: [0xffd59674, 0xffff8746]
|
117
|
+
}
|
118
|
+
}.freeze
|
119
|
+
end
|
120
|
+
end
|
@@ -1,203 +1,290 @@
|
|
1
|
-
module CyberarmEngine
|
2
|
-
class Vector
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
def self.
|
20
|
-
Vector.new(0,
|
21
|
-
end
|
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
|
-
def
|
196
|
-
|
197
|
-
end
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
1
|
+
module CyberarmEngine
|
2
|
+
class Vector
|
3
|
+
##
|
4
|
+
# Creates a up vector
|
5
|
+
#
|
6
|
+
# Vector.new(0, 1, 0)
|
7
|
+
#
|
8
|
+
# @return [CyberarmEngine::Vector]
|
9
|
+
def self.up
|
10
|
+
Vector.new(0, 1, 0)
|
11
|
+
end
|
12
|
+
|
13
|
+
##
|
14
|
+
# Creates a down vector
|
15
|
+
#
|
16
|
+
# Vector.new(0, -1, 0)
|
17
|
+
#
|
18
|
+
# @return [CyberarmEngine::Vector]
|
19
|
+
def self.down
|
20
|
+
Vector.new(0, -1, 0)
|
21
|
+
end
|
22
|
+
|
23
|
+
##
|
24
|
+
# Creates a left vector
|
25
|
+
#
|
26
|
+
# Vector.new(-1, 0, 0)
|
27
|
+
#
|
28
|
+
# @return [CyberarmEngine::Vector]
|
29
|
+
def self.left
|
30
|
+
Vector.new(-1, 0, 0)
|
31
|
+
end
|
32
|
+
|
33
|
+
##
|
34
|
+
# Creates a right vector
|
35
|
+
#
|
36
|
+
# Vector.new(1, 0, 0)
|
37
|
+
#
|
38
|
+
# @return [CyberarmEngine::Vector]
|
39
|
+
def self.right
|
40
|
+
Vector.new(1, 0, 0)
|
41
|
+
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Creates a forward vector
|
45
|
+
#
|
46
|
+
# Vector.new(0, 0, 1)
|
47
|
+
#
|
48
|
+
# @return [CyberarmEngine::Vector]
|
49
|
+
def self.forward
|
50
|
+
Vector.new(0, 0, 1)
|
51
|
+
end
|
52
|
+
|
53
|
+
##
|
54
|
+
# Creates a backward vector
|
55
|
+
#
|
56
|
+
# Vector.new(0, 0, -1)
|
57
|
+
#
|
58
|
+
# @return [CyberarmEngine::Vector]
|
59
|
+
def self.backward
|
60
|
+
Vector.new(0, 0, -1)
|
61
|
+
end
|
62
|
+
|
63
|
+
def initialize(x = 0, y = 0, z = 0, weight = 0)
|
64
|
+
@x, @y, @z, @weight = x, y, z, weight
|
65
|
+
end
|
66
|
+
|
67
|
+
def x; @x; end
|
68
|
+
def x=(n); @x = n; end
|
69
|
+
|
70
|
+
def y; @y; end
|
71
|
+
def y=(n); @y = n; end
|
72
|
+
|
73
|
+
def z; @z; end
|
74
|
+
def z=(n); @z = n; end
|
75
|
+
|
76
|
+
def weight; @weight; end
|
77
|
+
def weight=(n); @weight = n; end
|
78
|
+
|
79
|
+
alias w weight
|
80
|
+
alias w= weight=
|
81
|
+
|
82
|
+
# @return [Boolean]
|
83
|
+
def ==(other)
|
84
|
+
if other.is_a?(Numeric)
|
85
|
+
@x == other &&
|
86
|
+
@y == other &&
|
87
|
+
@z == other &&
|
88
|
+
@weight == other
|
89
|
+
elsif other.is_a?(Vector)
|
90
|
+
@x == other.x &&
|
91
|
+
@y == other.y &&
|
92
|
+
@z == other.z &&
|
93
|
+
@weight == other.weight
|
94
|
+
else
|
95
|
+
other == self
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Create a new vector using {x} and {y} values
|
100
|
+
# @return [CyberarmEngine::Vector]
|
101
|
+
def xy
|
102
|
+
Vector.new(@x, @y)
|
103
|
+
end
|
104
|
+
|
105
|
+
# Performs math operation, excluding {weight}
|
106
|
+
private def operator(function, other)
|
107
|
+
if other.is_a?(Numeric)
|
108
|
+
Vector.new(
|
109
|
+
@x.send(:"#{function}", other),
|
110
|
+
@y.send(:"#{function}", other),
|
111
|
+
@z.send(:"#{function}", other)
|
112
|
+
)
|
113
|
+
else
|
114
|
+
Vector.new(
|
115
|
+
@x.send(:"#{function}", other.x),
|
116
|
+
@y.send(:"#{function}", other.y),
|
117
|
+
@z.send(:"#{function}", other.z)
|
118
|
+
)
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Adds Vector and Numeric or Vector and Vector, excluding {weight}
|
123
|
+
# @return [CyberarmEngine::Vector]
|
124
|
+
def +(other)
|
125
|
+
operator("+", other)
|
126
|
+
end
|
127
|
+
|
128
|
+
# Subtracts Vector and Numeric or Vector and Vector, excluding {weight}
|
129
|
+
# @return [CyberarmEngine::Vector]
|
130
|
+
def -(other)
|
131
|
+
operator("-", other)
|
132
|
+
end
|
133
|
+
|
134
|
+
# Multiplies Vector and Numeric or Vector and Vector, excluding {weight}
|
135
|
+
# @return [CyberarmEngine::Vector]
|
136
|
+
def *(other)
|
137
|
+
operator("*", other)
|
138
|
+
end
|
139
|
+
|
140
|
+
# Divides Vector and Numeric or Vector and Vector, excluding {weight}
|
141
|
+
# @return [CyberarmEngine::Vector]
|
142
|
+
def /(other)
|
143
|
+
# Duplicated to protect from DivideByZero
|
144
|
+
if other.is_a?(Numeric)
|
145
|
+
Vector.new(
|
146
|
+
(@x == 0 ? 0 : @x / other),
|
147
|
+
(@y == 0 ? 0 : @y / other),
|
148
|
+
(@z == 0 ? 0 : @z / other)
|
149
|
+
)
|
150
|
+
else
|
151
|
+
Vector.new(
|
152
|
+
(@x == 0 ? 0 : @x / other.x),
|
153
|
+
(@y == 0 ? 0 : @y / other.y),
|
154
|
+
(@z == 0 ? 0 : @z / other.z)
|
155
|
+
)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
# dot product of {Vector}
|
160
|
+
# @return [Integer|Float]
|
161
|
+
def dot(other)
|
162
|
+
product = 0
|
163
|
+
|
164
|
+
a = self.to_a
|
165
|
+
b = other.to_a
|
166
|
+
|
167
|
+
3.times do |i|
|
168
|
+
product = product + (a[i] * b[i])
|
169
|
+
end
|
170
|
+
|
171
|
+
return product
|
172
|
+
end
|
173
|
+
|
174
|
+
# cross product of {Vector}
|
175
|
+
# @return [CyberarmEngine::Vector]
|
176
|
+
def cross(other)
|
177
|
+
a = self.to_a
|
178
|
+
b = other.to_a
|
179
|
+
|
180
|
+
Vector.new(
|
181
|
+
b[2] * a[1] - b[1] * a[2],
|
182
|
+
b[0] * a[2] - b[2] * a[0],
|
183
|
+
b[1] * a[0] - b[0] * a[1]
|
184
|
+
)
|
185
|
+
end
|
186
|
+
|
187
|
+
# returns degrees
|
188
|
+
# @return [Float]
|
189
|
+
def angle(other)
|
190
|
+
Math.acos( self.normalized.dot(other.normalized) ) * 180 / Math::PI
|
191
|
+
end
|
192
|
+
|
193
|
+
# returns magnitude of Vector, ignoring #weight
|
194
|
+
# @return [Float]
|
195
|
+
def magnitude
|
196
|
+
Math.sqrt((@x * @x) + (@y * @y) + (@z * @z))
|
197
|
+
end
|
198
|
+
|
199
|
+
##
|
200
|
+
# returns normalized {Vector}
|
201
|
+
#
|
202
|
+
# @example
|
203
|
+
# CyberarmEngine::Vector.new(50, 21.2, 45).normalized
|
204
|
+
# # => <CyberarmEngine::Vector:0x001 @x=0.7089... @y=0.3005... @z=0.6380... @weight=0>
|
205
|
+
#
|
206
|
+
# @return [CyberarmEngine::Vector]
|
207
|
+
def normalized
|
208
|
+
mag = magnitude
|
209
|
+
self / Vector.new(mag, mag, mag)
|
210
|
+
end
|
211
|
+
|
212
|
+
|
213
|
+
# returns a direction {Vector}
|
214
|
+
#
|
215
|
+
# z is pitch
|
216
|
+
#
|
217
|
+
# y is yaw
|
218
|
+
#
|
219
|
+
# x is roll
|
220
|
+
# @return [CyberarmEngine::Vector]
|
221
|
+
def direction
|
222
|
+
_x = -Math.sin(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
223
|
+
_y = Math.sin(@z.degrees_to_radians)
|
224
|
+
_z = Math.cos(@y.degrees_to_radians) * Math.cos(@z.degrees_to_radians)
|
225
|
+
|
226
|
+
Vector.new(_x, _y, _z)
|
227
|
+
end
|
228
|
+
|
229
|
+
# returns an inverse {Vector}
|
230
|
+
# @return [CyberarmEngine::Vector]
|
231
|
+
def inverse
|
232
|
+
Vector.new(1.0 / @x, 1.0 / @y, 1.0 / @z)
|
233
|
+
end
|
234
|
+
|
235
|
+
# Adds up values of {x}, {y}, and {z}
|
236
|
+
# @return [Integer|Float]
|
237
|
+
def sum
|
238
|
+
@x + @y + @z
|
239
|
+
end
|
240
|
+
|
241
|
+
##
|
242
|
+
# Linear interpolation: smoothly transition between two {Vector}
|
243
|
+
#
|
244
|
+
# CyberarmEngine::Vector.new(100, 100, 100).lerp( CyberarmEngine::Vector.new(0, 0, 0), 0.75 )
|
245
|
+
# # => <CyberarmEngine::Vector:0x0001 @x=75.0, @y=75.0, @z=75.0, @weight=0>
|
246
|
+
#
|
247
|
+
# @param other [CyberarmEngine::Vector | Integer | Float] value to subtract from
|
248
|
+
# @param factor [Float] how complete transition to _other_ is, in range [0.0..1.0]
|
249
|
+
# @return [CyberarmEngine::Vector]
|
250
|
+
def lerp(other, factor)
|
251
|
+
(self - other) * factor.clamp(0.0, 1.0)
|
252
|
+
end
|
253
|
+
|
254
|
+
# 2D distance using X and Y
|
255
|
+
# @return [Float]
|
256
|
+
def distance(other)
|
257
|
+
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2)
|
258
|
+
end
|
259
|
+
|
260
|
+
# 2D distance using X and Z
|
261
|
+
# @return [Float]
|
262
|
+
def gl_distance2d(other)
|
263
|
+
Math.sqrt((@x-other.x)**2 + (@z-other.z)**2)
|
264
|
+
end
|
265
|
+
|
266
|
+
# 3D distance using X, Y, and Z
|
267
|
+
# @return [Float]
|
268
|
+
def distance3d(other)
|
269
|
+
Math.sqrt((@x-other.x)**2 + (@y-other.y)**2 + (@z-other.z)**2)
|
270
|
+
end
|
271
|
+
|
272
|
+
# Converts {Vector} to Array
|
273
|
+
# @return [Array]
|
274
|
+
def to_a
|
275
|
+
[@x, @y, @z, @weight]
|
276
|
+
end
|
277
|
+
|
278
|
+
# Converts {Vector} to String
|
279
|
+
# @return [String]
|
280
|
+
def to_s
|
281
|
+
"X: #{@x}, Y: #{@y}, Z: #{@z}, Weight: #{@weight}"
|
282
|
+
end
|
283
|
+
|
284
|
+
# Converts {Vector} to Hash
|
285
|
+
# @return [Hash]
|
286
|
+
def to_h
|
287
|
+
{x: @x, y: @y, z: @z, weight: @weight}
|
288
|
+
end
|
289
|
+
end
|
203
290
|
end
|