Class: Arcade::GameObject

Inherits:
Object
  • Object
show all
Defined in:
lib/arcade/base.rb

Constant Summary collapse

PROPERTIES =
[:x, :y, :height, :width, :color, :name, :velocity, :score]
DEFAULTS =
{:color => Arcade::Color::WHITE,
:velocity => Arcade::Velocity::ZERO}
ALIASES =
{:x_position => :x,
:y_position => :y,
:set_x_position => :x=,
:set_y_position => :y=}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ GameObject

Returns a new instance of GameObject.



172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/arcade/base.rb', line 172

def initialize &block
  @state = OpenStruct.new
  @keypress_listeners  = {}
  @collision_listeners = {}
  @edge_callback       = nil

  setup_defaults
  instance_exec &block

  # Every GameObject must have a name
  raise Arcade::Errors::NameError, "#{self.class}#name is nil" if self.name.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/arcade/base.rb', line 134

def method_missing method, *args
  method = ALIASES.fetch(method) { method }

  mname = method.id2name

  if prop = mname.scan(/^set_(.+)/).flatten.first
    @state.send(:"#{prop}=", *args)
  else
    @state.send(method, *args).tap do |val|
      warn "WARNING: Called #{self.class}##{method}, but it returned nil. You should set a default value." if val.nil?
    end
  end
end

Instance Attribute Details

#keypress_listenersObject (readonly)

Returns the value of attribute keypress_listeners.



132
133
134
# File 'lib/arcade/base.rb', line 132

def keypress_listeners
  @keypress_listeners
end

#windowObject

Returns the value of attribute window.



131
132
133
# File 'lib/arcade/base.rb', line 131

def window
  @window
end

Class Method Details

.defaultsObject



157
158
159
# File 'lib/arcade/base.rb', line 157

def defaults
  @default_block
end

.has_defaults?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/arcade/base.rb', line 153

def has_defaults?
  !@default_block.nil?
end

.set_defaults(&block) ⇒ Object



149
150
151
# File 'lib/arcade/base.rb', line 149

def set_defaults &block
  @default_block = block
end

Instance Method Details

#_update(dt) ⇒ Object



197
198
199
200
201
202
203
204
205
206
# File 'lib/arcade/base.rb', line 197

def _update dt
  unless self.velocity.zero?
    self.x += self.velocity.element(0)
    self.y += self.velocity.element(1)
  end

  if self.respond_to? :update
    self.update(dt)
  end
end

#bottomObject



212
213
214
# File 'lib/arcade/base.rb', line 212

def bottom
  self.y + height
end

#can_collide_with?(klass) ⇒ Boolean

Returns:

  • (Boolean)


248
249
250
# File 'lib/arcade/base.rb', line 248

def can_collide_with? klass
  @collision_listeners.has_key? klass
end

#collided_with(other) ⇒ Object



256
257
258
259
260
261
# File 'lib/arcade/base.rb', line 256

def collided_with other
  klass = other.class
  if self.can_collide_with?(klass)
    instance_exec other, &@collision_listeners[klass]
  end
end

#collides_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


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
# File 'lib/arcade/base.rb', line 263

def collides_with? other
  if !other.kind_of?(Arcade::GameObject) || other == self
    false
  else
    hit = !(bottom < other.top ||
      top > other.bottom ||
      right < other.left ||
      left > other.right)

    if hit
      other.hit_side = if bottom >= other.top && top <= other.top && left <= other.right && right >= other.left
                         :top
                       elsif bottom >= other.bottom && top <= other.bottom && left <= other.right && right >= other.left
                         :bottom
                       elsif bottom >= other.top && top <= other.bottom && right >= other.left && left <= other.left
                         :left
                       else
                         :right
                       end

    else
      other.hit_side = false
    end

    hit
  end
end

#drawObject



189
190
191
# File 'lib/arcade/base.rb', line 189

def draw
  window.draw_square [self.x, self.y], self.width, self.height, self.color
end

#get_object(object_name) ⇒ Object



185
186
187
# File 'lib/arcade/base.rb', line 185

def get_object object_name
  window.get_object object_name
end

#hit_edge(edge) ⇒ Object

One of :top, :bottom, :left, or :right



296
297
298
299
300
# File 'lib/arcade/base.rb', line 296

def hit_edge edge
  if @edge_callback
    instance_exec edge, &@edge_callback
  end
end

#key_pressed(key) ⇒ Object



244
245
246
# File 'lib/arcade/base.rb', line 244

def key_pressed key
  instance_eval &@keypress_listeners[key]
end

#leftObject



216
217
218
# File 'lib/arcade/base.rb', line 216

def left
  self.x
end

#move_down(pixels) ⇒ Object



228
229
230
# File 'lib/arcade/base.rb', line 228

def move_down pixels
  self.y += pixels
end

#move_left(pixels) ⇒ Object



232
233
234
# File 'lib/arcade/base.rb', line 232

def move_left pixels
  self.x -= pixels
end

#move_right(pixels) ⇒ Object



236
237
238
# File 'lib/arcade/base.rb', line 236

def move_right pixels
  self.x += pixels
end

#move_up(pixels) ⇒ Object



224
225
226
# File 'lib/arcade/base.rb', line 224

def move_up pixels
  self.y -= pixels
end

#on_collides_with(klass, &block) ⇒ Object



252
253
254
# File 'lib/arcade/base.rb', line 252

def on_collides_with klass, &block
  @collision_listeners[klass] = block
end

#on_hit_edge(&block) ⇒ Object



291
292
293
# File 'lib/arcade/base.rb', line 291

def on_hit_edge &block
  @edge_callback = block
end

#on_keypress(key, &block) ⇒ Object



240
241
242
# File 'lib/arcade/base.rb', line 240

def on_keypress key, &block
  @keypress_listeners[key] = block
end

#rightObject



220
221
222
# File 'lib/arcade/base.rb', line 220

def right
  self.x + width
end

#setup_defaultsObject



162
163
164
165
166
167
168
169
170
# File 'lib/arcade/base.rb', line 162

def setup_defaults
  DEFAULTS.each do |prop, val|
    self.send(:"#{prop}=", val)
  end

  if self.class.has_defaults?
    instance_exec &self.class.defaults
  end
end

#topObject



208
209
210
# File 'lib/arcade/base.rb', line 208

def top
  self.y
end