removed positionmix entirely (now also in collection)

This commit is contained in:
Chanumask
2023-10-24 13:33:35 +02:00
parent c4ffdb4e44
commit 0c1d0f510b
13 changed files with 186 additions and 107 deletions

View File

@ -1,55 +1,4 @@
from typing import List, Tuple
from marl_factory_grid.environment import constants as c
from marl_factory_grid.environment.entity.entity import Entity
class PositionMixin:
_entity = Entity
var_is_blocking_light: bool = True
var_can_collide: bool = True
var_has_position: bool = True
def spawn(self, coords: List[Tuple[(int, int)]]):
self.add_items([self._entity(pos) for pos in coords])
def render(self):
return [y for y in [x.render() for x in self] if y is not None]
@classmethod
def from_coordinates(cls, positions: [(int, int)], *args, entity_kwargs=None, **kwargs, ):
collection = cls(*args, **kwargs)
collection.add_items(
[cls._entity(tuple(pos), **entity_kwargs if entity_kwargs is not None else {}) for pos in positions])
return collection
def __delitem__(self, name):
idx, obj = next((i, obj) for i, obj in enumerate(self) if obj.name == name)
try:
for observer in obj.observers:
observer.notify_del_entity(obj)
except AttributeError:
pass
super().__delitem__(name)
def by_pos(self, pos: (int, int)):
pos = tuple(pos)
try:
return self.pos_dict[pos]
except StopIteration:
pass
except ValueError:
print()
@property
def positions(self):
return [e.pos for e in self]
def notify_del_entity(self, entity: Entity):
try:
self.pos_dict[entity.pos].remove(entity)
except (ValueError, AttributeError):
pass
# noinspection PyUnresolvedReferences,PyTypeChecker