DSZI_Survival/src/game/Map.py

87 lines
3.5 KiB
Python
Raw Normal View History

2020-04-05 22:49:50 +02:00
import json
import pygame
from game.TerrainTile import TerrainTile
from game.Screen import Locations
2020-04-05 22:49:50 +02:00
from src.entities.Entity import Entity
from src.entities.Pickupable import Pickupable
from src.entities.Statistics import Statistics
2020-03-31 21:47:09 +02:00
class Map:
2020-04-03 23:09:00 +02:00
def __init__(self, filename, screen):
self.screen = screen
self.terrain = []
self.collidableTerrain = []
self.collidables = pygame.sprite.Group()
with open(filename, 'rt') as f:
for line in f:
self.terrain.append(line)
2020-04-03 23:07:12 +02:00
self.tileSize = int(self.screen.mapSize/20)
self.tileWidth = len(self.terrain[0])
self.tileHeight = len(self.terrain)
self.width = self.tileWidth * self.tileSize
self.height = self.tileHeight * self.tileSize
self.terrainDraw()
2020-04-05 14:53:54 +02:00
2020-04-05 22:49:50 +02:00
# 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):
if tile == 'w':
object = TerrainTile(col, row, 'wall.png', self.tileSize)
self.screen.draw(object, Locations.MAP, 0, 0)
self.collidables.add(object)
2020-04-03 23:07:12 +02:00
elif tile == ',':
self.screen.draw(TerrainTile(col, row, 'floor.png', self.tileSize), Locations.MAP, 0, 0)
2020-04-03 23:07:12 +02:00
elif tile == '.':
self.screen.draw(TerrainTile(col, row, 'grass.png', self.tileSize), Locations.MAP, 0, 0)
2020-04-05 21:46:04 +02:00
elif tile == 'x':
object = TerrainTile(col, row, 'water.jpg', self.tileSize)
self.screen.draw(object, Locations.MAP, 0, 0)
self.collidables.add(object)
2020-04-05 14:53:54 +02:00
def addEntity(self, entity):
self.screen.draw(entity, Locations.MAP, 0, 0)
self.collidables.add(entity)
2020-04-05 21:18:11 +02:00
def removeSpriteFromMap(self, entity):
if self.collidables.has(entity):
self.collidables.remove(entity)
self.screen.removeSprite(entity)
2020-04-05 20:20:37 +02:00
# add object to map.collidables list to be collidable
def collision(self, x, y):
for b in self.collidables:
if b.rect.x == x and b.rect.y == y:
return True
2020-04-05 22:49:50 +02:00
return False