55 lines
2.0 KiB
Python
55 lines
2.0 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)
|
|
|
|
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, taken):
|
|
break
|
|
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
|
|
|
|
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
|
|
|