from gamedata.objects.base import BaseObject from gamedata.objects.transition import Transition class Levels(BaseObject): def __init__(self,x,y,game,nodes,customvalues): super().__init__(0,0,game) self.nodes = nodes self.customvalues = customvalues self.launched = False self.blueflags = self.game.getSpriteDir("flags/blue/") self.greenflags= self.game.getSpriteDir("flags/green/") self.flagbase = self.game.sprite_lib["flags/base.png"] self.playerwalking = self.game.getSpriteDir("player/walking/") self.playerstill = self.game.sprite_lib["player/still.png"] self.medalwave = self.game.sprite_lib["medals/wave.png"] self.medalwaveoff = self.game.sprite_lib["medals/waveoff.png"] self.medalclock = self.game.sprite_lib["medals/clock.png"] self.medalclockoff = self.game.sprite_lib["medals/clockoff.png"] self.flip = False self.moving = False self.spriteindex = 0 self.flagsindex = [] self.names = [] self.textnumbers = [] for i,v in enumerate(self.nodes): v["x"]+=8 # Center on tiles v["y"]+=8 self.flagsindex.append(self.game.lib.randint(1,3)) # Get sprites for "Level N" sprite = self.game.getchars("Level "+str(i+1)) self.textnumbers.append(sprite) # Get sprites for the level's name if str(i) in customvalues.keys(): sprites = self.game.getchars(customvalues[str(i)].split(";")[0]) self.names.append(sprites) else: self.names.append(None) self.animspeed = 7 self.cursor = 0 # Which level is selected self.playerx = self.nodes[0]["x"] self.playery = self.nodes[1]["y"] self.tileset = self.game.gameloop.findname("TilesetRenderer")[0] self.maxwidth = self.tileset.layers[0]["surface"].get_width() def step(self): for i in range(len(self.flagsindex)): self.flagsindex[i]+=self.animspeed*self.game.dt if not self.moving and not self.launched: if self.game.inputs["keys"]["right"]["timer"]>0: if "Level "+str(self.cursor+1) in self.game.globals["finishedlevels"] or self.game.globals["allunlocked"]: self.cursor+=1 self.flip = False if self.game.inputs["keys"]["left"]["timer"]>0: self.cursor-=1 self.flip = True if self.game.inputs["keys"]["up"]["timer"]==1: # Launch the level t = Transition(self.game,level = "Level "+str(self.cursor+1)) self.launched = True self.game.globals["levelname"] = self.cursor if str(self.cursor) in self.customvalues.keys(): props = self.customvalues[str(self.cursor)].split(";") self.game.globals["levellore"] = props[0] self.game.globals["timetobeat"] = props[1] else: self.game.globals["levellore"] = None self.game.gameloop.summon(t) self.cursor = min(self.cursor,len(self.nodes)-1) self.cursor = max(0,self.cursor) speedx = (self.nodes[self.cursor]["x"]-self.playerx) speedy = (self.nodes[self.cursor]["y"]-self.playery) self.moving = (abs(speedx)+1)*(abs(speedy)+1)>3 if self.moving: self.spriteindex += self.animspeed*self.game.dt else: self.spriteindex = 0 self.playerx += speedx*4*self.game.dt self.playery += speedy*4*self.game.dt self.game.globals["camerax"] = self.playerx-self.game.globals["cameraw"]/2 self.game.globals["camerax"] = max(0,self.game.globals["camerax"]) self.game.globals["camerax"] = min(self.maxwidth-self.game.globals["cameraw"],self.game.globals["camerax"]) def draw(self): for i,v in enumerate(self.nodes): # Draw flags sprites = self.blueflags if i in self.game.globals["finishedlevels"]: sprites = self.greenflags sprite = sprites[int(self.flagsindex[i])%len(sprites)] x = v["x"]-sprite.get_width()/2-self.game.globals["camerax"]+8 y = v["y"]-sprite.get_height()-self.game.globals["cameray"] self.game.window.blit(self.flagbase,[v["x"]-self.game.globals["camerax"]-8,v["y"]-self.game.globals["cameray"]-2]) self.game.window.blit(sprite,[x,y]) # Draw player if self.moving: sprite = self.playerwalking[int(self.spriteindex)%len(self.playerwalking)] else: sprite = self.playerstill sprite = self.game.pygame.transform.flip(sprite,self.flip,False) x = self.playerx-self.game.globals["camerax"]-sprite.get_width()/2 y = self.playery-self.game.globals["cameray"]-sprite.get_height()/2-6 self.game.window.blit(sprite,[x,y]) # Draw level's number sprite = self.textnumbers[self.cursor] x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]-sprite.get_width()/2 y = self.nodes[self.cursor]["y"]-self.game.globals["cameray"]-sprite.get_height()/2-50 self.game.window.blit(sprite,[x,y]) # Medals wave = self.medalwaveoff if self.cursor in self.game.globals["completedlevels"]: wave = self.medalwave clock = self.medalclockoff if self.cursor in self.game.globals["speedrunlevels"]: clock = self.medalclock x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"] y = self.nodes[self.cursor]["y"]-72 self.game.window.blit(wave,[x-wave.get_width()-1,y]) self.game.window.blit(clock,[x+1,y]) # Draw the level's name if self.names[self.cursor]: sprite = self.names[self.cursor] x = self.nodes[self.cursor]["x"]-self.game.globals["camerax"]-sprite.get_width()/2 y = self.nodes[self.cursor]["y"]-self.game.globals["cameray"]-sprite.get_height()/2-40 self.game.window.blit(sprite,[x,y])