Male_zoo_Projekt_SI/spawner.py

55 lines
2.3 KiB
Python

import random
class Spawner:
def __init__(self, entity):
self.entity = entity
def spawn_animal(self, blocked, taken, enclosures):
self.enclosures = [enclosure for enclosure in enclosures if enclosure.type == self.entity.environment]
# Wyrażenie listowe filtrujące tylko te wybiegi, które pasują do środowiska zwierzęcia
enclosure = random.choice(self.enclosures)
enclosure.animals.add(self.entity) # Przydzielenie zwierzęcia do wybiegu
while True:
if self.entity.adult:
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2-2)
self.entity.y = random.randint(enclosure.y1+1, enclosure.y2-2)
else:
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2)
self.entity.y = random.randint(enclosure.y1+1, enclosure.y2)
if self.check(blocked | {(8,5),(3,10),(15,2),(26,2),(11,4),(15,7),(22,4),(24,10),(11,12),(19,12)}, taken):
break
def spawn_terrain_obstacles(self, blocked1, blocked2, taken, grid_width, grid_height):
blocked1 = blocked1 | {(2,0),(3,0),(2,1),(3,1),(8,5),(3,10),(15,2),(26,2),(11,4),(15,7),(22,4),(24,10),(11,12),(19,12)}
while True:
self.entity.x = random.randint(0, grid_width - 1)
self.entity.y = random.randint(0, grid_height - 1)
y = self.entity.y
x = self.entity.x
if (x, y) not in blocked1 and (x, y) not in blocked2 and (x, y) not in taken:
taken.add((self.entity.x, self.entity.y))
break
def check(self, blocked, taken):
x = self.entity.x
y = self.entity.y
if (x,y) in blocked or (x,y) in taken:
return False
if self.entity.adult:
adult_fields = [(x, y), (x+1,y), (x,y+1), (x+1,y+1)] # Duże zwierze zajmuje 4 pola
if any(field in taken for field in adult_fields): # Jeśli stawiane zwierze jest dorosłe i jakiekolwiek pole jest zajęte, to nie można postawić zwierzęcia
return False
for field in adult_fields: # Dodaj wszystkie pola zajęte przez duże zwierzę
taken.add(field)
else:
taken.add((x,y))
return True