2024-03-24 17:33:58 +01:00
|
|
|
import random
|
|
|
|
|
|
|
|
|
|
|
|
class Spawner:
|
|
|
|
def __init__(self, animal, enclosures):
|
|
|
|
self.animal = animal
|
|
|
|
self.enclosures = enclosures
|
|
|
|
|
2024-03-24 17:46:13 +01:00
|
|
|
def spawn_animal(self, blocked, taken):
|
2024-03-24 17:33:58 +01:00
|
|
|
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)
|
2024-03-24 17:46:13 +01:00
|
|
|
if self.check(blocked, taken):
|
2024-03-24 17:33:58 +01:00
|
|
|
break
|
|
|
|
|
2024-03-24 17:46:13 +01:00
|
|
|
def check(self, blocked, taken):
|
2024-03-24 17:33:58 +01:00
|
|
|
x = self.animal.x
|
|
|
|
y = self.animal.y
|
2024-03-24 17:46:13 +01:00
|
|
|
if (x,y) in blocked or (x,y) in taken:
|
2024-03-24 17:33:58 +01:00
|
|
|
return False
|
2024-03-24 17:46:13 +01:00
|
|
|
taken.add((x,y))
|
2024-03-24 17:33:58 +01:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|