Class: CyberarmEngine::GameObject
- Inherits:
-
Object
- Object
- CyberarmEngine::GameObject
show all
- Includes:
- Common
- Defined in:
- lib/cyberarm_engine/game_object.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Common
#alt_down?, #control_down?, #current_state, #darken, #draw_rect, #fill, #find_element_by_tag, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #shift_down?, #shift_state, #show_cursor, #show_cursor=, #window
Constructor Details
#initialize(options = {}) ⇒ GameObject
Returns a new instance of GameObject.
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
|
# File 'lib/cyberarm_engine/game_object.rb', line 9
def initialize(options = {})
window.current_state.add_game_object(self) if options[:auto_manage] || options[:auto_manage].nil?
@options = options
@image = options[:image] ? image(options[:image]) : nil
x = options[:x] || 0
y = options[:y] || 0
z = options[:z] || 0
@position = Vector.new(x, y, z)
@velocity = Vector.new
@last_position = Vector.new
@angle = options[:angle] || 0
@center_x = options[:center_x] || 0.5
@center_y = options[:center_y] || 0.5
@scale_x = options[:scale_x] || 1
@scale_y = options[:scale_y] || 1
@color = options[:color] || Gosu::Color.argb(0xff_ffffff)
@alpha = options[:alpha] || 255
@mode = options[:mode] || :default
@paused = false
@speed = 0
@debug_color = Gosu::Color::GREEN
@world_center_point = Vector.new(0, 0)
setup
@debug_text = Text.new("", color: @debug_color, y: @position.y - (height * scale), z: 9999)
@debug_text.x = @position.x
if @radius == 0 || @radius.nil?
@radius = if options[:radius]
options[:radius]
else
@image.respond_to?(:width) ? ((@image.width + @image.height) / 4) * scale : 1
end
end
end
|
Instance Attribute Details
#alpha ⇒ Object
Returns the value of attribute alpha.
7
8
9
|
# File 'lib/cyberarm_engine/game_object.rb', line 7
def alpha
@alpha
end
|
#angle ⇒ Object
Returns the value of attribute angle.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def angle
@angle
end
|
#center_x ⇒ Object
Returns the value of attribute center_x.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def center_x
@center_x
end
|
#center_y ⇒ Object
Returns the value of attribute center_y.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def center_y
@center_y
end
|
#color ⇒ Object
Returns the value of attribute color.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def color
@color
end
|
#image ⇒ Object
Returns the value of attribute image.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def image
@image
end
|
#last_position ⇒ Object
Returns the value of attribute last_position.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def last_position
@last_position
end
|
#mode ⇒ Object
Returns the value of attribute mode.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def mode
@mode
end
|
#options ⇒ Object
Returns the value of attribute options.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def options
@options
end
|
#paused ⇒ Object
Returns the value of attribute paused.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def paused
@paused
end
|
#position ⇒ Object
Returns the value of attribute position.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def position
@position
end
|
#radius ⇒ Object
Returns the value of attribute radius.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def radius
@radius
end
|
#scale_x ⇒ Object
Returns the value of attribute scale_x.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def scale_x
@scale_x
end
|
#scale_y ⇒ Object
Returns the value of attribute scale_y.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def scale_y
@scale_y
end
|
#velocity ⇒ Object
Returns the value of attribute velocity.
5
6
7
|
# File 'lib/cyberarm_engine/game_object.rb', line 5
def velocity
@velocity
end
|
Class Method Details
.destroy_all ⇒ Object
235
236
237
238
239
240
241
242
|
# File 'lib/cyberarm_engine/game_object.rb', line 235
def self.destroy_all
INSTANCES.clear
if window.current_state
window.current_state.game_objects.each do |o|
window.current_state.game_objects.delete(o) if o.is_a?(self.class)
end
end
end
|
.each_circle_collision(object, _resolve_with = :width, &block) ⇒ Object
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
# File 'lib/cyberarm_engine/game_object.rb', line 217
def self.each_circle_collision(object, _resolve_with = :width, &block)
if object.class != Class && object.instance_of?(object.class)
window.current_state.game_objects.select { |i| i.instance_of?(self) }.each do |o|
distance = Gosu.distance(o.x, o.y, object.x, object.y)
block.call(o, object) if distance <= o.radius + object.radius && block
end
else
lista = window.current_state.game_objects.select { |i| i.instance_of?(self) }
listb = window.current_state.game_objects.select { |i| i.instance_of?(object) }
lista.product(listb).each do |o, o2|
next if o == o2
distance = Gosu.distance(o.x, o.y, o2.x, o2.y)
block.call(o, o2) if distance <= o.radius + o2.radius && block
end
end
end
|
Instance Method Details
#_x_visible ⇒ Object
104
105
106
107
|
# File 'lib/cyberarm_engine/game_object.rb', line 104
def _x_visible
x.between?((window.width / 2) - @world_center_point.x, (window.width / 2) + @world_center_point.x) ||
x.between?((@world_center_point.x - window.width / 2), (window.width / 2) + @world_center_point.x)
end
|
#_y_visible ⇒ Object
109
110
111
112
|
# File 'lib/cyberarm_engine/game_object.rb', line 109
def _y_visible
y.between?((window.height / 2) - @world_center_point.y, (window.height / 2) + @world_center_point.y) ||
y.between?(@world_center_point.y - (window.height / 2), (window.height / 2) + @world_center_point.y)
end
|
#all ⇒ Object
NOTE: This could be implemented more reliably
213
214
215
|
# File 'lib/cyberarm_engine/game_object.rb', line 213
def all
INSTANCES.select { |i| i.instance_of?(self) }
end
|
159
160
|
# File 'lib/cyberarm_engine/game_object.rb', line 159
def button_down(id)
end
|
156
157
|
# File 'lib/cyberarm_engine/game_object.rb', line 156
def button_up(id)
end
|
#circle_collision?(object) ⇒ Boolean
181
182
183
184
|
# File 'lib/cyberarm_engine/game_object.rb', line 181
def circle_collision?(object)
distance = Gosu.distance(x, y, object.x, object.y)
distance <= radius + object.radius
end
|
#debug_text(text) ⇒ Object
70
71
72
73
74
|
# File 'lib/cyberarm_engine/game_object.rb', line 70
def debug_text(text)
@debug_text.text = text
@debug_text.x = @position.x - (@debug_text.width / 2)
@debug_text.y = @position.y - (@debug_text.height + radius + height)
end
|
#destroy ⇒ Object
204
205
206
207
208
209
210
|
# File 'lib/cyberarm_engine/game_object.rb', line 204
def destroy
if window.current_state
window.current_state.game_objects.each do |o|
window.current_state.game_objects.delete(o) if o.is_a?(self.class) && o == self
end
end
end
|
#draw ⇒ Object
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/cyberarm_engine/game_object.rb', line 50
def draw
if @image
@image.draw_rot(@position.x, @position.y, @position.z, @angle, @center_x, @center_y, @scale_x, @scale_y,
@color, @mode)
end
if $debug
show_debug_heading
Gosu.draw_circle(@position.x, @position.y, radius, 9999, @debug_color)
if @debug_text.text != ""
Gosu.draw_rect(@debug_text.x - 10, (@debug_text.y - 10), @debug_text.width + 20, @debug_text.height + 20,
Gosu::Color.rgba(0, 0, 0, 200), 9999)
@debug_text.draw
end
end
end
|
#each_circle_collision(object, _resolve_with = :width, &block) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
# File 'lib/cyberarm_engine/game_object.rb', line 187
def each_circle_collision(object, _resolve_with = :width, &block)
if object.class != Class && object.instance_of?(object.class)
window.current_state.game_objects.select { |i| i.instance_of?(object.class) }.each do |o|
distance = Gosu.distance(x, y, object.x, object.y)
block.call(o, object) if distance <= radius + object.radius && block
end
else
list = window.current_state.game_objects.select { |i| i.instance_of?(object) }
list.each do |o|
next if self == o
distance = Gosu.distance(x, y, o.x, o.y)
block.call(self, o) if distance <= radius + o.radius && block
end
end
end
|
#find_closest(game_object_class) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
# File 'lib/cyberarm_engine/game_object.rb', line 162
def find_closest(game_object_class)
best_object = nil
best_distance = 100_000_000_000
game_object_class.all.each do |object|
distance = Gosu.distance(x, y, object.x, object.y)
if distance <= best_distance
best_object = object
best_distance = distance
end
end
best_object
end
|
#heading(ahead_by = 100, _object = nil, angle_only = false) ⇒ Object
114
115
116
117
118
119
120
121
122
|
# File 'lib/cyberarm_engine/game_object.rb', line 114
def heading(ahead_by = 100, _object = nil, angle_only = false)
direction = Gosu.angle(@last_position.x, @last_position.x, @position.x, position.y).gosu_to_radians
_x = @position.x + (ahead_by * Math.cos(direction))
_y = @position.y + (ahead_by * Math.sin(direction))
return direction if angle_only
return Vector.new(_x, _y) unless angle_only
end
|
#height ⇒ Object
133
134
135
|
# File 'lib/cyberarm_engine/game_object.rb', line 133
def height
@image ? @image.height * scale : 0
end
|
#look_at(object) ⇒ Object
177
178
179
|
# File 'lib/cyberarm_engine/game_object.rb', line 177
def look_at(object)
end
|
#pause ⇒ Object
137
138
139
|
# File 'lib/cyberarm_engine/game_object.rb', line 137
def pause
@paused = true
end
|
#rotate(int) ⇒ Object
145
146
147
148
|
# File 'lib/cyberarm_engine/game_object.rb', line 145
def rotate(int)
self.angle += int
self.angle %= 360
end
|
#scale ⇒ Object
76
77
78
79
80
81
82
83
|
# File 'lib/cyberarm_engine/game_object.rb', line 76
def scale
if @scale_x == @scale_y
@scale_x
else
false
end
end
|
#scale=(int) ⇒ Object
85
86
87
88
89
|
# File 'lib/cyberarm_engine/game_object.rb', line 85
def scale=(int)
self.scale_x = int
self.scale_y = int
self.radius = ((@image.width + @image.height) / 4) * scale
end
|
#show_debug_heading ⇒ Object
124
125
126
127
|
# File 'lib/cyberarm_engine/game_object.rb', line 124
def show_debug_heading
_heading = heading
Gosu.draw_line(@position.x, @position.y, @debug_color, _heading.x, _heading.y, @debug_color, 9999)
end
|
#unpause ⇒ Object
141
142
143
|
# File 'lib/cyberarm_engine/game_object.rb', line 141
def unpause
@paused = false
end
|
#update ⇒ Object
67
68
|
# File 'lib/cyberarm_engine/game_object.rb', line 67
def update
end
|
#visible ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/cyberarm_engine/game_object.rb', line 91
def visible
true
end
|
#width ⇒ Object
129
130
131
|
# File 'lib/cyberarm_engine/game_object.rb', line 129
def width
@image ? @image.width * scale : 0
end
|