Overflown/gamedata/objects/ingame/tileset.py

164 lines
8.1 KiB
Python
Raw Normal View History

2021-06-25 16:13:52 +02:00
from gamedata.objects.base import BaseObject
2021-11-21 12:28:27 +01:00
from gamedata.objects.ingame.ennemies.robot import Robot
from gamedata.objects.ingame.ennemies.crate import Crate
from gamedata.objects.ingame.ennemies.balloon import Balloon
from gamedata.objects.ingame.ennemies.spring import Spring
from gamedata.objects.ingame.ennemies.waterchange import WaterChange
2021-11-24 15:47:52 +01:00
from gamedata.objects.ingame.ennemies.trashbag import TrashBag
2021-11-25 08:11:08 +01:00
from gamedata.objects.ingame.ennemies.squid import Squid
2021-11-25 15:11:23 +01:00
from gamedata.objects.ingame.ennemies.shooter import Shooter
2021-11-23 11:11:13 +01:00
from gamedata.objects.ingame.endflag import EndFlag
2021-11-22 22:14:09 +01:00
from gamedata.objects.levels import Levels
2021-11-24 16:04:34 +01:00
from gamedata.objects.ripple import Ripple
2021-06-25 16:13:52 +02:00
class TilesetRenderer(BaseObject):
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-11-23 11:11:13 +01:00
self.depth = -2
2021-06-25 16:13:52 +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-11-24 15:47:52 +01:00
self.game.globals["totaltrashes"] = 0
self.game.globals["trashes"] = 0
self.game.globals["timer"] = 0
2021-11-20 21:23:05 +01:00
self.bgs = [game.sprite_lib["fallbackground.png"]]
if len(self.level["backgrounds"]):
self.bgs = self.level["backgrounds"]
self.bgoffset = self.layers[0]["surface"].get_height()/2-self.game.globals["camerah"]
2021-07-02 22:10:01 +02:00
2021-11-21 18:08:58 +01:00
# Prescale backgrounds
self.sep = self.game.pygame.transform.scale(self.game.sprite_lib["backgroundseparator.png"],[self.game.globals["cameraw"],self.game.globals["camerah"]])
self.scaledbgs = []
wratio = self.game.globals["cameraw"]/self.bgs[0]["sprite"].get_width()
hratio = self.game.globals["camerah"]/self.bgs[0]["sprite"].get_height()
for bg in self.bgs:
width, height = round(bg["sprite"].get_width()*wratio),round(bg["sprite"].get_height()*hratio)
sprite = self.game.pygame.transform.scale(bg["sprite"],(width,height))
self.scaledbgs.append(sprite)
2021-11-16 23:36:56 +01:00
def step(self):
# Spawning ennemies
while len(self.queue)>0:
props = self.queue.pop()
2021-11-18 17:58:38 +01:00
e = props[0](props[1],props[2],self.game,props[3],props[4])
2021-11-16 23:36:56 +01:00
self.game.gameloop.summon(e)
2021-06-25 16:13:52 +02:00
def draw(self):
2021-11-20 21:23:05 +01:00
lastoffset = 0
2021-11-21 18:08:58 +01:00
for i,bg in enumerate(self.scaledbgs):
2021-11-20 21:23:05 +01:00
ratio = 1-(len(self.bgs)-i)/len(self.bgs)
2021-11-21 18:08:58 +01:00
width = bg.get_width()
height = bg.get_height()
2021-11-20 21:23:05 +01:00
nbs = self.game.globals["cameraw"]/width
lastoffset = self.rect[1]+(self.bgoffset-self.game.globals["cameray"])*ratio
2021-11-24 20:28:26 +01:00
for i in range(round(max(nbs+0.5,2))):
2021-11-21 18:08:58 +01:00
self.game.window.blit(bg,[(-self.game.globals["camerax"]*ratio)%width+(i-1)*width,lastoffset])
2021-11-20 21:23:05 +01:00
# Draw filler
if self.level["filler"]:
filleroffset = lastoffset+height
self.game.window.blit(self.level["filler"],[0,filleroffset])
# Separator
2021-11-21 18:08:58 +01:00
self.game.window.blit(self.sep,[0,0])
2021-11-20 21:23:05 +01:00
# Layers
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 16:18:36 +02:00
def reinit(self,level):
json = level["data"]
name = level["name"]
self.rects = []
2021-07-06 21:14:27 +02:00
self.semirects = []
2021-11-24 19:59:23 +01:00
self.spikes = []
2021-07-02 16:18:36 +02:00
self.layers = []
2021-11-16 10:50:02 +01:00
self.spawns = []
2021-11-27 19:27:14 +01:00
self.bgm = None
2021-11-28 11:33:00 +01:00
if self.game.globals["levelname"]!=None and not self.game.globals["overworld"]:
2021-11-27 19:27:14 +01:00
self.bgm = self.game.sound_lib["bgm/"+self.game.globals["bgms"][self.game.globals["levelname"]]]
2021-11-25 08:11:08 +01:00
if self.bgm:
self.bgm.play(-1)
2021-11-16 10:50:02 +01:00
spawnlists = {"Spawns":self.spawns}
2021-11-25 15:11:23 +01:00
ennemies = {"Robot":Robot,"Crate":Crate,"Balloon":Balloon,"Spring":Spring,"WaterChange":WaterChange,"Levels":Levels,"End":EndFlag,"TrashBag":TrashBag,"Ripple":Ripple,"Squid":Squid,"Shooter":Shooter}
2021-11-16 23:36:56 +01:00
self.queue = [] # For spawning ennemies after beeing initialized
2021-07-02 16:18:36 +02:00
if "layers" in json.keys() and type(json["layers"]).__name__=="list":
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
solidlayer = layer
2021-07-03 00:26:02 +02:00
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"]
2021-11-16 10:37:39 +01:00
spawnlists[entity["name"]].append(self.game.pygame.Rect((x,y,w,h)))
2021-07-03 00:26:02 +02:00
2021-11-16 23:36:56 +01:00
elif layer["name"] == "Ennemies" and "entities" in layer.keys():
for entity in layer["entities"]:
x,y = entity["x"],entity["y"]
nodes = []
2021-11-18 17:58:38 +01:00
customvalues = {}
2021-11-16 23:36:56 +01:00
if "nodes" in entity.keys():
nodes = entity["nodes"]
2021-11-18 17:58:38 +01:00
if "values" in entity.keys():
customvalues = entity["values"]
self.queue.append((ennemies[entity["name"]],x,y,nodes,customvalues))
2021-11-16 23:36:56 +01:00
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"]
2021-11-19 13:42:37 +01:00
storedlayer = {"offsets":[tileoffx,tileoffy],"name":layer["name"]}
2021-07-02 16:18:36 +02:00
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:
self.game.log("Erreur",name,"Les layers sont invalides")
2021-07-02 16:18:36 +02:00
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"]
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-11-24 19:59:23 +01:00
lists = {"Solid":self.rects,"SemiSolid":self.semirects,"Spikes":self.spikes}
2021-07-06 21:14:27 +02:00
lists[entity["name"]].append(self.game.pygame.Rect(x,y,w,h))
except:
2021-07-06 21:14:27 +02:00
self.game.log("Erreur",name,entity,"Propriétés invalides")
else:
self.game.log("Erreur",name,"Il manque les collisions, Entity Layer nommé Solids")
2021-11-19 13:42:37 +01:00
self.layers.sort(key=lambda x: x["name"])
self.layers.reverse()
else:
self.game.log("Erreur",name,"Pas de layers")