chingu 0.8rc1 → 0.8rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -49,7 +49,7 @@ module Chingu
49
49
  set_options(options, DEFAULTS)
50
50
 
51
51
  # it fails when setup in DEFAULTS as it needs existing $window
52
- @font ||= Gosu::Font.new($window, Gosu::default_font_name, 16)
52
+ @font ||= Gosu::Font[16]
53
53
 
54
54
  self.input = {:p => :pause, :f1 => :return_to_game, :esc => :return_to_game}
55
55
  end
@@ -35,7 +35,7 @@ module Chingu
35
35
  super
36
36
  @white = Color.new(255,255,255,255)
37
37
  @color = Gosu::Color.new(200,0,0,0)
38
- @font = Gosu::Font.new($window, default_font_name, 35)
38
+ @font = Gosu::Font[35]
39
39
  @text = "PAUSED - press ESC to return to game."
40
40
  end
41
41
 
@@ -75,8 +75,7 @@ module Chingu
75
75
  # those 2 words basicly have the same meaning and are both understandable.
76
76
  #
77
77
  def rotation_center(alignment = nil)
78
- #return @rotation_center unless alignment
79
- #@rotation_center = alignment
78
+ return unless alignment
80
79
  @center_x, @center_y = @@rotation_centers[alignment.to_sym]
81
80
  end
82
81
 
@@ -34,6 +34,13 @@ module Chingu
34
34
  end
35
35
  end
36
36
 
37
+ #
38
+ # "Chingu::GameObject" -> "GameObject"
39
+ #
40
+ def Inflector.demodulize(class_name_in_module)
41
+ class_name_in_module.to_s.gsub(/^.*::/, '')
42
+ end
43
+
37
44
  #
38
45
  # "FireBall" -> "fire_ball"
39
46
  #
@@ -1,254 +1,258 @@
1
- #--
2
- # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
- # Copyright (C) 2004-2008 John Croisant
4
- #
5
- # This library is free software; you can redistribute it and/or
6
- # modify it under the terms of the GNU Lesser General Public
7
- # License as published by the Free Software Foundation; either
8
- # version 2.1 of the License, or (at your option) any later version.
9
- #
10
- # This library is distributed in the hope that it will be useful,
11
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
- # Lesser General Public License for more details.
14
- #
15
- # You should have received a copy of the GNU Lesser General Public
16
- # License along with this library; if not, write to the Free Software
17
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
- #++
19
-
20
-
21
- module Chingu
22
-
23
- # NamedResource is a mix-in module to implement a globally-available
24
- # resource table, a @name variable and accessors, and a system for
25
- # automatically loading resources when they are first needed.
26
- #
27
- # This module is used for Rubygame::Music, Rubygame::Sound, and
28
- # Rubygame::Surface. You can use it in your own classes this way:
29
- #
30
- # 1. Do 'include Rubygame::NamedResource' in your class definition.
31
- #
32
- # 2. Set MyClass.autoload_dirs to an Array of directories to look
33
- # for files when autoloading. Tip: use File.join to create
34
- # paths that work on any operating system.
35
- #
36
- # 3. Define #autoload to implement the behavior for your class,
37
- # or leave it as the default if you don't need autoloading.
38
- # See the docs for #autoload for more information.
39
- #
40
- # Here's an example of how you could use this for a class which
41
- # loads maps from a file:
42
- #
43
- # class Map
44
- # include Rubygame::NamedResource
45
- #
46
- # Map.autoload_dirs = [ File.join("maps","world_1"),
47
- # File.join("maps","custom") ]
48
- #
49
- # def autoload( name )
50
- # # Searches autoload_dirs for the file
51
- # path = find_file( name )
52
- #
53
- # if( path )
54
- # return load_map( path )
55
- # else
56
- # return nil
57
- # end
58
- # end
59
- #
60
- # def load_map( path )
61
- # # Your code to do the real loading, then return
62
- # # the created instance of Map class.
63
- # # ...
64
- # return map_instance
65
- # end
66
- # end
67
- #
68
- # Here's an example of how you could then use the Map class:
69
- #
70
- # map = Map["level_1.map"]
71
- #
72
- # if( map )
73
- # start_playing( map )
74
- # else
75
- # raise "Oops! The map file for Level 1 doesn't exist!"
76
- # end
77
- #
78
- module NamedResource
79
-
80
-
81
- # Adds class methods when the NamedResource module is included
82
- # in a class. (Here, we are assuming that the NamedResource
83
- # module was included in a class called MyClass.)
84
- module NamedResourceClassMethods
85
-
86
- # An Array of paths to check for files. See #find_file.
87
- attr_accessor :autoload_dirs
88
-
89
-
90
- # call-seq:
91
- # MyClass[ name ] -> instance or nil
92
- #
93
- # Retrieves an instance of the class from a per-class resource
94
- # table (Hash).
95
- #
96
- # If no object has been saved under the given name, invoke
97
- # #autoload to try to load a new instance, store it in the
98
- # Hash table under this name, and sets the instance's @name
99
- # to this name.
100
- #
101
- def []( name )
102
- result = @resources[name]
103
-
104
- if result.nil?
105
- result = autoload(name)
106
- if result
107
- self[name] = result
108
- result.name = name
109
- end
110
- end
111
-
112
- return result
113
- end
114
-
115
-
116
- # call-seq:
117
- # MyClass[ name ] = instance
118
- #
119
- # Stores an instance of the class in a per-class resource table
120
- # (Hash) for future access. If another object is already stored
121
- # with this name, the old record is lost.
122
- #
123
- # May raise: TypeError, if you try to store anything
124
- # that is not kind of this class.
125
- #
126
- def []=( name, value )
127
- ##if( value.kind_of? self )
128
- @resources[name] = value
129
- ##else
130
- ## raise TypeError, "#{self}#[]= can only store instances of #{self}"
131
- ##end
132
- end
133
-
134
- # call-seq:
135
- # MyClass.autoload( name ) -> instance or nil
136
- #
137
- # This method is invoked when a non-existing resource is
138
- # accessed with #[]. By default, this method simply returns
139
- # nil, effectively disabling autoloading.
140
- #
141
- # You should override this method in your class to provide
142
- # class-specific loading behavior, or leave it as the default if
143
- # you don't need autoloading. Your method should return either
144
- # an instance of the class, or nil.
145
- #
146
- # NOTE: The #find_file method is useful for getting the full
147
- # path to a file which matches the name. That's what it's there
148
- # for, so you should use it!
149
- #
150
- def autoload( name )
151
- nil
152
- end
153
-
154
-
155
- # call-seq:
156
- # MyClass.basename( path ) -> filename
157
- #
158
- # Returns the basename for the path (i.e. the
159
- # filename without the directory). Same as
160
- # File.basename
161
- #
162
- def basename( path )
163
- File.basename( path )
164
- end
165
-
166
-
167
- # call-seq:
168
- # MyClass.exist?( path ) -> true or false
169
- #
170
- # True if the given path points to a file
171
- # that exists, otherwise false. Same as
172
- # File.exist?
173
- #
174
- def exist?( path )
175
- File.exist?(path)
176
- end
177
-
178
-
179
- # call-seq:
180
- # MyClass.find_file( filename ) -> path or nil
181
- #
182
- # Checks every directory in @autoload_dirs for
183
- # a file with the given name, and returns the
184
- # path (directory and name) for the first match.
185
- #
186
- # If no directories have a file with that name,
187
- # return nil.
188
- #
189
- def find_file( filename )
190
- dir = @autoload_dirs.find { |dir|
191
- exist?( File.join(dir,filename) )
192
- }
193
-
194
- if dir
195
- return File.join(dir,filename)
196
- else
197
- return nil
198
- end
199
- end
200
-
201
- end
202
-
203
-
204
- # Sets up the class when this module is included.
205
- # Adds the class methods and defines class instance
206
- # variables.
207
- def self.included( object ) # :nodoc:
208
-
209
- class << object
210
- include NamedResourceClassMethods
211
- end
212
-
213
- object.instance_eval do
214
- @resources = Hash.new
215
- @autoload_dirs = []
216
- end
217
-
218
- end
219
-
220
-
221
- # Returns the instance's @name. See also #name=.
222
- def name
223
- @name
224
- end
225
-
226
- #
227
- # Sets the instance's @name to the given String, or nil to
228
- # unset the name. See also #name.
229
- #
230
- # NOTE: This does not automatically store the instance in the
231
- # class resource table by name. Use the #[]= class method to do
232
- # that.
233
- #
234
- # The string is dup'ed and frozen before being stored.
235
- #
236
- # May raise: TypeError, if new_name is not a String or nil.
237
- #
238
- def name=( new_name )
239
- if new_name.nil?
240
- return @name = nil
241
- end
242
-
243
- unless new_name.kind_of? String
244
- raise TypeError, "name must be a String (got #{new_name.class})"
245
- end
246
-
247
- @name = new_name.dup
248
- @name.freeze
249
- end
250
-
251
-
252
- end
253
-
254
- end
1
+ #--
2
+ # Rubygame -- Ruby code and bindings to SDL to facilitate game creation
3
+ # Copyright (C) 2004-2008 John Croisant
4
+ #
5
+ # This library is free software; you can redistribute it and/or
6
+ # modify it under the terms of the GNU Lesser General Public
7
+ # License as published by the Free Software Foundation; either
8
+ # version 2.1 of the License, or (at your option) any later version.
9
+ #
10
+ # This library is distributed in the hope that it will be useful,
11
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
+ # Lesser General Public License for more details.
14
+ #
15
+ # You should have received a copy of the GNU Lesser General Public
16
+ # License along with this library; if not, write to the Free Software
17
+ # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
+ #++
19
+
20
+
21
+ module Chingu
22
+
23
+ # NamedResource is a mix-in module to implement a globally-available
24
+ # resource table, a @name variable and accessors, and a system for
25
+ # automatically loading resources when they are first needed.
26
+ #
27
+ # This module is used for Rubygame::Music, Rubygame::Sound, and
28
+ # Rubygame::Surface. You can use it in your own classes this way:
29
+ #
30
+ # 1. Do 'include Rubygame::NamedResource' in your class definition.
31
+ #
32
+ # 2. Set MyClass.autoload_dirs to an Array of directories to look
33
+ # for files when autoloading. Tip: use File.join to create
34
+ # paths that work on any operating system.
35
+ #
36
+ # 3. Define #autoload to implement the behavior for your class,
37
+ # or leave it as the default if you don't need autoloading.
38
+ # See the docs for #autoload for more information.
39
+ #
40
+ # Here's an example of how you could use this for a class which
41
+ # loads maps from a file:
42
+ #
43
+ # class Map
44
+ # include Rubygame::NamedResource
45
+ #
46
+ # Map.autoload_dirs = [ File.join("maps","world_1"),
47
+ # File.join("maps","custom") ]
48
+ #
49
+ # def autoload( name )
50
+ # # Searches autoload_dirs for the file
51
+ # path = find_file( name )
52
+ #
53
+ # if( path )
54
+ # return load_map( path )
55
+ # else
56
+ # return nil
57
+ # end
58
+ # end
59
+ #
60
+ # def load_map( path )
61
+ # # Your code to do the real loading, then return
62
+ # # the created instance of Map class.
63
+ # # ...
64
+ # return map_instance
65
+ # end
66
+ # end
67
+ #
68
+ # Here's an example of how you could then use the Map class:
69
+ #
70
+ # map = Map["level_1.map"]
71
+ #
72
+ # if( map )
73
+ # start_playing( map )
74
+ # else
75
+ # raise "Oops! The map file for Level 1 doesn't exist!"
76
+ # end
77
+ #
78
+ module NamedResource
79
+
80
+
81
+ # Adds class methods when the NamedResource module is included
82
+ # in a class. (Here, we are assuming that the NamedResource
83
+ # module was included in a class called MyClass.)
84
+ module NamedResourceClassMethods
85
+
86
+ # An Array of paths to check for files. See #find_file.
87
+ attr_accessor :autoload_dirs
88
+
89
+ # Clear all the cached assets
90
+ def clear
91
+ @resources.clear
92
+ end
93
+
94
+ # call-seq:
95
+ # MyClass[ name ] -> instance or nil
96
+ #
97
+ # Retrieves an instance of the class from a per-class resource
98
+ # table (Hash).
99
+ #
100
+ # If no object has been saved under the given name, invoke
101
+ # #autoload to try to load a new instance, store it in the
102
+ # Hash table under this name, and sets the instance's @name
103
+ # to this name.
104
+ #
105
+ def []( name )
106
+ result = @resources[name]
107
+
108
+ if result.nil?
109
+ result = autoload(name)
110
+ if result
111
+ self[name] = result
112
+ result.name = name
113
+ end
114
+ end
115
+
116
+ return result
117
+ end
118
+
119
+
120
+ # call-seq:
121
+ # MyClass[ name ] = instance
122
+ #
123
+ # Stores an instance of the class in a per-class resource table
124
+ # (Hash) for future access. If another object is already stored
125
+ # with this name, the old record is lost.
126
+ #
127
+ # May raise: TypeError, if you try to store anything
128
+ # that is not kind of this class.
129
+ #
130
+ def []=( name, value )
131
+ ##if( value.kind_of? self )
132
+ @resources[name] = value
133
+ ##else
134
+ ## raise TypeError, "#{self}#[]= can only store instances of #{self}"
135
+ ##end
136
+ end
137
+
138
+ # call-seq:
139
+ # MyClass.autoload( name ) -> instance or nil
140
+ #
141
+ # This method is invoked when a non-existing resource is
142
+ # accessed with #[]. By default, this method simply returns
143
+ # nil, effectively disabling autoloading.
144
+ #
145
+ # You should override this method in your class to provide
146
+ # class-specific loading behavior, or leave it as the default if
147
+ # you don't need autoloading. Your method should return either
148
+ # an instance of the class, or nil.
149
+ #
150
+ # NOTE: The #find_file method is useful for getting the full
151
+ # path to a file which matches the name. That's what it's there
152
+ # for, so you should use it!
153
+ #
154
+ def autoload( name )
155
+ nil
156
+ end
157
+
158
+
159
+ # call-seq:
160
+ # MyClass.basename( path ) -> filename
161
+ #
162
+ # Returns the basename for the path (i.e. the
163
+ # filename without the directory). Same as
164
+ # File.basename
165
+ #
166
+ def basename( path )
167
+ File.basename( path )
168
+ end
169
+
170
+
171
+ # call-seq:
172
+ # MyClass.exist?( path ) -> true or false
173
+ #
174
+ # True if the given path points to a file
175
+ # that exists, otherwise false. Same as
176
+ # File.exist?
177
+ #
178
+ def exist?( path )
179
+ File.exist?(path)
180
+ end
181
+
182
+
183
+ # call-seq:
184
+ # MyClass.find_file( filename ) -> path or nil
185
+ #
186
+ # Checks every directory in @autoload_dirs for
187
+ # a file with the given name, and returns the
188
+ # path (directory and name) for the first match.
189
+ #
190
+ # If no directories have a file with that name,
191
+ # return nil.
192
+ #
193
+ def find_file( filename )
194
+ dir = @autoload_dirs.find { |dir|
195
+ exist?( File.join(dir,filename) )
196
+ }
197
+
198
+ if dir
199
+ return File.join(dir,filename)
200
+ else
201
+ return nil
202
+ end
203
+ end
204
+
205
+ end
206
+
207
+
208
+ # Sets up the class when this module is included.
209
+ # Adds the class methods and defines class instance
210
+ # variables.
211
+ def self.included( object ) # :nodoc:
212
+
213
+ class << object
214
+ include NamedResourceClassMethods
215
+ end
216
+
217
+ object.instance_eval do
218
+ @resources = Hash.new
219
+ @autoload_dirs = []
220
+ end
221
+
222
+ end
223
+
224
+
225
+ # Returns the instance's @name. See also #name=.
226
+ def name
227
+ @name
228
+ end
229
+
230
+ #
231
+ # Sets the instance's @name to the given String, or nil to
232
+ # unset the name. See also #name.
233
+ #
234
+ # NOTE: This does not automatically store the instance in the
235
+ # class resource table by name. Use the #[]= class method to do
236
+ # that.
237
+ #
238
+ # The string is dup'ed and frozen before being stored.
239
+ #
240
+ # May raise: TypeError, if new_name is not a String or nil.
241
+ #
242
+ def name=( new_name )
243
+ if new_name.nil?
244
+ return @name = nil
245
+ end
246
+
247
+ unless new_name.kind_of? String
248
+ raise TypeError, "name must be a String (got #{new_name.class})"
249
+ end
250
+
251
+ @name = new_name.dup
252
+ @name.freeze
253
+ end
254
+
255
+
256
+ end
257
+
258
+ end