Skip to content

Commit ba51580

Browse files
committed
Make code sample less monolithic in component.
1 parent ca9ae8c commit ba51580

File tree

6 files changed

+96
-85
lines changed

6 files changed

+96
-85
lines changed

asset/component-uml-processed.psd

-2.29 MB
Binary file not shown.

asset/component-uml-scan.psd

-2.93 MB
Binary file not shown.

book/component.markdown

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,11 @@ just "the next version of Photoshop."
282282

283283
^code 1
284284

285-
`Bjorn` has an `update()` method that gets called once per frame by the game. It
286-
reads the joystick to determine how to accelerate the baker. Then it resolves
285+
`Bjorn` has an `update()` method that gets called once per frame by the game:
286+
287+
^code monolithic-update
288+
289+
It reads the joystick to determine how to accelerate the baker. Then it resolves
287290
its new position with the physics engine. Finally, it draws Bjørn onto the
288291
screen.
289292

@@ -561,20 +564,20 @@ likely support more than one at the same time in your designs.
561564

562565
* **By referring directly to each other:**
563566

564-
* The idea here is that components that need to talk will have direct
565-
references to each other without having to go through the container
566-
object at all.
567+
The idea here is that components that need to talk will have direct
568+
references to each other without having to go through the container
569+
object at all.
567570

568-
Let's say we want to let Bjørn jump. The graphics code needs to know if
569-
he should be drawn using a jump sprite or not. It can determine this by
570-
asking the physics engine if he's currently on the ground. An easy way
571-
to do this is by letting the graphics component know about the physics
572-
component directly:
571+
Let's say we want to let Bjørn jump. The graphics code needs to know if
572+
he should be drawn using a jump sprite or not. It can determine this by
573+
asking the physics engine if he's currently on the ground. An easy way
574+
to do this is by letting the graphics component know about the physics
575+
component directly:
573576

574-
^code 18
577+
^code 18
575578

576-
When we construct Bjørn's `GraphicsComponent`, we'll give it a reference
577-
to his corresponding `PhysicsComponent`.
579+
When we construct Bjørn's `GraphicsComponent`, we'll give it a reference
580+
to his corresponding `PhysicsComponent`.
578581

579582
* *It's simple and fast.* Communication is a direct method call from one
580583
object to another. The component can call any method that is supported

code/cpp/component.h

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,7 @@ namespace Monolithic
7777
x_(0), y_(0)
7878
{}
7979

80-
void update(World& world, Graphics& graphics)
81-
{
82-
// Apply user input to hero's velocity.
83-
switch (Controller::getJoystickDirection())
84-
{
85-
case DIR_LEFT:
86-
velocity_ -= WALK_ACCELERATION;
87-
break;
88-
89-
case DIR_RIGHT:
90-
velocity_ += WALK_ACCELERATION;
91-
break;
92-
//^omit
93-
case DIR_NONE: break; // Do nothing.
94-
//^omit
95-
}
96-
97-
// Modify position by velocity.
98-
x_ += velocity_;
99-
world.resolveCollision(volume_, x_, y_, velocity_);
100-
101-
// Draw the appropriate sprite.
102-
Sprite* sprite = &spriteStand_;
103-
if (velocity_ < 0)
104-
{
105-
sprite = &spriteWalkLeft_;
106-
}
107-
else if (velocity_ > 0)
108-
{
109-
sprite = &spriteWalkRight_;
110-
}
111-
112-
graphics.draw(*sprite, x_, y_);
113-
}
80+
void update(World& world, Graphics& graphics);
11481

11582
private:
11683
static const int WALK_ACCELERATION = 1;
@@ -125,6 +92,43 @@ namespace Monolithic
12592
Sprite spriteWalkRight_;
12693
};
12794
//^1
95+
96+
//^monolithic-update
97+
void Bjorn::update(World& world, Graphics& graphics)
98+
{
99+
// Apply user input to hero's velocity.
100+
switch (Controller::getJoystickDirection())
101+
{
102+
case DIR_LEFT:
103+
velocity_ -= WALK_ACCELERATION;
104+
break;
105+
106+
case DIR_RIGHT:
107+
velocity_ += WALK_ACCELERATION;
108+
break;
109+
//^omit
110+
case DIR_NONE: break; // Do nothing.
111+
//^omit
112+
}
113+
114+
// Modify position by velocity.
115+
x_ += velocity_;
116+
world.resolveCollision(volume_, x_, y_, velocity_);
117+
118+
// Draw the appropriate sprite.
119+
Sprite* sprite = &spriteStand_;
120+
if (velocity_ < 0)
121+
{
122+
sprite = &spriteWalkLeft_;
123+
}
124+
else if (velocity_ > 0)
125+
{
126+
sprite = &spriteWalkRight_;
127+
}
128+
129+
graphics.draw(*sprite, x_, y_);
130+
}
131+
//^monolithic-update
128132
}
129133

130134
namespace SplitInputComponent

html/component.html

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -272,37 +272,7 @@ <h3><a href="#a-monolithic-class" name="a-monolithic-class">A monolithic class</
272272
<span class="n">x_</span><span class="p">(</span><span class="mi">0</span><span class="p">),</span> <span class="n">y_</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
273273
<span class="p">{}</span>
274274

275-
<span class="kt">void</span> <span class="n">update</span><span class="p">(</span><span class="n">World</span><span class="o">&amp;</span> <span class="n">world</span><span class="p">,</span> <span class="n">Graphics</span><span class="o">&amp;</span> <span class="n">graphics</span><span class="p">)</span>
276-
<span class="p">{</span>
277-
<span class="c1">// Apply user input to hero&#39;s velocity.</span>
278-
<span class="k">switch</span> <span class="p">(</span><span class="n">Controller</span><span class="o">::</span><span class="n">getJoystickDirection</span><span class="p">())</span>
279-
<span class="p">{</span>
280-
<span class="k">case</span> <span class="n">DIR_LEFT</span>:
281-
<span class="n">velocity_</span> <span class="o">-=</span> <span class="n">WALK_ACCELERATION</span><span class="p">;</span>
282-
<span class="k">break</span><span class="p">;</span>
283-
284-
<span class="k">case</span> <span class="n">DIR_RIGHT</span>:
285-
<span class="n">velocity_</span> <span class="o">+=</span> <span class="n">WALK_ACCELERATION</span><span class="p">;</span>
286-
<span class="k">break</span><span class="p">;</span>
287-
<span class="p">}</span>
288-
289-
<span class="c1">// Modify position by velocity.</span>
290-
<span class="n">x_</span> <span class="o">+=</span> <span class="n">velocity_</span><span class="p">;</span>
291-
<span class="n">world</span><span class="p">.</span><span class="n">resolveCollision</span><span class="p">(</span><span class="n">volume_</span><span class="p">,</span> <span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">,</span> <span class="n">velocity_</span><span class="p">);</span>
292-
293-
<span class="c1">// Draw the appropriate sprite.</span>
294-
<span class="n">Sprite</span><span class="o">*</span> <span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteStand_</span><span class="p">;</span>
295-
<span class="k">if</span> <span class="p">(</span><span class="n">velocity_</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
296-
<span class="p">{</span>
297-
<span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteWalkLeft_</span><span class="p">;</span>
298-
<span class="p">}</span>
299-
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">velocity_</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
300-
<span class="p">{</span>
301-
<span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteWalkRight_</span><span class="p">;</span>
302-
<span class="p">}</span>
303-
304-
<span class="n">graphics</span><span class="p">.</span><span class="n">draw</span><span class="p">(</span><span class="o">*</span><span class="n">sprite</span><span class="p">,</span> <span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">);</span>
305-
<span class="p">}</span>
275+
<span class="kt">void</span> <span class="n">update</span><span class="p">(</span><span class="n">World</span><span class="o">&amp;</span> <span class="n">world</span><span class="p">,</span> <span class="n">Graphics</span><span class="o">&amp;</span> <span class="n">graphics</span><span class="p">);</span>
306276

307277
<span class="nl">private:</span>
308278
<span class="k">static</span> <span class="k">const</span> <span class="kt">int</span> <span class="n">WALK_ACCELERATION</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
@@ -319,8 +289,42 @@ <h3><a href="#a-monolithic-class" name="a-monolithic-class">A monolithic class</
319289
</pre></div>
320290

321291

322-
<p><code>Bjorn</code> has an <code>update()</code> method that gets called once per frame by the game. It
323-
reads the joystick to determine how to accelerate the baker. Then it resolves
292+
<p><code>Bjorn</code> has an <code>update()</code> method that gets called once per frame by the game:</p>
293+
<div class="codehilite"><pre><span class="kt">void</span> <span class="n">Bjorn</span><span class="o">::</span><span class="n">update</span><span class="p">(</span><span class="n">World</span><span class="o">&amp;</span> <span class="n">world</span><span class="p">,</span> <span class="n">Graphics</span><span class="o">&amp;</span> <span class="n">graphics</span><span class="p">)</span>
294+
<span class="p">{</span>
295+
<span class="c1">// Apply user input to hero&#39;s velocity.</span>
296+
<span class="k">switch</span> <span class="p">(</span><span class="n">Controller</span><span class="o">::</span><span class="n">getJoystickDirection</span><span class="p">())</span>
297+
<span class="p">{</span>
298+
<span class="k">case</span> <span class="n">DIR_LEFT</span>:
299+
<span class="n">velocity_</span> <span class="o">-=</span> <span class="n">WALK_ACCELERATION</span><span class="p">;</span>
300+
<span class="k">break</span><span class="p">;</span>
301+
302+
<span class="k">case</span> <span class="n">DIR_RIGHT</span>:
303+
<span class="n">velocity_</span> <span class="o">+=</span> <span class="n">WALK_ACCELERATION</span><span class="p">;</span>
304+
<span class="k">break</span><span class="p">;</span>
305+
<span class="p">}</span>
306+
307+
<span class="c1">// Modify position by velocity.</span>
308+
<span class="n">x_</span> <span class="o">+=</span> <span class="n">velocity_</span><span class="p">;</span>
309+
<span class="n">world</span><span class="p">.</span><span class="n">resolveCollision</span><span class="p">(</span><span class="n">volume_</span><span class="p">,</span> <span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">,</span> <span class="n">velocity_</span><span class="p">);</span>
310+
311+
<span class="c1">// Draw the appropriate sprite.</span>
312+
<span class="n">Sprite</span><span class="o">*</span> <span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteStand_</span><span class="p">;</span>
313+
<span class="k">if</span> <span class="p">(</span><span class="n">velocity_</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span>
314+
<span class="p">{</span>
315+
<span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteWalkLeft_</span><span class="p">;</span>
316+
<span class="p">}</span>
317+
<span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">velocity_</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">)</span>
318+
<span class="p">{</span>
319+
<span class="n">sprite</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">spriteWalkRight_</span><span class="p">;</span>
320+
<span class="p">}</span>
321+
322+
<span class="n">graphics</span><span class="p">.</span><span class="n">draw</span><span class="p">(</span><span class="o">*</span><span class="n">sprite</span><span class="p">,</span> <span class="n">x_</span><span class="p">,</span> <span class="n">y_</span><span class="p">);</span>
323+
<span class="p">}</span>
324+
</pre></div>
325+
326+
327+
<p>It reads the joystick to determine how to accelerate the baker. Then it resolves
324328
its new position with the physics engine. Finally, it draws Bj&oslash;rn onto the
325329
screen.</p>
326330
<p>The sample implementation here is trivially simple. There&#8217;s no gravity,
@@ -817,11 +821,9 @@ <h3><a href="#how-do-components-communicate-with-each-other" name="how-do-compon
817821
</li>
818822
<li>
819823
<p><strong>By referring directly to each other:</strong></p>
820-
<ul>
821-
<li>
822824
<p>The idea here is that components that need to talk will have direct
823-
references to each other without having to go through the container
824-
object at all.</p>
825+
references to each other without having to go through the container
826+
object at all.</p>
825827
<p>Let&#8217;s say we want to let Bj&oslash;rn jump. The graphics code needs to know if
826828
he should be drawn using a jump sprite or not. It can determine this by
827829
asking the physics engine if he&#8217;s currently on the ground. An easy way
@@ -862,7 +864,7 @@ <h3><a href="#how-do-components-communicate-with-each-other" name="how-do-compon
862864

863865
<p>When we construct Bj&oslash;rn&#8217;s <code>GraphicsComponent</code>, we&#8217;ll give it a reference
864866
to his corresponding <code>PhysicsComponent</code>.</p>
865-
</li>
867+
<ul>
866868
<li>
867869
<p><em>It&#8217;s simple and fast.</em> Communication is a direct method call from one
868870
object to another. The component can call any method that is supported

note/log.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2014-08-02 - layout component
2+
2014-08-01 - merge component changes
13
2014-07-28 - email
24
2014-07-27 - layout type object
35
2014-07-26 - merge type object changes

0 commit comments

Comments
 (0)