chingu 0.8rc1 → 0.8rc2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  require_rel 'helpers/class_inheritable_accessor'
2
+ require_rel 'inflector'
2
3
  module Chingu
3
4
  #
4
5
  # BasicGameObject. Resonating with 1.9.1, this is our most basic class that all game objects ultimate should build on.
@@ -45,7 +46,7 @@ module Chingu
45
46
  end
46
47
  end
47
48
  rescue
48
- puts $!
49
+ puts "Error in 'trait #{trait}': " + $!.to_s
49
50
  end
50
51
  end
51
52
  end
@@ -87,7 +88,9 @@ module Chingu
87
88
 
88
89
  # This will call #setup_trait on the latest trait mixed in
89
90
  # which then will pass it on to the next setup_trait() with a super-call.
90
- setup_trait(options)
91
+ setup_trait(options)
92
+
93
+ setup if respond_to?(:setup)
91
94
  end
92
95
 
93
96
  #
@@ -141,7 +144,16 @@ module Chingu
141
144
  def paused?
142
145
  @paused == true
143
146
  end
144
-
147
+
148
+ #
149
+ # Returns a filename-friendly string from the current class-name
150
+ #
151
+ # "FireBall" -> "fire_ball"
152
+ #
153
+ def filename
154
+ Chingu::Inflector.underscore(Chingu::Inflector.demodulize(self.class.to_s))
155
+ end
156
+
145
157
  #
146
158
  # Empty placeholders to be overridden
147
159
  #
@@ -0,0 +1,267 @@
1
+ #--
2
+ #
3
+ # Chingu -- OpenGL accelerated 2D game framework for Ruby
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
+ require_rel 'helpers/*'
23
+ module Chingu
24
+ #
25
+ # GameObject inherits from BasicGameObject to get traits and some class-methods like .all and .destroy
26
+ #
27
+ # On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
28
+ #
29
+ # In Chingu GameObject is a visual object, something to put on screen, centers around the .image-parameter.
30
+ #
31
+ # If you wan't a invisible object but with traits, use BasicGameObject.
32
+ #
33
+ class ClassicGameObject < Chingu::BasicGameObject
34
+ attr_accessor :image, :x, :y, :angle, :center_x, :center_y, :factor_x, :factor_y, :color, :mode, :zorder
35
+ attr_reader :factor, :center, :height, :width
36
+
37
+ include Chingu::Helpers::InputClient # Adds input and input=
38
+ include Chingu::Helpers::RotationCenter # Adds easy and verbose modification of @center_x and @center_y
39
+
40
+ def initialize(options = {})
41
+ super
42
+
43
+ #
44
+ # All encapsulated Gosu::Image.draw_rot arguments can be set with hash-options at creation time
45
+ #
46
+ if options[:image].is_a?(Gosu::Image)
47
+ @image = options[:image]
48
+ elsif options[:image].is_a? String
49
+ begin
50
+ # 1) Try loading the image the normal way
51
+ @image = Gosu::Image.new($window, options[:image])
52
+ rescue
53
+ # 2) Try looking up the picture using Chingus Image-cache
54
+ @image = Gosu::Image[options[:image]]
55
+ end
56
+ end
57
+
58
+ @x = options[:x] || 0
59
+ @y = options[:y] || 0
60
+ @angle = options[:angle] || 0
61
+
62
+ self.factor = options[:factor] || options[:scale] || $window.factor || 1.0
63
+ @factor_x = options[:factor_x].to_f if options[:factor_x]
64
+ @factor_y = options[:factor_y].to_f if options[:factor_y]
65
+
66
+ self.center = options[:center] || 0.5
67
+
68
+ @rotation_center = options[:rotation_center]
69
+ self.rotation_center(options[:rotation_center]) if options[:rotation_center]
70
+
71
+ @center_x = options[:center_x] if options[:center_x]
72
+ @center_y = options[:center_y] if options[:center_y]
73
+
74
+ if options[:color].is_a?(Gosu::Color)
75
+ @color = options[:color]
76
+ else
77
+ @color = Gosu::Color.new(options[:color] || 0xFFFFFFFF)
78
+ end
79
+
80
+ self.alpha = options[:alpha] if options[:alpha]
81
+
82
+ @mode = options[:mode] || :default # :additive is also available.
83
+ @zorder = options[:zorder] || 100
84
+
85
+ if @image
86
+ self.width = options[:width] if options[:width]
87
+ self.height = options[:height] if options[:height]
88
+ end
89
+
90
+ ### super ## This crashes
91
+ # Call setup, this class holds an empty setup() to be overriden
92
+ # setup() will be an easier method to override for init-stuff since you don't need to do super etc..
93
+ setup
94
+
95
+ end
96
+
97
+ def to_s
98
+ "#{self.class.to_s} @ #{x.to_i} / #{y.to_i} " <<
99
+ "(#{width.to_i} x #{height.to_i}) - " <<
100
+ " ratio: #{sprintf("%.2f",width/height)} scale: #{sprintf("%.2f", factor_x)}/#{sprintf("%.2f", factor_y)} angle: #{angle.to_i} zorder: #{zorder} alpha: #{alpha}"
101
+ end
102
+
103
+ #
104
+ # Get all settings from a game object in one array.
105
+ # Complemented by the GameObject#attributes= setter.
106
+ # Makes it easy to clone a objects x,y,angle etc.
107
+ #
108
+ def attributes
109
+ [@x, @y, @angle, @center_x, @center_y, @factor_x, @factor_y, @color.dup, @mode, @zorder]
110
+ end
111
+
112
+ #
113
+ # Set all attributes on 1 line
114
+ # Mainly used in combination with game_object1.attributes = game_object2.attributes
115
+ #
116
+ def attributes=(attributes)
117
+ self.x, self.y, self.angle, self.center_x, self.center_y, self.factor_x, self.factor_y, self.color, self.mode, self.zorder = *attributes
118
+ end
119
+
120
+ #
121
+ # Set an effective width for the object on screen.
122
+ # Chingu does this by setting factor_x depending on imge.width and width given.
123
+ # Usually better to have a large image and make it smaller then the other way around.
124
+ #
125
+ def width=(width)
126
+ @factor_x = width.to_f / @image.width.to_f
127
+ end
128
+
129
+ # Get effective on width by calculating it from image-width and factor
130
+ def width
131
+ (@image.width * @factor_x).abs
132
+ end
133
+
134
+ #
135
+ # Set an effective height for the object on screen.
136
+ # Chingu does this by setting factor_x depending on imge.width and width given.
137
+ # Usually better to have a large image and make it smaller then the other way around.
138
+ #
139
+ def height=(height)
140
+ @factor_y = height.to_f / @image.height.to_f
141
+ end
142
+
143
+ # Get effective on heightby calculating it from image-width and factor
144
+ def height
145
+ (@image.height.to_f * @factor_y).abs
146
+ end
147
+
148
+ # Set width and height in one swoop
149
+ def size=(size)
150
+ self.width, self.height = *size
151
+ end
152
+
153
+ # Get objects width and height in an array
154
+ def size
155
+ [self.width, self.height]
156
+ end
157
+
158
+
159
+ # Quick way of setting both factor_x and factor_y
160
+ def factor=(factor)
161
+ @factor = factor
162
+ @factor_x = @factor_y = factor
163
+ end
164
+ alias scale= factor=
165
+ alias scale factor
166
+
167
+ # Quick way of setting both center_x and center_y
168
+ def center=(center)
169
+ @center = center
170
+ @center_x = @center_y = center
171
+ end
172
+
173
+ # Get objects alpha-value (internally stored in @color.alpha)
174
+ def alpha
175
+ @color.alpha
176
+ end
177
+
178
+ # Set objects alpha-value (internally stored in @color.alpha)
179
+ # If out of range, set to closest working value. this makes fading simpler.
180
+ def alpha=(value)
181
+ value = 0 if value < 0
182
+ value = 255 if value > 255
183
+ @color.alpha = value
184
+ end
185
+
186
+ #
187
+ # Sets angle, normalize it to between 0..360
188
+ #
189
+ def angle=(value)
190
+ if value < 0
191
+ value = 360+value
192
+ elsif value > 360
193
+ value = value-360
194
+ end
195
+ @angle = value
196
+ end
197
+
198
+ #
199
+ # Disable automatic calling of draw and draw_trait each game loop
200
+ #
201
+ def hide!
202
+ @visible = false
203
+ end
204
+ alias :hide :hide!
205
+
206
+ #
207
+ # Enable automatic calling of draw and draw_trait each game loop
208
+ #
209
+ def show!
210
+ @visible = true
211
+ end
212
+ alias :show :show!
213
+
214
+ #
215
+ # Returns true if visible (not hidden)
216
+ #
217
+ def visible?
218
+ @visible == true
219
+ end
220
+
221
+
222
+ # Returns true if object is inside the game window, false if outside
223
+ def inside_window?(x = @x, y = @y)
224
+ x >= 0 && x <= $window.width && y >= 0 && y <= $window.height
225
+ end
226
+
227
+ # Returns true object is outside the game window
228
+ def outside_window?(x = @x, y = @y)
229
+ not inside_window?(x,y)
230
+ end
231
+
232
+ # Calculates the distance from self to a given object
233
+ def distance_to(object)
234
+ distance(self.x, self.y, object.x, object.y)
235
+ end
236
+
237
+ #
238
+ # Returns a filename-friendly string from the current class-name
239
+ #
240
+ # "FireBall" -> "fire_ball"
241
+ #
242
+ def filename
243
+ Chingu::Inflector.underscore(Chingu::Inflector.demodulize(self.class.to_s))
244
+ end
245
+
246
+ #
247
+ # Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true
248
+ #
249
+ def draw
250
+ @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
251
+ end
252
+
253
+ #
254
+ # Works as #draw() but takes offsets for all draw_rot()-arguments. Used among others by the viewport-trait.
255
+ #
256
+ def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0)
257
+ @image.draw_rot(@x+x, @y+y, @zorder+zorder, @angle+angle, @center_x+center_x, @center_y+center_y, @factor_x+factor_x, @factor_y+factor_y, @color, @mode) if @visible
258
+ end
259
+
260
+ #
261
+ # Works as #draw() but takes x/y arguments. Used among others by the edit-game state.
262
+ #
263
+ def draw_at(x, y)
264
+ @image.draw_rot(x, y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
265
+ end
266
+ end
267
+ end
@@ -23,245 +23,17 @@ require_rel 'helpers/*'
23
23
  module Chingu
24
24
  #
25
25
  # GameObject inherits from BasicGameObject to get traits and some class-methods like .all and .destroy
26
- #
27
26
  # On top of that, it encapsulates GOSUs Image#draw_rot and all its parameters.
28
- #
29
27
  # In Chingu GameObject is a visual object, something to put on screen, centers around the .image-parameter.
30
- #
31
28
  # If you wan't a invisible object but with traits, use BasicGameObject.
32
29
  #
33
30
  class GameObject < Chingu::BasicGameObject
34
- attr_accessor :image, :x, :y, :angle, :center_x, :center_y, :factor_x, :factor_y, :color, :mode, :zorder
35
- attr_reader :factor, :center, :height, :width
36
-
31
+ trait :sprite
37
32
  include Chingu::Helpers::InputClient # Adds input and input=
38
- include Chingu::Helpers::RotationCenter # Adds easy and verbose modification of @center_x and @center_y
39
-
40
- def initialize(options = {})
41
- super
42
-
43
- #
44
- # All encapsulated Gosu::Image.draw_rot arguments can be set with hash-options at creation time
45
- #
46
- if options[:image].is_a?(Gosu::Image)
47
- @image = options[:image]
48
- elsif options[:image].is_a? String
49
- begin
50
- # 1) Try loading the image the normal way
51
- @image = Gosu::Image.new($window, options[:image])
52
- rescue
53
- # 2) Try looking up the picture using Chingus Image-cache
54
- @image = Gosu::Image[options[:image]]
55
- end
56
- end
57
-
58
- @x = options[:x] || 0
59
- @y = options[:y] || 0
60
- @angle = options[:angle] || 0
61
-
62
- self.factor = options[:factor] || options[:scale] || $window.factor || 1.0
63
- @factor_x = options[:factor_x].to_f if options[:factor_x]
64
- @factor_y = options[:factor_y].to_f if options[:factor_y]
65
-
66
- self.center = options[:center] || 0.5
67
-
68
- @rotation_center = options[:rotation_center]
69
- self.rotation_center(options[:rotation_center]) if options[:rotation_center]
70
-
71
- @center_x = options[:center_x] if options[:center_x]
72
- @center_y = options[:center_y] if options[:center_y]
73
-
74
- if options[:color].is_a?(Gosu::Color)
75
- @color = options[:color]
76
- else
77
- @color = Gosu::Color.new(options[:color] || 0xFFFFFFFF)
78
- end
79
-
80
- self.alpha = options[:alpha] if options[:alpha]
81
-
82
- @mode = options[:mode] || :default # :additive is also available.
83
- @zorder = options[:zorder] || 100
84
-
85
- if @image
86
- self.width = options[:width] if options[:width]
87
- self.height = options[:height] if options[:height]
88
- end
89
-
90
- ### super ## This crashes
91
- # Call setup, this class holds an empty setup() to be overriden
92
- # setup() will be an easier method to override for init-stuff since you don't need to do super etc..
93
- setup
94
-
95
- end
96
-
97
- def to_s
98
- "#{self.class.to_s} @ #{x.to_i} / #{y.to_i} " <<
99
- "(#{width.to_i} x #{height.to_i}) - " <<
100
- " ratio: #{sprintf("%.2f",width/height)} scale: #{sprintf("%.2f", factor_x)}/#{sprintf("%.2f", factor_y)} angle: #{angle.to_i} zorder: #{zorder} alpha: #{alpha}"
101
- end
102
-
103
- #
104
- # Get all settings from a game object in one array.
105
- # Complemented by the GameObject#attributes= setter.
106
- # Makes it easy to clone a objects x,y,angle etc.
107
- #
108
- def attributes
109
- [@x, @y, @angle, @center_x, @center_y, @factor_x, @factor_y, @color.dup, @mode, @zorder]
110
- end
111
-
112
- #
113
- # Set all attributes on 1 line
114
- # Mainly used in combination with game_object1.attributes = game_object2.attributes
115
- #
116
- def attributes=(attributes)
117
- self.x, self.y, self.angle, self.center_x, self.center_y, self.factor_x, self.factor_y, self.color, self.mode, self.zorder = *attributes
118
- end
119
-
120
- #
121
- # Set an effective width for the object on screen.
122
- # Chingu does this by setting factor_x depending on imge.width and width given.
123
- # Usually better to have a large image and make it smaller then the other way around.
124
- #
125
- def width=(width)
126
- @factor_x = width.to_f / @image.width.to_f
127
- end
128
-
129
- # Get effective on width by calculating it from image-width and factor
130
- def width
131
- (@image.width * @factor_x).abs
132
- end
133
-
134
- #
135
- # Set an effective height for the object on screen.
136
- # Chingu does this by setting factor_x depending on imge.width and width given.
137
- # Usually better to have a large image and make it smaller then the other way around.
138
- #
139
- def height=(height)
140
- @factor_y = height.to_f / @image.height.to_f
141
- end
142
-
143
- # Get effective on heightby calculating it from image-width and factor
144
- def height
145
- (@image.height.to_f * @factor_y).abs
146
- end
147
-
148
- # Set width and height in one swoop
149
- def size=(size)
150
- self.width, self.height = *size
151
- end
152
-
153
- # Get objects width and height in an array
154
- def size
155
- [self.width, self.height]
156
- end
157
-
158
-
159
- # Quick way of setting both factor_x and factor_y
160
- def factor=(factor)
161
- @factor = factor
162
- @factor_x = @factor_y = factor
163
- end
164
- alias scale= factor=
165
- alias scale factor
166
-
167
- # Quick way of setting both center_x and center_y
168
- def center=(center)
169
- @center = center
170
- @center_x = @center_y = center
171
- end
172
-
173
- # Get objects alpha-value (internally stored in @color.alpha)
174
- def alpha
175
- @color.alpha
176
- end
177
-
178
- # Set objects alpha-value (internally stored in @color.alpha)
179
- # If out of range, set to closest working value. this makes fading simpler.
180
- def alpha=(value)
181
- value = 0 if value < 0
182
- value = 255 if value > 255
183
- @color.alpha = value
184
- end
185
-
186
- #
187
- # Sets angle, normalize it to between 0..360
188
- #
189
- def angle=(value)
190
- if value < 0
191
- value = 360+value
192
- elsif value > 360
193
- value = value-360
194
- end
195
- @angle = value
196
- end
197
-
198
- #
199
- # Disable automatic calling of draw and draw_trait each game loop
200
- #
201
- def hide!
202
- @visible = false
203
- end
204
- alias :hide :hide!
205
-
206
- #
207
- # Enable automatic calling of draw and draw_trait each game loop
208
- #
209
- def show!
210
- @visible = true
211
- end
212
- alias :show :show!
213
-
214
- #
215
- # Returns true if visible (not hidden)
216
- #
217
- def visible?
218
- @visible == true
219
- end
220
-
221
-
222
- # Returns true if object is inside the game window, false if outside
223
- def inside_window?(x = @x, y = @y)
224
- x >= 0 && x <= $window.width && y >= 0 && y <= $window.height
225
- end
226
-
227
- # Returns true object is outside the game window
228
- def outside_window?(x = @x, y = @y)
229
- not inside_window?(x,y)
230
- end
231
33
 
232
- # Calculates the distance from self to a given object
233
- def distance_to(object)
234
- distance(self.x, self.y, object.x, object.y)
235
- end
236
-
237
- #
238
- # Returns a filename-friendly string from the current class-name
239
- #
240
- # "FireBall" -> "fire_ball"
241
- #
242
- def filename
243
- Chingu::Inflector.underscore(self.class.to_s)
244
- end
245
-
246
- #
247
- # Our encapsulation of GOSU's image.draw_rot, uses the objects variables to draw it on screen if @visible is true
248
- #
249
- def draw
250
- @image.draw_rot(@x, @y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
251
- end
252
-
253
- #
254
- # Works as #draw() but takes offsets for all draw_rot()-arguments. Used among others by the viewport-trait.
255
- #
256
- def draw_relative(x=0, y=0, zorder=0, angle=0, center_x=0, center_y=0, factor_x=0, factor_y=0)
257
- @image.draw_rot(@x+x, @y+y, @zorder+zorder, @angle+angle, @center_x+center_x, @center_y+center_y, @factor_x+factor_x, @factor_y+factor_y, @color, @mode) if @visible
258
- end
259
-
260
- #
261
- # Works as #draw() but takes x/y arguments. Used among others by the edit-game state.
262
- #
263
- def draw_at(x, y)
264
- @image.draw_rot(x, y, @zorder, @angle, @center_x, @center_y, @factor_x, @factor_y, @color, @mode) if @visible
265
- end
266
- end
34
+ #def initialize(options = {})
35
+ # super
36
+ # setup
37
+ #end
38
+ end
267
39
  end