Esempio n. 1
0
def build_object(object, world: esper.World, window_options, draw2entity):
    logger = logging.getLogger(__name__)
    mxCell = object[0]
    mxGeometry = mxCell[0]
    # Get X, Y coordinates
    x = float(mxGeometry.attrib.get('x', 0))
    y = float(mxGeometry.attrib.get('y', 0))
    width = float(mxGeometry.attrib.get('width', 0))
    height = float(mxGeometry.attrib.get('height', 0))
    pos = Position(x, y, 0, width, height)
    x += (width // 2)
    y += (height // 2)
    # Get the Map for simulation or create one
    # Entity 1 is the simulation entity
    if world.has_component(1, Map):
        simulation_map = world.component_for_entity(1, Map)
    else:
        simulation_map = Map()
        world.add_component(1, simulation_map)
    # Get POI tag
    if 'tag' in object.attrib:
        tag = object.attrib['tag']
    else:
        tag = 'POI_' + str(len(simulation_map.pois))
        logger.warning(f'POI ({x}, {y}) with no TAG. Using {tag}')
    simulation_map.pois[tag] = (x, y)
    # Alternatively display the POI
    if object.attrib.get('display', False):
        skeleton = Skeleton(object.attrib['id'], mxCell.attrib['style'], tag)
        world.create_entity(pos, skeleton)
    return {}, [], {}
Esempio n. 2
0
def build_object(cell, world: esper.World, window_options, draw2entity):
    logger = logging.getLogger(__name__)
    mxCell = cell[0]
    points = path_from_mxCell(mxCell, draw2entity, world)
    if len(points) <= 1:
        raise Exception(f'Map path has {len(points)} points. Minimum is 2.')

    # Entity 1 is the simulation entity
    if world.has_component(1, Map):
        simulation_map = world.component_for_entity(1, Map)
    else:
        simulation_map = Map()
        world.add_component(1, simulation_map)
    add_nodes_from_points(simulation_map, points)
    return {}, [], {}
Esempio n. 3
0
def build_simulation_objects(content_root, batch: pyglet.graphics.Batch,
                             world: esper.World, window_options):
    # Create Walls
    draw2entity = {}
    interactive = {}
    objects = []
    windowSize = window_options[0]
    for cell in content_root:
        if cell.tag == 'mxCell' and 'style' in cell.attrib:
            (components,
             style) = mxCellDecoder.parse_mxCell(cell, batch, window_options)
            ent = world.create_entity()
            for c in components:
                world.add_component(ent, c)
            draw2entity[style['id']] = [ent, style]
        if cell.tag == 'object':
            if cell.attrib['type'] == 'robot':
                (components,
                 style) = mxCellDecoder.parse_object(cell, batch,
                                                     window_options)
                ent = world.create_entity()
                # Custom components
                for key, val in cell.attrib.items():
                    if key.startswith('component_'):
                        component_name = key[
                            10:]  # removes "component_" from the name
                        init_values = json.loads(val)
                        component = dynamic_importer.init_component(
                            component_name, init_values)
                        components.append(component)
                for c in components:
                    world.add_component(ent, c)
                draw2entity[style['id']] = [ent, style]
                objects.append((ent, style['id']))
            elif cell.attrib['type'] == 'pickable':
                skeleton = copy.copy(cell)
                (components,
                 style) = mxCellDecoder.parse_object(cell, batch,
                                                     window_options)
                pick = Pickable(float(cell.attrib['weight']),
                                cell.attrib['name'], skeleton)
                components.append(pick)
                ent = world.create_entity()
                for c in components:
                    world.add_component(ent, c)
                interactive[style['name']] = ent
            elif cell.attrib['type'] == 'path':
                mxCell = cell[0]
                points = Path.from_mxCell(mxCell, windowSize[1])
                obj = mxCell.attrib.get('source', None)
                (ent, _) = draw2entity.get(obj, (None, None))
                if ent is None:
                    print(f"Path origin ({obj}) not found. Trying target.")
                else:
                    print(f"Adding path to entity {ent}")
                    world.add_component(ent, Path(points))
            elif cell.attrib['type'] == 'map-path':
                mxCell = cell[0]
                points = Path.from_mxCell(mxCell, windowSize[1])
                objId = cell.attrib.get('origin', '')
                key = cell.attrib.get('key', '')
                if key == '':
                    print(f"Map entry without key. Using default value")
                    key = 'Default'
                (ent, _) = draw2entity.get(objId, (None, None))
                if ent is None:
                    print(f"Path origin ({obj}) not found. Trying target.")
                else:
                    if world.has_component(ent, Map):
                        map = world.component_for_entity(ent, Map)
                        if key == 'Default':
                            key += str(len(map))
                        map.paths[key] = points
                    else:
                        if key == 'Default':
                            key += '0'
                        newMap = Map({key: points})
                        world.add_component(ent, newMap)
            else:
                print("Unrecognized object", cell)
    return draw2entity, objects, interactive