Overflown/gamedata/objects/combat/tileset.py
2021-11-17 16:14:29 +01:00

111 lines
5.4 KiB
Python

from gamedata.objects.base import BaseObject
from gamedata.objects.combat.ennemies.robot import Robot
from gamedata.objects.combat.ennemies.crate import Crate
class TilesetRenderer(BaseObject):
def __init__(self,x,y,game,mapfoldername):
super().__init__(x,y,game)
self.tilew = 64
self.tileh = 64
self.level = game.levels_lib[mapfoldername]
self.reinit(self.level)
self.bg = game.sprite_lib["fallbackground.png"]
if self.level["background"]:
self.bg = self.level["background"]
def step(self):
# Spawning ennemies
while len(self.queue)>0:
props = self.queue.pop()
e = props[0](props[1],props[2],self.game,nodes=props[3])
self.game.gameloop.summon(e)
def draw(self):
self.game.window.blit(self.game.pygame.transform.scale(self.bg,(self.game.globals["cameraw"],self.game.globals["camerah"])),[0,0])
for layer in self.layers:
self.game.window.blit(layer["surface"],(layer["offsets"][0]-self.game.globals["camerax"],layer["offsets"][1]-self.game.globals["cameray"]))
def reinit(self,level):
json = level["data"]
name = level["name"]
self.rects = []
self.semirects = []
self.layers = []
self.spawns = []
spawnlists = {"Spawns":self.spawns}
ennemies = {"Robot":Robot,"Crate":Crate}
self.queue = [] # For spawning ennemies after beeing initialized
if "layers" in json.keys() and type(json["layers"]).__name__=="list":
solidlayer = False
for layer in json["layers"]:
#try:
if True:
if layer["name"] == "Solids" and "entities" in layer.keys() : # Je trouve les collisions
solidlayer = layer
elif layer["name"] == "Spawns" and "entities" in layer.keys() : # Je trouve les spawns
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"]
spawnlists[entity["name"]].append(self.game.pygame.Rect((x,y,w,h)))
elif layer["name"] == "Ennemies" and "entities" in layer.keys():
for entity in layer["entities"]:
x,y = entity["x"],entity["y"]
nodes = []
if "nodes" in entity.keys():
nodes = entity["nodes"]
self.queue.append((ennemies[entity["name"]],x,y,nodes))
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)
#except:
else:
self.game.log("Erreur",name,"Les layers sont invalides")
if solidlayer: # Je crée les collisions
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"]
w,h = self.solidtilew,self.solidtileh
if "width" in entity.keys():
w = entity["width"]
if "height" in entity.keys():
h = entity["height"]
lists = {"Solid":self.rects,"SemiSolid":self.semirects}
lists[entity["name"]].append(self.game.pygame.Rect(x,y,w,h))
except:
self.game.log("Erreur",name,entity,"Propriétés invalides")
else:
self.game.log("Erreur",name,"Il manque les collisions, Entity Layer nommé Solids")
else:
self.game.log("Erreur",name,"Pas de layers")