2020-04-05 22:49:50 +02:00
|
|
|
import json
|
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
import pygame
|
2020-04-06 10:35:58 +02:00
|
|
|
|
|
|
|
from src.entities.Interactable import Interactable
|
2020-04-05 23:42:41 +02:00
|
|
|
from src.game.TerrainTile import TerrainTile
|
|
|
|
from src.game.Screen import Locations
|
2020-04-03 19:36:13 +02:00
|
|
|
|
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):
|
2020-04-03 19:36:13 +02:00
|
|
|
self.screen = screen
|
|
|
|
self.terrain = []
|
2020-04-05 15:41:42 +02:00
|
|
|
self.collidableTerrain = []
|
2020-04-05 19:08:08 +02:00
|
|
|
self.collidables = pygame.sprite.Group()
|
2020-04-05 15:41:42 +02:00
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
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)
|
2020-04-03 19:36:13 +02:00
|
|
|
|
|
|
|
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 23:14:21 +02:00
|
|
|
for entity in self.loadEntities(filename):
|
|
|
|
self.addEntity(entity)
|
|
|
|
|
2020-04-05 22:49:50 +02:00
|
|
|
# Returns a list of entities loaded from mapfile
|
|
|
|
def loadEntities(self, mapFileName):
|
2020-04-05 23:14:21 +02:00
|
|
|
mapFile = mapFileName.split('.txt')[0]
|
|
|
|
entitiesFilePath = mapFile + "Entities.json"
|
2020-04-05 22:49:50 +02:00
|
|
|
actualEntities = []
|
2020-04-05 23:14:21 +02:00
|
|
|
with open(entitiesFilePath, 'rt') as file:
|
|
|
|
entityListJson = json.loads(file.read())
|
|
|
|
for entity in entityListJson:
|
|
|
|
try:
|
2020-04-06 10:35:58 +02:00
|
|
|
# Creates a pickupable object
|
2020-04-05 23:14:21 +02:00
|
|
|
if entity["isPickupable"]:
|
2020-04-06 00:43:20 +02:00
|
|
|
actualEntities.append(Pickupable(entity["name"] + ".png",
|
2020-04-05 23:14:21 +02:00
|
|
|
self.tileSize,
|
|
|
|
(entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize),
|
|
|
|
Statistics(entity["effect"]["hp"],
|
|
|
|
entity["effect"]["hunger"],
|
|
|
|
entity["effect"]["thirst"],
|
|
|
|
entity["effect"]["stamina"])))
|
2020-04-06 10:35:58 +02:00
|
|
|
# Creates an interactable object
|
|
|
|
elif "effect" in entity:
|
|
|
|
actualEntities.append(Interactable(entity["name"] + ".png",
|
|
|
|
self.tileSize,
|
|
|
|
(entity["position"]["x"] * self.tileSize,
|
|
|
|
entity["position"]["y"] * self.tileSize),
|
|
|
|
Statistics(entity["effect"]["hp"],
|
|
|
|
entity["effect"]["hunger"],
|
|
|
|
entity["effect"]["thirst"],
|
|
|
|
entity["effect"]["stamina"])))
|
|
|
|
# Creates plain entity
|
2020-04-05 23:14:21 +02:00
|
|
|
else:
|
2020-04-06 00:43:20 +02:00
|
|
|
actualEntities.append(Entity(entity["name"] + ".png",
|
2020-04-05 23:14:21 +02:00
|
|
|
self.tileSize,
|
|
|
|
(entity["position"]["x"] * self.tileSize, entity["position"]["y"] * self.tileSize)))
|
|
|
|
except KeyError:
|
|
|
|
print("Failed to load entity " + entity)
|
2020-04-05 22:49:50 +02:00
|
|
|
return actualEntities
|
|
|
|
|
2020-04-03 19:36:13 +02:00
|
|
|
def terrainDraw(self):
|
|
|
|
for row, tiles in enumerate(self.terrain):
|
|
|
|
for col, tile in enumerate(tiles):
|
2020-04-05 22:07:37 +02:00
|
|
|
if tile == 's':
|
|
|
|
self.screen.draw(TerrainTile(col, row, 'sand.png', self.tileSize), Locations.MAP, 0, 0)
|
2020-04-03 23:07:12 +02:00
|
|
|
elif tile == ',':
|
2020-04-05 19:08:08 +02:00
|
|
|
self.screen.draw(TerrainTile(col, row, 'floor.png', self.tileSize), Locations.MAP, 0, 0)
|
2020-04-03 23:07:12 +02:00
|
|
|
elif tile == '.':
|
2020-04-05 19:08:08 +02:00
|
|
|
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':
|
2020-04-06 00:43:20 +02:00
|
|
|
object = TerrainTile(col, row, 'water.png', self.tileSize)
|
2020-04-05 21:46:04 +02:00
|
|
|
self.screen.draw(object, Locations.MAP, 0, 0)
|
|
|
|
self.collidables.add(object)
|
2020-04-05 22:07:37 +02:00
|
|
|
elif tile == 'w':
|
|
|
|
object = TerrainTile(col, row, 'wall.png', self.tileSize)
|
|
|
|
self.screen.draw(object, Locations.MAP, 0, 0)
|
|
|
|
self.collidables.add(object)
|
2020-04-05 14:53:54 +02:00
|
|
|
|
2020-04-05 23:42:41 +02:00
|
|
|
def getEntityOnCoord(self, coord):
|
|
|
|
result = None
|
|
|
|
for entity in self.collidables:
|
2020-04-05 23:43:15 +02:00
|
|
|
if entity.rect.x == coord[0] and entity.rect.y == coord[1]:
|
2020-04-05 23:42:41 +02:00
|
|
|
result = entity
|
|
|
|
return result
|
|
|
|
|
2020-04-05 14:53:54 +02:00
|
|
|
def addEntity(self, entity):
|
|
|
|
self.screen.draw(entity, Locations.MAP, 0, 0)
|
2020-04-05 20:24:28 +02:00
|
|
|
self.collidables.add(entity)
|
2020-04-03 19:36:13 +02:00
|
|
|
|
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
|