diff --git a/src/game/Map.py b/src/game/Map.py index dbc976f..9f5db5d 100644 --- a/src/game/Map.py +++ b/src/game/Map.py @@ -1,7 +1,14 @@ +import json + import pygame from game.TerrainTile import TerrainTile from game.Screen import Locations +from src.entities.Entity import Entity +from src.entities.Pickupable import Pickupable +from src.entities.Statistics import Statistics + + class Map: def __init__(self, filename, screen): self.screen = screen @@ -22,6 +29,30 @@ class Map: self.terrainDraw() + # Returns a list of entities loaded from mapfile + def loadEntities(self, mapFileName): + mapFile = mapFileName.split('.')[0] + entitiesFile = mapFile + "Entities.json" + entityListJson = json.loads(entitiesFile) + actualEntities = [] + for entity in entityListJson: + try: + if entity["isPickupable"]: + actualEntities.append(Pickupable(entity["name"], + (entity["position"]["x"], entity["position"]["y"]), + self.tileSize, + Statistics(entity["effect"]["hp"], + entity["effect"]["hunger"], + entity["effect"]["thirst"], + entity["effect"]["stamina"]))) + else: + actualEntities.append(Entity(entity["name"], + (entity["position"]["x"], entity["position"]["y"]), + self.tileSize)) + except KeyError: + print("Failed to load entity " + entity) + return actualEntities + def terrainDraw(self): for row, tiles in enumerate(self.terrain): for col, tile in enumerate(tiles): @@ -52,4 +83,4 @@ class Map: for b in self.collidables: if b.rect.x == x and b.rect.y == y: return True - return False \ No newline at end of file + return False