2024-03-24 17:33:58 +01:00
|
|
|
import random
|
|
|
|
|
|
|
|
class Spawner:
|
2024-04-26 23:45:06 +02:00
|
|
|
def __init__(self, entity):
|
|
|
|
self.entity = entity
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-04-26 23:45:06 +02:00
|
|
|
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
|
2024-04-16 14:50:06 +02:00
|
|
|
enclosure = random.choice(self.enclosures)
|
|
|
|
|
2024-03-24 17:33:58 +01:00
|
|
|
while True:
|
2024-04-26 23:45:06 +02:00
|
|
|
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)
|
2024-04-16 14:50:06 +02:00
|
|
|
else:
|
2024-04-26 23:45:06 +02:00
|
|
|
self.entity.x = random.randint(enclosure.x1+1, enclosure.x2)
|
|
|
|
self.entity.y = random.randint(enclosure.y1+1, enclosure.y2)
|
2024-04-16 14:50:06 +02:00
|
|
|
|
2024-05-08 11:54:49 +02:00
|
|
|
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):
|
2024-03-24 17:33:58 +01:00
|
|
|
break
|
2024-05-08 11:54:49 +02:00
|
|
|
|
2024-04-26 23:45:06 +02:00
|
|
|
def spawn_terrain_obstacles(self, blocked1, blocked2, taken, grid_width, grid_height):
|
2024-05-08 11:54:49 +02:00
|
|
|
blocked1 = blocked1 | {(8,5),(3,10),(15,2),(26,2),(11,4),(15,7),(22,4),(24,10),(11,12),(19,12)}
|
2024-04-26 23:45:06 +02:00
|
|
|
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
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-03-24 17:46:13 +01:00
|
|
|
def check(self, blocked, taken):
|
2024-04-26 23:45:06 +02:00
|
|
|
x = self.entity.x
|
|
|
|
y = self.entity.y
|
2024-04-16 14:50:06 +02:00
|
|
|
|
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-04-26 23:45:06 +02:00
|
|
|
if self.entity.adult:
|
2024-04-16 14:50:06 +02:00
|
|
|
|
|
|
|
adult_fields = [(x, y), (x+1,y), (x,y+1), (x+1,y+1)] # Duże zwierze zajmuje 4 pola
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-04-16 14:50:06 +02:00
|
|
|
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))
|
2024-03-24 17:33:58 +01:00
|
|
|
|
2024-05-08 11:54:49 +02:00
|
|
|
return True
|