Пример #1
0
 def save(self):
   assert self.levelName
   # TODO: test whether this will create folders
   with resourceOpen('Levels/uncompressed/'+self.levelName+'/level.txt', 'w') as f:
     for entityID, entity in self.ids.iteritems():
       if entityID == 0:
         f.write('# 0 reserved for player\n')
       else:
         f.write('{0}, {1}\n'.format(entityID, repr(entity)))
Пример #2
0
  def loadEntities(self, levelName):
    # TODO: uncompression

    lineCount = 0
    with resourceOpen('Levels/uncompressed/'+levelName+'/level.txt') as levelFile:
      for line in levelFile:
        lineCount += 1
        lineErr = "(line " + str(lineCount) + ")"
        if len(line.strip()) == 0:       continue # skip blank lines
        if line.strip().startswith('#'): continue # skip comment lines

        try:
          data = literal_eval(line)
        except:
          print lineErr, "This line could not be parsed:", line
          continue

        entityID, entityType, args = data[0], data[1], data[2:]
        if entityType not in Level.constructors:
          print lineErr, "This is not a valid entity type:", entityType
          print lineErr, "Possible types:", Level.constructors.keys()
          continue
        constructor = self.constructors[entityType]

        try:
          newEntity = constructor(self, *args)
        except:
          print lineErr, "Constructing", entityType, "failed. probably weird arguments:", args
          if self.chrashOnFail: raise  # TODO: test me!
          continue

        if entityID == 1 and entityType != 'SpawnPoint':
          raise Level.InvalidLevelError('EntityID 1 must be a SpawnPoint!')
        elif entityID == 2 and entityType != 'LevelEnd':
          raise Level.InvalidLevelError('EntityID 2 must be a LevelEnd!')
        else:
          self.addEntity(entityID, newEntity)

    # check that we have entityIDs 1 and 2 (for spawnPoint and levelEnd)
    if 1 not in self.ids or 2 not in self.ids:
      raise Level.InvalidLevelError('There must be a SpawnPoint and LevelEnd for IDs 1 and 2')