Added loading sprites from json

This commit is contained in:
Marcin Kostrzewski 2020-04-05 23:14:21 +02:00
parent 1ef4d65045
commit 864877b866

View File

@ -29,28 +29,32 @@ class Map:
self.terrainDraw() self.terrainDraw()
for entity in self.loadEntities(filename):
self.addEntity(entity)
# Returns a list of entities loaded from mapfile # Returns a list of entities loaded from mapfile
def loadEntities(self, mapFileName): def loadEntities(self, mapFileName):
mapFile = mapFileName.split('.')[0] mapFile = mapFileName.split('.txt')[0]
entitiesFile = mapFile + "Entities.json" entitiesFilePath = mapFile + "Entities.json"
entityListJson = json.loads(entitiesFile)
actualEntities = [] actualEntities = []
for entity in entityListJson: with open(entitiesFilePath, 'rt') as file:
try: entityListJson = json.loads(file.read())
if entity["isPickupable"]: for entity in entityListJson:
actualEntities.append(Pickupable(entity["name"], try:
(entity["position"]["x"], entity["position"]["y"]), if entity["isPickupable"]:
self.tileSize, actualEntities.append(Pickupable(entity["name"] + ".jpg",
Statistics(entity["effect"]["hp"], self.tileSize,
entity["effect"]["hunger"], (entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize),
entity["effect"]["thirst"], Statistics(entity["effect"]["hp"],
entity["effect"]["stamina"]))) entity["effect"]["hunger"],
else: entity["effect"]["thirst"],
actualEntities.append(Entity(entity["name"], entity["effect"]["stamina"])))
(entity["position"]["x"], entity["position"]["y"]), else:
self.tileSize)) actualEntities.append(Entity(entity["name"],
except KeyError: self.tileSize,
print("Failed to load entity " + entity) (entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize)))
except KeyError:
print("Failed to load entity " + entity)
return actualEntities return actualEntities
def terrainDraw(self): def terrainDraw(self):