45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
import random
|
|
|
|
|
|
class Spawner:
|
|
def __init__(self, animal, enclosures):
|
|
self.animal = animal
|
|
self.enclosures = enclosures
|
|
|
|
def spawn_animal(self, blocked, taken):
|
|
possibilities = self.enclosures
|
|
fitting = []
|
|
for option in possibilities:
|
|
if option.type == self.animal.environment:
|
|
fitting.append(option)
|
|
enclosure = random.choice(fitting)
|
|
while True:
|
|
if enclosure.x1 < enclosure.x2:
|
|
self.animal.x = random.randint(enclosure.x1, enclosure.x2)
|
|
if enclosure.y1 < enclosure.y2:
|
|
self.animal.y = random.randint(enclosure.y1, enclosure.y2)
|
|
if enclosure.y1 > enclosure.y2:
|
|
self.animal.y = random.randint(enclosure.y2, enclosure.y1)
|
|
if enclosure.x1 > enclosure.x2:
|
|
self.animal.x = random.randint(enclosure.x2, enclosure.x1)
|
|
if enclosure.y1 < enclosure.y2:
|
|
self.animal.y = random.randint(enclosure.y1, enclosure.y2)
|
|
if enclosure.y1 > enclosure.y2:
|
|
self.animal.y = random.randint(enclosure.y2, enclosure.y1)
|
|
if self.check(blocked, taken):
|
|
break
|
|
|
|
def check(self, blocked, taken):
|
|
x = self.animal.x
|
|
y = self.animal.y
|
|
if (x,y) in blocked or (x,y) in taken:
|
|
return False
|
|
taken.add((x,y))
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|