chingu 0.8rc1 → 0.8rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +3 -3
- data/chingu.gemspec +201 -198
- data/examples/example21.yml +39 -39
- data/examples/example26_splash_screen.rb +48 -0
- data/examples/example3_parallax.rb +6 -10
- data/examples/game1.rb +4 -3
- data/examples/game_of_life.rb +11 -9
- data/examples/{example24_input_codes.rb → tool1_input_codes.rb} +1 -1
- data/lib/chingu.rb +1 -1
- data/lib/chingu/assets.rb +58 -0
- data/lib/chingu/basic_game_object.rb +15 -3
- data/lib/chingu/classic_game_object.rb +267 -0
- data/lib/chingu/game_object.rb +6 -234
- data/lib/chingu/game_states/debug.rb +1 -1
- data/lib/chingu/game_states/pause.rb +1 -1
- data/lib/chingu/helpers/rotation_center.rb +1 -2
- data/lib/chingu/inflector.rb +7 -0
- data/lib/chingu/named_resource.rb +258 -254
- data/lib/chingu/parallax.rb +6 -4
- data/lib/chingu/text.rb +4 -2
- data/lib/chingu/traits/sprite.rb +22 -19
- data/lib/chingu/window.rb +17 -0
- data/spec/chingu/game_object_spec.rb +69 -1
- data/spec/chingu/parallax_spec.rb +34 -0
- metadata +25 -21
- data/.gitignore +0 -6
@@ -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
|
52
|
+
@font ||= Gosu::Font[16]
|
53
53
|
|
54
54
|
self.input = {:p => :pause, :f1 => :return_to_game, :esc => :return_to_game}
|
55
55
|
end
|
@@ -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
|
-
|
79
|
-
#@rotation_center = alignment
|
78
|
+
return unless alignment
|
80
79
|
@center_x, @center_y = @@rotation_centers[alignment.to_sym]
|
81
80
|
end
|
82
81
|
|
data/lib/chingu/inflector.rb
CHANGED
@@ -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
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
#
|
95
|
-
#
|
96
|
-
#
|
97
|
-
#
|
98
|
-
#
|
99
|
-
#
|
100
|
-
#
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
#
|
121
|
-
#
|
122
|
-
#
|
123
|
-
#
|
124
|
-
#
|
125
|
-
#
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
##
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
#
|
139
|
-
#
|
140
|
-
#
|
141
|
-
#
|
142
|
-
#
|
143
|
-
#
|
144
|
-
#
|
145
|
-
#
|
146
|
-
#
|
147
|
-
#
|
148
|
-
#
|
149
|
-
#
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
#
|
160
|
-
#
|
161
|
-
#
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
#
|
172
|
-
#
|
173
|
-
#
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
#
|
184
|
-
#
|
185
|
-
#
|
186
|
-
#
|
187
|
-
#
|
188
|
-
#
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
object
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
#
|
231
|
-
#
|
232
|
-
#
|
233
|
-
#
|
234
|
-
#
|
235
|
-
#
|
236
|
-
#
|
237
|
-
#
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
end
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
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
|