2022-03-10 22:13:25 +01:00
|
|
|
import random
|
|
|
|
|
2022-04-10 20:28:50 +02:00
|
|
|
from common.constants import COLUMNS, ROWS
|
|
|
|
|
2022-03-11 19:42:17 +01:00
|
|
|
|
2022-03-10 22:13:25 +01:00
|
|
|
class Spawner:
|
2022-04-10 20:28:50 +02:00
|
|
|
def __init__(self, map):
|
|
|
|
self.map = map
|
|
|
|
|
|
|
|
def __is_free_field(self, field):
|
2022-04-11 19:18:03 +02:00
|
|
|
return field in ['g', 's', ' ']
|
2022-04-10 20:28:50 +02:00
|
|
|
|
|
|
|
def spawn_in_area(self, objects: list, spawn_area_pos_row=0, spawn_area_pos_column=0, spawn_area_width=0,
|
|
|
|
spawn_area_height=0, size=1):
|
|
|
|
spawned_objects_count = 0
|
2022-03-10 22:13:25 +01:00
|
|
|
|
2022-04-10 20:28:50 +02:00
|
|
|
while spawned_objects_count != len(objects):
|
|
|
|
x = random.randint(0, spawn_area_height) + spawn_area_pos_row
|
|
|
|
y = random.randint(0, spawn_area_width) + spawn_area_pos_column
|
2022-04-11 19:18:03 +02:00
|
|
|
if x < ROWS - 1 and y < COLUMNS - 1 and self.__is_free_field(self.map[x][y]):
|
2022-04-10 20:28:50 +02:00
|
|
|
for i in range(size):
|
|
|
|
for j in range(size):
|
2022-04-11 19:18:03 +02:00
|
|
|
self.map[x - i][y - j] = objects[spawned_objects_count]
|
2022-04-10 20:28:50 +02:00
|
|
|
spawned_objects_count += 1
|
2022-03-24 12:54:22 +01:00
|
|
|
|
2022-04-10 20:28:50 +02:00
|
|
|
def spawn_where_possible(self, objects: list):
|
|
|
|
spawned_objects_count = 0
|
|
|
|
while spawned_objects_count != len(objects):
|
2022-04-11 19:18:03 +02:00
|
|
|
x = random.randint(0, ROWS - 1)
|
|
|
|
y = random.randint(0, COLUMNS - 1)
|
2022-04-10 20:28:50 +02:00
|
|
|
if self.__is_free_field(self.map[x][y]):
|
|
|
|
self.map[x][y] = objects[spawned_objects_count]
|
|
|
|
spawned_objects_count += 1
|