Class: RubyGame::MovingObject

Inherits:
StaticObject show all
Defined in:
lib/ruby_game/moving_object.rb

Direct Known Subclasses

Monster, Player

Instance Attribute Summary

Attributes inherited from StaticObject

#x, #y

Instance Method Summary collapse

Methods inherited from StaticObject

#draw, #init_image

Constructor Details

#initialize(x, y, image_name) ⇒ MovingObject

Returns a new instance of MovingObject.



3
4
5
6
# File 'lib/ruby_game/moving_object.rb', line 3

def initialize(x, y, image_name)
  super
  @velocity = 2
end

Instance Method Details

#init_limits(max_width, max_height, border_with, border_top_with) ⇒ Object



8
9
10
11
# File 'lib/ruby_game/moving_object.rb', line 8

def init_limits(max_width, max_height, border_with, border_top_with)
  @max_width, @max_height = max_width, max_height
  @border_with, @border_top_with = border_with, border_top_with
end

#move_down(velocity = @velocity) ⇒ Object



29
30
31
# File 'lib/ruby_game/moving_object.rb', line 29

def move_down(velocity = @velocity)
  @y += velocity if @y < @max_height - @border_with - (@image.height/2)
end

#move_left(velocity = @velocity) ⇒ Object



17
18
19
# File 'lib/ruby_game/moving_object.rb', line 17

def move_left(velocity = @velocity)
  @x -= velocity if @x > @border_with + (@image.width/2)
end

#move_right(velocity = @velocity) ⇒ Object



21
22
23
# File 'lib/ruby_game/moving_object.rb', line 21

def move_right(velocity = @velocity)
  @x += velocity if @x < @max_width - @border_with - (@image.width/2)
end

#move_up(velocity = @velocity) ⇒ Object



25
26
27
# File 'lib/ruby_game/moving_object.rb', line 25

def move_up(velocity = @velocity)
  @y -= velocity if @y > @border_with + @border_top_with + (@image.height/2)
end

#touch?(object) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ruby_game/moving_object.rb', line 13

def touch?(object)
  Math.hypot(object.x - @x, object.y - @y) <= object.width/2
end