Class: Rong::Elements::Ball

Inherits:
Object
  • Object
show all
Includes:
Entity
Defined in:
lib/rong/elements/ball.rb

Constant Summary collapse

WIDTH =
8
HEIGHT =
8
SPEED =
10

Instance Attribute Summary collapse

Attributes included from Entity

#height, #width, #x, #y

Instance Method Summary collapse

Methods included from Entity

#bottom, #intersects?, #left, #move_to, #right, #top

Constructor Details

#initialize(start_x, start_y, angle) ⇒ Ball

Returns a new instance of Ball.



13
14
15
16
17
# File 'lib/rong/elements/ball.rb', line 13

def initialize(start_x, start_y, angle)
  self.angle = angle
  self.x_direction = self.y_direction = 1
  super(start_x, start_y)
end

Instance Attribute Details

#angleObject

Returns the value of attribute angle.



11
12
13
# File 'lib/rong/elements/ball.rb', line 11

def angle
  @angle
end

#x_directionObject

Returns the value of attribute x_direction.



11
12
13
# File 'lib/rong/elements/ball.rb', line 11

def x_direction
  @x_direction
end

#y_directionObject

Returns the value of attribute y_direction.



11
12
13
# File 'lib/rong/elements/ball.rb', line 11

def y_direction
  @y_direction
end

Instance Method Details

#moveObject



28
29
30
31
# File 'lib/rong/elements/ball.rb', line 28

def move
  self.x += SPEED * Math.cos(angle_radians) * x_direction
  self.y += SPEED * Math.sin(angle_radians) * y_direction
end

#reflect_x(x_position = nil) ⇒ Object



19
20
21
22
# File 'lib/rong/elements/ball.rb', line 19

def reflect_x(x_position=nil)
  move_to(x_position, y) if x_position
  self.x_direction *= -1
end

#reflect_yObject



24
25
26
# File 'lib/rong/elements/ball.rb', line 24

def reflect_y
  self.y_direction *= -1
end

#serve_from(x, y) ⇒ Object



55
56
57
# File 'lib/rong/elements/ball.rb', line 55

def serve_from(x, y)
  rand(2) == 1 ? serve_right_from(x, y) : serve_left_from(x, y)
end

#serve_left_from(x, y) ⇒ Object



41
42
43
44
45
46
# File 'lib/rong/elements/ball.rb', line 41

def serve_left_from(x, y)
  move_to(x, y)
  self.y_direction = 1
  self.x_direction = -1
  self.angle = 45
end

#serve_right_from(x, y) ⇒ Object



48
49
50
51
52
53
# File 'lib/rong/elements/ball.rb', line 48

def serve_right_from(x, y)
  move_to(x, y)
  self.y_direction = 1
  self.x_direction = 1
  self.angle = 45
end

#stop(resest_x = nil, reset_y = nil) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rong/elements/ball.rb', line 33

def stop(resest_x=nil, reset_y=nil)
  self.x_direction = self.y_direction = 0

  reset_x ||= x
  reset_y ||= y
  move_to(reset_x, reset_y)
end