ippa-chingu 0.2.0
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.
- data/History.txt +5 -0
- data/Manifest.txt +34 -0
- data/README.rdoc +329 -0
- data/Rakefile +19 -0
- data/chingu.gemspec +34 -0
- data/examples/example1.rb +30 -0
- data/examples/example2.rb +76 -0
- data/examples/example3.rb +37 -0
- data/examples/example4.rb +110 -0
- data/examples/media/Parallax-scroll-example-layer-0.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-1.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-2.png +0 -0
- data/examples/media/Parallax-scroll-example-layer-3.png +0 -0
- data/examples/media/background1.png +0 -0
- data/examples/media/fire_bullet.png +0 -0
- data/examples/media/spaceship.png +0 -0
- data/examples/media/stickfigure.bmp +0 -0
- data/examples/media/stickfigure.png +0 -0
- data/lib/chingu/animation.rb +109 -0
- data/lib/chingu/assets.rb +50 -0
- data/lib/chingu/chipmunk_object.rb +117 -0
- data/lib/chingu/data_structures.rb +6 -0
- data/lib/chingu/fpscounter.rb +21 -0
- data/lib/chingu/game_object.rb +101 -0
- data/lib/chingu/game_state.rb +39 -0
- data/lib/chingu/game_state_manager.rb +84 -0
- data/lib/chingu/helpers.rb +41 -0
- data/lib/chingu/input.rb +100 -0
- data/lib/chingu/named_resource.rb +254 -0
- data/lib/chingu/parallax.rb +80 -0
- data/lib/chingu/rect.rb +612 -0
- data/lib/chingu/text.rb +46 -0
- data/lib/chingu/window.rb +161 -0
- data/lib/chingu.rb +29 -0
- metadata +99 -0
@@ -0,0 +1,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
|
+
|
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
|
@@ -0,0 +1,80 @@
|
|
1
|
+
#
|
2
|
+
# Class for simple parallaxscrolling
|
3
|
+
# See: http://en.wikipedia.org/wiki/Parallax_scrolling
|
4
|
+
#
|
5
|
+
module Chingu
|
6
|
+
class Parallax < Chingu::GameObject
|
7
|
+
attr_reader :backgrounds
|
8
|
+
|
9
|
+
#
|
10
|
+
# repeat: [true|false] When one background ends within the screen, repeat/loop it
|
11
|
+
#
|
12
|
+
def initialize(options)
|
13
|
+
super(options)
|
14
|
+
@repeat = options[:repeat] || true
|
15
|
+
@backgrounds = Array.new
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Add one background, either an ParallaxBackground-object or a Hash of options to create one
|
20
|
+
#
|
21
|
+
def add_background(arg)
|
22
|
+
@backgrounds << (arg.is_a?(ParallaxBackground) ? arg : ParallaxBackground.new(arg))
|
23
|
+
end
|
24
|
+
|
25
|
+
#
|
26
|
+
# Shortcut for #add_background
|
27
|
+
#
|
28
|
+
def <<(arg)
|
29
|
+
self.add_background(arg)
|
30
|
+
end
|
31
|
+
|
32
|
+
def update
|
33
|
+
@backgrounds.each do |background|
|
34
|
+
background.x = -@x / background.damping
|
35
|
+
background.y = @y / background.damping
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Draw
|
41
|
+
#
|
42
|
+
def draw
|
43
|
+
@backgrounds.each do |background|
|
44
|
+
#background.image.draw(real_x, real_y, background.zorder)
|
45
|
+
background.draw
|
46
|
+
|
47
|
+
save_x = background.x
|
48
|
+
## If background lands inside our screen, repeat it
|
49
|
+
while (background.x + background.image.width) < $window.width
|
50
|
+
background.x += background.image.width
|
51
|
+
background.draw
|
52
|
+
#background.x -= background.image.width
|
53
|
+
#background.image.draw(real_x+background.image.width, real_y, background.zorder)
|
54
|
+
end
|
55
|
+
background.x = save_x
|
56
|
+
end
|
57
|
+
self
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
#
|
62
|
+
# One background item
|
63
|
+
#
|
64
|
+
class ParallaxBackground < Chingu::GameObject
|
65
|
+
@@zorder_counter = 0
|
66
|
+
attr_reader :damping
|
67
|
+
|
68
|
+
def initialize(options)
|
69
|
+
## No auto update/draw, the parentclass Parallax takes care of that!
|
70
|
+
options.merge!(:draw => false, :update => false)
|
71
|
+
|
72
|
+
# If no zorder is given, use a global incrementing counter. First added, furthest behind when drawn.
|
73
|
+
options.merge!(:zorder => (@@zorder_counter+=1)) if options[:zorder].nil?
|
74
|
+
|
75
|
+
super(options)
|
76
|
+
|
77
|
+
@damping = options[:damping] || 10
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|