From 1ef4d65045fce7524dd1a5cf91d2d31403e3ac70 Mon Sep 17 00:00:00 2001 From: Marcin Kostrzewski Date: Sun, 5 Apr 2020 22:49:50 +0200 Subject: [PATCH] Implemented loading entities from json --- src/game/Map.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) 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