gosu_extensions 0.3.1 → 0.3.2

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.1
1
+ 0.3.2
@@ -37,6 +37,15 @@ class GameWindow < Gosu::Window
37
37
  :proceed_condition,
38
38
  :collisions
39
39
 
40
+ # TODO Move to Debug Module
41
+ #
42
+ def draw_circle x, y, r, color
43
+ 0.step(360, 10) { |a1|
44
+ a2 = a1 + 10
45
+ draw_line x + Gosu.offset_x(a1, r), y + Gosu.offset_y(a1, r), color, x + Gosu.offset_x(a2, r), y + Gosu.offset_y(a2, r), color, 0
46
+ }
47
+ end
48
+
40
49
  def initialize
41
50
  setup_window
42
51
 
@@ -127,8 +136,18 @@ class GameWindow < Gosu::Window
127
136
  # default is no background
128
137
  end
129
138
 
139
+ def debug?
140
+ @debug
141
+ end
142
+
130
143
  class << self
131
144
 
145
+ def debug
146
+ InitializerHooks.register self do
147
+ @debug = true
148
+ end
149
+ end
150
+
132
151
  # Sets the Esc button to close the window.
133
152
  #
134
153
  def default_controls
data/lib/core/layer.rb CHANGED
@@ -7,5 +7,5 @@
7
7
  # * Background (Fixed, true Background)
8
8
  #
9
9
  module Layer
10
- Background, Ambient, Players, UI = 0, 1, 2, 3
10
+ Background, Ambient, Players, UI, Debug = 0, 1, 2, 3, 4
11
11
  end
@@ -0,0 +1,22 @@
1
+ module CP
2
+ module Shape
3
+
4
+ class Circle
5
+
6
+ attr_reader :radius, :center
7
+
8
+ def initialize_with_radius body, radius, center
9
+ @radius, @center = radius, center
10
+ initialize_without_radius body, radius, center
11
+ end
12
+ alias_method_chain :initialize, :radius
13
+
14
+
15
+ def debug window, _
16
+ window.draw_circle body.p.x, body.p.y, radius, Gosu::Color::RED
17
+ end
18
+
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module CP
2
+ module Shape
3
+
4
+ class Poly
5
+
6
+ attr_reader :verts, :offset
7
+
8
+ def initialize_with_verts body, verts, offset
9
+ @verts, @offset = verts, offset
10
+ initialize_without_verts body, verts, offset
11
+ end
12
+ alias_method_chain :initialize, :verts
13
+
14
+ def debug window, thing
15
+ colors = [Gosu::Color::RED]*verts.size
16
+ points = verts.map{ |vert| vert = vert.rotate(body.a.radians_to_vec2); vert += body.p; [vert.x, vert.y] }.zip(colors).flatten
17
+ points << 0 << :default
18
+ window.draw_quad *points
19
+ end
20
+
21
+ end
22
+
23
+ end
24
+ end
@@ -18,6 +18,8 @@ $:.unshift File.join(File.dirname(__FILE__), '/extensions')
18
18
  require 'module'
19
19
  require 'numeric'
20
20
  require 'math'
21
+ require 'circle'
22
+ require 'poly'
21
23
 
22
24
  $:.unshift File.join(File.dirname(__FILE__), '/core')
23
25
  require 'resources'
@@ -40,22 +40,32 @@ module Imageable extend Trait
40
40
  def image
41
41
  @image || raise(ImageMissingError.new)
42
42
  end
43
+ # Set this thing's image using a path.
44
+ #
45
+ def image_from path, *args
46
+ self.image = Gosu::Image.new self.window, File.join(Resources.root, path), *args
47
+ end
48
+ # Set this thing's image in the form of a sequenced image.
49
+ #
50
+ def sequenced_image_from path, width, height, frequency = 10, &block
51
+ @image_sequence_started = Time.now
52
+ self.image = Gosu::Image::load_tiles self.window, File.join(Resources.root, path), width, height, false
53
+ end
43
54
 
44
55
  module ClassMethods
45
56
 
46
57
  def image path, *args
47
58
  InitializerHooks.register self do
48
- self.image = Gosu::Image.new self.window, File.join(Resources.root, path), *args
59
+ image_from path, *args
49
60
  end
50
61
  end
51
62
 
52
63
  def sequenced_image path, width, height, frequency = 10, &block
53
64
  InitializerHooks.register self do
54
- @image_sequence_started = Time.now
55
- self.image = Gosu::Image::load_tiles self.window, File.join(Resources.root, path), width, height, false
65
+ sequenced_image_from path, width, height, frequency, &block
56
66
  end
57
67
  # divider = 1000 / frequency
58
-
68
+
59
69
  # Override image.
60
70
  #
61
71
  define_method :image do
@@ -102,8 +102,8 @@ module Shooter extend Trait
102
102
  self.shot_type.new @window
103
103
  end
104
104
 
105
- def muzzle_position
106
- instance_eval &(@muzzle_position || @muzzle_position = lambda { |*| self.position + self.rotation_vector*self.radius })
105
+ def muzzle_position distance = 10
106
+ instance_eval &(@muzzle_position || @muzzle_position = lambda { |*| self.position + self.rotation_vector * distance })
107
107
  end
108
108
  def muzzle_velocity target = nil
109
109
  instance_eval &(@muzzle_velocity || @muzzle_velocity = lambda { |*| self.rotation_vector })
data/lib/units/sprite.rb CHANGED
@@ -144,7 +144,6 @@ class Sprite
144
144
  # Draws its image.
145
145
  #
146
146
  def draw
147
- # p [self.class, self.layer, self.drawing_rotation, 0.5, 0.5, *self.current_size] unless self.position
148
147
  self.image.draw_rot self.position.x, self.position.y, self.layer, self.drawing_rotation, 0.5, 0.5, *self.current_size
149
148
  end
150
149
  def current_size
data/lib/units/thing.rb CHANGED
@@ -91,6 +91,10 @@ class Thing < Sprite
91
91
  #
92
92
  def move;end
93
93
 
94
+ def draw
95
+ window.debug? ? shape.debug(window, self) : super
96
+ end
97
+
94
98
  #
95
99
  #
96
100
  def speed= v
@@ -13,4 +13,7 @@ describe Layer do
13
13
  it "should be the right layer" do
14
14
  Layer::UI.should == 3
15
15
  end
16
+ it "should be the right layer" do
17
+ Layer::Debug.should == 4
18
+ end
16
19
  end
@@ -126,9 +126,9 @@ describe Shooter do
126
126
  describe 'muzzle_position' do
127
127
  context 'default' do
128
128
  it 'should use the default calculation' do
129
- @shooter.stub! :position => "position + ", :rotation_vector => "rotation", :radius => 3
129
+ @shooter.stub! :position => "position + ", :rotation_vector => "rotation"
130
130
 
131
- @shooter.muzzle_position.should == "position + rotationrotationrotation"
131
+ @shooter.muzzle_position.should == "position + rotationrotationrotationrotationrotationrotationrotationrotationrotationrotation"
132
132
  end
133
133
  end
134
134
  context 'non-default' do
@@ -35,10 +35,18 @@ describe Thing do
35
35
  :drawing_rotation => :some_drawing_rotation,
36
36
  :current_size => [:size_x, :size_y]
37
37
  end
38
- it "should delegate to the image" do
39
- @image.should_receive(:draw_rot).once.with :some_x, :some_y, :some_layer, :some_drawing_rotation, 0.5, 0.5, :size_x, :size_y
38
+ context 'debug on' do
40
39
 
41
- @thing.draw
40
+ end
41
+ context 'debug off' do
42
+ before(:each) do
43
+ @window.stub! :debug? => false
44
+ end
45
+ it "should delegate to the image" do
46
+ @image.should_receive(:draw_rot).once.with :some_x, :some_y, :some_layer, :some_drawing_rotation, 0.5, 0.5, :size_x, :size_y
47
+
48
+ @thing.draw
49
+ end
42
50
  end
43
51
  end
44
52
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 3
8
- - 1
9
- version: 0.3.1
8
+ - 2
9
+ version: 0.3.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Florian Hanke
@@ -74,9 +74,11 @@ files:
74
74
  - lib/core/vector_utilities.rb
75
75
  - lib/core/wave.rb
76
76
  - lib/core/waves.rb
77
+ - lib/extensions/circle.rb
77
78
  - lib/extensions/math.rb
78
79
  - lib/extensions/module.rb
79
80
  - lib/extensions/numeric.rb
81
+ - lib/extensions/poly.rb
80
82
  - lib/gosu_extensions.rb
81
83
  - lib/traits/attachable.rb
82
84
  - lib/traits/controllable.rb