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,38 @@
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
+ module Traits
25
+ module Input
26
+
27
+ def input=(input_map)
28
+ @input = input_map
29
+ @parent.add_input_client(self) if @parent
30
+ end
31
+
32
+ def input
33
+ @input
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,53 @@
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
+ module Traits
25
+ #
26
+ # A chingu trait providing easier handling of the "retrofy" effect (non-blurry zoom)
27
+ # Aims to help out when using zoom-factor to create a retrofeeling with big pixels.
28
+ # Provides screen_x and screen_y which takes the zoom into account
29
+ # Also provides new code for draw() which uses screen_x / screen_y instead of x / y
30
+ #
31
+ module Retrofy
32
+
33
+ def setup_trait(options)
34
+ @retrofy_options = {:debug => false}.merge(options)
35
+
36
+ super
37
+ end
38
+
39
+ def screen_x
40
+ (@x * self.factor).to_i
41
+ end
42
+
43
+ def screen_y
44
+ (@y * self.factor).to_i
45
+ end
46
+
47
+ def draw
48
+ @image.draw_rot(self.screen_x, self.screen_y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode)
49
+ end
50
+
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,84 @@
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
+ module Traits
25
+ module RotationCenter
26
+ #
27
+ # returns [center_x, center_y] (see Gosu::Image#draw_rot)
28
+ #
29
+ @@rotation_centers = {
30
+ :top_left => [0,0],
31
+ :left_top => [0,0],
32
+
33
+ :center_left => [0,0.5],
34
+ :middle_left => [0,0.5],
35
+ :left_center => [0,0.5],
36
+ :left_middle => [0,0.5],
37
+
38
+ :bottom_left => [0,1],
39
+ :left_bottom => [0,1],
40
+
41
+ :top_center => [0.5,0],
42
+ :top_middle => [0.5,0],
43
+ :center_top => [0.5,0],
44
+ :middle_top => [0.5,0],
45
+
46
+ :center_center => [0.5,0.5],
47
+ :middle_middle => [0.5,0.5],
48
+ :center => [0.5,0.5],
49
+ :middle => [0.5,0.5],
50
+
51
+ :bottom_center => [0.5,1],
52
+ :bottom_middle => [0.5,1],
53
+ :center_bottom => [0.5,1],
54
+ :middle_bottom => [0.5,1],
55
+
56
+ :top_right => [1,0],
57
+ :right_top => [1,0],
58
+
59
+ :center_right => [1,0.5],
60
+ :middle_right => [1,0.5],
61
+ :right_center => [1,0.5],
62
+ :right_middle => [1,0.5],
63
+
64
+ :bottom_right => [1,1],
65
+ :right_bottom => [1,1]
66
+ }
67
+
68
+ #
69
+ # Sets @center_x and @center_y according to given alignment. Available alignments are:
70
+ #
71
+ # :top_left, :center_left, :bottom_left, :top_center,
72
+ # :center_center, :bottom_center, :top_right, :center_right and :bottom_right
73
+ #
74
+ # They're also available in the opposite order with the same meaning.
75
+ # They're also available (hey, hashlookups are speedy) with "middle" instead of "center" since
76
+ # those 2 words basicly have the same meaning and are both understandable.
77
+ #
78
+ def rotation_center(alignment)
79
+ @center_x, @center_y = @@rotation_centers[alignment.to_sym]
80
+ end
81
+
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,90 @@
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
+ module Traits
25
+ #
26
+ # A chingu trait providing timer-methods to its includer, examples:
27
+ # during(300) { @color = Color.new(0xFFFFFFFF) } # forces @color to white ever update for 300 ms
28
+ # after(400) { self.destroy! } # destroy object after 400 ms
29
+ # between(1000,2000) { self.rotate(10) } # starting after 1 second, call rotate(10) each update during 1 second
30
+ #
31
+ # All the above can be combined with a 'then { do_something }'. For example, a classic shmup damage effect:
32
+ # during(100) { @color.alpha = 100 }.then { @color.alpha = 255 }
33
+ #
34
+ module Timer
35
+ def setup_trait(options)
36
+ @timer_options = {:debug => false}.merge(options)
37
+
38
+ #
39
+ # Timers are saved as an array of arrays where each entry contains:
40
+ # [start_time, end_time (or nil if one-shot), &block]
41
+ #
42
+ @_timers = Array.new
43
+ super
44
+ end
45
+
46
+ def during(time, &block)
47
+ ms = milliseconds()
48
+ @_last_timer = [ms, ms + time, block]
49
+ @_timers << @_last_timer
50
+ self
51
+ end
52
+
53
+ def after(time, &block)
54
+ ms = milliseconds()
55
+ @_last_timer = [ms + time, nil, block]
56
+ @_timers << @_last_timer
57
+ self
58
+ end
59
+
60
+ def between(start_time, end_time, &block)
61
+ ms = milliseconds()
62
+ @_last_timer = [ms + start_time, ms + end_time, block]
63
+ @_timers << @_last_timer
64
+ self
65
+ end
66
+
67
+ def then(&block)
68
+ # ...use one-shots start_time for our trailing "then".
69
+ # ...use durable timers end_time for our trailing "then".
70
+ start_time = @_last_timer[1].nil? ? @_last_timer[0] : @_last_timer[1]
71
+ @_timers << [start_time, nil, block]
72
+ end
73
+
74
+ def update_trait
75
+ ms = milliseconds()
76
+ @_timers.each do |start_time, end_time, block|
77
+ if ms > start_time && (end_time == nil || ms < end_time)
78
+ block.call
79
+ end
80
+ end
81
+
82
+ # Remove one-shot timers (only a start_time, no end_time) and all timers which have expired
83
+ @_timers.reject! { |start_time, end_time, block| (ms > start_time && end_time == nil) || (end_time != nil && ms > end_time) }
84
+
85
+ super
86
+ end
87
+
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,67 @@
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
+ module Traits
25
+ #
26
+ # A chingu trait providing velocity and acceleration logic.
27
+ #
28
+ module Velocity
29
+ attr_accessor :velocity_x, :velocity_y, :acceleration_x, :acceleration_y, :max_velocity
30
+
31
+ #def self.initialize_trait(options)
32
+ # @velocity_options = {:debug => false}.merge(options)
33
+ # puts "Velocity#initialize" if @velocity_options[:debug]
34
+ # super
35
+ #end
36
+
37
+ def setup_trait(options)
38
+ @velocity_options = {:debug => false}.merge(options)
39
+ puts "Velocity#setup" if @velocity_options[:debug]
40
+
41
+ @velocity_x = options[:velocity_x] || 0
42
+ @velocity_y = options[:velocity_y] || 0
43
+ @acceleration_x = options[:acceleration_x] || 0
44
+ @acceleration_y = options[:acceleration_y] || 0
45
+ @max_velocity = options[:max_velocity] || 1000
46
+ super
47
+ end
48
+
49
+ #
50
+ # Modifies X & Y of parent
51
+ #
52
+ def update_trait
53
+ puts "Velocity#update" if @velocity_options[:debug]
54
+
55
+ @velocity_y += @acceleration_y if (@velocity_y + @acceleration_y).abs < @max_velocity
56
+ @velocity_x += @acceleration_x if (@velocity_x + @acceleration_x).abs < @max_velocity
57
+ @y += @velocity_y
58
+ @x += @velocity_x
59
+ super
60
+ end
61
+
62
+ def stop
63
+ @acceleration_y = @acceleration_x = @velocity_y = @acceleration_y = 0
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,170 @@
1
+ module Chingu
2
+
3
+ class Window < Gosu::Window
4
+ # adds push_game_state, pop_game_state, current_game_state and previous_game_state
5
+ include Chingu::GameStateHelpers
6
+
7
+ # adds fill() etc...
8
+ include Chingu::GFXHelpers
9
+
10
+ # adds game_objects_of_class etc ...
11
+ include Chingu::GameObjectHelpers
12
+
13
+ # Input dispatch helpers
14
+ include Chingu::InputDispatcher
15
+
16
+ # input= and input
17
+ include Chingu::InputClient
18
+
19
+ attr_reader :root, :game_state_manager, :game_objects, :milliseconds_since_last_tick
20
+
21
+ #
22
+ # See http://www.libgosu.org/rdoc/classes/Gosu/Window.html
23
+ #
24
+ # On top of that we add:
25
+ # - Default widht / height, --fullscreen option from console
26
+ # - Global variable $window
27
+ # - Standard #update which updates all Chingu::GameObject's
28
+ # - Standard #draw which goes through
29
+ # - Assethandling with Image["picture.png"] and Sample["shot.wav"]
30
+ # - Default input mapping escape to close
31
+ #
32
+ def initialize(width = 800, height = 600, fullscreen = false, update_interval = 16.666666)
33
+ fullscreen ||= ARGV.include?("--fullscreen")
34
+ $window = super(width, height, fullscreen, update_interval)
35
+
36
+ @root = File.dirname(File.expand_path($0))
37
+ Gosu::Image.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")]
38
+ Gosu::Sample.autoload_dirs = [".", File.join(@root, "sound"), File.join(@root, "media")]
39
+ Gosu::Tile.autoload_dirs = [".", File.join(@root, "gfx"), File.join(@root, "media")]
40
+ Gosu::Song.autoload_dirs = [".", File.join(@root, "sfx"), File.join(@root, "media")]
41
+
42
+ @game_objects = GameObjectList.new
43
+ @input_clients = Set.new # Set is like a unique Array with Hash lookupspeed
44
+
45
+ @fps_counter = FPSCounter.new
46
+ @game_state_manager = GameStateManager.new
47
+ @milliseconds_since_last_tick = 0
48
+ end
49
+
50
+ def current_parent
51
+ game_state_manager.current_game_state || self
52
+ end
53
+
54
+ #
55
+ # Frames per second, access with $window.fps or $window.framerate
56
+ #
57
+ def fps
58
+ @fps_counter.fps
59
+ end
60
+ alias :framerate :fps
61
+
62
+ #
63
+ # Total amount of game iterations (ticks)
64
+ #
65
+ def ticks
66
+ @fps_counter.ticks
67
+ end
68
+
69
+ #
70
+ # Mathematical short name for "milliseconds since last tick"
71
+ #
72
+ def dt
73
+ @milliseconds_since_last_tick
74
+ end
75
+
76
+ #
77
+ # Chingus core-logic / loop. Gosu will call this each game-iteration.
78
+ #
79
+ def update
80
+ #
81
+ # Register a tick with our rather standard tick/framerate counter.
82
+ # Returns the amount of milliseconds since last tick. This number is used in all update()-calls.
83
+ # Without this self.fps would return an incorrect value.
84
+ # If you override this in your Chingu::Window class, make sure to call super.
85
+ #
86
+ @milliseconds_since_last_tick = @fps_counter.register_tick
87
+
88
+ intermediate_update
89
+ end
90
+
91
+ #
92
+ # "game logic" update that is safe to call even between Gosus update-calls
93
+ #
94
+ def intermediate_update
95
+ #
96
+ # Dispatch inputmap for main window
97
+ #
98
+ dispatch_input_for(self)
99
+
100
+ #
101
+ # Dispatch input for all input-clients handled by to main window (game objects with input created in main win)
102
+ #
103
+ @input_clients.each { |game_object| dispatch_input_for(game_object) }
104
+
105
+
106
+ #
107
+ # Call update() on all game objects belonging to the main window.
108
+ #
109
+ update_game_objects
110
+
111
+ #
112
+ # Call update() on all game objects belonging to the current game state.
113
+ #
114
+ update_game_state_manager
115
+ end
116
+
117
+ #
118
+ # Chingus main screen manupulation method.
119
+ # If you override this in your Chingu::Window class, make sure to call super.
120
+ # Gosu will call this each game-iteration just after #update
121
+ #
122
+ def draw
123
+ #
124
+ # Draw all game objects associated with the main window.
125
+ #
126
+ @game_objects.draw
127
+
128
+ #
129
+ # Let the game state manager call draw on the active game state (if any)
130
+ #
131
+ @game_state_manager.draw
132
+ end
133
+
134
+ #
135
+ # Call update() on all game objects in main game window.
136
+ #
137
+ def update_game_objects
138
+ @game_objects.update
139
+ end
140
+
141
+ #
142
+ # Call update() on our game_state_manger
143
+ # -> call update on active state
144
+ # -> call update on all game objects in that state
145
+ #
146
+ def update_game_state_manager
147
+ @game_state_manager.update
148
+ end
149
+
150
+ #
151
+ # By default button_up sends the keyevent to the GameStateManager
152
+ # .. Which then is responsible to send it to the right GameState(s)
153
+ #
154
+ def button_up(id)
155
+ dispatch_button_up(id, self)
156
+ @input_clients.each { |object| dispatch_button_up(id, object) }
157
+ @game_state_manager.button_up(id)
158
+ end
159
+
160
+ #
161
+ # By default button_down sends the keyevent to the GameStateManager
162
+ # .. Which then is responsible to send it to the right GameState(s)
163
+ #
164
+ def button_down(id)
165
+ dispatch_button_down(id, self)
166
+ @input_clients.each { |object| dispatch_button_down(id, object) }
167
+ @game_state_manager.button_down(id)
168
+ end
169
+ end
170
+ end