Skip to content

Commit 692a891

Browse files
author
monkstone
committed
rationalize lib
1 parent a5b7576 commit 692a891

File tree

7 files changed

+185
-364
lines changed

7 files changed

+185
-364
lines changed
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
1-
require 'forwardable'
2-
3-
# The boundary class is used to create fixtures
41
class Boundary
5-
extend Forwardable
6-
def_delegators(:@app, :fill, :stroke, :rect, :rect_mode, :box2d)
7-
attr_reader :x, :y, :w, :h, :b
8-
def initialize(app, x, y, w, h)
9-
@app, @x, @y, @w, @h = app, x, y, w, h
2+
include Processing::Proxy
3+
attr_reader :box2d, :b, :pos, :size, :a
4+
5+
def initialize(b2d, pos, sz, a = 0)
6+
@box2d, @pos, @size, @a = b2d, pos, sz, a
7+
# Define the polygon
108
sd = PolygonShape.new
11-
box2d_w = box2d.scale_to_world(w / 2)
12-
box2d_h = box2d.scale_to_world(h / 2)
13-
sd.setAsBox(box2d_w, box2d_h)
9+
# Figure out the box2d coordinates
10+
box2d_w = box2d.scale_to_world(size.x / 2)
11+
box2d_h = box2d.scale_to_world(size.y / 2)
12+
# We're just a box
13+
sd.set_as_box(box2d_w, box2d_h)
1414
# Create the body
1515
bd = BodyDef.new
1616
bd.type = BodyType::STATIC
17-
bd.position.set(box2d.processing_to_world(x, y))
17+
bd.angle = a
18+
bd.position.set(box2d.processing_to_world(pos.x, pos.y))
1819
@b = box2d.create_body(bd)
1920
# Attached the shape to the body using a Fixture
2021
b.create_fixture(sd, 1)
2122
end
2223

23-
# Draw the boundary, if it were at an angle we'd have to do something fancier
24+
# Draw the boundary, it doesn't move so we don't have to ask the Body for location
2425
def display
2526
fill(0)
2627
stroke(0)
27-
rect_mode(Java::ProcessingCore::PConstants::CENTER)
28-
rect(x, y, w, h)
28+
stroke_weight(1)
29+
rect_mode(CENTER)
30+
a = b.get_angle
31+
push_matrix
32+
translate(pos.x, pos.y)
33+
rotate(-a)
34+
rect(0, 0, size.x,size.y)
35+
pop_matrix
2936
end
3037
end

samples/external_library/ruby_gem/jbox2d/lib/custom_listener.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'pbox2d'
2-
31
class CustomShape
42
include Processing::Proxy
53
# We need to keep track of a Body and a width and height
@@ -15,16 +13,16 @@ def initialize(b2d, x, y)
1513
# This function removes the particle from the box2d world
1614
def kill_body!
1715
box2d.destroy_body(body)
18-
true
1916
end
2017

2118
# Is the particle ready for deletion?
22-
def done?
19+
def done
2320
# Let's find the screen position of the particle
2421
pos = box2d.body_coord(body)
2522
# Is it off the bottom of the screen?
2623
return false unless pos.y > box2d.height
2724
kill_body!
25+
true
2826
end
2927

3028
# Drawing the box
@@ -73,63 +71,4 @@ def make_body(center)
7371
end
7472
end
7573

76-
class Boundary
77-
include Processing::Proxy
78-
attr_reader :box2d, :b, :x, :y, :w, :h
79-
def initialize(b2d, x, y, w, h, a)
80-
@box2d, @x, @y, @w, @h = b2d, x, y, w, h
81-
# Define the polygon
82-
sd = PolygonShape.new
83-
# Figure out the box2d coordinates
84-
box2d_w = box2d.scale_to_world(w / 2)
85-
box2d_h = box2d.scale_to_world(h / 2)
86-
# We're just a box
87-
sd.set_as_box(box2d_w, box2d_h)
88-
# Create the body
89-
bd = BodyDef.new
90-
bd.type = BodyType::STATIC
91-
bd.angle = a
92-
bd.position.set(box2d.processing_to_world(x, y))
93-
@b = box2d.create_body(bd)
94-
# Attached the shape to the body using a Fixture
95-
b.create_fixture(sd, 1)
96-
end
97-
98-
# Draw the boundary, it doesn't move so we don't have to ask
99-
# the Body for location
100-
def display
101-
fill(0)
102-
stroke(0)
103-
stroke_weight(1)
104-
rect_mode(CENTER)
105-
a = b.get_angle
106-
push_matrix
107-
translate(x, y)
108-
rotate(-a)
109-
rect(0, 0, w, h)
110-
pop_matrix
111-
end
112-
end
113-
114-
module Runnable
115-
def run
116-
reject! { |item| item.done? }
117-
each { |item| item.display }
118-
end
119-
end
12074

121-
class ParticleSystem
122-
include Enumerable, Runnable
123-
extend Forwardable
124-
attr_reader :particles
125-
126-
def_delegators(:@particles, :each, :reject!, :<<)
127-
128-
def initialize
129-
@particles = []
130-
end
131-
132-
def add_shape(b2d, x, y)
133-
@particles << CustomShape.new(b2d, x, y)
134-
end
135-
end

samples/external_library/ruby_gem/jbox2d/lib/particle.rb

Lines changed: 0 additions & 73 deletions
This file was deleted.

samples/external_library/ruby_gem/jbox2d/lib/particle_system.rb

Lines changed: 20 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
module Runnable
44
def run
5-
reject!(&:done)
6-
each(&:display)
5+
reject! { |item| item.done }
6+
each { |item| item.display }
77
end
88
end
99

@@ -14,18 +14,18 @@ class ParticleSystem
1414
def_delegator(:@particles, :empty?, :dead?)
1515

1616
attr_reader :x, :y
17-
18-
def initialize(app, num, x, y)
17+
18+
def initialize(bd, num, x, y)
1919
@particles = [] # Initialize the Array
20-
@x, @y = x, y # Store the origin point
20+
@x, @y = x, y # Store the origin point
2121
num.times do
22-
self << Particle.new(app, x, y)
22+
self << Particle.new(bd, x, y)
2323
end
2424
end
25-
26-
def add_particles(app, n)
25+
26+
def add_particles(bd, n)
2727
n.times do
28-
self << Particle.new(app, x, y)
28+
self << Particle.new(bd, x, y)
2929
end
3030
end
3131
end
@@ -34,40 +34,37 @@ def add_particles(app, n)
3434
require 'pbox2d'
3535

3636
class Particle
37-
extend Forwardable
38-
def_delegators(:@app, :box2d, :begin_shape, :end_shape,
39-
:vertex, :translate, :rotate, :stroke,
40-
:fill, :no_fill, :stroke_weight)
37+
include Processing::Proxy
4138
TRAIL_SIZE = 6
4239
# We need to keep track of a Body
43-
44-
attr_reader :trail, :body
45-
40+
41+
attr_reader :trail, :body, :box2d
42+
4643
# Constructor
47-
def initialize(app, x, y)
48-
@app = app
44+
def initialize(b2d, x, y)
45+
@box2d = b2d
4946
@trail = Array.new(TRAIL_SIZE, [x, y])
5047
# Add the box to the box2d world
5148
# Here's a little trick, let's make a tiny tiny radius
5249
# This way we have collisions, but they don't overwhelm the system
5350
make_body(x, y, 0.2)
5451
end
55-
52+
5653
# This function removes the particle from the box2d world
5754
def kill_body
5855
box2d.destroy_body(body)
5956
end
60-
57+
6158
# Is the particle ready for deletion?
6259
def done
6360
# Let's find the screen position of the particle
6461
pos = box2d.body_coord(body)
6562
# Is it off the bottom of the screen?
66-
return false unless pos.y > box2d.height + 20
63+
return false unless (pos.y > box2d.height + 20)
6764
kill_body
6865
true
6966
end
70-
67+
7168
# Drawing the box
7269
def display
7370
# We look at each body and get its screen position
@@ -87,7 +84,7 @@ def display
8784
end
8885
end_shape
8986
end
90-
87+
9188
# This function adds the rectangle to the box2d world
9289
def make_body(x, y, r)
9390
# Define and create the body
@@ -111,44 +108,3 @@ def make_body(x, y, r)
111108
body.create_fixture(fd)
112109
end
113110
end
114-
115-
class Boundary
116-
extend Forwardable
117-
def_delegators(:@app, :box2d, :push_matrix, :pop_matrix, :stroke, :width,
118-
:vertex, :translate, :rotate, :rect_mode, :rect,
119-
:fill, :no_fill, :stroke_weight)
120-
attr_reader :b, :x, :y, :h
121-
122-
def initialize(app, x, y, h, a)
123-
@app, @x, @y, @h = app, x, y, h
124-
# Define the polygon
125-
sd = PolygonShape.new
126-
# Figure out the box2d coordinates
127-
box2d_w = box2d.scale_to_world(width / 2)
128-
box2d_h = box2d.scale_to_world(h / 2)
129-
# We're just a box
130-
sd.set_as_box(box2d_w, box2d_h)
131-
# Create the body
132-
bd = BodyDef.new
133-
bd.type = BodyType::STATIC
134-
bd.angle = a
135-
bd.position.set(box2d.processing_to_world(x, y))
136-
@b = box2d.create_body(bd)
137-
# Attached the shape to the body using a Fixture
138-
b.create_fixture(sd, 1)
139-
end
140-
141-
# Draw the boundary, it doesn't move so we don't have to ask the Body for location
142-
def display
143-
fill(0)
144-
stroke(0)
145-
stroke_weight(1)
146-
rect_mode(Java::ProcessingCore::PConstants::CENTER)
147-
a = b.get_angle
148-
push_matrix
149-
translate(x, y)
150-
rotate(-a)
151-
rect(0, 0, width, h)
152-
pop_matrix
153-
end
154-
end

0 commit comments

Comments
 (0)