Game Development Reference
In-Depth Information
Spawning Powerups On Map
Powerups are implemented, but not yet spawned. We will spawn 20 - 30 random powerups
when generating the map:
class
Map
# ...
def
initialize
(object_pool)
# ...
generate_powerups
end
# ...
def
generate_powerups
pups
= 0
target_pups
=
rand
(
20.
.
30
)
while
pups
<
target_pups
do
x
=
rand
(
0.
.
MAP_WIDTH
*
TILE_SIZE
)
y
=
rand
(
0.
.
MAP_HEIGHT
*
TILE_SIZE
)
if
tile_at(x, y)
!=
@water
random_powerup
.
new(
@object_pool
, x, y)
pups
+= 1
end
end
end
def
random_powerup
[
HealthPowerup
,
RepairPowerup
,
FireRatePowerup
,
TankSpeedPowerup
].
sample
end
# ...
end
The code is very similar to generating boxes. It's probably not the best way to distribute
powerups on map, but it will have to do for now.
Respawning Powerups After Pickup
When we pick up a powerup, we want it to reappear in same spot 30 seconds later. A
thought “we can start a new
Thread
with
sleep
and initialize the same powerup there”
sounds very bad, but I had it for a few seconds. Then
PowerupRespawnQueue
was
born.