chingu 0.5.5.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,36 @@
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
+ # Effect-class
25
+ #
26
+ module Chingu
27
+ class Effect
28
+ def initialize(options)
29
+ super({:mode => :additive}.merge(options))
30
+ @trail = options[:trail] || 10
31
+ end
32
+
33
+ def update
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,62 @@
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
+ # Calculates a fps and a tick-time for use in update-calls
26
+ # register_tick() must be called every game loop iteration
27
+ #
28
+ class FPSCounter
29
+ attr_reader :fps, :milliseconds_since_last_tick, :ticks
30
+
31
+ def initialize
32
+ @current_second = Gosu::milliseconds / 1000
33
+ @accum_fps = 0
34
+ @fps = 0
35
+ @ticks = 0
36
+
37
+ @milliseconds_since_last_tick = 0
38
+ @last_value = Gosu::milliseconds
39
+ end
40
+
41
+ #
42
+ # This should be called once every game-iteration, preferable in update()
43
+ #
44
+ def register_tick
45
+ @accum_fps += 1
46
+ @ticks += 1
47
+ current_second = Gosu::milliseconds / 1000
48
+ if current_second != @current_second
49
+ @current_second = current_second
50
+ @fps = @accum_fps
51
+ @accum_fps = 0
52
+ end
53
+
54
+ #
55
+ # Calculate how many milliseconds passed since last game loop iteration.
56
+ #
57
+ @milliseconds_since_last_tick = Gosu::milliseconds - @last_value
58
+ @last_value = Gosu::milliseconds
59
+ return @milliseconds_since_last_tick
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,127 @@
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
+ # GameObject is our BasisGameObject (class with framespecific stuff)
26
+ #
27
+ # On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
28
+ #
29
+ class GameObject < Chingu::BasicGameObject
30
+ attr_accessor :image, :x, :y, :angle, :center_x, :center_y, :factor_x, :factor_y, :color, :mode, :zorder
31
+ attr_reader :paused, :visible, :factor, :center
32
+ has_trait :input, :rotation_center
33
+
34
+ def initialize(options = {})
35
+ super
36
+
37
+ # All encapsulated draw_rot arguments can be set with hash-options at creation time
38
+ if options[:image].is_a?(Gosu::Image)
39
+ @image = options[:image]
40
+ elsif options[:image].is_a? String
41
+ @image = Image[options[:image]]
42
+ end
43
+
44
+ @x = options[:x] || 0
45
+ @y = options[:y] || 0
46
+ @angle = options[:angle] || 0
47
+
48
+ self.center = options[:center] || 0.5
49
+ self.factor = options[:factor] || 1.0
50
+ @center_x = options[:center_x] if options[:center_x]
51
+ @center_y = options[:center_y] if options[:center_y]
52
+ @factor_x = options[:factor_x] if options[:factor_x]
53
+ @factor_y = options[:factor_y] if options[:factor_y]
54
+
55
+ if options[:color].is_a?(Gosu::Color)
56
+ @color = options[:color]
57
+ else
58
+ @color = Gosu::Color.new(options[:color] || 0xFFFFFFFF)
59
+ end
60
+
61
+ @mode = options[:mode] || :default # :additive is also available.
62
+ @zorder = options[:zorder] || 100
63
+
64
+ # gameloop/framework logic (TODO: use or get rid of)
65
+ @paused = options[:paused] || false
66
+ @visible = options[:visible] || true
67
+
68
+ setup_trait(options) if respond_to?(:setup_trait)
69
+ end
70
+
71
+ #
72
+ # Disable auto-updating of traits
73
+ #
74
+ def pause!
75
+ @paused = true
76
+ end
77
+ #
78
+ # Enable auto-update of traits
79
+ #
80
+ def unpause!
81
+ @paused = false
82
+ end
83
+ #
84
+ # Disable auto-drawing of object
85
+ #
86
+ def hide!
87
+ @visible = false
88
+ end
89
+ #
90
+ # Enable auto-drawing of object
91
+ #
92
+ def show!
93
+ @visible = true
94
+ end
95
+
96
+ # Quick way of setting both factor_x and factor_y
97
+ def factor=(factor)
98
+ @factor = factor
99
+ @factor_x = @factor_y = factor
100
+ end
101
+
102
+ # Quick way of setting both center_x and center_y
103
+ def center=(center)
104
+ @center = center
105
+ @center_x = @center_y = center
106
+ end
107
+
108
+ # Returns true if object is inside the game window, false if outside
109
+ def inside_window?(x = @x, y = @y)
110
+ x >= 0 && x <= $window.width && y >= 0 && y <= $window.height
111
+ end
112
+
113
+ # Returns true object is outside the game window
114
+ def outside_window?(x = @x, y = @y)
115
+ not inside_window?(x,y)
116
+ end
117
+
118
+ # Calculates the distance from self to a given objevt
119
+ def distance_to(object)
120
+ distance(self.x, self.y, object.x, object.y)
121
+ end
122
+
123
+ def draw
124
+ @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
125
+ end
126
+ end
127
+ 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
+ module Chingu
24
+ #
25
+ # Manages a list of game objects
26
+ # An instance of GameObjectList is automaticly created as "game_objects" if using Chingu::Window
27
+ #
28
+ class GameObjectList
29
+
30
+ def initialize
31
+ @game_objects = Array.new
32
+ #@game_objects_by_class = Hash.new
33
+ end
34
+
35
+ def to_s
36
+ "#{@game_objects.size} game objects."
37
+ end
38
+
39
+ def of_class(klass)
40
+ @game_objects.select { |game_object| game_object.is_a? klass }
41
+ #@game_objects_by_class[klass] || []
42
+ end
43
+
44
+ def remove_all
45
+ @game_objects.clear
46
+ #@game_objects_of_class.clear
47
+ end
48
+ alias :clear :remove_all
49
+
50
+ def add_game_object(object)
51
+ @game_objects.push(object)
52
+ #(@game_objects_by_class[object.class] ||= []).push(object)
53
+ end
54
+
55
+ def remove_game_object(object)
56
+ @game_objects.delete(object)
57
+ #@game_objects_by_class[object.class].delete(object)
58
+ end
59
+
60
+ def destroy_if
61
+ @game_objects.reject! { |object| yield(object) }
62
+ #@game_objects_by_class.delete_if { |klass, object| yield(object) }
63
+ end
64
+
65
+ def size
66
+ @game_objects.size
67
+ end
68
+
69
+ def draw
70
+ @game_objects.each{ |object| object.visible }.each do |object|
71
+ object.draw_trait
72
+ object.draw
73
+ end
74
+ end
75
+
76
+ def update
77
+ @game_objects.select{ |object| not object.paused }.each do |object|
78
+ object.update_trait
79
+ object.update
80
+ end
81
+ end
82
+
83
+ def each
84
+ @game_objects.each { |object| yield object }
85
+ end
86
+
87
+ def select
88
+ @game_objects.select { |object| yield object }
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,137 @@
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
+ # Chingu incorporates a basic push/pop game state system (as discussed here: http://www.gamedev.net/community/forums/topic.asp?topic_id=477320).
26
+ # Game states is a way of organizing your intros, menus, levels.
27
+ # Game states aren't complicated. In Chingu a GameState is a class that behaves mostly like your default Gosu::Window (or in our case Chingu::Window) game loop.
28
+ #
29
+ # # A simple GameState-example
30
+ # class Intro < Chingu::GameState
31
+ # def update
32
+ # # game logic here
33
+ # end
34
+ #
35
+ # def draw
36
+ # # screen manipulation here
37
+ # end
38
+ #
39
+ # # Called when we enter the game state
40
+ # def setup
41
+ # @player.angle = 0 # point player upwards
42
+ # end
43
+ #
44
+ # # Called when we leave the current game state
45
+ # def finalize
46
+ # push_game_state(Menu) # switch to game state "Menu"
47
+ # end
48
+ # end
49
+ #
50
+
51
+ class GameState
52
+ include Chingu::GameStateHelpers # Easy access to the global game state-queue
53
+ include Chingu::GFXHelpers # Adds fill(), fade() etc to each game state
54
+ include Chingu::GameObjectHelpers # adds game_objects_of_class etc ...
55
+ include Chingu::InputDispatcher # dispatch-helpers
56
+ include Chingu::InputClient
57
+
58
+ attr_reader :options # so jlnr can access his :level-number
59
+ attr_accessor :game_state_manager, :game_objects
60
+
61
+ def initialize(options = {})
62
+ @options = options
63
+ ## @game_state_manager = options[:game_state_manager] || $window.game_state_manager
64
+ @game_objects = GameObjectList.new
65
+ @input_clients = Set.new # Set is like a unique Array with Hash lookupspeed
66
+
67
+ # Game state mamanger can be run alone
68
+ if defined?($window) && $window.respond_to?(:game_state_manager)
69
+ $window.game_state_manager.inside_state = self
70
+ end
71
+ end
72
+
73
+ #
74
+ # An unique identifier for the GameState-class,
75
+ # Used in game state manager to keep track of created states.
76
+ #
77
+ def to_sym
78
+ self.class.to_s.to_sym
79
+ end
80
+
81
+ def to_s
82
+ self.class.to_s
83
+ end
84
+
85
+ def setup
86
+ # Your game state setup logic here.
87
+ end
88
+
89
+ #
90
+ # Called when a button is pressed and a game state is active
91
+ #
92
+ def button_down(id)
93
+ dispatch_button_down(id, self)
94
+ @input_clients.each { |object| dispatch_button_down(id, object) } if @input_clients
95
+ end
96
+
97
+ #
98
+ # Called when a button is released and a game state active
99
+ #
100
+ def button_up(id)
101
+ dispatch_button_up(id, self)
102
+ @input_clients.each { |object| dispatch_button_up(id, object) } if @input_clients
103
+ end
104
+
105
+ #
106
+ # Calls update on each game object that has current game state as parent (created inside that game state)
107
+ #
108
+ def update
109
+ dispatch_input_for(self)
110
+
111
+ @input_clients.each { |game_object| dispatch_input_for(game_object) }
112
+
113
+ @game_objects.update
114
+ end
115
+
116
+ #
117
+ # Calls Draw on each game object that has current game state as parent (created inside that game state)
118
+ #
119
+ def draw
120
+ @game_objects.draw
121
+ end
122
+
123
+ #
124
+ # Closes game state by poping it off the stack (and activating the game state below)
125
+ #
126
+ def close
127
+ pop_game_state
128
+ end
129
+
130
+ #
131
+ # Closes main window and terminates the application
132
+ #
133
+ def close_game
134
+ $window.close
135
+ end
136
+ end
137
+ end