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
There are some readers whose skin is crawling right now because I'm using
352
-
inheritance on the main entity class to define different behaviors. If you don't
352
+
inheritance on the main `Entity` class to define different behaviors. If you don't
353
353
happen to see the problem, I'll provide some context.
354
354
355
355
When the game industry emerged from the primordial seas of 6502 assembly code
@@ -366,7 +366,7 @@ the Gang of Four knew this in 1994 when they wrote:
366
366
367
367
<asidename="subclass">
368
368
369
-
Just between you and me, I think the pendulum has swung a bit too far *away*
369
+
Between you and me, I think the pendulum has swung a bit too far *away*
370
370
from subclassing. I generally avoid it, but being dogmatic about *not* using
371
371
inheritance is as bad as being dogmatic about using it. You can use it in
372
372
moderation without having to be a teetotaler.
@@ -403,21 +403,21 @@ new entity that implements `update()` appropriately:
403
403
As you can see, we pretty much just cut that chunk of code from the game loop
404
404
earlier in the chapter and pasted it into `Skeleton`’s `update()` method.
405
405
The one minor difference is that `patrollingLeft_` has been made into a field
406
-
instead of a local variable. That way its value sticks around between calls to
406
+
instead of a local variable. That way, its value sticks around between calls to
407
407
`update()`.
408
408
409
409
Let's do this again with the statue:
410
410
411
411
^code statue
412
412
413
-
Again, most of the change is just moving code from the game loop into the class
413
+
Again, most of the change is moving code from the game loop into the class
414
414
and renaming some stuff. In this case, though, we've actually made the codebase
415
415
simpler. In the original nasty imperative code, there were separate local
416
416
variables for each statue's frame counter and rate of fire.
417
417
418
418
Now that those have been moved into the `Statue` class itself, you can create as
419
419
many as you want and each instance will have its own little timer. That's really
420
-
the motivation behind this pattern: it's now much easier to add new entities to
420
+
the motivation behind this pattern -- it's now much easier to add new entities to
421
421
the game world because each one brings along everything it needs to take care of
422
422
itself.
423
423
@@ -427,7 +427,7 @@ like a separate data file or level editor.
427
427
428
428
<spanname="uml"></span>
429
429
430
-
<imgsrc="images/update-method-uml.png"alt="A UML diagram. World has a collection of Entities, each of which has an update() method. Skeleton and Statue both inherit from Entity." />
430
+
<imgsrc="images/update-method-uml.png"alt="A UML diagram. World has a collection of Entities, each of which has an update() method. Skeleton and Statue both inherit from `Entity`." />
431
431
432
432
<asidename="uml">
433
433
@@ -461,13 +461,13 @@ so:
461
461
^code variable
462
462
463
463
Now, the distance the skeleton moves increases as the elapsed time grows. You
464
-
can also see the additional complexity of dealing with a variable time step. The
465
-
skeleton may overshoot the bounds of its patrol with a large time slice and we
466
-
have to carefully handle that.
464
+
can see the additional complexity of dealing with a variable time step. The
465
+
skeleton may overshoot the bounds of its patrol with a large time slice, and we
466
+
have to handle that carefully.
467
467
468
468
## Design Decisions
469
469
470
-
With a simple pattern like this, there isn't too much variation, but there's
470
+
With a simple pattern like this, there isn't too much variation, but there are
471
471
still a couple of knobs you can turn.
472
472
473
473
### What class does the update method live on?
@@ -477,22 +477,22 @@ The most obvious and most important decision you'll make is what class to put
477
477
478
478
***The entity class:**
479
479
480
-
This is the simplest option if you already have an entity class since it
480
+
This is the simplest option if you already have an `Entity` class since it
481
481
doesn't bring any additional classes into play. This may work if you don't
482
482
have too many kinds of entities, but the industry is generally moving away
483
483
from this.
484
484
485
485
Having to subclass `Entity` every time you want a new behavior is brittle
486
486
and painful when you have a large number of different kinds. You'll
487
487
eventually find yourself wanting to reuse pieces of code in a way that
488
-
doesn't gracefully map to a single inheritance hierarchy and then you're
488
+
doesn't gracefully map to a single inheritance hierarchy, and then you're
489
489
stuck.
490
490
491
491
***The component class:**
492
492
493
493
If you're already using the <a href="component.html"
494
494
class="pattern">Component</a> pattern, this is a no-brainer. It lets each
495
-
component update itself independently. In the same way that the update
495
+
component update itself independently. In the same way that the Update Method
496
496
pattern in general lets you decouple game entities from each other in the
497
497
game world, this lets you decouple *parts of a single entity* from each
498
498
other. Rendering, physics, and AI can all take care of themselves.
@@ -508,8 +508,8 @@ The most obvious and most important decision you'll make is what class to put
508
508
509
509
If you're using one of those patterns, it's natural to put `update()` on
510
510
that delegated class. In that case, you may still have the `update()` method
511
-
on the main class, but it will be non-virtual and just forward to the
512
-
delegate object. Something like:
511
+
on the main class, but it will be non-virtual and will forward to the
512
+
delegated object. Something like:
513
513
514
514
^code forward
515
515
@@ -519,7 +519,7 @@ The most obvious and most important decision you'll make is what class to put
519
519
520
520
### How are dormant objects handled?
521
521
522
-
You often have a number of objects in the world that for whatever reason
522
+
You often have a number of objects in the world that, for whatever reason,
523
523
temporarily don't need to be updated. They could be disabled, or off-screen, or
524
524
not unlocked yet. If a large number of objects are in this state, it can be a
525
525
waste of CPU cycles to walk over them each frame only to do nothing.
@@ -570,9 +570,9 @@ collection that avoids them during your core game loop.
570
570
571
571
## See Also
572
572
573
-
* This pattern is part of a trinity with <a href="game-loop.html"
573
+
* This pattern, along with <a href="game-loop.html"
574
574
class="pattern">Game Loop</a> and <a href="component.html"
575
-
class="pattern">Component</a>that often form the nucleus of a game engine.
575
+
class="pattern">Component</a>, is part of a triinity that often forms the nucleus of a game engine.
576
576
577
577
* When you start caring about the cache performance of updating a bunch of
578
578
entities or components in a loop each frame, the <a
0 commit comments