Esempio n. 1
0
    def spawn(self, thing, position):
        """Add a thing to the world."""
        if not inside_map(position, self.size):
            message = "Can't spawn things outside the map {}".format(position)
            raise Exception(message)

        other = self.things.get(position)
        if other is None:
            self.things[position] = thing
            thing.position = position
        else:
            message = "Can't place {} in a position occupied by {}."
            raise Exception(message.format(thing, other))
Esempio n. 2
0
def move(thing, world, target_position):
    obstacle = world.things.get(target_position)
    if obstacle is not None:
        event = 'hit {} with his head'.format(obstacle.name)
    elif not inside_map(target_position, world.size):
        event = "want's to get out of the world"
    else:
        # we store position in the things, because they need to know it,
        # but also in our dict, for faster access
        world.things[target_position] = thing
        del world.things[thing.position]
        thing.position = target_position

        event = 'moved to {}'.format(target_position)

    return event