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.



153
154
155
156
157
158
159
160
161
# File 'lib/arcade/base.rb', line 153

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

  setup_defaults
  instance_exec &block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/arcade/base.rb', line 117

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)
  end
end

Instance Attribute Details

#keypress_listenersObject (readonly)

Returns the value of attribute keypress_listeners.



115
116
117
# File 'lib/arcade/base.rb', line 115

def keypress_listeners
  @keypress_listeners
end

#windowObject

Returns the value of attribute window.



114
115
116
# File 'lib/arcade/base.rb', line 114

def window
  @window
end

Class Method Details

.defaultsObject



138
139
140
# File 'lib/arcade/base.rb', line 138

def defaults
  @default_block
end

.has_defaults?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/arcade/base.rb', line 134

def has_defaults?
  !@default_block.nil?
end

.set_defaults(&block) ⇒ Object



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

def set_defaults &block
  @default_block = block
end

Instance Method Details

#_update(dt) ⇒ Object



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

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



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

def bottom
  self.y + height
end

#can_collide_with?(klass) ⇒ Boolean

Returns:

  • (Boolean)


226
227
228
# File 'lib/arcade/base.rb', line 226

def can_collide_with? klass
  @collision_listeners.has_key? klass
end

#collided_with(other) ⇒ Object



234
235
236
237
238
239
240
# File 'lib/arcade/base.rb', line 234

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)


242
243
244
245
246
247
248
249
250
251
# File 'lib/arcade/base.rb', line 242

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

#configObject



163
164
165
# File 'lib/arcade/base.rb', line 163

def config
  self
end

#drawObject



167
168
169
# File 'lib/arcade/base.rb', line 167

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

#hit_edge(edge) ⇒ Object

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



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

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

#key_pressed(key) ⇒ Object



222
223
224
# File 'lib/arcade/base.rb', line 222

def key_pressed key
  instance_eval &@keypress_listeners[key]
end

#leftObject



194
195
196
# File 'lib/arcade/base.rb', line 194

def left
  self.x
end

#move_down(pixels) ⇒ Object



206
207
208
# File 'lib/arcade/base.rb', line 206

def move_down pixels
  self.y += pixels
end

#move_left(pixels) ⇒ Object



210
211
212
# File 'lib/arcade/base.rb', line 210

def move_left pixels
  self.x -= pixels
end

#move_right(pixels) ⇒ Object



214
215
216
# File 'lib/arcade/base.rb', line 214

def move_right pixels
  self.x += pixels
end

#move_up(pixels) ⇒ Object



202
203
204
# File 'lib/arcade/base.rb', line 202

def move_up pixels
  self.y -= pixels
end

#on_collides_with(klass, &block) ⇒ Object



230
231
232
# File 'lib/arcade/base.rb', line 230

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

#on_hit_edge(&block) ⇒ Object



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

def on_hit_edge &block
  @edge_callback = block
end

#on_keypress(key, &block) ⇒ Object



218
219
220
# File 'lib/arcade/base.rb', line 218

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

#rightObject



198
199
200
# File 'lib/arcade/base.rb', line 198

def right
  self.x + width
end

#setup_defaultsObject



143
144
145
146
147
148
149
150
151
# File 'lib/arcade/base.rb', line 143

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



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

def top
  self.y
end