Add unit tests for UnitFactory functionality and initialize units package
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import random
|
||||
import uuid
|
||||
from units import rat, bomb
|
||||
|
||||
class UnitManager:
|
||||
def count_rats(self):
|
||||
count = 0
|
||||
for unit in self.units.values():
|
||||
if isinstance(unit, rat.Rat):
|
||||
count += 1
|
||||
return count
|
||||
|
||||
def spawn_rat(self, position=None):
|
||||
if position is None:
|
||||
position = self.choose_start()
|
||||
rat_class = rat.Male if random.random() < 0.5 else rat.Female
|
||||
self.spawn_unit(rat_class, position)
|
||||
|
||||
def spawn_bomb(self, position):
|
||||
self.spawn_unit(bomb.Timer, position)
|
||||
|
||||
def spawn_unit(self, unit, position, **kwargs):
|
||||
id = uuid.uuid4()
|
||||
self.units[id] = unit(self, position, id, **kwargs)
|
||||
|
||||
def choose_start(self):
|
||||
if not hasattr(self, '_valid_positions'):
|
||||
self._valid_positions = [
|
||||
(x, y) for y in range(1, self.map.height-1)
|
||||
for x in range(1, self.map.width-1)
|
||||
if self.map.matrix[y][x]
|
||||
]
|
||||
return random.choice(self._valid_positions)
|
||||
Reference in New Issue
Block a user