You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: book/state.markdown
+17-18Lines changed: 17 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -359,36 +359,35 @@ The <a href="object-pool.html" class="pattern">Object Pool</a> pattern can help.
359
359
## Enter and Exit Actions
360
360
361
361
The goal of the State pattern is to encapsulate all of the behavior and data for
362
-
one state in a single class. We've done that pretty well, but there's still a
363
-
loose end. We moved handling user input and updating the charge time into
364
-
`DuckingState`, but there's a bit of ducking-specific code left outside. Over in
365
-
the standing state, when we *start* ducking, it does some initialization:
362
+
one state in a single class. We're partway there, but we still have
363
+
some loose ends.
366
364
367
-
^code start-ducking
365
+
When the heroine changes state, we also switch her sprite. Right now, that code is owned by the state she's switching *from*. When she goes from ducking to standing, the ducking state sets her image:
368
366
369
-
The ducking state should take care of resetting the charge time (after
370
-
all, it's the object that has that field) and playing the animation. We can
367
+
^code enter-standing-before
368
+
369
+
What we really want is each state to control its own graphics. We can
371
370
handle that by giving the state an *entry action*:
372
371
373
-
^code ducking-with-enter
372
+
^code standing-with-enter
374
373
375
-
Back in `Heroine`, we wrap changing the state in a little convenience method
376
-
that calls that on the new state:
374
+
Back in `Heroine`, we modify the code for handling state changes to call that
375
+
on the new state:
377
376
378
377
^code change-state
379
378
380
-
In the standing code we use it like:
379
+
This lets us simplify the ducking code to:
381
380
382
-
^code enter-ducking
381
+
^code enter-standing
383
382
384
-
Now ducking really is encapsulated. One particularly nice thing about entry
385
-
actions is that they run when you enter the state regardless of which state
386
-
you're coming *from*.
383
+
All it does is switch to standing and the standing state takes care of
384
+
the graphics. Now our states really are encapsulated. One particularly nice thing
385
+
about entry actions is that they run when you enter the state regardless of
386
+
which state you're coming *from*.
387
387
388
388
Most real-world state graphs have multiple transitions into the same state. For
389
-
example, maybe our heroine can fire her weapon while standing, ducking, and
390
-
jumping. That means you end up duplicating some code everywhere that transition
391
-
occurs. Entry actions give you a place to consolidate that.
389
+
example, our heroine will also end up standing after she lands a jump or dive. That means we would end up duplicating some code everywhere that transition
390
+
occurs. Entry actions give us a place to consolidate that.
392
391
393
392
We can, of course, also extend this to support an *exit action*. This is just a
394
393
method we call on the state we're *leaving* right before we switch to the new
one state in a single class. We’re partway there, but we still have
596
+
some loose ends.</p>
597
+
<p>When the heroine changes state, we also switch her sprite. Right now, that code is owned by the state she’s switching <em>from</em>. When she goes from ducking to standing, the ducking state sets her image:</p>
<p>Now ducking really is encapsulated. One particularly nice thing about entry
646
-
actions is that they run when you enter the state regardless of which state
647
-
you’re coming <em>from</em>.</p>
658
+
<p>All it does is switch to standing and the standing state takes care of
659
+
the graphics. Now our states really are encapsulated. One particularly nice thing
660
+
about entry actions is that they run when you enter the state regardless of
661
+
which state you’re coming <em>from</em>.</p>
648
662
<p>Most real-world state graphs have multiple transitions into the same state. For
649
-
example, maybe our heroine can fire her weapon while standing, ducking, and
650
-
jumping. That means you end up duplicating some code everywhere that transition
651
-
occurs. Entry actions give you a place to consolidate that.</p>
663
+
example, our heroine will also end up standing after she lands a jump or dive. That means we would end up duplicating some code everywhere that transition
664
+
occurs. Entry actions give us a place to consolidate that.</p>
652
665
<p>We can, of course, also extend this to support an <em>exit action</em>. This is just a
653
666
method we call on the state we’re <em>leaving</em> right before we switch to the new
0 commit comments