Game Development Reference
In-Depth Information
When the specified delay time for calling
DecreaseSize()
is over, Unity calls the function.
The job of this function is to reset all variables to their default values; so the scale is set
back to
Vector3.one
, the radius of the collector is divided by
currentSize
, and finally
cur-
rentSize
is set back to zero. It is important to notice that as long as
cuurentSize
is not zero,
this means that the effect of an already taken food is still active, and this case no additional
food collectable can be taken. This rule is enforced by
Food
script.
Food
script in Listing 32 handles
Collected
message sent by
Collectable
like what we have
seen in
Coin
script (Listing 30 in page 80). As we have already seen,
Coin
script depends
on
InventoryBox
, so if the latter is missing, hitting a coin does not have any effect. Similarly,
Food
script depends on
SizeChanger
, since its job is to increase the size of the collector.
Therefore, the first step in
Collect()
function is to make sure that the collector trying to
collect this food has a
SizeChanger
, otherwise nothing happens. If
SizeChanger
exists, we
declare the variable
canTake
, to test whether
SizeChanger
is in a state that allows it to take the
food. Recalling
IncreaseSize()
function in
SizeChanger
, it returns
false
if the size is already
increased. This returned value is stored in
canTake
to be checked in the next step (line 31).
A
false
value of
canTake
means that this food has not been taken by
SizeChanger
, so we
just ignore the hit and the food game object is not destroyed. However, if
canTake
is
true
,
this means that the food has been taken by
SizeChanegr
and the size of the collector has been
incareased. In this case, the food object is destroyed.