chingu 0.7.0 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/History.txt +1 -1
  2. data/README.rdoc +110 -49
  3. data/benchmarks/game_objects_benchmark.rb +50 -0
  4. data/chingu.gemspec +24 -12
  5. data/examples/example10_traits_retrofy.rb +6 -4
  6. data/examples/example11_animation.rb +14 -23
  7. data/examples/example12_trait_timer.rb +1 -1
  8. data/examples/example13_high_scores.rb +1 -1
  9. data/examples/example14_bounding_box_circle.rb +6 -10
  10. data/examples/example15_trait_timer2.rb +1 -1
  11. data/examples/example16_online_high_scores.rb +1 -1
  12. data/examples/example17_gosu_tutorial.rb +4 -4
  13. data/examples/example18_animation_trait.rb +98 -0
  14. data/examples/example19_edit_viewport.rb +223 -0
  15. data/examples/example19_game_objects.yml +1591 -0
  16. data/examples/example20_trait_inheritence_test.rb +58 -0
  17. data/examples/example3_parallax.rb +1 -1
  18. data/examples/example4_gamestates.rb +1 -1
  19. data/examples/example7_gfx_helpers.rb +14 -9
  20. data/examples/example8_traits.rb +4 -4
  21. data/examples/example9_collision_detection.rb +1 -1
  22. data/examples/game1.rb +33 -38
  23. data/examples/game_of_life.rb +291 -0
  24. data/examples/media/droid_11x15.bmp +0 -0
  25. data/examples/media/droid_11x15.gal +0 -0
  26. data/examples/media/heli.bmp +0 -0
  27. data/examples/media/heli.gal +0 -0
  28. data/examples/media/star_25x25_default.png +0 -0
  29. data/examples/media/star_25x25_explode.gal +0 -0
  30. data/examples/media/star_25x25_explode.png +0 -0
  31. data/examples/media/stone_wall.bmp +0 -0
  32. data/lib/chingu.rb +1 -1
  33. data/lib/chingu/animation.rb +78 -9
  34. data/lib/chingu/basic_game_object.rb +16 -8
  35. data/lib/chingu/game_object.rb +36 -7
  36. data/lib/chingu/game_object_list.rb +20 -3
  37. data/lib/chingu/game_state.rb +8 -7
  38. data/lib/chingu/game_states/edit.rb +177 -90
  39. data/lib/chingu/helpers/class_inheritable_accessor.rb +12 -5
  40. data/lib/chingu/helpers/game_object.rb +45 -4
  41. data/lib/chingu/helpers/gfx.rb +150 -172
  42. data/lib/chingu/helpers/input_client.rb +7 -0
  43. data/lib/chingu/inflector.rb +16 -2
  44. data/lib/chingu/traits/animation.rb +84 -0
  45. data/lib/chingu/traits/bounding_box.rb +16 -3
  46. data/lib/chingu/traits/bounding_circle.rb +18 -4
  47. data/lib/chingu/traits/collision_detection.rb +10 -1
  48. data/lib/chingu/traits/velocity.rb +26 -3
  49. data/lib/chingu/traits/viewport.rb +10 -9
  50. data/lib/chingu/viewport.rb +103 -22
  51. data/lib/chingu/window.rb +8 -2
  52. metadata +46 -16
  53. data/examples/example18_viewport.rb +0 -173
  54. data/examples/media/city1.csv +0 -2
  55. data/examples/media/plane.csv +0 -2
  56. data/examples/media/saucer.csv +0 -4
  57. data/examples/media/stickfigure.bmp +0 -0
  58. data/examples/media/stickfigure.png +0 -0
@@ -8,15 +8,15 @@ module Helpers
8
8
  def self.included(base)
9
9
  base.extend(ClassMethods)
10
10
  end
11
-
11
+
12
12
  module ClassMethods
13
13
  def class_inheritable_accessor(*args)
14
14
  @cattr_inheritable_attrs ||= [:cattr_inheritable_attrs]
15
15
  @cattr_inheritable_attrs += args
16
16
  args.each do |arg|
17
- class_eval %(
18
- class << self; attr_accessor :#{arg} end
19
- )
17
+ class_eval %(
18
+ class << self; attr_accessor :#{arg} end
19
+ )
20
20
  end
21
21
  @cattr_inheritable_attrs
22
22
  end
@@ -24,8 +24,15 @@ module Helpers
24
24
  def inherited(subclass)
25
25
  @cattr_inheritable_attrs.each do |inheritable_attribute|
26
26
  instance_var = "@#{inheritable_attribute}"
27
- subclass.instance_variable_set(instance_var, instance_variable_get(instance_var))
27
+ subclass.instance_variable_set(instance_var, instance_variable_get(instance_var).dup)
28
+
29
+ #if instance_var =~ /trait_options/
30
+ # puts "#{self.to_s} -> #{subclass.to_s}: #{instance_var}" # DEBUG
31
+ # puts instance_variable_get(instance_var)
32
+ # puts "------"
33
+ #end
28
34
  end
35
+ super
29
36
  end
30
37
  end
31
38
  end
@@ -52,18 +52,20 @@ module Chingu
52
52
  # Creates game objects from a Chingu-spezed game objects file (created with game state 'Edit')
53
53
  #
54
54
  def load_game_objects(options = {})
55
- filename = options[:file] || "#{self.class.to_s.downcase}.yml"
55
+ file = options[:file] || "#{self.class.to_s.downcase}.yml"
56
+ debug = options[:debug]
56
57
 
57
58
  require 'yaml'
58
59
 
59
- if File.exists?(filename)
60
- game_objects = YAML.load_file(filename)
60
+ puts "* Loading game objects from #{file}" if debug
61
+ if File.exists?(file)
62
+ game_objects = YAML.load_file(file)
61
63
  game_objects.each do |game_object|
62
64
  game_object.each_pair do |klassname, attributes|
63
65
  begin
64
66
  klass = Kernel::const_get(klassname)
65
67
  unless klass.class == "GameObject"
66
- puts "Creating #{klassname.to_s}: #{attributes.to_s}"
68
+ puts "Creating #{klassname.to_s}: #{attributes.to_s}" if debug
67
69
  klass.create(attributes)
68
70
  end
69
71
  rescue
@@ -73,6 +75,45 @@ module Chingu
73
75
  end
74
76
  end
75
77
  end
78
+
79
+ #
80
+ # Save given game_objects to a file. Hashoptions
81
+ #
82
+ # :file - a String, name of file to write to, default is current game_state class name.
83
+ # :game_objects - an Array, game objects to save
84
+ # :classes - an Array, save only game objects of theese classes
85
+ #
86
+ def save_game_objects(options = {})
87
+ file = options[:file] || "#{self.class.to_s.downcase}.yml"
88
+ game_objects = options[:game_objects]
89
+ classes = options[:classes]
90
+
91
+ require 'yaml'
92
+ objects = []
93
+ game_objects.each do |game_object|
94
+ next if classes and !classes.include?(game_object.class)
95
+
96
+ objects << {game_object.class.to_s =>
97
+ {
98
+ :x => game_object.x,
99
+ :y => game_object.y,
100
+ :angle => game_object.angle,
101
+ :zorder => game_object.zorder,
102
+ #:factor_x => game_object.factor_x,
103
+ #:factor_y => game_object.factor_y,
104
+ #:center_x => game_object.center_x,
105
+ #:center_y => game_object.center_y,
106
+ }
107
+ }
108
+ end
109
+
110
+
111
+ #Marshal.dump(previous_game_state.game_objects, File.open(@filename, "w"))
112
+ File.open(file, 'w') do |out|
113
+ YAML.dump(objects, out)
114
+ end
115
+ end
116
+
76
117
 
77
118
  end
78
119
 
@@ -1,173 +1,151 @@
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
- module Chingu
23
- module Helpers
24
-
25
- #
26
- # Various helper-methods to manipulate the screen.
27
- # All drawings depend on the global variable $window which should be an instance of Gosu::Window or Chingu::Window
28
- #
29
- module GFX
30
-
31
- #
32
- # Fills whole window with specified 'color' and 'zorder'
33
- #
34
- #def fill(color, zorder = 0)
35
- # $window.draw_quad(0, 0, color,
36
- # $window.width, 0, color,
37
- # $window.width, $window.height, color,
38
- # 0, $window.height, color,
39
- # zorder, :default)
40
- #end
41
- #
42
-
43
- # Fills window or a given rect with a gradient between two colors.
44
- #
45
- # :from - Start with this color
46
- # :to - End with this color
47
- # :rect - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
48
- # :orientation - Either :vertical (top to bottom) or :horizontal (left to right)
49
- #
50
-
51
- def fill(options, zorder = 99)
52
- #
53
- # if only 1 color-argument is given, assume fullscreen simple color fill.
54
- #
55
- if options.is_a?(Gosu::Color)
56
- $window.draw_quad(0, 0, options,
57
- $window.width, 0, options,
58
- $window.width, $window.height, options,
59
- 0, $window.height, options, 0, :default)
60
- return
61
- end
62
-
63
- default_options = { :colors => [Gosu::Color.new(0xFFFFFFFF), Gosu::Color.new(0xFF000000)],
64
- :orientation => :vertical,
65
- :rect => Rect.new([0, 0, $window.width, $window.height]),
66
- :zorder => 0,
67
- :mode => :default
68
- }
69
- options = default_options.merge(options)
70
- rect = Rect.new(options[:rect])
71
-
72
- if options[:orientation] == :vertical
73
- x = rect.x
74
- y = rect.y
75
- right = rect.right
76
- bottom = rect.bottom
77
-
78
- step = (rect.x + rect.right) / options[:colors].size
79
- 1.upto(options[:colors].size).each do |nr|
80
- from = options[:colors]
81
- to =
82
-
83
- $window.draw_quad( x, y, options[:from],
84
- x + step, rect.y, options[:from],
85
- x + step, rect.bottom, options[:to],
86
- x, rect.bottom, options[:to],
87
- options[:zorder], options[:mode]
88
- )
89
- end
90
- else
91
- $window.draw_quad( rect.x, rect.y, options[:from],
92
- rect.x, rect.bottom, options[:from],
93
- rect.right, rect.bottom, options[:to],
94
- rect.right, rect.y, options[:to],
95
- options[:zorder], options[:mode]
96
- )
97
- end
98
- end
99
-
100
- #
101
- # Draws an unfilled rect in given color
102
- #
103
- def draw_rect(rect, color, zorder)
104
- $window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
105
- $window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
106
- $window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
107
- $window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
108
- end
109
-
110
-
111
- #
112
- # Draws an unfilled circle, thanks shawn24!
113
- #
114
- CIRCLE_STEP = 10
115
- def draw_circle(cx,cy,r,color)
116
- 0.step(360, CIRCLE_STEP) do |a1|
117
- a2 = a1 + CIRCLE_STEP
118
- $window.draw_line cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, 9999
119
- end
120
- end
121
-
122
- #
123
- # Fills a given Rect 'rect' with Color 'color', drawing with zorder 'zorder'
124
- #
125
- def fill_rect(rect, color, zorder = 0)
126
- rect = Rect.new(rect) # Make sure it's a rect
127
- $window.draw_quad( rect.x, rect.y, color,
128
- rect.right, rect.y, color,
129
- rect.right, rect.bottom, color,
130
- rect.x, rect.bottom, color,
131
- zorder, :default)
132
- end
133
-
134
- #
135
- # Fills window or a given rect with a gradient between two colors.
136
- #
137
- # :from - Start with this color
138
- # :to - End with this color
139
- # :rect - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
140
- # :orientation - Either :vertical (top to bottom) or :horizontal (left to right)
141
- #
142
- def fill_gradient(options)
143
- default_options = { :from => Gosu::Color.new(255,0,0,0),
144
- :to => Gosu::Color.new(255,255,255,255),
145
- :thickness => 10,
146
- :orientation => :vertical,
147
- :rect => Rect.new([0, 0, $window.width, $window.height]),
148
- :zorder => 0,
149
- :mode => :default
150
- }
151
- options = default_options.merge(options)
152
- rect = Rect.new(options[:rect])
153
-
154
- if options[:orientation] == :vertical
155
- $window.draw_quad( rect.x, rect.y, options[:from],
156
- rect.right, rect.y, options[:from],
157
- rect.right, rect.bottom, options[:to],
158
- rect.x, rect.bottom, options[:to],
159
- options[:zorder], options[:mode]
160
- )
161
- else
162
- $window.draw_quad( rect.x, rect.y, options[:from],
163
- rect.x, rect.bottom, options[:from],
164
- rect.right, rect.bottom, options[:to],
165
- rect.right, rect.y, options[:to],
166
- options[:zorder], options[:mode]
167
- )
168
- end
169
- end
170
- end
171
-
172
- end
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
+ module Chingu
23
+ module Helpers
24
+
25
+ #
26
+ # Various helper-methods to manipulate the screen.
27
+ # All drawings depend on the global variable $window which should be an instance of Gosu::Window or Chingu::Window
28
+ #
29
+ module GFX
30
+
31
+ #
32
+ # Fills whole window with specified 'color' and 'zorder'
33
+ #
34
+ #def fill(color, zorder = 0)
35
+ # $window.draw_quad(0, 0, color,
36
+ # $window.width, 0, color,
37
+ # $window.width, $window.height, color,
38
+ # 0, $window.height, color,
39
+ # zorder, :default)
40
+ #end
41
+ #
42
+
43
+ # Fills window or a given rect with a gradient between two colors.
44
+ #
45
+ # :from - Start with this color
46
+ # :to - End with this color
47
+ # :rect - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
48
+ # :orientation - Either :vertical (top to bottom) or :horizontal (left to right)
49
+ #
50
+
51
+ def fill(options, zorder = 0)
52
+ #
53
+ # if only 1 color-argument is given, assume fullscreen simple color fill.
54
+ #
55
+ if options.is_a?(Gosu::Color)
56
+ $window.draw_quad(0, 0, options,
57
+ $window.width, 0, options,
58
+ $window.width, $window.height, options,
59
+ 0, $window.height, options, zorder, :default)
60
+ else
61
+ fill_gradient(options)
62
+ end
63
+ end
64
+
65
+ #
66
+ # Draws an unfilled rect in given color
67
+ #
68
+ def draw_rect(rect, color, zorder)
69
+ $window.draw_line(rect.x, rect.y, color, rect.right, rect.y, color, zorder)
70
+ $window.draw_line(rect.right, rect.y, color, rect.right, rect.bottom, color, zorder)
71
+ $window.draw_line(rect.right, rect.bottom, color, rect.x, rect.bottom, color, zorder)
72
+ $window.draw_line(rect.x, rect.bottom, color, rect.x, rect.y, color, zorder)
73
+ end
74
+
75
+
76
+ #
77
+ # Draws an unfilled circle, thanks shawn24!
78
+ #
79
+ CIRCLE_STEP = 10
80
+ def draw_circle(cx,cy,r,color)
81
+ 0.step(360, CIRCLE_STEP) do |a1|
82
+ a2 = a1 + CIRCLE_STEP
83
+ $window.draw_line cx + Gosu.offset_x(a1, r), cy + Gosu.offset_y(a1, r), color, cx + Gosu.offset_x(a2, r), cy + Gosu.offset_y(a2, r), color, 9999
84
+ end
85
+ end
86
+
87
+ #
88
+ # Fills a given Rect 'rect' with Color 'color', drawing with zorder 'zorder'
89
+ #
90
+ def fill_rect(rect, color, zorder = 0)
91
+ rect = Rect.new(rect) # Make sure it's a rect
92
+ $window.draw_quad( rect.x, rect.y, color,
93
+ rect.right, rect.y, color,
94
+ rect.right, rect.bottom, color,
95
+ rect.x, rect.bottom, color,
96
+ zorder, :default)
97
+ end
98
+
99
+ #
100
+ # Fills window or a given rect with a gradient between two colors.
101
+ #
102
+ # :from - Start with this color
103
+ # :to - End with this color
104
+ # :rect - Only fill rectangle :rect with the gradient, either a Rect-instance or [x,y,width,height] Array.
105
+ # :orientation - Either :vertical (top to bottom) or :horizontal (left to right)
106
+ #
107
+ def fill_gradient(options)
108
+ default_options = { :from => Gosu::Color::BLACK,
109
+ :to => Gosu::Color::WHITE,
110
+ :thickness => 10,
111
+ :orientation => :vertical,
112
+ :rect => Rect.new([0, 0, $window.width, $window.height]),
113
+ :zorder => 0,
114
+ :mode => :default
115
+ }
116
+ options = default_options.merge(options)
117
+
118
+ rect = Rect.new(options[:rect])
119
+ colors = options[:colors] || options.values_at(:from, :to)
120
+
121
+ case options[:orientation]
122
+ when :vertical
123
+ rect.height /= colors.count - 1
124
+ colors.each_cons(2) do |from, to|
125
+ $window.draw_quad( rect.left, rect.top, from,
126
+ rect.right, rect.top, from,
127
+ rect.right, rect.bottom, to,
128
+ rect.left, rect.bottom, to,
129
+ options[:zorder], options[:mode]
130
+ )
131
+ rect.top += rect.height
132
+ end
133
+ when :horizontal
134
+ rect.width /= colors.count - 1
135
+ colors.each_cons(2) do |from, to|
136
+ $window.draw_quad( rect.left, rect.top, from,
137
+ rect.left, rect.bottom, from,
138
+ rect.right, rect.bottom, to,
139
+ rect.right, rect.top, to,
140
+ options[:zorder], options[:mode]
141
+ )
142
+ rect.left += rect.width
143
+ end
144
+ else
145
+ raise ArgumentError, "bad gradient orientation: #{options[:orientation]}"
146
+ end
147
+ end
148
+ end
149
+
150
+ end
173
151
  end
@@ -44,6 +44,13 @@ module Chingu
44
44
  #
45
45
  #
46
46
  module InputClient
47
+ #
48
+ # Returns true or false depending on if the key is pressed
49
+ #
50
+ def holding?(key)
51
+ $window.button_down?(Chingu::Input::SYMBOL_TO_CONSTANT[key])
52
+ end
53
+
47
54
  def input=(input_map)
48
55
  @input ||= Hash.new
49
56
  #@input = input_map
@@ -21,7 +21,11 @@
21
21
 
22
22
 
23
23
  module Chingu
24
- module Inflector
24
+ module Inflector
25
+
26
+ #
27
+ # "automatic_assets" -> "AutomaticAssets"
28
+ #
25
29
  def Inflector.camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
26
30
  if first_letter_in_uppercase
27
31
  lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
@@ -29,6 +33,16 @@ module Chingu
29
33
  lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1]
30
34
  end
31
35
  end
32
-
36
+
37
+ #
38
+ # "FireBall" -> "fire_ball"
39
+ #
40
+ def Inflector.underscore(camel_cased_word)
41
+ camel_cased_word.to_s.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
42
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
43
+ tr("-", "_").
44
+ downcase
45
+ end
46
+
33
47
  end
34
48
  end