def __init__(self, squareSize, isWall, x, y): MapObject.__init__(self, squareSize*x, squareSize*y) self.coord = Coordinates(x,y) self.isWall = isWall self.size = squareSize self.cameFrom = None self.shape = None
def __init__(self, x, y, speed, damage, enemy, ptype): MapObject.__init__(self, x, y) self.speed = speed self.damage = damage self.enemy = enemy self.ptype = ptype self.rotation = 0
def __init__(self, parent): super().__init__(CreationMode.CreateGeometry, parent) self.mOverlayPolygonObject = MapObject() self.mOverlayObjectGroup = ObjectGroup() self.mOverlayObjectGroup.addObject(self.mOverlayPolygonObject) highlight = QApplication.palette().highlight().color() self.mOverlayObjectGroup.setColor(highlight)
def __init__(self,x,y,damage,trange,ttype): MapObject.__init__(self, x, y) self.damage = damage self.range = trange self.ttype = ttype self.rest = False self.firesAt = [] self.lines = []
def __init__(self,x,y,damage,trange,firerate,projSpeed,ttype): MapObject.__init__(self, x, y) self.ttype = ttype self.damage = damage self.firerate = firerate self.rest = False self.waitTime = 0 self.range = trange self.projSpeed = projSpeed
def createNewMapObject(self): if (not self.mTile): return None newMapObject = MapObject() newMapObject.setShape(MapObject.Rectangle) newMapObject.setCell(Cell(self.mTile)) newMapObject.setSize(self.mTile.size()) return newMapObject
def __init__(self, x, y, speed, hp, gamemap, moveto, etype): MapObject.__init__(self, x, y) self.map = gamemap self.speed = speed self.maxHp = hp self.hp = hp self.etype = etype self.r = 0 self.dead = False self.goal = False self.moveTowards = moveto self.coord = None self.setCoordinates() self.getNext() self.direction = None self.getDirection()
def test_map_objects_to_objects(self): map_objects_array = map_objects_to_objects( get_objects('../maps/test.yaml')) objects_array = [ MapObject('trafficlight', [2.2, 2.2], 45, 0.4, True, False) ] for obj, new_obj in zip(map_objects_array, objects_array): self.assertEqual(obj.kind, new_obj.kind) self.assertEqual(obj.position, new_obj.position) self.assertEqual(obj.rotation, new_obj.rotation) self.assertEqual(obj.height, new_obj.height) self.assertEqual(obj.optional, new_obj.optional) self.assertEqual(obj.static, new_obj.static)
class CreateMultipointObjectTool(CreateObjectTool): def __init__(self, parent): super().__init__(CreationMode.CreateGeometry, parent) self.mOverlayPolygonObject = MapObject() self.mOverlayObjectGroup = ObjectGroup() self.mOverlayObjectGroup.addObject(self.mOverlayPolygonObject) highlight = QApplication.palette().highlight().color() self.mOverlayObjectGroup.setColor(highlight) def startNewMapObject(self, pos, objectGroup): super().startNewMapObject(pos, objectGroup) newMapObject = self.mNewMapObjectItem.mapObject() polygon = QPolygonF() polygon.append(QPointF()) newMapObject.setPolygon(polygon) polygon.append(QPointF()) # The last point is connected to the mouse self.mOverlayPolygonObject.setPolygon(polygon) self.mOverlayPolygonObject.setShape(newMapObject.shape()) self.mOverlayPolygonObject.setPosition(pos) self.mOverlayPolygonItem = MapObjectItem(self.mOverlayPolygonObject, self.mapDocument(), self.mObjectGroupItem) def languageChanged(self): pass def mouseMovedWhileCreatingObject(self, pos, modifiers): renderer = self.mapDocument().renderer() pixelCoords = renderer.screenToPixelCoords_(pos) SnapHelper(renderer, modifiers).snap(pixelCoords) pixelCoords -= self.mNewMapObjectItem.mapObject().position() polygon = self.mOverlayPolygonObject.polygon() polygon[-1] = pixelCoords self.mOverlayPolygonItem.setPolygon(polygon) def mousePressedWhileCreatingObject(self, event): if (event.button() == Qt.RightButton): self.finishNewMapObject() elif (event.button() == Qt.LeftButton): current = self.mNewMapObjectItem.mapObject().polygon() next = self.mOverlayPolygonObject.polygon() # If the last position is still the same, ignore the click if (next.last() == current.last()): return # Assign current overlay polygon to the new object self.mNewMapObjectItem.setPolygon(next) # Add a new editable point to the overlay next.append(next.last()) self.mOverlayPolygonItem.setPolygon(next)
def map_objects_to_objects(map_objects): map_objects_array = [] if not map_objects: return None for object in map_objects: x, y = re.sub(r"[\[\]]", "", object['pos']).split(',') position = [float(x), float(y)] rotation = float(object['rotate']) height = float(object['height']) optional = True if object['optional'] == 'true' else False static = True if object['static'] == 'True' else False map_objects_array.append( MapObject(object['kind'], position, rotation, height, optional, static)) return map_objects_array
def getbuildings(self): buildings = {} minimapwidth = self.size // MINITILEWIDTH for bx, by in self.minimap: bname = self.minimap[(bx, by)] x = bx * MINITILEWIDTH y = by * MINITILEWIDTH if bname == 'wall': pos1 = (x + 2, y) pos2 = (x + 2, y + 2) pos3 = (x + 2, y + 4) pos4 = (x, y + 2) pos5 = (x + 4, y + 2) buildings[(pos1)] = MapObject('wall', pos1[0], pos1[1]) buildings[(pos2)] = MapObject('wall', pos2[0], pos2[1]) buildings[(pos3)] = MapObject('wall', pos3[0], pos3[1]) buildings[(pos4)] = MapObject('wall-front', pos4[0], pos4[1]) buildings[(pos5)] = MapObject('wall-front', pos5[0], pos5[1]) elif bname == 'wall-front': pos1 = (x + 2, y) pos2 = (x + 2, y + 2) pos3 = (x + 2, y + 3) pos4 = (x, y + 2) pos5 = (x + 4, y + 2) buildings[(pos1)] = MapObject('wall', pos1[0], pos1[1]) buildings[(pos2)] = MapObject('wall', pos2[0], pos2[1]) buildings[(pos3)] = MapObject('wall-front', pos3[0], pos3[1]) buildings[(pos4)] = MapObject('wall-front', pos4[0], pos4[1]) buildings[(pos5)] = MapObject('wall-front', pos5[0], pos5[1]) elif bname == 'guardhouse': # guardhouses only happen on side walls, not front or back pos1 = (x + 2, y) pos2 = (x + 2, y + 2) pos3 = (x + 2, y + 4) pos4 = (x, y + 2) pos5 = (x + 4, y + 2) buildings[(pos1)] = MapObject('wall', pos1[0], pos1[1]) buildings[(pos2)] = MapObject('wall', pos2[0], pos2[1]) buildings[(pos3)] = MapObject('wall', pos3[0], pos3[1]) buildings[(pos4)] = MapObject('guardhouse', pos4[0], pos4[1]) buildings[(pos5)] = MapObject('guardhouse', pos5[0], pos5[1]) else: x = x + BUILDING_MINITILE_OFFSET_X buildings[(x, y)] = MapObject(bname, x, y) return buildings
def toObjectGroup(self, variantMap): objectGroup = ObjectGroup(variantMap.get("name", ''), variantMap.get("x", 0), variantMap.get("y", 0), variantMap.get("width", 0), variantMap.get("height", 0)) opacity = variantMap.get("opacity", 0.0) visible = variantMap.get("visible", True) objectGroup.setOpacity(opacity) objectGroup.setVisible(visible) objectGroup.setColor(variantMap.get("color", '')) drawOrderString = variantMap.get("draworder", '') if drawOrderString != '': objectGroup.setDrawOrder(drawOrderFromString(drawOrderString)) if (objectGroup.drawOrder() == ObjectGroup.DrawOrder.UnknownOrder): self.mError = self.tr("Invalid draw order: %s" % drawOrderString) return None for objectVariant in variantMap.get("objects", []): objectVariantMap = objectVariant name = objectVariantMap.get("name", '') type = objectVariantMap.get("type", '') id = objectVariantMap.get("id", 0.0) gid = objectVariantMap.get("gid", 0.0) x = objectVariantMap.get("x", 0.0) y = objectVariantMap.get("y", 0.0) width = objectVariantMap.get("width", 0.0) height = objectVariantMap.get("height", 0.0) rotation = objectVariantMap.get("rotation", 0.0) pos = QPointF(x, y) size = QSizeF(width, height) object = MapObject(name, type, pos, size) object.setId(id) object.setRotation(rotation) if (gid): cell, ok = self.mGidMapper.gidToCell(gid) object.setCell(cell) if not object.cell().isEmpty(): tileSize = object.cell().tile.size() if width == 0: object.setWidth(tileSize.width()) if height == 0: object.setHeight(tileSize.height()) if (objectVariantMap.__contains__("visible")): object.setVisible(objectVariantMap.get("visible", True)) object.setProperties( self.toProperties(objectVariantMap.get("properties", {}))) objectGroup.addObject(object) polylineVariant = objectVariantMap.get("polyline", []) polygonVariant = objectVariantMap.get("polygon", []) if len(polygonVariant) > 0: object.setShape(MapObject.Polygon) object.setPolygon(self.toPolygon(polygonVariant)) if len(polylineVariant) > 0: object.setShape(MapObject.Polyline) object.setPolygon(self.toPolygon(polylineVariant)) if (objectVariantMap.__contains__("ellipse")): object.setShape(MapObject.Ellipse) return objectGroup
def createNewMapObject(self): newMapObject = MapObject() newMapObject.setShape(MapObject.Rectangle) return newMapObject
def toObjectGroup(self, variantMap): objectGroup = ObjectGroup(variantMap.get("name",''), variantMap.get("x",0), variantMap.get("y",0), variantMap.get("width",0), variantMap.get("height",0)) opacity = variantMap.get("opacity", 0.0) visible = variantMap.get("visible", True) objectGroup.setOpacity(opacity) objectGroup.setVisible(visible) objectGroup.setColor(variantMap.get("color", '')) drawOrderString = variantMap.get("draworder", '') if drawOrderString != '': objectGroup.setDrawOrder(drawOrderFromString(drawOrderString)) if (objectGroup.drawOrder() == ObjectGroup.DrawOrder.UnknownOrder): self.mError = self.tr("Invalid draw order: %s"%drawOrderString) return None for objectVariant in variantMap.get("objects", []): objectVariantMap = objectVariant name = objectVariantMap.get("name",'') type = objectVariantMap.get("type", '') id = objectVariantMap.get("id",0.0) gid = objectVariantMap.get("gid",0.0) x = objectVariantMap.get("x",0.0) y = objectVariantMap.get("y",0.0) width = objectVariantMap.get("width",0.0) height = objectVariantMap.get("height",0.0) rotation = objectVariantMap.get("rotation", 0.0) pos = QPointF(x, y) size = QSizeF(width, height) object = MapObject(name, type, pos, size) object.setId(id) object.setRotation(rotation) if (gid): cell, ok = self.mGidMapper.gidToCell(gid) object.setCell(cell) if not object.cell().isEmpty(): tileSize = object.cell().tile.size() if width == 0: object.setWidth(tileSize.width()) if height == 0: object.setHeight(tileSize.height()) if (objectVariantMap.__contains__("visible")): object.setVisible(objectVariantMap.get("visible", True)) object.setProperties(self.toProperties(objectVariantMap.get("properties", {}))) objectGroup.addObject(object) polylineVariant = objectVariantMap.get("polyline", []) polygonVariant = objectVariantMap.get("polygon", []) if len(polygonVariant) > 0: object.setShape(MapObject.Polygon) object.setPolygon(self.toPolygon(polygonVariant)) if len(polylineVariant) > 0: object.setShape(MapObject.Polyline) object.setPolygon(self.toPolygon(polylineVariant)) if (objectVariantMap.__contains__("ellipse")): object.setShape(MapObject.Ellipse) return objectGroup
def __readObject(self): atts = self.xml.attributes() id = Int(atts.value("id")) name = atts.value("name") gid = Int(atts.value("gid")) x = Float(atts.value("x")) y = Float(atts.value("y")) width = Float(atts.value("width")) height = Float(atts.value("height")) type = atts.value("type") visibleRef = atts.value("visible") pos = QPointF(x, y) size = QSizeF(width, height) object = MapObject(name, type, pos, size) object.setId(id) try: rotation = Float(atts.value("rotation")) ok = True except: ok = False if (ok): object.setRotation(rotation) if (gid): object.setCell(self.__cellForGid(gid)) if (not object.cell().isEmpty()): tileSize = object.cell().tile.size() if (width == 0): object.setWidth(tileSize.width()) if (height == 0): object.setHeight(tileSize.height()) try: visible = int(visibleRef) ok = True except: ok = False if ok: object.setVisible(visible) while (self.xml.readNextStartElement()): if (self.xml.name() == "properties"): object.mergeProperties(self.__readProperties()) elif (self.xml.name() == "polygon"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polygon) elif (self.xml.name() == "polyline"): object.setPolygon(self.__readPolygon()) object.setShape(MapObject.Polyline) elif (self.xml.name() == "ellipse"): self.xml.skipCurrentElement() object.setShape(MapObject.Ellipse) else: self.__readUnknownElement() return object
def createNewMapObject(self): newMapObject = MapObject() newMapObject.setShape(MapObject.Polygon) return newMapObject
def createNewMapObject(self): newMapObject = MapObject() newMapObject.setShape(MapObject.Polyline) return newMapObject
def read(self, fileName): file = QFile(fileName) if (not file.open (QIODevice.ReadOnly)): self.mError = self.tr("Could not open file for reading.") return None # default to values of the original flare alpha game. map = Map(Map.Orientation.Isometric, 256, 256, 64, 32) stream = QTextStream(file) line = QString() sectionName = QString() newsection = False path = QFileInfo(file).absolutePath() base = 10 gidMapper = GidMapper() gid = 1 tilelayer = None objectgroup = None mapobject = None tilesetsSectionFound = False headerSectionFound = False tilelayerSectionFound = False # tile layer or objects while (not stream.atEnd()): line = stream.readLine() if line == '': continue startsWith = line[0] if (startsWith == '['): sectionName = line[1:line.index(']')] newsection = True continue if (sectionName == "header"): headerSectionFound = True #get map properties epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "width"): map.setWidth(Int(value)) elif (key == "height"): map.setHeight(Int(value)) elif (key == "tilewidth"): map.setTileWidth(Int(value)) elif (key == "tileheight"): map.setTileHeight(Int(value)) elif (key == "orientation"): map.setOrientation(orientationFromString(value)) else: map.setProperty(key, value) elif (sectionName == "tilesets"): tilesetsSectionFound = True epos = line.index('=') key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "tileset"): _list = value.split(',') absoluteSource = _list[0] if (QDir.isRelativePath(absoluteSource)): absoluteSource = path + '/' + absoluteSource tilesetwidth = 0 tilesetheight = 0 if len(_list) > 2: tilesetwidth = Int(_list[1]) tilesetheight = Int(_list[2]) tileset = Tileset.create(QFileInfo(absoluteSource).fileName(), tilesetwidth, tilesetheight) ok = tileset.loadFromImage(absoluteSource) if not ok: self.mError = self.tr("Error loading tileset %s, which expands to %s. Path not found!"%(_list[0], absoluteSource)) return None else : if len(_list) > 4: tileset.setTileOffset(QPoint(Int(_list[3]),Int(_list[4]))) gidMapper.insert(gid, tileset) if len(_list) > 5: gid += Int(_list[5]) else : gid += tileset.tileCount() map.addTileset(tileset) elif (sectionName == "layer"): if (not tilesetsSectionFound): self.mError = self.tr("No tilesets section found before layer section.") return None tilelayerSectionFound = True epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "type"): tilelayer = TileLayer(value, 0, 0, map.width(),map.height()) map.addLayer(tilelayer) elif (key == "format"): if (value == "dec"): base = 10 elif (value == "hex"): base = 16 elif (key == "data"): for y in range(map.height()): line = stream.readLine() l = line.split(',') for x in range(min(map.width(), len(l))): ok = False tileid = int(l[x], base) c, ok = gidMapper.gidToCell(tileid) if (not ok): self.mError += self.tr("Error mapping tile id %1.").arg(tileid) return None tilelayer.setCell(x, y, c) else : tilelayer.setProperty(key, value) else : if (newsection): if (map.indexOfLayer(sectionName) == -1): objectgroup = ObjectGroup(sectionName, 0,0,map.width(), map.height()) map.addLayer(objectgroup) else : objectgroup = map.layerAt(map.indexOfLayer(sectionName)) mapobject = MapObject() objectgroup.addObject(mapobject) newsection = False if (not mapobject): continue if (startsWith == '#'): name = line[1].strip() mapobject.setName(name) epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "type"): mapobject.setType(value) elif (key == "location"): loc = value.split(',') x,y = 0.0, 0.0 w,h = 0, 0 if (map.orientation() == Map.Orthogonal): x = loc[0].toFloat()*map.tileWidth() y = loc[1].toFloat()*map.tileHeight() if len(loc) > 3: w = Int(loc[2])*map.tileWidth() h = Int(loc[3])*map.tileHeight() else : w = map.tileWidth() h = map.tileHeight() else : x = loc[0].toFloat()*map.tileHeight() y = loc[1].toFloat()*map.tileHeight() if len(loc) > 3: w = Int(loc[2])*map.tileHeight() h = Int(loc[3])*map.tileHeight() else : w = h = map.tileHeight() mapobject.setPosition(QPointF(x, y)) mapobject.setSize(w, h) else : mapobject.setProperty(key, value) if (not headerSectionFound or not tilesetsSectionFound or not tilelayerSectionFound): self.mError = self.tr("This seems to be no valid flare map. " "A Flare map consists of at least a header " "section, a tileset section and one tile layer.") return None return map
def read(self, fileName): file = QFile(fileName) if (not file.open(QIODevice.ReadOnly)): self.mError = self.tr("Could not open file for reading.") return None # default to values of the original flare alpha game. map = Map(Map.Orientation.Isometric, 256, 256, 64, 32) stream = QTextStream(file) line = QString() sectionName = QString() newsection = False path = QFileInfo(file).absolutePath() base = 10 gidMapper = GidMapper() gid = 1 tilelayer = None objectgroup = None mapobject = None tilesetsSectionFound = False headerSectionFound = False tilelayerSectionFound = False # tile layer or objects while (not stream.atEnd()): line = stream.readLine() if line == '': continue startsWith = line[0] if (startsWith == '['): sectionName = line[1:line.index(']')] newsection = True continue if (sectionName == "header"): headerSectionFound = True #get map properties epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "width"): map.setWidth(Int(value)) elif (key == "height"): map.setHeight(Int(value)) elif (key == "tilewidth"): map.setTileWidth(Int(value)) elif (key == "tileheight"): map.setTileHeight(Int(value)) elif (key == "orientation"): map.setOrientation(orientationFromString(value)) else: map.setProperty(key, value) elif (sectionName == "tilesets"): tilesetsSectionFound = True epos = line.index('=') key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "tileset"): _list = value.split(',') absoluteSource = _list[0] if (QDir.isRelativePath(absoluteSource)): absoluteSource = path + '/' + absoluteSource tilesetwidth = 0 tilesetheight = 0 if len(_list) > 2: tilesetwidth = Int(_list[1]) tilesetheight = Int(_list[2]) tileset = Tileset.create( QFileInfo(absoluteSource).fileName(), tilesetwidth, tilesetheight) ok = tileset.loadFromImage(absoluteSource) if not ok: self.mError = self.tr( "Error loading tileset %s, which expands to %s. Path not found!" % (_list[0], absoluteSource)) return None else: if len(_list) > 4: tileset.setTileOffset( QPoint(Int(_list[3]), Int(_list[4]))) gidMapper.insert(gid, tileset) if len(_list) > 5: gid += Int(_list[5]) else: gid += tileset.tileCount() map.addTileset(tileset) elif (sectionName == "layer"): if (not tilesetsSectionFound): self.mError = self.tr( "No tilesets section found before layer section.") return None tilelayerSectionFound = True epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "type"): tilelayer = TileLayer(value, 0, 0, map.width(), map.height()) map.addLayer(tilelayer) elif (key == "format"): if (value == "dec"): base = 10 elif (value == "hex"): base = 16 elif (key == "data"): for y in range(map.height()): line = stream.readLine() l = line.split(',') for x in range(min(map.width(), len(l))): ok = False tileid = int(l[x], base) c, ok = gidMapper.gidToCell(tileid) if (not ok): self.mError += self.tr( "Error mapping tile id %1.").arg( tileid) return None tilelayer.setCell(x, y, c) else: tilelayer.setProperty(key, value) else: if (newsection): if (map.indexOfLayer(sectionName) == -1): objectgroup = ObjectGroup(sectionName, 0, 0, map.width(), map.height()) map.addLayer(objectgroup) else: objectgroup = map.layerAt( map.indexOfLayer(sectionName)) mapobject = MapObject() objectgroup.addObject(mapobject) newsection = False if (not mapobject): continue if (startsWith == '#'): name = line[1].strip() mapobject.setName(name) epos = line.index('=') if (epos != -1): key = line[:epos].strip() value = line[epos + 1:].strip() if (key == "type"): mapobject.setType(value) elif (key == "location"): loc = value.split(',') x, y = 0.0, 0.0 w, h = 0, 0 if (map.orientation() == Map.Orthogonal): x = loc[0].toFloat() * map.tileWidth() y = loc[1].toFloat() * map.tileHeight() if len(loc) > 3: w = Int(loc[2]) * map.tileWidth() h = Int(loc[3]) * map.tileHeight() else: w = map.tileWidth() h = map.tileHeight() else: x = loc[0].toFloat() * map.tileHeight() y = loc[1].toFloat() * map.tileHeight() if len(loc) > 3: w = Int(loc[2]) * map.tileHeight() h = Int(loc[3]) * map.tileHeight() else: w = h = map.tileHeight() mapobject.setPosition(QPointF(x, y)) mapobject.setSize(w, h) else: mapobject.setProperty(key, value) if (not headerSectionFound or not tilesetsSectionFound or not tilelayerSectionFound): self.mError = self.tr( "This seems to be no valid flare map. " "A Flare map consists of at least a header " "section, a tileset section and one tile layer.") return None return map
def createNewMapObject(self): newMapObject = MapObject() newMapObject.setShape(MapObject.Ellipse) return newMapObject