chingu 0.8.0.5 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -28,7 +28,6 @@ module Chingu
28
28
  # :x, :y, :angle, :factor_x, :factor_y, :center_x, :center_y, :zorder, :mode, :visible
29
29
  #
30
30
  module Sprite
31
- include Chingu::Helpers::OptionsSetter
32
31
  include Chingu::Helpers::RotationCenter # Adds easy and verbose modification of @center_x and @center_y
33
32
 
34
33
  module ClassMethods
@@ -37,21 +36,31 @@ module Chingu
37
36
  end
38
37
  end
39
38
 
40
- attr_accessor :x, :y, :angle, :factor_x, :factor_y, :center_x, :center_y, :zorder, :mode, :visible, :color
39
+ attr_accessor :x, :y, :angle, :factor_x, :factor_y, :center_x, :center_y, :zorder, :mode, :color
41
40
  attr_reader :factor, :center, :height, :width, :image
41
+ attr_accessor :visible # kill this? force use of setter
42
42
 
43
43
  def setup_trait(object_options = {})
44
- # default settings for all variables unless set in constructor
45
- defaults = {
46
- :x => 0, :y => 0, :angle => 0, :factor => ($window.factor||1.0),
47
- :zorder => 100, :center_x => 0.5, :center_y => 0.5,
48
- :mode => :default, :color => nil, :visible => true
49
- }
44
+ @visible = true unless options[:visible] == false
45
+ self.image = options[:image] if options[:image]
46
+ self.color = options[:color] || ::Gosu::Color::WHITE.dup
47
+ self.alpha = options[:alpha] if options[:alpha]
48
+ self.mode = options[:mode] || :default
49
+ self.x = options[:x] || 0
50
+ self.y = options[:y] || 0
51
+ self.zorder = options[:zorder] || 100
52
+ self.angle = options[:angle] || 0
50
53
 
51
- # if user specs :image take care of it first since width, height etc depends on it.
52
- self.image = object_options.delete(:image) if object_options[:image]
54
+ self.factor = options[:factor] || options[:scale] || $window.factor || 1.0
55
+ self.factor_x = options[:factor_x].to_f if options[:factor_x]
56
+ self.factor_y = options[:factor_y].to_f if options[:factor_y]
57
+ self.rotation_center = options[:rotation_center] || :center_center
53
58
 
54
- set_options(trait_options[:sprite].merge(object_options), defaults)
59
+ if self.image
60
+ self.width = options[:width] if options[:width]
61
+ self.height = options[:height] if options[:height]
62
+ self.size = options[:size] if options[:size]
63
+ end
55
64
 
56
65
  super
57
66
  end
@@ -196,6 +205,7 @@ module Chingu
196
205
  # Disable automatic calling of draw and draw_trait each game loop
197
206
  #
198
207
  def hide!
208
+ @parent.game_objects.hide_game_object(self) if @parent && @visible == true
199
209
  @visible = false
200
210
  end
201
211
 
@@ -203,6 +213,7 @@ module Chingu
203
213
  # Enable automatic calling of draw and draw_trait each game loop
204
214
  #
205
215
  def show!
216
+ @parent.game_objects.show_game_object(self) if @parent && @visible == false
206
217
  @visible = true
207
218
  end
208
219
 
@@ -3,6 +3,9 @@ require 'spec_helper'
3
3
  class MyBasicGameObject < Chingu::BasicGameObject
4
4
  end
5
5
 
6
+ class MyBasicGameObject2 < Chingu::BasicGameObject
7
+ end
8
+
6
9
  class MyBasicGameObjectWithSetup < Chingu::BasicGameObject
7
10
  def setup
8
11
  @paused = true
@@ -28,10 +31,11 @@ module Chingu
28
31
  it { should respond_to(:draw_trait) }
29
32
  it { should respond_to(:filename) }
30
33
 
31
- context "A class inherited from BasicGameObject" do
34
+ context "A class inherited from BasicGameObject using classmethod create" do
35
+
32
36
  it "should be automatically stored in $window.game_objects" do
37
+ MyBasicGameObject.instances = []
33
38
  3.times { go = MyBasicGameObject.create }
34
- $window.update
35
39
  $window.game_objects.size.should == 3
36
40
  end
37
41
 
@@ -41,34 +45,47 @@ module Chingu
41
45
  end
42
46
 
43
47
  it "should keep track of its instances in class#all" do
48
+ MyBasicGameObject.instances = []
44
49
  3.times { MyBasicGameObject.create }
45
50
  #
46
51
  # Can/should we remove the dependency on #update here before the created objects gets saved?
47
52
  # We mostly protect against adding to the object array while iterating over it
48
53
  #
49
- $window.update
50
54
  MyBasicGameObject.all.size.should == 3
51
55
  MyBasicGameObject.size.should == 3
52
56
  end
53
57
 
54
58
  it "should be removed from game_objects list when destroy() is called" do
59
+ MyBasicGameObject.instances = []
55
60
  go = MyBasicGameObject.create
56
- $window.update
57
61
  $window.game_objects.size.should == 1
58
62
  go.destroy
59
- $window.update
60
63
  $window.game_objects.size.should == 0
61
64
  end
62
65
 
63
- it "should have all instances removed with classmethod #destroy_all()" do
66
+ it "should have all internal list cleared with classmethod destroy_all()" do
67
+ MyBasicGameObject.instances = []
64
68
  3.times { MyBasicGameObject.create }
65
- $window.update
66
69
  MyBasicGameObject.destroy_all
67
- $window.update
68
- $window.game_objects.size.should == 0
70
+ MyBasicGameObject.size.should == 0
69
71
  end
70
72
 
73
+ it "should have all instances removed from parent-list with classmethod destroy_all()" do
74
+ MyBasicGameObject.instances = []
75
+ 3.times { MyBasicGameObject.create }
76
+ MyBasicGameObject.destroy_all
77
+ $window.game_objects.size.should == 0
78
+ end
79
+ end
80
+
81
+ context "A class inherited from BasicGameObject" do
82
+ it "should return empty array on classmethod all() if no objects have been created" do
83
+ # Only place MyBasicGameObject2 is used
84
+ MyBasicGameObject2.all.should == []
85
+ end
86
+
71
87
  it "should take hash-argument, parse it and save in options" do
88
+ MyBasicGameObject.instances = []
72
89
  game_object = MyBasicGameObject.new(:paused => false, :foo => :bar)
73
90
  game_object.paused?.should == false
74
91
  game_object.options.should == {:paused => false, :foo => :bar}
@@ -96,8 +113,7 @@ module Chingu
96
113
 
97
114
  end
98
115
 
99
- context "when created with defaults in Chingu::Window" do
100
-
116
+ context "when created with defaults in Chingu::Window" do
101
117
  it "should belong to main window it not created inside a game state" do
102
118
  subject.parent.should == @game
103
119
  end
@@ -6,7 +6,11 @@ module Chingu
6
6
  before :each do
7
7
  @console = Chingu::Console.new
8
8
  end
9
-
9
+
10
+ after :each do
11
+ $window = nil
12
+ end
13
+
10
14
  it { @console.should respond_to :start }
11
15
  it { @console.should respond_to :fps }
12
16
  it { @console.should respond_to :update }
@@ -4,7 +4,7 @@ module Chingu
4
4
 
5
5
  describe GameObjectList do
6
6
  before :each do
7
- @game = Chingu::Window.new
7
+ @game = Chingu::Window.new
8
8
  end
9
9
 
10
10
  after :each do
@@ -45,6 +45,24 @@ module Chingu
45
45
  @game.game_objects.update
46
46
  end
47
47
 
48
+ it "should keep track of unpaused game objects" do
49
+ go = GameObject.create
50
+ go.should_receive(:update)
51
+ @game.game_objects.update
52
+ go.pause
53
+ go.should_not_receive(:update)
54
+ @game.game_objects.update
55
+ end
56
+
57
+ it "should keep track of visible game objects" do
58
+ go = GameObject.create
59
+ go.should_receive(:draw)
60
+ @game.game_objects.draw
61
+ go.hide!
62
+ go.should_not_receive(:draw)
63
+ @game.game_objects.draw
64
+ end
65
+
48
66
  it "should call draw() on all visible game objects" do
49
67
  GameObject.create.should_receive(:draw)
50
68
  GameObject.create(:visible => false).should_not_receive(:draw)
@@ -0,0 +1,96 @@
1
+ require 'spec_helper'
2
+ include Chingu
3
+
4
+
5
+ # Creates 20x20 pixel sized objects
6
+ class MyGameObject < Chingu::GameObject
7
+ trait :bounding_box
8
+ def setup
9
+ @image = Gosu::Image["rect_20x20.png"]
10
+ self.rotation_center = :top_left
11
+ end
12
+ end
13
+
14
+ # Creates 40x40 pixel sized objects
15
+ class MyBigGameObject < Chingu::GameObject
16
+ trait :bounding_box
17
+ def setup
18
+ @image = Gosu::Image["rect_20x20.png"]
19
+ self.rotation_center = :top_left
20
+ self.size = [40,40]
21
+ end
22
+ end
23
+
24
+ module Chingu
25
+
26
+ describe "GameObjectMap with grid [20,20]" do
27
+ before :each do
28
+ @game = Chingu::Window.new
29
+
30
+ # Gosu uses the paths based on where rspec is, not where this file is, so we need to do it manually!
31
+ Gosu::Image::autoload_dirs.unshift File.join(File.dirname(File.expand_path(__FILE__)), 'images')
32
+
33
+ MyGameObject.destroy_all
34
+ MyBigGameObject.destroy_all
35
+ @game_object = MyGameObject.create
36
+ @big_game_object = MyBigGameObject.create
37
+ end
38
+
39
+ after :each do
40
+ @game.close
41
+ end
42
+
43
+ context "setup of test should consist of" do
44
+ it "should contain 1 MyGameObject" do
45
+ MyGameObject.size.should == 1
46
+ end
47
+ it "should contain 1 MyBigGameObject" do
48
+ MyBigGameObject.size.should == 1
49
+ end
50
+
51
+ end
52
+
53
+ context "containing a game object size 20x20 postion 0,0" do
54
+ before :each do
55
+ @game_object_map = GameObjectMap.new(:game_objects => MyGameObject.all, :grid => [20,20])
56
+ end
57
+
58
+ it "should be found at 0,0" do
59
+ @game_object_map.at(0,0).should == @game_object
60
+ end
61
+
62
+ it "should be found at 10,10" do
63
+ @game_object_map.at(10,10).should == @game_object
64
+ end
65
+
66
+ it "should be found at 20,20" do
67
+ @game_object_map.at(20,20).should == nil
68
+ end
69
+
70
+ it "should Not be found at 21,21" do
71
+ @game_object_map.at(21,21).should == nil
72
+ end
73
+ end
74
+
75
+ context "containing a game object size 40x40 postion 0,0" do
76
+ before :each do
77
+ @game_object_map = GameObjectMap.new(:game_objects => MyBigGameObject.all, :grid => [20,20])
78
+ end
79
+
80
+ it "should be found at 0,0" do
81
+ @game_object_map.at(0,0).should == @big_game_object
82
+ end
83
+ it "should be found at 20,20" do
84
+ @game_object_map.at(20,20).should == @big_game_object
85
+ end
86
+ it "should be found at 39,39" do
87
+ @game_object_map.at(39,39).should == @big_game_object
88
+ end
89
+
90
+ it "should Not be found at 40,40" do
91
+ @game_object_map.at(40,40).should == nil
92
+ end
93
+
94
+ end
95
+ end
96
+ end
@@ -171,6 +171,7 @@ module Chingu
171
171
 
172
172
  context "class methods" do
173
173
  it "should go through all instances of class on #each" do
174
+ GameObject.destroy_all
174
175
  go1 = GameObject.create
175
176
  go2 = GameObject.create
176
177
 
@@ -183,6 +184,7 @@ module Chingu
183
184
  end
184
185
 
185
186
  it "should go through all instances of class on #each_with_index" do
187
+ GameObject.destroy_all
186
188
  go1 = GameObject.create
187
189
  go2 = GameObject.create
188
190
 
metadata CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 8
8
- - 0
9
- - 5
10
- version: 0.8.0.5
8
+ - 1
9
+ version: 0.8.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - ippa
@@ -15,7 +14,7 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-01-03 00:00:00 +01:00
17
+ date: 2011-01-08 00:00:00 +01:00
19
18
  default_executable:
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
@@ -116,15 +115,20 @@ files:
116
115
  - README.rdoc
117
116
  - Rakefile
118
117
  - benchmarks/README.txt
118
+ - benchmarks/array_vs_hash.rb
119
119
  - benchmarks/arrays_bench.rb
120
120
  - benchmarks/benchmark.rb
121
121
  - benchmarks/benchmark3.rb
122
122
  - benchmarks/benchmark4.rb
123
123
  - benchmarks/benchmark5.rb
124
124
  - benchmarks/benchmark6.rb
125
+ - benchmarks/game_object_list_benchmark.rb
125
126
  - benchmarks/game_objects_benchmark.rb
127
+ - benchmarks/lookup_benchmark.rb
126
128
  - benchmarks/meta_benchmark.rb
127
129
  - benchmarks/meta_benchmark2.rb
130
+ - benchmarks/trait_benchmark.rb
131
+ - benchmarks/window_benchmark.rb
128
132
  - chingu.gemspec
129
133
  - examples/example10_traits_retrofy.rb
130
134
  - examples/example11_animation.rb
@@ -248,6 +252,7 @@ files:
248
252
  - lib/chingu/traits/collision_detection.rb
249
253
  - lib/chingu/traits/effect.rb
250
254
  - lib/chingu/traits/retrofy.rb
255
+ - lib/chingu/traits/simple_sprite.rb
251
256
  - lib/chingu/traits/sprite.rb
252
257
  - lib/chingu/traits/timer.rb
253
258
  - lib/chingu/traits/velocity.rb
@@ -260,6 +265,7 @@ files:
260
265
  - spec/chingu/console_spec.rb
261
266
  - spec/chingu/fpscounter_spec.rb
262
267
  - spec/chingu/game_object_list_spec.rb
268
+ - spec/chingu/game_object_map_spec.rb
263
269
  - spec/chingu/game_object_spec.rb
264
270
  - spec/chingu/game_state_manager_spec.rb
265
271
  - spec/chingu/helpers/input_client_spec.rb
@@ -343,6 +349,7 @@ test_files:
343
349
  - spec/chingu/console_spec.rb
344
350
  - spec/chingu/fpscounter_spec.rb
345
351
  - spec/chingu/game_object_list_spec.rb
352
+ - spec/chingu/game_object_map_spec.rb
346
353
  - spec/chingu/game_object_spec.rb
347
354
  - spec/chingu/game_state_manager_spec.rb
348
355
  - spec/chingu/helpers/input_client_spec.rb