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,26 +29,30 @@ 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 = []
with open(entitiesFilePath, 'rt') as file:
entityListJson = json.loads(file.read())
for entity in entityListJson: for entity in entityListJson:
try: try:
if entity["isPickupable"]: if entity["isPickupable"]:
actualEntities.append(Pickupable(entity["name"], actualEntities.append(Pickupable(entity["name"] + ".jpg",
(entity["position"]["x"], entity["position"]["y"]),
self.tileSize, self.tileSize,
(entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize),
Statistics(entity["effect"]["hp"], Statistics(entity["effect"]["hp"],
entity["effect"]["hunger"], entity["effect"]["hunger"],
entity["effect"]["thirst"], entity["effect"]["thirst"],
entity["effect"]["stamina"]))) entity["effect"]["stamina"])))
else: else:
actualEntities.append(Entity(entity["name"], actualEntities.append(Entity(entity["name"],
(entity["position"]["x"], entity["position"]["y"]), self.tileSize,
self.tileSize)) (entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize)))
except KeyError: except KeyError:
print("Failed to load entity " + entity) print("Failed to load entity " + entity)
return actualEntities return actualEntities