gosu_extensions 0.3.2 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.2
1
+ 0.3.3
@@ -6,6 +6,7 @@
6
6
  class GameWindow < Gosu::Window
7
7
 
8
8
  include InitializerHooks
9
+ include Threading
9
10
 
10
11
  # TODO handle more elegantly
11
12
  #
@@ -401,26 +402,6 @@ class GameWindow < Gosu::Window
401
402
  @uis.delete thing
402
403
  end
403
404
 
404
- # Scheduling
405
- #
406
-
407
- # Run some code at relative time <time>.
408
- #
409
- # Example:
410
- # # Will destroy the object that calls this method
411
- # # in 20 steps.
412
- # #
413
- # window.threaded 20 do
414
- # destroy!
415
- # end
416
- #
417
- # Note: You can also use after instead of threaded.
418
- #
419
- def threaded time = 1, &code
420
- @scheduling.add time, &code
421
- end
422
- alias after threaded
423
-
424
405
 
425
406
  # Utility Methods
426
407
  #
@@ -0,0 +1,37 @@
1
+ # Use this if you want something to run only some times,
2
+ # or something to run after x time.
3
+ #
4
+ module Threading
5
+
6
+ # Do something threaded.
7
+ #
8
+ # Default is: Instantly, in the next step.
9
+ #
10
+ # Note: Can also be called with after.
11
+ #
12
+ def threaded time = 1, &code
13
+ self.window.scheduling.add time, &code
14
+ end
15
+ alias after threaded
16
+
17
+ # Some things you can only do every x time units.
18
+ #
19
+ # Example:
20
+ # sometimes :loading, self.frequency do
21
+ # projectile = self.shot.shoot_from self
22
+ # projectile.rotation = self.muzzle_rotation[target]
23
+ # projectile.speed = self.muzzle_velocity[target] * projectile.velocity
24
+ # end
25
+ #
26
+ def sometimes variable, units = 1, &block
27
+ name = :"@__sometimes_#{variable}"
28
+ return if instance_variable_get(name)
29
+ instance_variable_set name, true
30
+ result = block.call
31
+ threaded units.to_i do
32
+ self.instance_variable_set name, false
33
+ end
34
+ result
35
+ end
36
+
37
+ end
@@ -32,6 +32,7 @@ require 'trait'
32
32
  require 'traits'
33
33
  require 'it_is_a'
34
34
 
35
+ require 'threading'
35
36
  require 'background'
36
37
  require 'sprites'
37
38
  require 'things'
@@ -11,8 +11,8 @@ module Hitpoints extend Trait
11
11
  Example:
12
12
  hitpoints 10_000
13
13
 
14
- Call hit(damage = 1) to remove hitpoints. This will call
15
- * hit! if hitpoints are still higher than 0.
14
+ Call hit!(damage = 1) to remove hitpoints. This will call
15
+ * callback hit if hitpoints are still higher than 0.
16
16
  * kill!, and if not available, destroy! if hitpoints are lower than 0.
17
17
  MANUAL
18
18
 
@@ -39,11 +39,9 @@ module Hitpoints extend Trait
39
39
 
40
40
  attr_accessor :hitpoints
41
41
 
42
- # Override to handle hit!
42
+ # Override to handle hit.
43
43
  #
44
- def hit!
45
-
46
- end
44
+ def hit; end
47
45
 
48
46
  # Hit the thing with that much damage.
49
47
  #
@@ -51,10 +49,19 @@ module Hitpoints extend Trait
51
49
  # kill!-s if lower, or destroy!-s if kill!
52
50
  # is not available.
53
51
  #
54
- def hit damage = 1
52
+ def hit! damage = 1
55
53
  self.hitpoints -= damage
56
- hit! if hitpoints > 0
57
- respond_to?(:kill!) ? kill! : destroy! if hitpoints == 0
54
+ hit if hitpoints > 0
55
+ respond_to?(:kill!) ? kill! : destroy! if hitpoints < 0
56
+ end
57
+
58
+ # kill! must reset hitpoints
59
+ #
60
+ # TODO Do irrespective of kill! call order
61
+ #
62
+ def kill!
63
+ super
64
+ self.hitpoints = self.class.prototype_hitpoints
58
65
  end
59
66
 
60
67
  end
data/lib/traits/lives.rb CHANGED
@@ -34,9 +34,9 @@ module Lives extend Trait
34
34
 
35
35
  attr_accessor :lives
36
36
 
37
- # Override to handle killed!
37
+ # Override to handle kill!
38
38
  #
39
- def killed!; end
39
+ def killed; end
40
40
 
41
41
  # Does three things:
42
42
  # * Deduct 1 live.
@@ -45,7 +45,7 @@ module Lives extend Trait
45
45
  #
46
46
  def kill!
47
47
  self.lives -= 1
48
- killed! if self.lives > 0
48
+ killed if self.lives > 0
49
49
  destroy! if self.lives == 0
50
50
  end
51
51
 
@@ -17,7 +17,7 @@ module Turnable extend Trait
17
17
  #
18
18
  module ClassMethods
19
19
  def turn_speed amount
20
- amount = amount.to_f / 2
20
+ amount = (amount.to_f*SUBSTEPS) / 10
21
21
  define_method :turn_speed do
22
22
  amount
23
23
  end
data/lib/units/sprite.rb CHANGED
@@ -7,6 +7,7 @@ class Sprite
7
7
  include VectorUtilities
8
8
  include Imageable
9
9
  include InitializerHooks
10
+ include Threading
10
11
  include ItIsA
11
12
  it_is Moveable
12
13
 
@@ -78,37 +79,6 @@ class Sprite
78
79
 
79
80
  end
80
81
 
81
- # Do something threaded.
82
- #
83
- # Default is: Instantly, in the next step.
84
- #
85
- # Note: Can also be called with after.
86
- #
87
- def threaded time = 1, &code
88
- self.window.threaded time, &code
89
- end
90
- alias after threaded
91
-
92
- # Some things you can only do every x time units.
93
- #
94
- # Example:
95
- # sometimes :loading, self.frequency do
96
- # projectile = self.shot.shoot_from self
97
- # projectile.rotation = self.muzzle_rotation[target]
98
- # projectile.speed = self.muzzle_velocity[target] * projectile.velocity
99
- # end
100
- #
101
- def sometimes variable, units = 1, &block
102
- name = :"@#{variable}"
103
- return if instance_variable_get(name)
104
- instance_variable_set name, true
105
- result = block.call
106
- threaded units.to_i do
107
- self.instance_variable_set name, false
108
- end
109
- result
110
- end
111
-
112
82
  # A sprite is not added to the physical environment.
113
83
  #
114
84
  # Override if you want it to.
data/lib/units/thing.rb CHANGED
@@ -35,7 +35,7 @@ class Thing < Sprite
35
35
  params = []
36
36
  params << CP::Body.new(self.mass, self.moment)
37
37
  params += args
38
- params << CP::Vec2.new(0.0, 0.0)
38
+ params << CP::Vec2.new(0.0, 0.0) unless CP::Vec2 === args.first
39
39
 
40
40
  @shape = shape_class.new *params
41
41
  end
@@ -0,0 +1,35 @@
1
+ require File.join(File.dirname(__FILE__), '/../../spec_helper')
2
+
3
+ describe Threading do
4
+
5
+ before(:each) do
6
+ @window = stub :window, :things => []
7
+ @threaded = test_class_with(Threading).new @window
8
+ end
9
+
10
+ describe "sometimes" do
11
+ it "should only call the block's content every x times" do
12
+ @threaded.stub! :threaded => nil
13
+
14
+ @threaded.sometimes(:some_id, :some_time) { :some_result }.should == :some_result
15
+ @threaded.sometimes(:some_id, :some_time) { :some_result }.should == nil
16
+
17
+ @threaded.instance_variable_set(:'@__sometimes_some_id', false)
18
+ @threaded.sometimes(:some_id, :some_time) { :some_result }.should == :some_result
19
+ end
20
+ end
21
+
22
+ describe "threaded" do
23
+ before(:each) do
24
+ @scheduling = stub :scheduling
25
+ @window.stub! :scheduling => @scheduling
26
+ end
27
+ it "should delegate to the window's scheduling" do
28
+ some_block = lambda {}
29
+ @scheduling.should_receive(:add).once.with :some_time, &some_block
30
+
31
+ @threaded.threaded :some_time, &some_block
32
+ end
33
+ end
34
+
35
+ end
@@ -41,21 +41,21 @@ describe Turnable do
41
41
  it "should have a defined turn_speed method" do
42
42
  lambda { @turnable.turn_speed }.should_not raise_error
43
43
  end
44
- it "should return the class defined turn_speed divided by 2" do
45
- @turnable.turn_speed.should == 0.65
44
+ it "should return the class defined turn_speed" do
45
+ @turnable.turn_speed.should == 1.3
46
46
  end
47
47
  end
48
48
 
49
49
  describe "turn_left" do
50
50
  it "should subtract an amount from the rotation" do
51
- @turnable.should_receive(:rotation=).once.with 0.9935
51
+ @turnable.should_receive(:rotation=).once.with 0.987
52
52
 
53
53
  @turnable.turn_left
54
54
  end
55
55
  end
56
56
  describe "turn_right" do
57
57
  it "should add an amount to the rotation" do
58
- @turnable.should_receive(:rotation=).once.with 1.0065
58
+ @turnable.should_receive(:rotation=).once.with 1.013
59
59
 
60
60
  @turnable.turn_right
61
61
  end
@@ -70,10 +70,14 @@ describe Thing do
70
70
  end
71
71
 
72
72
  describe "threaded" do
73
+ before(:each) do
74
+ @scheduling = stub :scheduling
75
+ @window.stub! :scheduling => @scheduling
76
+ end
73
77
  it "should delegate to the window" do
74
78
  some_block = Proc.new {}
75
79
 
76
- @window.should_receive(:threaded).once.with :some_time, &some_block
80
+ @scheduling.should_receive(:add).once.with :some_time, &some_block
77
81
 
78
82
  @thing.threaded :some_time, &some_block
79
83
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 2
9
- version: 0.3.2
8
+ - 3
9
+ version: 0.3.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -15,8 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-04-15 00:00:00 +02:00
19
- default_executable: gogogosu
18
+ date: 2010-04-16 00:00:00 +02:00
19
+ default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  name: gosu
@@ -44,8 +44,8 @@ dependencies:
44
44
  version_requirements: *id002
45
45
  description: ""
46
46
  email: florian.hanke@gmail.com
47
- executables:
48
- - gogogosu
47
+ executables: []
48
+
49
49
  extensions: []
50
50
 
51
51
  extra_rdoc_files: []
@@ -69,6 +69,7 @@ files:
69
69
  - lib/core/scheduling.rb
70
70
  - lib/core/sprites.rb
71
71
  - lib/core/things.rb
72
+ - lib/core/threading.rb
72
73
  - lib/core/trait.rb
73
74
  - lib/core/traits.rb
74
75
  - lib/core/vector_utilities.rb
@@ -138,6 +139,7 @@ test_files:
138
139
  - spec/lib/core/initializer_hooks_spec.rb
139
140
  - spec/lib/core/it_is_a_spec.rb
140
141
  - spec/lib/core/layer_spec.rb
142
+ - spec/lib/core/threading_spec.rb
141
143
  - spec/lib/core/trait_spec.rb
142
144
  - spec/lib/core/traits_spec.rb
143
145
  - spec/lib/extensions/module_spec.rb
data/bin/gogogosu DELETED
@@ -1,33 +0,0 @@
1
- #!/usr/local/bin/ruby
2
- # TODO remove
3
-
4
- Signal.trap("INT") { puts; exit } # CTRL-C
5
-
6
- File.open(File.join(File.dirname(__FILE__), '../VERSION')) do |f|
7
- puts "Gosu Extensions #{f.read}"
8
- end
9
-
10
- application = ARGV.first
11
-
12
- puts "Usage: gogogosu <application_name>" and exit(1) unless application
13
-
14
- require File.dirname(__FILE__) + '/../generator/gogogosu'
15
-
16
- generator = Generator::Gogogosu.new
17
-
18
- generator.dir application do
19
- generator.dir 'lib' do
20
-
21
- end
22
- generator.dir 'media' do
23
-
24
- end
25
- end
26
-
27
- puts <<-START
28
- Great!
29
-
30
- Now proceed as follows:
31
- 1. cd #{application}
32
- 2.
33
- START