Game Development Reference
In-Depth Information
Fire()
and
ReleaseTrigger()
functions are related. If the weapon is not
automatic
,
ReleaseTrigger()
must be called each time after
Fire()
.
Fire()
on the other hand must ensure
that
•
the weapon is currently in hand,
•
is not reloading,
•
either automatic or the trigger is currently released, and
•
there are enough bullets in the magazine.
If all of these conditions are met, it sends
OnWeaponFire
message, sets
triggerPulled
flag,
and reduces bullet count in the current magazine. If the number of bullets remaining in
the magazine is less than the number needed to fire,
Reload()
is automatically called.
Re-
load()
is responsible for
initiating
reload process rather than instantly reloading the weapon.
The variable
reloadProgress
represent the time passed since the last time
Reload()
has
been called. If
reloadProgress
is zero, this means the weapon is not currently reloading.
Therefore,
Reload()
must check that
reloadProgress
equals zero before initiating reload
process. It is also necessary to have at least one additional magazine in order for reloading
to take place. Therefore,
Reload()
checks the value of
magazineCount
in addition to
magazineCapacity
and
magazineSize
, to make sure that the magazine we are trying to
replace is not full. If all conditions are met, we set the value of
reloadProgress
to
Time.deltaTime
. As a result, the value of reload progress will accumulate through
Update()
calls by adding
Time.deltaTime
during each frame. When the value of
reloadPro-
gress
exceeds
reloadTime
, reloading is completed by decrementing the count of magazines
remaining and setting the size of the current magazine to magazine capacity.
The last function we are going to cover in this scriptis
GetReloadProgress()
. If the weapon
is currently reloading, it returns reloading progress as a float value between 0 and 1. If
this function returns zero, it means that the weapon is not currently reloading. The returned
value can be used to interpolate some animations or control progress bars etc. Illustration 89
shows the
GeneralWeapon
for the three weapons and how the values vary between them.