chingu 0.5.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. data.tar.gz.sig +0 -0
  2. data/History.txt +21 -0
  3. data/LICENSE +504 -0
  4. data/Manifest.txt +72 -0
  5. data/README.rdoc +588 -0
  6. data/Rakefile +19 -0
  7. data/benchmarks/README.txt +1 -0
  8. data/benchmarks/benchmark.rb +6 -0
  9. data/benchmarks/benchmark3.rb +23 -0
  10. data/benchmarks/benchmark4.rb +71 -0
  11. data/benchmarks/benchmark5.rb +91 -0
  12. data/benchmarks/benchmark6.rb +23 -0
  13. data/benchmarks/meta_benchmark.rb +67 -0
  14. data/benchmarks/meta_benchmark2.rb +39 -0
  15. data/chingu.gemspec +34 -0
  16. data/examples/example1.rb +37 -0
  17. data/examples/example10.rb +75 -0
  18. data/examples/example11.rb +51 -0
  19. data/examples/example12.rb +67 -0
  20. data/examples/example2.rb +115 -0
  21. data/examples/example3.rb +40 -0
  22. data/examples/example4.rb +175 -0
  23. data/examples/example5.rb +107 -0
  24. data/examples/example6.rb +57 -0
  25. data/examples/example7.rb +133 -0
  26. data/examples/example8.rb +109 -0
  27. data/examples/example9.rb +106 -0
  28. data/examples/media/Parallax-scroll-example-layer-0.png +0 -0
  29. data/examples/media/Parallax-scroll-example-layer-1.png +0 -0
  30. data/examples/media/Parallax-scroll-example-layer-2.png +0 -0
  31. data/examples/media/Parallax-scroll-example-layer-3.png +0 -0
  32. data/examples/media/background1.png +0 -0
  33. data/examples/media/fire_bullet.png +0 -0
  34. data/examples/media/fireball.png +0 -0
  35. data/examples/media/particle.png +0 -0
  36. data/examples/media/ruby.png +0 -0
  37. data/examples/media/spaceship.png +0 -0
  38. data/examples/media/stickfigure.bmp +0 -0
  39. data/examples/media/stickfigure.png +0 -0
  40. data/examples/media/video_games.png +0 -0
  41. data/lib/chingu.rb +32 -0
  42. data/lib/chingu/actor.rb +17 -0
  43. data/lib/chingu/animation.rb +142 -0
  44. data/lib/chingu/assets.rb +64 -0
  45. data/lib/chingu/basic_game_object.rb +132 -0
  46. data/lib/chingu/core_extensions.rb +53 -0
  47. data/lib/chingu/effects.rb +36 -0
  48. data/lib/chingu/fpscounter.rb +62 -0
  49. data/lib/chingu/game_object.rb +127 -0
  50. data/lib/chingu/game_object_list.rb +91 -0
  51. data/lib/chingu/game_state.rb +137 -0
  52. data/lib/chingu/game_state_manager.rb +284 -0
  53. data/lib/chingu/game_states/debug.rb +65 -0
  54. data/lib/chingu/game_states/fade_to.rb +91 -0
  55. data/lib/chingu/game_states/pause.rb +57 -0
  56. data/lib/chingu/gfx_helpers.rb +89 -0
  57. data/lib/chingu/helpers.rb +166 -0
  58. data/lib/chingu/inflector.rb +34 -0
  59. data/lib/chingu/input.rb +100 -0
  60. data/lib/chingu/named_resource.rb +254 -0
  61. data/lib/chingu/parallax.rb +83 -0
  62. data/lib/chingu/particle.rb +21 -0
  63. data/lib/chingu/rect.rb +612 -0
  64. data/lib/chingu/require_all.rb +133 -0
  65. data/lib/chingu/text.rb +56 -0
  66. data/lib/chingu/traits/collision_detection.rb +172 -0
  67. data/lib/chingu/traits/effect.rb +113 -0
  68. data/lib/chingu/traits/input.rb +38 -0
  69. data/lib/chingu/traits/retrofy.rb +53 -0
  70. data/lib/chingu/traits/rotation_center.rb +84 -0
  71. data/lib/chingu/traits/timer.rb +90 -0
  72. data/lib/chingu/traits/velocity.rb +67 -0
  73. data/lib/chingu/window.rb +170 -0
  74. metadata +162 -0
  75. metadata.gz.sig +1 -0
@@ -0,0 +1,284 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ module Chingu
24
+ #
25
+ # GameStateManger is responsible for keeping track of game states with a simple pop/push stack.
26
+ #
27
+ # More about the concept of states in games:
28
+ # http://gamedevgeek.com/tutorials/managing-game-states-in-c/
29
+ # http://www.gamedev.net/community/forums/topic.asp?topic_id=477320
30
+ #
31
+ # Chingu::Window automatically creates a @game_state_manager and makes it accessible in our game loop.
32
+ # By default the game loop calls update, draw, button_up(id) and button_down(id) on the active state.
33
+ #
34
+ # ==== Chingu Examples
35
+ #
36
+ # Enter a new game state, Level, don't call finalize() on the game state we're leaving.
37
+ # push_game_state(Level, :finalize => false)
38
+ #
39
+ # Return to the previous game state, don't call setup() on it when it becomes active.
40
+ # pop_game_state(:setup => false)
41
+ #
42
+ # If you want to use Chingus GameStateManager _without_ Chingu::Windoe, see example5.rb
43
+ #
44
+ class GameStateManager
45
+ attr_accessor :inside_state
46
+
47
+ def initialize
48
+ @inside_state = nil
49
+ @game_states = []
50
+ @transitional_game_state = nil
51
+ @transitional_game_state_options = {}
52
+ @previous_game_state = nil
53
+ end
54
+
55
+ #
56
+ # Gets the currently active gamestate (top of stack)
57
+ #
58
+ def current_game_state
59
+ @game_states.last
60
+ end
61
+ alias :current current_game_state
62
+
63
+ #
64
+ # Returns all gamestates with currenlty active game state on top.
65
+ #
66
+ def game_states
67
+ @game_states.reverse
68
+ end
69
+
70
+ #
71
+ # Sets a game state to be called between the old and the new game state
72
+ # whenever a game state is switched,pushed or popped.
73
+ #
74
+ # The transitional game state is responsible for switching to the "new game state".
75
+ # It should do so with ":transitional => false" not to create an infinite loop.
76
+ #
77
+ # The new game state is the first argument to the transitional game states initialize().
78
+ #
79
+ # Example:
80
+ # transitional_game_state(FadeIn)
81
+ # push_game_state(Level2)
82
+ #
83
+ # would in practice become:
84
+ #
85
+ # push_game_state(FadeIn.new(Level2))
86
+ #
87
+ # This would be the case for every game state change until the transitional game state is removed:
88
+ # transitional_game_state(nil) # or false
89
+ #
90
+ # Very useful for fading effect between scenes.
91
+ #
92
+ def transitional_game_state(game_state, options = {})
93
+ @transitional_game_state = game_state
94
+ @transitional_game_state_options = options
95
+ end
96
+
97
+ #
98
+ # Switch to a given game state, _replacing_ the current active one.
99
+ # By default setup() is called on the game state we're switching _to_.
100
+ # .. and finalize() is called on the game state we're switching _from_.
101
+ #
102
+ def switch_game_state(state, options = {})
103
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
104
+
105
+ new_state = game_state_instance(state)
106
+
107
+ if new_state
108
+ # Make sure the game state knows about the manager
109
+ new_state.game_state_manager = self
110
+
111
+
112
+ # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
113
+ @previous_game_state = current_game_state
114
+ current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
115
+
116
+ # Call setup
117
+ new_state.setup if new_state.respond_to?(:setup) && options[:setup]
118
+
119
+ if @transitional_game_state && options[:transitional]
120
+ # If we have a transitional, switch to that instead, with new_state as first argument
121
+ transitional_game_state = @transitional_game_state.new(new_state, @transitional_game_state_options)
122
+ self.switch_game_state(transitional_game_state, :transitional => false)
123
+ else
124
+ if current_game_state.nil?
125
+ @game_states << new_state
126
+ else
127
+ # Replace last (active) state with new one
128
+ @game_states[-1] = new_state
129
+ end
130
+ end
131
+ self.inside_state = current_game_state
132
+ end
133
+ end
134
+ alias :switch :switch_game_state
135
+
136
+ #
137
+ # Adds a state to the game state-stack and activates it.
138
+ # By default setup() is called on the new game state
139
+ # .. and finalize() is called on the game state we're leaving.
140
+ #
141
+ def push_game_state(state, options = {})
142
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
143
+
144
+ new_state = game_state_instance(state)
145
+
146
+ if new_state
147
+ # Call setup
148
+ new_state.setup if new_state.respond_to?(:setup) && options[:setup]
149
+
150
+ # Make sure the game state knows about the manager
151
+ new_state.game_state_manager = self
152
+
153
+ # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
154
+ @previous_game_state = current_game_state
155
+ current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
156
+
157
+ if @transitional_game_state && options[:transitional]
158
+ # If we have a transitional, push that instead, with new_state as first argument
159
+ transitional_game_state = @transitional_game_state.new(new_state, @transitional_game_state_options)
160
+ self.push_game_state(transitional_game_state, :transitional => false)
161
+ else
162
+ # Push new state on top of stack and therefore making it active
163
+ @game_states.push(new_state)
164
+ end
165
+ self.inside_state = current_game_state
166
+ end
167
+ end
168
+ alias :push :push_game_state
169
+
170
+ #
171
+ # Pops a state off the game state-stack, activating the previous one.
172
+ # By default setup() is called on the game state that becomes active.
173
+ # .. and finalize() is called on the game state we're leaving.
174
+ #
175
+ def pop_game_state(options = {})
176
+ options = {:setup => true, :finalize => true, :transitional => true}.merge(options)
177
+
178
+ #
179
+ # Give the soon-to-be-disabled state a chance to clean up by calling finalize() on it.
180
+ #
181
+ @previous_game_state = current_game_state
182
+ current_game_state.finalize if current_game_state.respond_to?(:finalize) && options[:finalize]
183
+
184
+ #
185
+ # Activate the game state "bellow" current one with a simple Array.pop
186
+ #
187
+ @game_states.pop
188
+
189
+ # Call setup on the new current state
190
+ current_game_state.setup if current_game_state.respond_to?(:setup) && options[:setup]
191
+
192
+ if @transitional_game_state && options[:transitional]
193
+ # If we have a transitional, push that instead, with new_state as first argument
194
+ transitional_game_state = @transitional_game_state.new(current_game_state, @transitional_game_state_options)
195
+ self.switch_game_state(transitional_game_state, :transitional => false)
196
+ end
197
+ self.inside_state = current_game_state
198
+ end
199
+ alias :pop :pop_game_state
200
+
201
+ #
202
+ # Returns the previous game state. Shortcut: "previous"
203
+ #
204
+ def previous_game_state
205
+ @previous_game_state
206
+ end
207
+ alias :previous previous_game_state
208
+
209
+ #
210
+ # Remove all game states from stack. Shortcut: "clear"
211
+ #
212
+ def clear_game_states
213
+ @game_states.clear
214
+ self.inside_state = nil
215
+ end
216
+ alias :clear :clear_game_states
217
+
218
+ #
219
+ # Pops through all game states until matching a given game state
220
+ #
221
+ def pop_until_game_state(new_state)
222
+ while (state = @game_states.pop)
223
+ break if state == new_state
224
+ end
225
+ end
226
+
227
+ #
228
+ # This method should be called from button_down(id) inside your main loop.
229
+ # Enables the game state manager to call button_down(id) on active game state.
230
+ #
231
+ # If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
232
+ #
233
+ def button_down(id)
234
+ current_game_state.button_down(id) if current_game_state
235
+ end
236
+
237
+ #
238
+ # This method should be called from button_up(id) inside your main loop.
239
+ # Enables the game state manager to call button_up(id) on active game state.
240
+ #
241
+ # If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
242
+ #
243
+ def button_up(id)
244
+ current_game_state.button_up(id) if current_game_state
245
+ end
246
+
247
+ #
248
+ # This method should be called from update() inside your main loop.
249
+ # Enables the game state manager to call update() on active game state.
250
+ #
251
+ # If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
252
+ #
253
+ def update
254
+ current_game_state.update if current_game_state
255
+ end
256
+
257
+ #
258
+ # This method should be called from draw() inside your main loop.
259
+ # Enables the game state manager to call update() on active game state.
260
+ #
261
+ # If you're using Chingu::Window instead of Gosu::Window this will automaticly be called.
262
+ #
263
+ def draw
264
+ current_game_state.draw if current_game_state
265
+ end
266
+
267
+ private
268
+
269
+ #
270
+ # Returns a GameState-instance from either a GameState class or GameState-object
271
+ #
272
+ def game_state_instance(state)
273
+ new_state = state
274
+ #
275
+ # If state is a GameState-instance, just queue it.
276
+ # If state is a GameState-class, instance it.
277
+ #
278
+ new_state = state.new if state.is_a? Class
279
+
280
+ return new_state # if new_state.kind_of? Chingu::GameState # useless check.
281
+ end
282
+
283
+ end
284
+ end
@@ -0,0 +1,65 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ #
24
+ # Debug game state (F1 is default key to start/exit debug win, 'p' to pause game)
25
+ #
26
+ module Chingu
27
+ module GameStates
28
+ class Debug < Chingu::GameState
29
+ def initialize(options)
30
+ super
31
+ @white = Color.new(255,255,255,255)
32
+ @fade_color = Gosu::Color.new(100,255,255,255)
33
+
34
+ @font = Gosu::Font.new($window, default_font_name, 15)
35
+ @paused = true
36
+
37
+ self.input = {:p => :pause, :f1 => :return_to_game, :esc => :return_to_game}
38
+ end
39
+
40
+ def return_to_game
41
+ game_state_manager.pop_game_state
42
+ end
43
+
44
+ def pause
45
+ @paused = @paused ? false : true
46
+ end
47
+
48
+ def update
49
+ game_state_manager.previous_game_state.update unless @paused
50
+ end
51
+
52
+ def draw
53
+ game_state_manager.previous_game_state.draw
54
+
55
+ $window.draw_quad( 0,0,@fade_color,
56
+ $window.width,0,@fade_color,
57
+ $window.width,$window.height,@fade_color,
58
+ 0,$window.height,@fade_color,10)
59
+
60
+ text = "DEBUG CONSOLE"
61
+ @font.draw(text, $window.width - @font.text_width(text), @font.height, 999)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,91 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+ #
24
+ # Premade game state for chingu - Fade between two game states
25
+ # Fade from the current game state to a new one whenever with:
26
+ #
27
+ # push_game_state(Chingu::GameStates::FadeTo.new(new_game_state, :speed => 3))
28
+ #
29
+ # .. Or make your whole game look better with 1 line:
30
+ #
31
+ # transitional_game_state(Chingu::GameStates::FadeTo, :speed => 10)
32
+ #
33
+ module Chingu
34
+ module GameStates
35
+ class FadeTo < Chingu::GameState
36
+
37
+ def initialize(new_game_state, options = {})
38
+ @options = {:speed => 3}.merge(options)
39
+ @new_game_state = new_game_state
40
+ @manager = options[:game_state_manager] || self
41
+ #@manager = game_state_manager
42
+ end
43
+
44
+ def setup
45
+ @color = Gosu::Color.new(0,0,0,0)
46
+ if @manager.previous_game_state
47
+ @fading_in = false
48
+ @alpha = 0.0
49
+ else
50
+ @fading_in = true
51
+ @alpha = 255.0
52
+ end
53
+ # @new_game_state.update # Make sure states game logic is run Once (for a correct draw())
54
+ update # Since draw is called before update
55
+ end
56
+
57
+ def update
58
+ @alpha += (@fading_in ? -@options[:speed] : @options[:speed])
59
+ @alpha = 0 if @alpha < 0
60
+ @alpha = 255 if @alpha > 255
61
+
62
+ @fading_in = true if @alpha == 255
63
+ @color.alpha = @alpha.to_i
64
+ @drawn = false
65
+ end
66
+
67
+ def draw
68
+ # Stop endless loops
69
+ if @drawn == false
70
+ @drawn = true
71
+
72
+ if @fading_in
73
+ @new_game_state.draw
74
+ else
75
+ @manager.previous_game_state.draw
76
+ end
77
+
78
+ $window.draw_quad( 0,0,@color,
79
+ $window.width,0,@color,
80
+ $window.width,$window.height,@color,
81
+ 0,$window.height,@color,999)
82
+ end
83
+
84
+ if @fading_in && @alpha == 0
85
+ @manager.switch_game_state(@new_game_state, :transitional => false)
86
+ end
87
+
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,57 @@
1
+ #--
2
+ #
3
+ # Chingu -- Game framework built on top of the opengl accelerated gamelib Gosu
4
+ # Copyright (C) 2009 ippa / ippa@rubylicio.us
5
+ #
6
+ # This library is free software; you can redistribute it and/or
7
+ # modify it under the terms of the GNU Lesser General Public
8
+ # License as published by the Free Software Foundation; either
9
+ # version 2.1 of the License, or (at your option) any later version.
10
+ #
11
+ # This library is distributed in the hope that it will be useful,
12
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
+ # Lesser General Public License for more details.
15
+ #
16
+ # You should have received a copy of the GNU Lesser General Public
17
+ # License along with this library; if not, write to the Free Software
18
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
+ #
20
+ #++
21
+
22
+
23
+
24
+ #
25
+ # Premade game state for chingu - A simple pause state.
26
+ # Pause whenever with:
27
+ # push_game_state(Chingu::GameStates::Pause)
28
+ #
29
+ # requires global $window
30
+ #
31
+ module Chingu
32
+ module GameStates
33
+ class Pause < Chingu::GameState
34
+ def initialize(options = {})
35
+ super
36
+ @white = Color.new(255,255,255,255)
37
+ @color = Gosu::Color.new(200,0,0,0)
38
+ @font = Gosu::Font.new($window, default_font_name, 35)
39
+ @text = "PAUSED - press key to continue"
40
+ end
41
+
42
+ def button_down(id)
43
+ game_state_manager.pop_game_state(:setup => false) # Return the previous game state, dont call setup()
44
+ end
45
+
46
+ def draw
47
+ game_state_manager.previous_game_state.draw # Draw prev game state onto screen (in this case our level)
48
+ $window.draw_quad( 0,0,@color,
49
+ $window.width,0,@color,
50
+ $window.width,$window.height,@color,
51
+ 0,$window.height,@color,10)
52
+
53
+ @font.draw(@text, ($window.width/2 - @font.text_width(@text)/2), $window.height/2 - @font.height, 999)
54
+ end
55
+ end
56
+ end
57
+ end