Male_zoo_Projekt_SI/spawner.py

55 lines
2.0 KiB
Python
Raw Normal View History

import random
class Spawner:
2024-04-26 23:45:06 +02:00
def __init__(self, entity):
self.entity = entity
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
enclosure = random.choice(self.enclosures)
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)
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-03-24 17:46:13 +01:00
if self.check(blocked, taken):
break
2024-04-26 23:45:06 +02:00
def spawn_terrain_obstacles(self, blocked1, blocked2, taken, grid_width, grid_height):
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: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-03-24 17:46:13 +01:00
if (x,y) in blocked or (x,y) in taken:
return False
2024-04-26 23:45:06 +02:00
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))
2024-04-26 23:45:06 +02:00
return True