rong-elements 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,58 @@
1
+ module Rong
2
+ module Elements
3
+ class Ball
4
+ WIDTH = 8
5
+ HEIGHT = 8
6
+ SPEED = 10
7
+ include Entity
8
+
9
+ attr_accessor :angle, :x_direction, :y_direction
10
+
11
+ def initialize(start_x, start_y, angle)
12
+ self.angle = angle
13
+ self.x_direction = self.y_direction = 1
14
+ super(start_x, start_y)
15
+ end
16
+
17
+ def reflect_x
18
+ self.x_direction *= -1
19
+ end
20
+
21
+ def reflect_y
22
+ self.y_direction *= -1
23
+ end
24
+
25
+ def move
26
+ self.x += SPEED * Math.cos(angle_radians) * x_direction
27
+ self.y += SPEED * Math.sin(angle_radians) * y_direction
28
+ end
29
+
30
+ def stop
31
+ self.x_direction = self.y_direction = 0
32
+ end
33
+
34
+ def serve_left_from(x, y)
35
+ move_to(x, y)
36
+ self.y_direction = 1
37
+ self.x_direction = -1
38
+ self.angle = 45
39
+ end
40
+
41
+ def serve_right_from(x, y)
42
+ move_to(x, y)
43
+ self.y_direction = 1
44
+ self.x_direction = 1
45
+ self.angle = 45
46
+ end
47
+
48
+ def serve_from(x, y)
49
+ rand(2) == 1 ? serve_right_from(x, y) : serve_left_from(x, y)
50
+ end
51
+
52
+ protected
53
+ def angle_radians
54
+ angle * (Math::PI / 180)
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,58 @@
1
+ module Rong
2
+ module Elements
3
+ module Entity
4
+ attr_accessor :x, :y
5
+ attr_reader :height, :width
6
+
7
+ def initialize(start_x, start_y)
8
+ self.x = start_x
9
+ self.y = start_y
10
+ @height = self.class::HEIGHT
11
+ @width = self.class::WIDTH
12
+ end
13
+
14
+ def move_to(x, y)
15
+ self.x = x
16
+ self.y = y
17
+ end
18
+
19
+ def top
20
+ y - height / 2
21
+ end
22
+
23
+ def bottom
24
+ y + height / 2
25
+ end
26
+
27
+ def left
28
+ x - width / 2
29
+ end
30
+
31
+ def right
32
+ x + width / 2
33
+ end
34
+
35
+ def intersects?(other)
36
+ if other.left < right && other.left > left
37
+ if other.bottom < bottom && other.bottom > top
38
+ true
39
+ elsif other.top > top && other.top < bottom
40
+ true
41
+ else
42
+ false
43
+ end
44
+ elsif other.right > left && other.right < right
45
+ if other.bottom < bottom && other.bottom > top
46
+ true
47
+ elsif other.top > top && other.top < bottom
48
+ true
49
+ else
50
+ false
51
+ end
52
+ else
53
+ false
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,127 @@
1
+ require 'rong/elements/ball'
2
+ require 'rong/elements/paddle'
3
+ require 'rong/elements/window_constants'
4
+
5
+ module Rong
6
+ module Elements
7
+ class Game
8
+ include WindowConstants
9
+ WINNING_SCORE = 12
10
+
11
+ attr_accessor :paddles, :ball, :winner,
12
+ :left_score, :right_score,
13
+ :score_callbacks, :hit_callbacks,
14
+ :bounce_callbacks, :win_callbacks
15
+
16
+ def initialize
17
+ self.left_score = self.right_score = 0
18
+
19
+ self.score_callbacks = []
20
+ self.hit_callbacks = []
21
+ self.bounce_callbacks = []
22
+ self.win_callbacks = []
23
+
24
+ self.ball = Ball.new(WINDOW_CENTER_X, WINDOW_CENTER_Y, 0)
25
+ self.paddles = [Paddle.new("Player 1", LEFT_PADDLE_X, PADDLE_Y),
26
+ Paddle.new("Player 2", RIGHT_PADDLE_X, PADDLE_Y)]
27
+ end
28
+
29
+ def update
30
+ return if winner
31
+ ball.move
32
+ paddles.each {|p| check_bounds(p) }
33
+ check_ball_bounds
34
+ check_win_condition
35
+ paddles.each do |paddle|
36
+ if paddle.intersects?(ball)
37
+ paddle.hit(ball)
38
+ hit
39
+ end
40
+ end
41
+ end
42
+
43
+ def reset
44
+ self.winner = nil
45
+ self.left_score = self.right_score = 0
46
+ reset_paddles
47
+ ball.serve_from(SERVE_FROM_X, SERVE_FROM_Y)
48
+ end
49
+
50
+ def on_score(&block)
51
+ score_callbacks << block if block_given?
52
+ end
53
+
54
+ def score
55
+ score_callbacks.each {|cb| cb.call(left_score, right_score) }
56
+ end
57
+
58
+ def on_hit(&block)
59
+ hit_callbacks << block if block_given?
60
+ end
61
+
62
+ def hit
63
+ hit_callbacks.each {|cb| cb.call }
64
+ end
65
+
66
+ def on_bounce(&block)
67
+ bounce_callbacks << block if block_given?
68
+ end
69
+
70
+ def bounce
71
+ bounce_callbacks.each {|cb| cb.call }
72
+ end
73
+
74
+ def on_win(&block)
75
+ win_callbacks << block if block_given?
76
+ end
77
+
78
+ def reset_paddles
79
+ paddles.first.move_to(LEFT_PADDLE_X, PADDLE_Y)
80
+ paddles.last.move_to(RIGHT_PADDLE_X, PADDLE_Y)
81
+ end
82
+
83
+ def declare_winner(who)
84
+ ball.move_to(WINDOW_CENTER_X, WINDOW_CENTER_Y)
85
+ ball.stop
86
+ self.winner = who
87
+ win_callbacks.each {|cb| cb.call(who) }
88
+ end
89
+
90
+ def check_bounds(entity)
91
+ if entity.top < 0
92
+ entity.move_to(entity.x, 0 + entity.height / 2)
93
+ end
94
+ if entity.bottom > WINDOW_HEIGHT
95
+ entity.move_to(entity.x, WINDOW_HEIGHT - entity.height / 2)
96
+ end
97
+ end
98
+
99
+ def check_ball_bounds
100
+ if ball.top < 0 || ball.bottom > WINDOW_HEIGHT
101
+ bounce
102
+ ball.reflect_y
103
+ end
104
+
105
+ if ball.left < 0 || ball.right > WINDOW_WIDTH
106
+ if ball.left < 0
107
+ self.left_score += 1
108
+ ball.serve_right_from(SERVE_FROM_X, SERVE_FROM_Y)
109
+ else
110
+ self.right_score += 1
111
+ ball.serve_left_from(SERVE_FROM_X, SERVE_FROM_Y)
112
+ end
113
+ score
114
+ reset_paddles
115
+ end
116
+ end
117
+
118
+ def check_win_condition
119
+ if left_score >= WINNING_SCORE
120
+ declare_winner(paddles.first)
121
+ elsif right_score >= WINNING_SCORE
122
+ declare_winner(paddles.last)
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,35 @@
1
+ module Rong
2
+ module Elements
3
+ class Paddle
4
+ WIDTH = 10
5
+ HEIGHT = 50
6
+ SPEED = 10
7
+ include Entity
8
+
9
+ attr_accessor :name
10
+
11
+ def initialize(name, start_x, start_y)
12
+ self.name = name
13
+ super(start_x, start_y)
14
+ end
15
+
16
+ def move_up
17
+ self.y -= SPEED
18
+ end
19
+
20
+ def move_down
21
+ self.y += SPEED
22
+ end
23
+
24
+ def hit(ball)
25
+ ball.reflect_x
26
+ if ball.y < y
27
+ ball.reflect_y
28
+ end
29
+
30
+ strength = (y - ball.y).abs / (height / 2)
31
+ ball.angle = 45 * strength
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ module Rong
2
+ module Elements
3
+ module WindowConstants
4
+ WINDOW_WIDTH = 640
5
+ WINDOW_HEIGHT = 480
6
+ WINDOW_CENTER_X = WINDOW_WIDTH / 2
7
+ WINDOW_CENTER_Y = WINDOW_HEIGHT / 2
8
+
9
+ LEFT_PADDLE_X = 100
10
+ RIGHT_PADDLE_X = 540
11
+ PADDLE_Y = 240
12
+
13
+ SERVE_FROM_X = WINDOW_CENTER_X
14
+ SERVE_FROM_Y = WINDOW_CENTER_Y - 210
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ require 'rong/elements/window_constants'
2
+ require 'rong/elements/entity'
3
+ require 'rong/elements/ball'
4
+ require 'rong/elements/paddle'
5
+ require 'rong/elements/game'
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Matt Yoho
@@ -14,23 +14,22 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-09-03 00:00:00 -05:00
17
+ date: 2011-04-07 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rspec
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ~>
26
27
  - !ruby/object:Gem::Version
27
28
  segments:
28
29
  - 2
30
+ - 5
29
31
  - 0
30
- - 0
31
- - beta
32
- - 19
33
- version: 2.0.0.beta.19
32
+ version: 2.5.0
34
33
  type: :development
35
34
  version_requirements: *id001
36
35
  description: Rong-elements contains shared game models and utilities for Rong, a Ruby implementation of Pong that consists of a server module for managing games, a client module for for interfacing with the server, and hopefully a few client implementations for playing some rad Pong, brah.
@@ -42,11 +41,12 @@ extensions: []
42
41
  extra_rdoc_files: []
43
42
 
44
43
  files:
45
- - lib/rong/elements/behavior.rb
46
- - lib/rong/elements/entities/ball.rb
47
- - lib/rong/elements/entities/game_state.rb
48
- - lib/rong/elements/entities/paddle.rb
49
- - lib/rong/elements/entities.rb
44
+ - lib/rong/elements/ball.rb
45
+ - lib/rong/elements/entity.rb
46
+ - lib/rong/elements/game.rb
47
+ - lib/rong/elements/paddle.rb
48
+ - lib/rong/elements/window_constants.rb
49
+ - lib/rong/elements.rb
50
50
  has_rdoc: true
51
51
  homepage: http://github.com/mattyoho/rong
52
52
  licenses: []
@@ -57,6 +57,7 @@ rdoc_options: []
57
57
  require_paths:
58
58
  - lib
59
59
  required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
60
61
  requirements:
61
62
  - - ">="
62
63
  - !ruby/object:Gem::Version
@@ -66,18 +67,19 @@ required_ruby_version: !ruby/object:Gem::Requirement
66
67
  - 7
67
68
  version: 1.8.7
68
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
+ none: false
69
71
  requirements:
70
72
  - - ">="
71
73
  - !ruby/object:Gem::Version
72
74
  segments:
73
75
  - 1
74
76
  - 3
75
- - 6
76
- version: 1.3.6
77
+ - 7
78
+ version: 1.3.7
77
79
  requirements: []
78
80
 
79
81
  rubyforge_project:
80
- rubygems_version: 1.3.6
82
+ rubygems_version: 1.3.7
81
83
  signing_key:
82
84
  specification_version: 3
83
85
  summary: Ruby-based Pong server implementation
@@ -1 +0,0 @@
1
- require 'rong/elements/behavior/bounding'
@@ -1,13 +0,0 @@
1
- module Rong
2
- module Elements
3
- module Entities
4
- class Ball
5
- attr_reader :x, :y
6
-
7
- def initialize(x=0, y=0)
8
- @x, @y = x, y
9
- end
10
- end
11
- end
12
- end
13
- end
@@ -1,30 +0,0 @@
1
- module Rong
2
- module Elements
3
- module Entities
4
- class GameState
5
- attr_reader :paddle_positions, :ball_positions
6
-
7
- def initialize(paddle_positions=[0,0], ball_positions=[0,0])
8
- @ball_positions = ball_positions
9
- @paddle_positions = paddle_positions
10
- end
11
-
12
- %w(first second third fourth).each_with_index do |ordinal, index|
13
- define_method("#{ordinal}_paddle_position") do
14
- @paddle_positions[index]
15
- end
16
-
17
- define_method("#{ordinal}_paddle_position=") do |value|
18
- @paddle_positions[index] = value
19
- end
20
- end
21
-
22
- def ball_position
23
- position = ball_positions.first
24
- return position if position.kind_of? Array
25
- ball_positions
26
- end
27
- end
28
- end
29
- end
30
- end
@@ -1,26 +0,0 @@
1
- module Rong
2
- module Elements
3
- module Entities
4
- class Paddle
5
- attr_reader :velocity, :y
6
-
7
- def initialize(y_coord)
8
- @velocity = 0
9
- @y = y_coord
10
- end
11
-
12
- def up
13
- @velocity = 1
14
- end
15
-
16
- def down
17
- @velocity = -1
18
- end
19
-
20
- def rest
21
- @velocity = 0
22
- end
23
- end
24
- end
25
- end
26
- end
@@ -1,3 +0,0 @@
1
- require 'rong/elements/entities/ball'
2
- require 'rong/elements/entities/paddle'
3
- require 'rong/elements/entities/game_state'