2021-06-25 16:13:52 +02:00
|
|
|
from gamedata.objects.base import BaseObject
|
|
|
|
|
|
|
|
class TilesetRenderer(BaseObject):
|
|
|
|
|
2021-07-02 11:10:45 +02:00
|
|
|
def __init__(self,x,y,game,mapfoldername):
|
2021-06-25 16:13:52 +02:00
|
|
|
|
|
|
|
super().__init__(x,y,game)
|
|
|
|
|
|
|
|
self.tilew = 64
|
|
|
|
self.tileh = 64
|
|
|
|
|
2021-07-02 11:10:45 +02:00
|
|
|
self.level = game.levels_lib[mapfoldername]
|
2021-07-02 16:18:36 +02:00
|
|
|
self.reinit(self.level)
|
2021-06-25 16:13:52 +02:00
|
|
|
|
2021-07-02 22:10:01 +02:00
|
|
|
self.bg = game.sprite_lib["fallbackground.png"]
|
|
|
|
if self.level["background"]:
|
|
|
|
self.bg = self.level["background"]
|
|
|
|
|
2021-06-25 16:13:52 +02:00
|
|
|
def draw(self):
|
2021-07-02 22:10:01 +02:00
|
|
|
self.game.window.blit(self.game.pygame.transform.scale(self.bg,(self.game.globals["cameraw"],self.game.globals["camerah"])),[0,0])
|
2021-07-02 16:18:36 +02:00
|
|
|
for layer in self.layers:
|
2021-07-02 21:06:34 +02:00
|
|
|
self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"]))
|
2021-07-02 11:10:45 +02:00
|
|
|
|
2021-07-02 16:18:36 +02:00
|
|
|
def reinit(self,level):
|
2021-07-02 11:10:45 +02:00
|
|
|
json = level["data"]
|
|
|
|
name = level["name"]
|
|
|
|
self.rects = []
|
2021-07-06 21:14:27 +02:00
|
|
|
self.semirects = []
|
2021-07-02 16:18:36 +02:00
|
|
|
self.layers = []
|
2021-07-03 00:31:39 +02:00
|
|
|
self.spawns = [[self.game.DISPLAY_WIDTH//2,self.game.DISPLAY_HEIGHT//2]] # Valeur par défaut
|
2021-07-02 16:18:36 +02:00
|
|
|
if "layers" in json.keys() and type(json["layers"]).__name__=="list":
|
2021-07-02 11:10:45 +02:00
|
|
|
solidlayer = False
|
|
|
|
for layer in json["layers"]:
|
2021-07-03 00:26:02 +02:00
|
|
|
#try:
|
|
|
|
if True:
|
2021-07-02 16:18:36 +02:00
|
|
|
if layer["name"] == "Solids" and "entities" in layer.keys() : # Je trouve les collisions
|
2021-07-02 11:10:45 +02:00
|
|
|
solidlayer = layer
|
2021-07-03 00:26:02 +02:00
|
|
|
elif layer["name"] == "Spawns" and "entities" in layer.keys() : # Je trouve les spawns
|
2021-07-03 00:31:39 +02:00
|
|
|
if len(layer["entities"])>0:
|
|
|
|
self.spawns = [] # J'enlève la valeur par défaut
|
2021-07-03 00:26:02 +02:00
|
|
|
for entity in layer["entities"]:
|
|
|
|
x,y = entity["x"],entity["y"]
|
|
|
|
w,h = layer["gridCellWidth"],layer["gridCellHeight"]
|
|
|
|
if "width" in entity.keys():
|
|
|
|
w = entity["width"]
|
|
|
|
if "height" in entity.keys():
|
|
|
|
h = entity["height"]
|
|
|
|
self.spawns.append(self.game.pygame.Rect((x,y,w,h)))
|
|
|
|
|
2021-07-02 16:18:36 +02:00
|
|
|
else: # J'ajoute un layer de tiles
|
|
|
|
tileset = level["tilesets"][layer["tileset"]]
|
|
|
|
tilew,tileh = layer["gridCellWidth"],layer["gridCellHeight"]
|
|
|
|
tileoffx,tileoffy = layer["offsetX"],layer["offsetY"]
|
|
|
|
nbtilesx,nbtilesy = layer["gridCellsX"],layer["gridCellsY"]
|
|
|
|
storedlayer = {"offsets":[tileoffx,tileoffy]}
|
|
|
|
surface = self.game.pygame.Surface((tilew*nbtilesx,tileh*nbtilesy),flags=self.game.pygame.SRCALPHA)
|
|
|
|
for y in range(len(layer["dataCoords2D"])):
|
|
|
|
for x in range(len(layer["dataCoords2D"][y])):
|
|
|
|
coords = layer["dataCoords2D"][y][x]
|
|
|
|
if len(coords)>1:
|
|
|
|
areatocopy = [coords[0]*tilew,coords[1]*tileh,tilew,tileh]
|
|
|
|
surface.blit(tileset,(x*tilew,y*tileh),area=areatocopy)
|
|
|
|
storedlayer["surface"] = surface
|
|
|
|
self.layers.append(storedlayer)
|
2021-07-03 00:26:02 +02:00
|
|
|
#except:
|
|
|
|
else:
|
2021-07-02 11:10:45 +02:00
|
|
|
self.game.log("Erreur",name,"Les layers sont invalides")
|
2021-07-02 16:18:36 +02:00
|
|
|
if solidlayer: # Je crée les collisions
|
2021-07-02 11:10:45 +02:00
|
|
|
if "gridCellWidth" in solidlayer.keys() and "gridCellHeight" in solidlayer.keys():
|
|
|
|
self.solidtilew = solidlayer["gridCellWidth"]
|
|
|
|
self.solidtileh = solidlayer["gridCellHeight"]
|
|
|
|
else:
|
|
|
|
self.solidtilew,self.solidtileh = 32,32
|
|
|
|
self.game.log("Erreur",name,"Pas de taille de tiles précisée, défaut à 32x32")
|
|
|
|
for entity in solidlayer["entities"]:
|
|
|
|
try:
|
|
|
|
x,y = entity["x"],entity["y"]
|
2021-07-03 00:26:02 +02:00
|
|
|
w,h = self.solidtilew,self.solidtileh
|
|
|
|
if "width" in entity.keys():
|
|
|
|
w = entity["width"]
|
|
|
|
if "height" in entity.keys():
|
|
|
|
h = entity["height"]
|
2021-07-06 21:14:27 +02:00
|
|
|
lists = {"Solid":self.rects,"SemiSolid":self.semirects}
|
|
|
|
lists[entity["name"]].append(self.game.pygame.Rect(x,y,w,h))
|
2021-07-02 11:10:45 +02:00
|
|
|
except:
|
2021-07-06 21:14:27 +02:00
|
|
|
self.game.log("Erreur",name,entity,"Propriétés invalides")
|
2021-07-02 11:10:45 +02:00
|
|
|
else:
|
|
|
|
self.game.log("Erreur",name,"Il manque les collisions, Entity Layer nommé Solids")
|
|
|
|
|
|
|
|
else:
|
|
|
|
self.game.log("Erreur",name,"Pas de layers")
|