Class: CyberarmEngine::GameObject
- Inherits:
-
Object
- Object
- CyberarmEngine::GameObject
- Includes:
- Common
- Defined in:
- lib/cyberarm_engine/game_object.rb
Instance Attribute Summary collapse
-
#alpha ⇒ Object
Returns the value of attribute alpha.
-
#angle ⇒ Object
Returns the value of attribute angle.
-
#center_x ⇒ Object
Returns the value of attribute center_x.
-
#center_y ⇒ Object
Returns the value of attribute center_y.
-
#color ⇒ Object
Returns the value of attribute color.
-
#image ⇒ Object
Returns the value of attribute image.
-
#last_position ⇒ Object
Returns the value of attribute last_position.
-
#mode ⇒ Object
Returns the value of attribute mode.
-
#options ⇒ Object
Returns the value of attribute options.
-
#paused ⇒ Object
Returns the value of attribute paused.
-
#position ⇒ Object
Returns the value of attribute position.
-
#radius ⇒ Object
Returns the value of attribute radius.
-
#scale_x ⇒ Object
Returns the value of attribute scale_x.
-
#scale_y ⇒ Object
Returns the value of attribute scale_y.
-
#velocity ⇒ Object
Returns the value of attribute velocity.
Class Method Summary collapse
Instance Method Summary collapse
- #_x_visible ⇒ Object
- #_y_visible ⇒ Object
-
#all ⇒ Object
NOTE: This could be implemented more reliably.
- #button_down(id) ⇒ Object
- #button_up(id) ⇒ Object
- #circle_collision?(object) ⇒ Boolean
- #debug_text(text) ⇒ Object
- #destroy ⇒ Object
- #draw ⇒ Object
- #draw_rect(x, y, width, height, color, z = 0) ⇒ Object
-
#each_circle_collision(object, _resolve_with = :width, &block) ⇒ Object
Duplication…
- #find_closest(game_object_class) ⇒ Object
- #heading(ahead_by = 100, _object = nil, angle_only = false) ⇒ Object
- #height ⇒ Object
-
#initialize(options = {}) ⇒ GameObject
constructor
A new instance of GameObject.
- #look_at(object) ⇒ Object
- #pause ⇒ Object
- #rotate(int) ⇒ Object
- #scale ⇒ Object
- #scale=(int) ⇒ Object
- #show_debug_heading ⇒ Object
- #unpause ⇒ Object
- #update ⇒ Object
- #visible ⇒ Object
- #width ⇒ Object
Methods included from Common
#current_state, #darken, #fill, #get_asset, #get_image, #get_sample, #get_song, #lighten, #opacity, #pop_state, #previous_state, #push_state, #show_cursor, #show_cursor=, #window
Constructor Details
#initialize(options = {}) ⇒ 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( = {}) $window.current_state.add_game_object(self) if [:auto_manage] || [:auto_manage].nil? = @image = [:image] ? image([:image]) : nil x = [:x] || 0 y = [:y] || 0 z = [:z] || 0 @position = Vector.new(x, y, z) @velocity = Vector.new @last_position = Vector.new @angle = [:angle] || 0 @center_x = [:center_x] || 0.5 @center_y = [:center_y] || 0.5 @scale_x = [:scale_x] || 1 @scale_y = [:scale_y] || 1 @color = [:color] || Gosu::Color.argb(0xff_ffffff) @alpha = [:alpha] || 255 @mode = [: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 [:radius] [:radius] else defined?(@image.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 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
239 240 241 242 243 244 245 246 |
# File 'lib/cyberarm_engine/game_object.rb', line 239 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
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/cyberarm_engine/game_object.rb', line 221 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
217 218 219 |
# File 'lib/cyberarm_engine/game_object.rb', line 217 def all INSTANCES.select { |i| i.instance_of?(self) } end |
#button_down(id) ⇒ Object
163 164 |
# File 'lib/cyberarm_engine/game_object.rb', line 163 def (id) end |
#button_up(id) ⇒ Object
160 161 |
# File 'lib/cyberarm_engine/game_object.rb', line 160 def (id) end |
#circle_collision?(object) ⇒ Boolean
185 186 187 188 |
# File 'lib/cyberarm_engine/game_object.rb', line 185 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
208 209 210 211 212 213 214 |
# File 'lib/cyberarm_engine/game_object.rb', line 208 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 $window.draw_circle(@position.x, @position.y, radius, 9999, @debug_color) if @debug_text.text != "" $window.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 |
#draw_rect(x, y, width, height, color, z = 0) ⇒ Object
156 157 158 |
# File 'lib/cyberarm_engine/game_object.rb', line 156 def draw_rect(x, y, width, height, color, z = 0) $window.draw_rect(x, y, width, height, color, z) end |
#each_circle_collision(object, _resolve_with = :width, &block) ⇒ Object
Duplication… so DRY.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/cyberarm_engine/game_object.rb', line 191 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
166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/cyberarm_engine/game_object.rb', line 166 def find_closest(game_object_class) best_object = nil best_distance = 100_000_000_000 # Huge default number 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
181 182 183 |
# File 'lib/cyberarm_engine/game_object.rb', line 181 def look_at(object) # TODO: Implement 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 # maths? 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 # if _x_visible # if _y_visible # true # else # false # end # else # false # end end |
#width ⇒ Object
129 130 131 |
# File 'lib/cyberarm_engine/game_object.rb', line 129 def width @image ? @image.width * scale : 0 end |