Class: Rong::Elements::Game

Inherits:
Object
  • Object
show all
Includes:
WindowConstants
Defined in:
lib/rong/elements/game.rb

Constant Summary collapse

WINNING_SCORE =
12

Constants included from WindowConstants

WindowConstants::LEFT_PADDLE_X, WindowConstants::PADDLE_Y, WindowConstants::RIGHT_PADDLE_X, WindowConstants::SERVE_FROM_X, WindowConstants::SERVE_FROM_Y, WindowConstants::WINDOW_CENTER_X, WindowConstants::WINDOW_CENTER_Y, WindowConstants::WINDOW_HEIGHT, WindowConstants::WINDOW_WIDTH

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Game

Returns a new instance of Game.

Yields:

  • (_self)

Yield Parameters:



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rong/elements/game.rb', line 16

def initialize(&block)
  self.left_score = self.right_score = 0

  self.score_callbacks  = []
  self.hit_callbacks    = []
  self.bounce_callbacks = []
  self.win_callbacks    = []

  self.ball    = Ball.new(WINDOW_CENTER_X, WINDOW_CENTER_Y, 0)
  self.paddles = [Paddle.new("Player 1", :left, LEFT_PADDLE_X,  PADDLE_Y),
                  Paddle.new("Player 2", :right, RIGHT_PADDLE_X, PADDLE_Y)]
  yield self if block_given?
end

Instance Attribute Details

#ballObject

Returns the value of attribute ball.



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

def ball
  @ball
end

#bounce_callbacksObject

Returns the value of attribute bounce_callbacks.



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

def bounce_callbacks
  @bounce_callbacks
end

#hit_callbacksObject

Returns the value of attribute hit_callbacks.



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

def hit_callbacks
  @hit_callbacks
end

#left_scoreObject

Returns the value of attribute left_score.



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

def left_score
  @left_score
end

#paddlesObject

Returns the value of attribute paddles.



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

def paddles
  @paddles
end

#right_scoreObject

Returns the value of attribute right_score.



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

def right_score
  @right_score
end

#score_callbacksObject

Returns the value of attribute score_callbacks.



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

def score_callbacks
  @score_callbacks
end

#win_callbacksObject

Returns the value of attribute win_callbacks.



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

def win_callbacks
  @win_callbacks
end

#winnerObject

Returns the value of attribute winner.



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

def winner
  @winner
end

Instance Method Details

#bounceObject



80
81
82
# File 'lib/rong/elements/game.rb', line 80

def bounce
  bounce_callbacks.each {|cb| cb.call }
end

#check_ball_boundsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rong/elements/game.rb', line 108

def check_ball_bounds
  if ball.top < 0 || ball.bottom > WINDOW_HEIGHT
    bounce
    ball.reflect_y
  end

  if ball.left < 0 || ball.right > WINDOW_WIDTH
    if ball.left < 0
      self.right_score += 1
      ball.serve_left_from(SERVE_FROM_X, SERVE_FROM_Y)
    else
      self.left_score += 1
      ball.serve_right_from(SERVE_FROM_X, SERVE_FROM_Y)
    end
    score
    reset_paddles
  end
end

#check_bounds(entity) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rong/elements/game.rb', line 99

def check_bounds(entity)
  if entity.top < 0
    entity.move_to(entity.x, 0 + entity.height / 2)
  end
  if entity.bottom > WINDOW_HEIGHT
    entity.move_to(entity.x, WINDOW_HEIGHT - entity.height / 2)
  end
end

#check_win_conditionObject



127
128
129
130
131
132
133
# File 'lib/rong/elements/game.rb', line 127

def check_win_condition
  if left_score >= WINNING_SCORE
    declare_winner(left_paddle)
  elsif right_score >= WINNING_SCORE
    declare_winner(right_paddle)
  end
end

#declare_winner(who) ⇒ Object



93
94
95
96
97
# File 'lib/rong/elements/game.rb', line 93

def declare_winner(who)
  ball.stop(WINDOW_CENTER_X, WINDOW_CENTER_Y)
  self.winner = who
  win_callbacks.each {|cb| cb.call(who) }
end

#hitObject



72
73
74
# File 'lib/rong/elements/game.rb', line 72

def hit
  hit_callbacks.each {|cb| cb.call }
end

#on_bounce(&block) ⇒ Object



76
77
78
# File 'lib/rong/elements/game.rb', line 76

def on_bounce(&block)
  bounce_callbacks << block if block_given?
end

#on_hit(&block) ⇒ Object



68
69
70
# File 'lib/rong/elements/game.rb', line 68

def on_hit(&block)
  hit_callbacks << block if block_given?
end

#on_score(&block) ⇒ Object



60
61
62
# File 'lib/rong/elements/game.rb', line 60

def on_score(&block)
  score_callbacks << block if block_given?
end

#on_win(&block) ⇒ Object



84
85
86
# File 'lib/rong/elements/game.rb', line 84

def on_win(&block)
  win_callbacks << block if block_given?
end

#resetObject



53
54
55
56
57
58
# File 'lib/rong/elements/game.rb', line 53

def reset
  self.winner     = nil
  self.left_score = self.right_score = 0
  reset_paddles
  ball.serve_from(SERVE_FROM_X, SERVE_FROM_Y)
end

#reset_paddlesObject



88
89
90
91
# File 'lib/rong/elements/game.rb', line 88

def reset_paddles
  left_paddle.move_to(LEFT_PADDLE_X,  PADDLE_Y)
  right_paddle.move_to(RIGHT_PADDLE_X,  PADDLE_Y)
end

#scoreObject



64
65
66
# File 'lib/rong/elements/game.rb', line 64

def score
  score_callbacks.each {|cb| cb.call(left_score, right_score) }
end

#updateObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rong/elements/game.rb', line 39

def update
  return if winner
  ball.move
  paddles.each {|p| check_bounds(p) }
  check_ball_bounds
  check_win_condition
  paddles.each do |paddle|
    if paddle.intersects?(ball)
      paddle.hit(ball)
      hit
    end
  end
end