Game Development Reference
In-Depth Information
Illustration 36:
Coin collection mechanism with the interactions among all involved scripts
Diagram in Illustration 36 summarizes the programmatic steps of coin collection. In step A the
collector calls
Collect()
function of
Collectable
script attached to the coin, giving itself as the
owner of what is to going to be collected. In step B, the collectable sends the message
Col-
lected
to all scripts attached to the coin. In step C,
Coin
script receives the message by ex-
ecuting its own function
Collected()
, which eventually performs steps D and E.
Coin
script
tries in step D to find
InventoryBox
script inside the collector. Recall that the variable
own-
er
inside
Collected()
function refers to the collector. Step E (money amount increment) is ex-
ecuted in case the inventory box is found, otherwise this step is not executed, and hence the
coin is neither collected nor destroyed.
A similar process takes place in case of food collection. However, collection logic as well
as the effect on the collector are different. We are going to have two types of food, and
both of them are going to have the same structure. Additionally, we are going to create a
separate prefab for each type of them. Taking food shall increase the size of the ball for a
limited time. The two types of food we are going to create vary in these two values. Listing
31 shows
SizeChanger
script, which handles food collectables. At the other end, Listing 32
shows
Food
script, which we are going to add to our food objects. Food collectables are also
going to have
Collectable
and
YRotator
scripts as well.