Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:04,950 --> 00:00:10,860
Now, suppose we wanted to take the complexity of our special abilities to the next level, and we want
2
00:00:10,860 --> 00:00:15,510
to have something like a power attack, which is a sort of sequence type.
3
00:00:15,870 --> 00:00:24,150
So this power tank would be composed of other things, such as a particle effect ability, such as a
4
00:00:24,150 --> 00:00:27,330
do damage over time, parallel ability.
5
00:00:27,630 --> 00:00:33,270
And that parallel ability itself would be composed of an instant damage, which might be of a damage
6
00:00:33,270 --> 00:00:38,160
ability type and a delayed damage, which is of the delay decorator type.
7
00:00:38,160 --> 00:00:42,990
And then that delay decorator itself references the instant damage object.
8
00:00:42,990 --> 00:00:50,220
So you end up with these complicated object hierarchies, which reuse a lot of code and allow you or
9
00:00:50,220 --> 00:00:56,460
your designers to easily put together a new special abilities out of component parts a little bit like
10
00:00:56,460 --> 00:00:57,210
Lego bricks.
11
00:00:57,570 --> 00:01:03,150
So if you wanted to be able to do something like this, then you would need the composite pattern,
12
00:01:03,360 --> 00:01:09,540
which looks very similar to the strategy pattern and decorator pattern we've already seen.
13
00:01:09,570 --> 00:01:16,440
So instead of having a decorator say if I want to have a sequence ability instead of having one thing
14
00:01:16,440 --> 00:01:21,870
that we wrap up in the sequence, which would be the decorator pattern, the composite pattern says
15
00:01:21,870 --> 00:01:24,840
instead of having one thing, let's have an array of things.
16
00:01:24,840 --> 00:01:31,470
So the sequence would be an array of children which are all members of the ability interface.
17
00:01:31,740 --> 00:01:35,580
Then my sequence would dictate how do those children get run?
18
00:01:35,580 --> 00:01:41,240
So it makes sense that in a sequence, they would get run one after another, the first one would complete
19
00:01:41,250 --> 00:01:47,160
and the second one, etc., etc. However, if you had a parallel ability for whatever reason and your
20
00:01:47,160 --> 00:01:53,160
use was more asynchronous, maybe it started a co routine, for example, then it would run all the
21
00:01:53,350 --> 00:01:57,300
routines all at once and said the sequence would probably wait for one car routine to finish before
22
00:01:57,300 --> 00:01:58,230
going on to the next.
23
00:01:58,560 --> 00:02:04,450
So you can see how you could come up with different ways of composing arrays of abilities.
24
00:02:04,650 --> 00:02:06,260
And that is the composite pattern.
25
00:02:06,270 --> 00:02:10,949
Obviously, the formulation is typically more generic than relating to abilities.
26
00:02:10,949 --> 00:02:15,440
You'd have these components again, very much the same as the decorator pattern.
27
00:02:15,450 --> 00:02:20,100
You would have a leaf, which is kind of referring to the fact that the thing we had before looked a
28
00:02:20,100 --> 00:02:21,900
little bit like a tree upside-down.
29
00:02:22,110 --> 00:02:25,230
So a leaf would be an operation that actually does something.
30
00:02:25,230 --> 00:02:30,210
The composite would be the branches of the tree because it's the branching out points.
31
00:02:30,210 --> 00:02:33,390
It so says, let's sequence these, let's do these in parallel, etc..
32
00:02:33,690 --> 00:02:38,490
By the way, if you've come across behavior trees, this is the way you would implement behavior trees,
33
00:02:38,700 --> 00:02:41,460
essentially by using the composite pattern.
34
00:02:42,120 --> 00:02:47,640
So how do we go ahead and implement the composite pattern in our ability code base?
35
00:02:47,970 --> 00:02:53,610
So let's jump over to it now and give ourselves a little bit more room because here I before and decorate,
36
00:02:53,610 --> 00:02:59,640
I want to create a new public class, which is going to be the sequence composite.
37
00:02:59,910 --> 00:03:06,320
And it's going to inherit from my ability, and it's going to implement the interface of my ability,
38
00:03:06,330 --> 00:03:11,670
basically the use and well, we're going to have to have some sort of children in here.
39
00:03:11,670 --> 00:03:13,590
But you've done this before with the decorator.
40
00:03:13,830 --> 00:03:15,270
So I'm not going to hold your hand here.
41
00:03:15,280 --> 00:03:21,390
I'm going to challenge you to implement the composite pattern in the same vein as the decorator remembering
42
00:03:21,390 --> 00:03:25,290
that instead of single liability, we might want to use an array or a list.
43
00:03:25,560 --> 00:03:27,540
So pause the video and have a go.
44
00:03:30,290 --> 00:03:30,620
OK.
45
00:03:30,650 --> 00:03:32,870
Welcome back, so I'm going to make it a private variable.
46
00:03:32,990 --> 00:03:36,380
I'm going to make it for simplicity's sake.
47
00:03:36,380 --> 00:03:39,980
I'm going to use an array that you'd probably want to use a list or something.
48
00:03:40,250 --> 00:03:44,150
So it's an array of liability and we're going to call this the children.
49
00:03:44,510 --> 00:03:49,020
And we're going to create a constructor which takes in that children array.
50
00:03:49,040 --> 00:03:55,970
So when you create a new sequence, you're going to pass in an array of children and that is going to
51
00:03:55,970 --> 00:03:56,690
be set here.
52
00:03:56,720 --> 00:03:59,600
And the use method would do something very, very simple.
53
00:03:59,990 --> 00:04:08,720
For example, it would do a for each loop over the child in children, and it would call the child's
54
00:04:08,720 --> 00:04:14,510
child dot use method passing in the current game object like so.
55
00:04:15,000 --> 00:04:18,230
So that's a very simple way of creating a sequence composite.
56
00:04:18,230 --> 00:04:21,740
Obviously, you can have all sorts of composites, as I've just explained ones.
57
00:04:21,980 --> 00:04:27,380
You could have a random composite one that basically selects a random option from this list instead
58
00:04:27,380 --> 00:04:29,180
of create calling them all in sequence.
59
00:04:29,390 --> 00:04:31,850
You can have one that calls them in reverse sequence.
60
00:04:32,240 --> 00:04:39,380
The abilities are endless, and you can really split these down into tiny, little reusable chunks so
61
00:04:39,380 --> 00:04:44,180
that you can easily program all sorts of abilities into the system.
62
00:04:44,660 --> 00:04:47,490
So how do we go ahead and construct it again?
63
00:04:47,510 --> 00:04:49,190
That could be a challenge for you.
64
00:04:49,400 --> 00:04:51,020
I'm going to go ahead and do it.
65
00:04:51,080 --> 00:04:53,990
So new sequence composite like.
66
00:04:53,990 --> 00:05:01,220
So now in here, we need to pass in a new eye ability array, which we're going to construct within
67
00:05:01,220 --> 00:05:02,390
here and in here.
68
00:05:02,390 --> 00:05:04,490
You could put something like a first.
69
00:05:04,490 --> 00:05:12,050
We'll start off with a new heel ability as the first child and then after that we might run the new
70
00:05:12,320 --> 00:05:21,080
rage ability and then in there just to show how flexible the system is, we could run a delayed decorator,
71
00:05:21,080 --> 00:05:27,650
which itself couldn't contains another range ability, something along these sorts of lines.
72
00:05:27,890 --> 00:05:30,620
It's entirely possible using our composite pattern.
73
00:05:30,620 --> 00:05:36,560
Obviously, you can have composites of composites and the trees go on endlessly because of this self
74
00:05:36,560 --> 00:05:42,650
referring pattern, because of the fact that the sequence composite is an eye ability and has liabilities
75
00:05:42,650 --> 00:05:46,640
within it because the delay decorator is a liability and has liability within it.
76
00:05:46,820 --> 00:05:52,190
That's really the genius of these decorator and composite patterns is that it allows you to wrap them
77
00:05:52,190 --> 00:05:56,480
indefinitely into these tree structures that are very, very powerful.
78
00:05:56,600 --> 00:06:02,570
So that's it for this little trio of patterns, the strategy, the decorator and composite the next
79
00:06:02,570 --> 00:06:02,810
video.
80
00:06:02,810 --> 00:06:08,030
We're going to be moving on to a completely new paradigm of pattern, the model view controller.
81
00:06:08,060 --> 00:06:08,750
I'll see you there.
8927
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.