gosu_extensions 0.1.11 → 0.1.12

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.11
1
+ 0.1.12
@@ -37,6 +37,7 @@ require 'targetable'
37
37
  require 'turnable'
38
38
  require 'controllable'
39
39
  require 'moveable'
40
+ require 'imageable'
40
41
  require 'short_lived'
41
42
 
42
43
  $:.unshift File.join(File.dirname(__FILE__), '/units')
@@ -26,9 +26,19 @@
26
26
  #
27
27
  module Damaging
28
28
 
29
- # TODO Explain better what to do.
30
- #
31
- class DamageMissingError < StandardError; end
29
+ class DamageMissingError < RuntimeError
30
+ def initialize
31
+ super <<-MESSAGE
32
+ In a Damaging thing, you need to define method
33
+ damage damage = nil, &block
34
+ with params
35
+ damage 13 # some value
36
+ or
37
+ damage { 13 + rand(7) } # some block
38
+ to define how much damage the thing does.
39
+ MESSAGE
40
+ end
41
+ end
32
42
 
33
43
  def self.included klass
34
44
  klass.extend ClassMethods
@@ -0,0 +1,49 @@
1
+ module Imageable
2
+
3
+ class ImageMissingError < RuntimeError
4
+ def initialize
5
+ super <<-MESSAGE
6
+ In an Imageable, you either need to define method
7
+ image path, *args
8
+ for an unchanging image
9
+ or
10
+ sequenced_image path, width, height, frequency = 10, &block
11
+ for a sprite sequence.
12
+ MESSAGE
13
+ end
14
+ end
15
+
16
+ def self.included klass
17
+ klass.extend ClassMethods
18
+ end
19
+
20
+ def initialize window
21
+ raise ImageMissingError.new unless self.respond_to? :image
22
+ super window
23
+ end
24
+
25
+ module ClassMethods
26
+
27
+ def image path, *args
28
+ InitializerHooks.register self do
29
+ @image = Gosu::Image.new self.window, File.join(Resources.root, path), *args
30
+ end
31
+ define_method :image do
32
+ @image
33
+ end
34
+ end
35
+
36
+ def sequenced_image path, width, height, frequency = 10, &block
37
+ InitializerHooks.register self do
38
+ @image_sequence_started = Time.now
39
+ @image = Gosu::Image::load_tiles self.window, File.join(Resources.root, path), width, height, false
40
+ end
41
+ # divider = 1000 / frequency
42
+ define_method :image do
43
+ @image[(block ? block : lambda { (Time.now - @image_sequence_started)*frequency % @image.size })[]]
44
+ end
45
+ end
46
+
47
+ end
48
+
49
+ end
@@ -1,8 +1,18 @@
1
1
  module ShortLived
2
2
 
3
- # TODO Explain better what to do.
4
- #
5
- class LifetimeMissingError < StandardError; end
3
+ class LifetimeMissingError < RuntimeError
4
+ def initialize
5
+ super <<-MESSAGE
6
+ A ShortLived thing must define method
7
+ lifetime lifetime = nil, &block
8
+ with either params
9
+ lifetime 74 # some value
10
+ or
11
+ lifetime { 50 + rand(50) } # some block
12
+ to define how long the thing should live until it is destroyed.
13
+ MESSAGE
14
+ end
15
+ end
6
16
 
7
17
  def self.included klass
8
18
  klass.extend ClassMethods
data/lib/units/thing.rb CHANGED
@@ -4,8 +4,9 @@ class Thing
4
4
  include InitializerHooks
5
5
  include ItIsA
6
6
 
7
- # TODO Move this.
7
+ # TODO Move these.
8
8
  #
9
+ # it_is Imageable
9
10
  it_is Moveable
10
11
 
11
12
  attr_writer :layer
@@ -26,27 +27,6 @@ class Thing
26
27
  end
27
28
 
28
29
  class << self
29
-
30
- # TODO Move to module.
31
- #
32
- def image path, *args
33
- InitializerHooks.register self do
34
- @image = Gosu::Image.new self.window, File.join(Resources.root, path), *args
35
- end
36
- define_method :image do
37
- @image
38
- end
39
- end
40
- def sequenced_image path, width, height, frequency = 10, &block
41
- InitializerHooks.register self do
42
- @image_sequence_started = Time.now
43
- @image = Gosu::Image::load_tiles self.window, File.join(Resources.root, path), width, height, false
44
- end
45
- # divider = 1000 / frequency
46
- define_method :image do
47
- @image[(block ? block : lambda { (Time.now - @image_sequence_started)*frequency % @image.size })[]]
48
- end
49
- end
50
30
  @@form_shape_class_mapping = { :circle => CP::Shape::Circle }
51
31
  def shape form
52
32
  form_shape_class_mapping = @@form_shape_class_mapping
@@ -33,6 +33,18 @@ describe Damaging do
33
33
  it "should raise a DamageMissingError" do
34
34
  lambda { @damaging_class.new(@window) }.should raise_error(Damaging::DamageMissingError)
35
35
  end
36
+ it "should raise with the right message" do
37
+ lambda { @damaging_class.new(@window) }.should raise_error(Damaging::DamageMissingError, <<-MESSAGE
38
+ In a Damaging thing, you need to define method
39
+ damage damage = nil, &block
40
+ with params
41
+ damage 13 # some value
42
+ or
43
+ damage { 13 + rand(7) } # some block
44
+ to define how much damage the thing does.
45
+ MESSAGE
46
+ )
47
+ end
36
48
  end
37
49
 
38
50
  end
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), '/../../spec_helper')
2
+
3
+ describe Imageable do
4
+
5
+ before(:each) do
6
+ @window = stub :window
7
+ Resources.stub! :root => 'some/root'
8
+ end
9
+
10
+ context 'image defined on class' do
11
+ before(:each) do
12
+ @imageable_class = test_class_with(Imageable) do
13
+ image 'some/path.png'
14
+ end
15
+ end
16
+ it "should define a method lifetime which returns the result of the block" do
17
+ Gosu::Image.stub! :new => :some_image
18
+
19
+ @imageable_class.new(@window).image.should == :some_image
20
+ end
21
+ end
22
+ context 'sequenced image defined on class' do
23
+ before(:each) do
24
+ @imageable_class = test_class_with(Imageable) do
25
+ sequenced_image 'some/path', :some_width, :some_height, 10.0
26
+ end
27
+ end
28
+ it "should define a method damage which returns the set value" do
29
+ image = stub :image
30
+ sequenced_image = stub :image, :size => 10, :[] => image
31
+
32
+ Gosu::Image.stub! :load_tiles => sequenced_image
33
+
34
+ @imageable_class.new(@window).image.should == image
35
+ end
36
+ end
37
+ context 'no image given – what now?' do
38
+ before(:each) do
39
+ @imageable_class = Class.new do
40
+ include Imageable
41
+ end
42
+ end
43
+ it "should raise a ImageMissingError" do
44
+ lambda { @imageable_class.new(@window) }.should raise_error(Imageable::ImageMissingError)
45
+ end
46
+ it "should raise with the right message" do
47
+ lambda { @imageable_class.new(@window) }.should raise_error(Imageable::ImageMissingError, <<-MESSAGE
48
+ In an Imageable, you either need to define method
49
+ image path, *args
50
+ for an unchanging image
51
+ or
52
+ sequenced_image path, width, height, frequency = 10, &block
53
+ for a sprite sequence.
54
+ MESSAGE
55
+ )
56
+ end
57
+ end
58
+
59
+ end
@@ -48,13 +48,23 @@ describe ShortLived do
48
48
  end
49
49
  context 'no lifetime given – what now?' do
50
50
  before(:each) do
51
- @short_lived_class = Class.new(SuperClass) do
52
- include ShortLived
53
- end
51
+ @short_lived_class = test_class_with ShortLived
54
52
  end
55
53
  it "should raise a LifetimeMissingError" do
56
54
  lambda { @short_lived_class.new(@window) }.should raise_error(ShortLived::LifetimeMissingError)
57
55
  end
56
+ it "should raise with the right message" do
57
+ lambda { @short_lived_class.new(@window) }.should raise_error(ShortLived::LifetimeMissingError, <<-MESSAGE
58
+ A ShortLived thing must define method
59
+ lifetime lifetime = nil, &block
60
+ with either params
61
+ lifetime 74 # some value
62
+ or
63
+ lifetime { 50 + rand(50) } # some block
64
+ to define how long the thing should live until it is destroyed.
65
+ MESSAGE
66
+ )
67
+ end
58
68
  end
59
69
 
60
70
  end
@@ -0,0 +1,32 @@
1
+ require File.join(File.dirname(__FILE__), '/../../spec_helper')
2
+
3
+ describe Thing do
4
+
5
+ before(:each) do
6
+ @window = stub :window
7
+ @thing = Thing.new @window
8
+ end
9
+
10
+ describe "window" do
11
+ it "should return the window" do
12
+ @thing.window.should == @window
13
+ end
14
+ end
15
+
16
+ describe "layer" do
17
+ context 'default' do
18
+ it "should be on the player layer" do
19
+ @thing.layer.should == Layer::Players
20
+ end
21
+ end
22
+ context 'non-default' do
23
+ before(:each) do
24
+ @thing.layer = :non_default_layer
25
+ end
26
+ it "should be on the non default layer" do
27
+ @thing.layer.should == :non_default_layer
28
+ end
29
+ end
30
+ end
31
+
32
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 11
9
- version: 0.1.11
8
+ - 12
9
+ version: 0.1.12
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -66,6 +66,7 @@ files:
66
66
  - lib/traits/controllable.rb
67
67
  - lib/traits/damaging.rb
68
68
  - lib/traits/generator.rb
69
+ - lib/traits/imageable.rb
69
70
  - lib/traits/initializer_hooks.rb
70
71
  - lib/traits/it_is_a.rb
71
72
  - lib/traits/lives.rb
@@ -116,9 +117,11 @@ test_files:
116
117
  - spec/lib/extensions/numeric_spec.rb
117
118
  - spec/lib/traits/attachable_spec.rb
118
119
  - spec/lib/traits/damaging_spec.rb
120
+ - spec/lib/traits/imageable_spec.rb
119
121
  - spec/lib/traits/shooter_spec.rb
120
122
  - spec/lib/traits/short_lived_spec.rb
121
123
  - spec/lib/traits/shot_spec.rb
122
124
  - spec/lib/traits/targetable_spec.rb
123
125
  - spec/lib/traits/targeting_spec.rb
124
126
  - spec/lib/traits/turnable_spec.rb
127
+ - spec/lib/units/thing_spec.rb