Skip to content
Snippets Groups Projects
Commit ce12d020 authored by Tessaris Sergio's avatar Tessaris Sergio
Browse files

added access to location/objects indexes

parent 723125e1
No related branches found
No related tags found
No related merge requests found
......@@ -117,7 +117,7 @@ class GridWorld(object):
self._size = size
self._blocks = set(blocks)
self._objects: Dict[WorldObject, Coordinate] = {}
self._location: Dict[Coordinate, Iterable[Coordinate]] = {}
self._location: Dict[Coordinate, Iterable[WorldObject]] = {}
@classmethod
def from_string(cls, world_desc: str) -> 'GridWorld':
......@@ -159,6 +159,16 @@ class GridWorld(object):
"""Return the set of coordinates where blocks are placed."""
return self._blocks
@property
def object_locations(self) -> Dict[WorldObject, Coordinate]:
"""Return a dictionary associating objects to their coordinate."""
return self._objects
@property
def location_objects(self) -> Dict[Coordinate, Iterable[WorldObject]]:
"""Return a dictionary associating locations to the objects at the given coordinate."""
return self._location
def isBlock(self, pos: Coordinate) -> bool:
"""Return true if in the coordinate there's a block."""
return pos in self.blocks
......@@ -169,24 +179,24 @@ class GridWorld(object):
def objects_at(self, pos: Coordinate) -> Iterable[WorldObject]:
"""Return an iterable over the objects at the given coordinate."""
return self._location.get(pos, [])
return self.location_objects.get(pos, [])
def location_of(self, obj: WorldObject) -> Coordinate:
"""Return the coordinate of the object within the world, or none if it's not in it."""
return self._objects.get(obj, None)
return self.object_locations.get(obj, None)
def empty_cells(self, count_objects=False) -> Iterable[Coordinate]:
"""Return an iterable object over the cells without blocks. If count_objects is not False then also other objects are taken into account."""
all_cells = set([coord(x, y) for x in range(0, self.size.x) for y in range(0, self.size.y)])
all_cells.difference_update(self.blocks)
if count_objects:
all_cells.difference_update(self._location.keys())
all_cells.difference_update(self.location_objects.keys())
return all_cells
@property
def objects(self) -> Iterable[WorldObject]:
"""Return an iterable over the objects within the world."""
return self._objects.keys()
return self.object_locations.keys()
def removeBlock(self, pos: Coordinate):
self._blocks.discard(pos)
......@@ -199,17 +209,17 @@ class GridWorld(object):
raise OutOfBounds('Placing {} outside the world at {}'.format(obj, pos))
if self.isBlock(pos):
raise Collision('Placing {} inside a block {}'.format(obj, pos))
if pos in self._location:
self._location[pos].append(obj)
if pos in self.location_objects:
self.location_objects[pos].append(obj)
else:
self._location[pos] = [obj]
self._objects[obj] = pos
self.location_objects[pos] = [obj]
self.object_locations[obj] = pos
obj._setWorld(self)
def removeObject(self, obj: WorldObject):
try:
self._location[self.location_of(obj)].remove(obj)
del self._objects[obj]
self.location_objects[self.location_of(obj)].remove(obj)
del self.object_locations[obj]
obj._setWorld(None)
except KeyError:
pass
......@@ -221,14 +231,14 @@ class GridWorld(object):
raise OutOfBounds('Moving {} outside the world at {}'.format(obj, pos))
if self.isBlock(pos):
raise Collision('Moving {} inside a block {}'.format(obj, pos))
old_pos = self._objects.get(obj, None)
if old_pos in self._location:
self._location[old_pos].remove(obj)
if pos in self._location:
self._location[pos].append(obj)
old_pos = self.object_locations.get(obj, None)
if old_pos in self.location_objects:
self.location_objects[old_pos].remove(obj)
if pos in self.location_objects:
self.location_objects[pos].append(obj)
else:
self._location[pos] = [obj]
self._objects[obj] = pos
self.location_objects[pos] = [obj]
self.object_locations[obj] = pos
obj._setWorld(self)
def __str__(self):
......@@ -240,7 +250,7 @@ class GridWorld(object):
for pos in self.blocks:
maze_strs[pos.y][pos.x] = BLOCK
for obj, pos in self._objects.items():
for obj, pos in self.object_locations.items():
maze_strs[pos.y][pos.x] = obj.charSymbol().ljust(CELL_WIDTH)
top_frame = '' + '' * CELL_WIDTH * self.size.x + '' + '\n'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment