Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
In this course, we're going to model a simple tower defense game. We'll start by using the knowledge we already have - using primitive data types, collections and tuples. This way has its limitations and we'll take a look at how structs make it much more elegant to implement custom data types.
-
0:00
[MUSIC]
-
0:04
Hi there.
-
0:05
My name is Passan and
-
0:06
welcome to the course on object oriented programming in swift.
-
0:10
This course requires a basic understanding of the swift programming language.
-
0:14
So before you begin,
-
0:15
make sure that you have completed all the prerequisites associated with this course.
-
0:20
So far, we've worked with many of swift's built in data types.
-
0:24
We've worked with strings, numbers in the form of ints, doubles, and
-
0:28
floats, collections such as arrays and dictionaries, and
-
0:32
logic in the form of boolean true and false.
-
0:35
These types have served us well and enabled us to write plenty of code.
-
0:39
But there comes a time when the built in types don't model data well enough for us.
-
0:45
For example, when I think of my fellow teacher Ben,
-
0:48
I don't think of Ben as a constant called name with a string value Ben,
-
0:52
an integer variable for his age and so on.
-
0:55
These individual types allows us to create data points to model
-
0:59
different aspects of Ben but individually they don't make much sense.
-
1:04
Ben's name and age are a part of who Ben is, not a random data point.
-
1:09
Luckily for us, swift allows us to create complex and custom data types.
-
1:15
Fair warning though,
-
1:16
we're about to make a cognitive leap here in programming concepts.
-
1:19
So it's important that you pay both close attention and practice a lot.
-
1:23
You may even have to watch these videos more than once.
-
1:27
Let's build an example from the ground up, so
-
1:29
we understand what a custom data type looks like.
-
1:33
Let's imagine we're building a game that's always interesting, right?
-
1:37
Imagine a simple 2D tower defense game.
-
1:40
If you haven't played one of those, think of it this way.
-
1:43
We have a map which for
-
1:45
our sake will be a simple 2D map that we view from above like a chess board.
-
1:50
The map is split up into a system of x and y coordinates.
-
1:55
On this map we can place towers in random positions.
-
1:58
These towers can fire on enemies if they get too close.
-
2:02
This is all really complex, so let's start super simple.
-
2:06
We're not going to build a game, mind you.
-
2:08
So sorry if I got you excited.
-
2:10
We're going to take a stab at how we can model data and create custom types.
-
2:15
Let's think about how this game works.
-
2:17
Towers are placed at certain points and can fire at a number of other points.
-
2:22
Enemies can also exist at certain points on the map.
-
2:26
The common denominator amongst all of this is the point.
-
2:30
We need to figure out a way to store the location or
-
2:34
coordinate information where either a tower or an enemy is.
-
2:38
Without this information, we can't really proceed and model anything else.
-
2:43
Since this is a simple 2D game, we can use X and
-
2:46
Y coordinates like a graph on a piece of paper to specify positions.
-
2:51
The bottom left of the map is the starting position and has X and Y values of 0.
-
2:57
Moving to the left horizontally, increases X values,
-
3:01
moving up vertically along the Y axis increases Y values.
-
3:04
So that this point two spaces up and to do the right has an X value of 2 and
-
3:09
a Y value of 2 or a coordinate 2,2.
-
3:14
Let's try and store this information using the knowledge we currently have.
-
3:17
Now I have a new playground here, so go ahead and create one and
-
3:21
call it Object Oriented Swift.
-
3:23
We could declare two constants x1 and y1 to store a coordinate.
-
3:28
So we'll say, let x1 = 0 and let y1 = 0.
-
3:33
These two values together represent a single coordinate point.
-
3:39
And this works, but we need these two data points to be related to one another
-
3:45
because by themselves, they only paint half the picture.
-
3:48
In the course on functions, we learned about a construct called tuples
-
3:53
that we could use to return multiple values from functions.
-
3:56
We can use a tuple here as well to combine these numbers
-
4:00
into one single compound value.
-
4:03
So I can say, let coordinate1 and
-
4:07
then I'll give you the type explicitly and this type is going to be a tuple and
-
4:13
we'll say, it's going to be an integer and an integer.
-
4:17
And remember we can also give tuples labels or say this is x and this is y.
-
4:23
And now to this, we can assign 0,0 as our coordinate.
-
4:29
Say here I'm specifying that type of the constant as a tuple with 2 Ints.
-
4:33
I can then assign names to these values, so
-
4:36
that we can refer to them easily like so.
-
4:39
So we can say coordinate1.x and we'll gain 0, all right?
-
4:43
This seems to work, but
-
4:44
we're going to be dealing with lots of different coordinates all over our map,
-
4:48
locations where the enemies are and where the towers are placed and so on.
-
4:52
We could store different values like this in an array but
-
4:56
that's also inconvenient because there is no meaningful way to understand what point
-
5:01
is associated with a particular enemy.
-
5:04
We could also do this with a dictionary where let's say, an enemy is the key and
-
5:08
then we have values for points and things on that but that still is cumbersome.
-
5:13
So, to do this,
-
5:14
we're going to create a custom type using a construct known as a structure.
-
5:20
A structure, or struct, is a flexible data type
-
5:24
that allows you to group together related values and model them as a unit.
-
5:30
We'll see what this means in just a second.
-
5:32
I should warn you, we're about to learn a large number of new terms here.
-
5:36
So either refer to the Swift book, which I've linked to in the teacher's notes,
-
5:41
to get more clarity on the meanings.
-
5:43
Or you should write down these definitions that come up so
-
5:46
that you can become more intimately familiar with them.
-
5:49
Let's dive right in.
-
5:50
We're going to create a struct to model a point on our map.
-
5:54
So as always, remember, new topics means new syntax, and
-
5:59
here we start with the struct keyword.
-
6:02
We then give our struct a name, and
-
6:04
this name is the name of our custom data type, so I'm going to call it Point.
-
6:10
Earlier, you learned that when naming functions or
-
6:13
variables, you need to use the camel case convention.
-
6:16
That's the first letter is lowercase and
-
6:19
the first letter of subsequent words are uppercase.
-
6:22
Now, this is more specifically called lower camel case because of that
-
6:26
first letter being lowercase.
-
6:29
When naming structs or any other custom data type for
-
6:32
that matter, we use upper camel case which means that, it's just like
-
6:36
lower camel case except that the first letter is also upper case and
-
6:40
then the first letter of subsequent words are uppercase as well.
-
6:44
For this reason the name Point starts with an upper case P.
-
6:48
We then start the body of the struct with a set of curly braces.
-
6:54
Inside the body, we're going to add two lines of code to begin with.
-
6:57
So say let x, and we'll give it a type Int.
-
7:01
Let y, and we'll also give it a type Int.
-
7:05
Now before I explain, what this is to you?
-
7:07
This looks like we're simply declaring two constants and
-
7:10
explicitly giving them a type and that's basically it.
-
7:15
A struct can define constants or variables which we call properties to store values.
-
7:21
So, remember these are called properties.
-
7:24
More specifically, they're called stored properties and what we've done here is,
-
7:28
create two stored properties.
-
7:31
Keep that term in mind.
-
7:33
So the first stored property here is a constant,
-
7:36
has the name x, and the type Int.
-
7:40
You'll notice that unlike anything we've done before,
-
7:42
we're not assigning any value to it and that's okay.
-
7:46
The second line creates a second stored property,
-
7:49
also a constant named y this time, and also of type Int.
-
7:53
Now outside of the struct below it, let's add another line of code.
-
7:58
So we'll say, let p1 = and then we type Point and then an open parentheses.
-
8:05
And you'll see that x code autocompletes to give us something like this.
-
8:10
So this looks like a function that takes an argument for x and y.
-
8:15
So let's give it one, we'll say 0 and 0.
-
8:18
Despite being seemingly simple, there's a lot going on on this one line of code.
-
8:24
But let's take a break here and
-
8:25
in the next video prepare to have your mind blown.
You need to sign up for Treehouse in order to download course files.
Sign up