Implemented loading entities from json
This commit is contained in:
parent
15a2010c5d
commit
1ef4d65045
@ -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
|
||||
return False
|
||||
|
Loading…
Reference in New Issue
Block a user