Implemented loading entities from json

This commit is contained in:
Marcin Kostrzewski 2020-04-05 22:49:50 +02:00
parent 15a2010c5d
commit 1ef4d65045

View File

@ -1,7 +1,14 @@
import json
import pygame import pygame
from game.TerrainTile import TerrainTile from game.TerrainTile import TerrainTile
from game.Screen import Locations 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: class Map:
def __init__(self, filename, screen): def __init__(self, filename, screen):
self.screen = screen self.screen = screen
@ -22,6 +29,30 @@ class Map:
self.terrainDraw() 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): def terrainDraw(self):
for row, tiles in enumerate(self.terrain): for row, tiles in enumerate(self.terrain):
for col, tile in enumerate(tiles): for col, tile in enumerate(tiles):